diff options
author | PatrikBuhr <patrik.buhr@est.tech> | 2020-11-23 13:24:45 +0100 |
---|---|---|
committer | PatrikBuhr <patrik.buhr@est.tech> | 2020-11-24 14:47:16 +0100 |
commit | 3b56b4ff40eba05108651d04df4b9a2ef3a4eb1d (patch) | |
tree | b071a923bd9295617f79e3cd95d943e04ab34a8a /a1-policy-management | |
parent | 50fe65ea5daca6935cdbc1155adbfb53f5a28c40 (diff) |
Added GET config
Corrected config so that PUT config receives an object intestead of a string
Change-Id: Id784cc2840a2e2e981b0c4e874b8a848964e00ea
Issue-ID: CCSDK-2966
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Diffstat (limited to 'a1-policy-management')
5 files changed, 68 insertions, 25 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java index 7a432f83..86ae2734 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java @@ -18,9 +18,10 @@ package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -28,18 +29,19 @@ import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import java.io.IOException; +import java.util.Optional; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ErrorResponse.ErrorInfo; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +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.RestController; @@ -52,29 +54,56 @@ public class ConfigurationController { @Autowired ConfigurationFile configurationFile; + private static Gson gson = new GsonBuilder() // + .create(); // + @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE) - @ApiOperation(value = "Replace the current configuration with the given configuration") + @ApiOperation(value = "Replace the current configuration file with the given configuration", // + notes = "Note that the file is ignored if the Consul is used.") @ApiResponses(value = { // @ApiResponse(code = 200, message = "Configuration updated", response = VoidResponse.class), // @ApiResponse(code = 400, message = "Invalid configuration provided", response = ErrorInfo.class), // @ApiResponse(code = 500, message = "Something went wrong when replacing the configuration. Try again.", response = ErrorResponse.ErrorInfo.class) // }) - public ResponseEntity<Object> putConfiguration(@RequestBody String configuration) { + public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) { try { - JsonObject configJson = JsonParser.parseString(configuration).getAsJsonObject(); + String configAsString = gson.toJson(configuration); + JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject(); ApplicationConfigParser configParser = new ApplicationConfigParser(); configParser.parse(configJson); configurationFile.writeFile(configJson); logger.info("Configuration changed through REST call."); - } catch (ServiceException | JsonSyntaxException e) { - return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()), - HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(HttpStatus.OK); } catch (IOException ioe) { logger.warn("Configuration file not written, {}.", ioe.getMessage()); - ErrorResponse.create("Internal error when writing the configuration. Try again.", + return ErrorResponse.create("Internal error when writing the configuration. Try again.", HttpStatus.INTERNAL_SERVER_ERROR); + } catch (Exception e) { + return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()), + HttpStatus.BAD_REQUEST); } - return new ResponseEntity<>(HttpStatus.OK); } + + @GetMapping(path = Consts.V2_API_ROOT + "/configuration", produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Returns the contents of the configuration file", // + notes = "Note that the file contents is not relevant if the Consul is used.") // + @ApiResponses(value = { // + @ApiResponse(code = 200, message = "Configuration", response = Object.class), // + @ApiResponse(code = 404, message = "File is not found or readable", + response = ErrorResponse.ErrorInfo.class)} // + ) + public ResponseEntity<Object> getConfiguration() { + try { + Optional<JsonObject> rootObject = configurationFile.readFile(); + if (rootObject.isPresent()) { + return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK); + } else { + return ErrorResponse.create("File does not exist", HttpStatus.NOT_FOUND); + } + } catch (Exception e) { + return ErrorResponse.create(e, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java index e1961c17..81021b15 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java @@ -30,7 +30,7 @@ public class Consts { public static final String V2_API_ROOT = "/v2"; public static final String V2_API_NAME = "A1 Policy Management Version 2.0 (in progress)"; - public static final String V2_CONFIG_API_NAME = "A1 Policy Management Configuration"; + public static final String V2_CONFIG_API_NAME = "Component Configuration"; private Consts() {} } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java index dfb8d49e..29d2e4eb 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java @@ -71,7 +71,8 @@ public class RefreshConfigTask { public Properties systemEnvironment; /** - * The time between refreshes of the configuration. Not final so tests can modify it. + * The time between refreshes of the configuration. Not final so tests can + * modify it. */ private static Duration configRefreshInterval = Duration.ofMinutes(1); diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java index 516ddd5f..30ce4c3e 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java @@ -30,10 +30,12 @@ import static org.mockito.Mockito.doReturn; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; import java.io.PrintStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; @@ -211,12 +213,14 @@ class ApplicationTest { } @Test - void createApiDoc() throws FileNotFoundException { + void createApiDoc() throws IOException { String url = "https://localhost:" + this.port + "/v2/api-docs"; ResponseEntity<String> resp = restClient("", false).getForEntity(url).block(); assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK); String indented = (new JSONObject(resp.getBody())).toString(4); - try (PrintStream out = new PrintStream(new FileOutputStream("../docs/offeredapis/swagger/pms-api.json"))) { + String docDir = "../docs/offeredapis/swagger/"; + Files.createDirectories(Paths.get(docDir)); + try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) { out.print(indented); } } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java index 8f9d236b..376ce76e 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java @@ -105,27 +105,36 @@ class ConfigurationControllerTest { @Test void putValidConfigurationWithNewRic_shouldUpdateRepository() throws Exception { - String url = "https://localhost:" + this.port + "/v2/configuration"; + String url = "/v2/configuration"; - File configFile = new File(getClass().getClassLoader() - .getResource("test_application_configuration_with_dmaap_config.json").getFile()); - String configFileAsString = FileUtils.readFileToString(configFile, "UTF-8"); - - String resp = restClient().put(url, configFileAsString).block(); + String resp = restClient().put(url, configAsString()).block(); assertThat(resp).isEmpty(); await().until(rics::size, equalTo(2)); + + // GET config + resp = restClient().get(url).block(); + assertThat(resp).contains("config"); + } + + @Test + void getNoFileExists() { + String url = "/v2/configuration"; + testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND, "File does not exist"); + } + + private String configAsString() throws Exception { + File configFile = new File(getClass().getClassLoader() + .getResource("test_application_configuration_with_dmaap_config.json").getFile()); + return FileUtils.readFileToString(configFile, "UTF-8"); } @Test void putInvalidConfiguration_shouldReturnError400() throws Exception { - String url = "https://localhost:" + this.port + "/v2/configuration"; + String url = "/v2/configuration"; // Valid JSON but invalid configuration. testErrorCode(restClient().put(url, "{\"error\":\"error\"}"), HttpStatus.BAD_REQUEST, "Faulty configuration"); - - // Invalid JSON. - testErrorCode(restClient().put(url, "{\"error\":\"error\""), HttpStatus.BAD_REQUEST, "Faulty configuration"); } private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) { |