Skip to content

Commit

Permalink
Merge pull request #2100 from reportportal/EPMRPP-96935
Browse files Browse the repository at this point in the history
EPMRPP-96935 || Add a new admin endpoint for updating server settings
  • Loading branch information
pbortnik authored Nov 15, 2024
2 parents 6986950 + 4699313 commit 2807275
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions project-properties.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ project.ext {
(migrationsUrl + '/migrations/86_add_retention_policy_launch.up.sql') : 'V086__add_retention_policy_launch.sql',
(migrationsUrl + '/migrations/88_analytics_data_table.up.sql') : 'V088__analytics_data_table.sql',
(migrationsUrl + '/migrations/90_add_user_fields.up.sql') : 'V090__scim_user_fields.sql',
(migrationsUrl + '/migrations/91_settings_users_sso.up.sql') : 'V091__settings_users_sso.sql',
]
excludeTests = ['**/entity/**',
'**/aop/**',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.epam.ta.reportportal.model.settings.AnalyticsResource;
import com.epam.ta.reportportal.model.settings.ServerSettingsResource;
import com.epam.ta.reportportal.model.settings.UpdateSettingsRq;
import com.epam.ta.reportportal.ws.reporting.OperationCompletionRS;
import java.util.Map;

Expand All @@ -42,4 +43,12 @@ public interface ServerAdminHandler {
* @return Operation results
*/
OperationCompletionRS saveAnalyticsSettings(AnalyticsResource analyticsResource);

/**
* Update server settings
*
* @param request {@link UpdateSettingsRq}
* @return Operation results
*/
OperationCompletionRS updateServerSettings(UpdateSettingsRq request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toMap;

import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.dao.ServerSettingsRepository;
import com.epam.ta.reportportal.entity.ServerSettings;
import com.epam.ta.reportportal.model.settings.AnalyticsResource;
import com.epam.ta.reportportal.model.settings.UpdateSettingsRq;
import com.epam.ta.reportportal.ws.converter.converters.ServerSettingsConverter;
import com.epam.ta.reportportal.ws.reporting.OperationCompletionRS;
import java.util.Map;
Expand Down Expand Up @@ -73,6 +76,16 @@ public OperationCompletionRS saveAnalyticsSettings(AnalyticsResource analyticsRe
return new OperationCompletionRS("Server Settings were successfully updated.");
}

@Override
public OperationCompletionRS updateServerSettings(UpdateSettingsRq request) {
ServerSettings serverSettings = serverSettingsRepository.findByKey(request.getKey())
.orElseThrow(() -> new ReportPortalException(ErrorType.SERVER_SETTINGS_NOT_FOUND,
request.getKey()));
serverSettings.setValue(request.getValue());
serverSettingsRepository.save(serverSettings);
return new OperationCompletionRS("Server Settings were successfully updated.");
}

private Map<String, ServerSettings> findServerSettings() {
return serverSettingsRepository.selectServerSettings().stream()
.collect(toMap(ServerSettings::getKey, s -> s, (prev, curr) -> prev));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.epam.ta.reportportal.model.settings;

import javax.validation.constraints.NotEmpty;
import lombok.Data;

@Data
public class UpdateSettingsRq {

@NotEmpty
private String key;
@NotEmpty
private String value;

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.core.admin.ServerAdminHandler;
import com.epam.ta.reportportal.model.settings.AnalyticsResource;
import com.epam.ta.reportportal.model.settings.UpdateSettingsRq;
import com.epam.ta.reportportal.ws.reporting.OperationCompletionRS;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -32,6 +33,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -56,19 +58,29 @@ public SettingsController(ServerAdminHandler serverHandler) {
}

@Transactional
@RequestMapping(value = "/analytics", method = { RequestMethod.PUT, RequestMethod.POST })
@RequestMapping(value = "/analytics", method = {RequestMethod.PUT, RequestMethod.POST})
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Update analytics settings")
@Operation(summary = "Update analytics settings")
public OperationCompletionRS saveAnalyticsSettings(
@RequestBody @Validated AnalyticsResource request,
@AuthenticationPrincipal ReportPortalUser user) {
return serverHandler.saveAnalyticsSettings(request);
}

@Transactional
@PutMapping
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Update server settings with specified key")
public OperationCompletionRS updateServerSettings(
@RequestBody @Validated UpdateSettingsRq request,
@AuthenticationPrincipal ReportPortalUser user) {
return serverHandler.updateServerSettings(request);
}

@Transactional(readOnly = true)
@GetMapping
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Get server settings")
@Operation(summary = "Get server settings")
public Map<String, String> getServerSettings(@AuthenticationPrincipal ReportPortalUser user) {
return serverHandler.getServerSettings();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.epam.ta.reportportal.model.settings.AnalyticsResource;
import com.epam.ta.reportportal.model.settings.UpdateSettingsRq;
import com.epam.ta.reportportal.ws.BaseMvcTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
Expand All @@ -42,6 +43,28 @@ void getServerSettings() throws Exception {
.andExpect(status().isOk());
}

@Test
void updateSettingsNegative() throws Exception {
UpdateSettingsRq mockRequest = new UpdateSettingsRq();
mockRequest.setKey("invalid_key");
mockRequest.setValue("true");
mockMvc.perform(put("/v1/settings").with(token(oAuthHelper.getSuperadminToken()))
.contentType(APPLICATION_JSON).content(objectMapper.writeValueAsBytes(mockRequest)))
.andExpect(status().isNotFound());
}

@Test
void updateSettings() throws Exception {
UpdateSettingsRq mockRequest = new UpdateSettingsRq();
mockRequest.setKey("server.users.sso");
mockRequest.setValue("true");

mockMvc.perform(put("/v1/settings").with(token(oAuthHelper.getSuperadminToken()))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsBytes(mockRequest)))
.andExpect(status().isOk());
}

@Test
void updateAnalyticsSettings() throws Exception {
AnalyticsResource resource = new AnalyticsResource();
Expand Down

0 comments on commit 2807275

Please sign in to comment.