diff options
author | raviteja.karumuri <raviteja.karumuri@est.tech> | 2024-08-20 17:22:15 +0100 |
---|---|---|
committer | RAVITEJA KARUMURI <raviteja.karumuri@est.tech> | 2024-08-22 09:50:56 +0000 |
commit | 6cfcc83721edef3b91c4050c3cb23596c512d4cb (patch) | |
tree | cf647a58985810a88f068c38dc0f82dd76dde40b | |
parent | 987ca55ce327ab3f29c6e1f1cef2366cb9ba4c2b (diff) |
Improve tests/issues found in Sonar report - A1 Oslo/NewDelhi/Montreal/London-Part3
Issue-ID: CCSDK-4037
Change-Id: I44c9b2cc241999685be63bbc9f759f7dd8eee563
Signed-off-by: Raviteja Karumuri <raviteja.karumuri@est.tech>
5 files changed, 130 insertions, 23 deletions
diff --git a/a1-policy-management/pom.xml b/a1-policy-management/pom.xml index 05c14f17..d010a8c3 100644 --- a/a1-policy-management/pom.xml +++ b/a1-policy-management/pom.xml @@ -38,6 +38,8 @@ <java.version.target>17</java.version.target> <gson.version>2.10.1</gson.version> <json.version>20230227</json.version> + <localstack.version>1.20.1</localstack.version> + <junit.jupiter.version>1.20.1</junit.jupiter.version> <formatter-maven-plugin.version>2.22.0</formatter-maven-plugin.version> <spotless-maven-plugin.version>2.35.0</spotless-maven-plugin.version> <commons-io.version>2.11.0</commons-io.version> @@ -158,6 +160,18 @@ </dependency> <!-- TEST --> <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>junit-jupiter</artifactId> + <version>${junit.jupiter.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>localstack</artifactId> + <version>${localstack.version}</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>${springdoc.version}</version> diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java index ab93ea3e..66d32110 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/A1AdapterJsonHelper.java @@ -92,26 +92,4 @@ class A1AdapterJsonHelper { return Mono.error(ex); } } - - public static Mono<String> getValueFromResponse(String response, String key) { - return getOutput(response) // - .map(responseParams -> { - if (!responseParams.has(key)) { - return ""; - } - return responseParams.get(key).toString(); - }); - } - - public static Mono<String> extractPolicySchema(String inputString) { - try { - JSONObject jsonObject = new JSONObject(inputString); - JSONObject schemaObject = jsonObject.getJSONObject("policySchema"); - String schemaString = schemaObject.toString(); - return Mono.just(schemaString); - } catch (Exception ex) { // invalid json - logger.debug("Invalid json {}", ex.getMessage()); - return Mono.error(ex); - } - } } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStore.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStore.java index 1c67ff7d..d014ad53 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStore.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStore.java @@ -141,7 +141,7 @@ class S3ObjectStore implements DataStore { return Mono.fromFuture(future) // .map(f -> s3Bucket) // .doOnError(t -> logger.debug("Could not create S3 bucket: {}", t.getMessage())) - .onErrorResume(t -> Mono.just(s3Bucket)); + .onErrorResume(t -> Mono.just("Not Created")); } @Override 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 c1525ffa..1de3b2e9 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 @@ -502,6 +502,7 @@ class ApplicationTest { info.isTransient = isTransient; } info.statusNotificationUri = statusNotificationUri; + info.validate(); return gson.toJson(info); } diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStoreTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStoreTest.java new file mode 100644 index 00000000..be447111 --- /dev/null +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/datastore/S3ObjectStoreTest.java @@ -0,0 +1,114 @@ +package org.onap.ccsdk.oran.a1policymanagementservice.datastore; + +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig; +import org.reactivestreams.Publisher; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; +import org.testcontainers.containers.localstack.LocalStackContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; +import reactor.test.StepVerifier; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.function.Predicate; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@Testcontainers +@ExtendWith({MockitoExtension.class, OutputCaptureExtension.class}) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class S3ObjectStoreTest { + + static ApplicationConfig appConfig; + private static S3ObjectStore s3ObjectStore; + private static final String bucketName = "s3bucket"; + + @Container + private static final LocalStackContainer localstack = + new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3")) + .withServices(LocalStackContainer.Service.S3); + + @BeforeAll + static void init() { + appConfig = mock(ApplicationConfig.class); + when(appConfig.isS3Enabled()).thenReturn(Boolean.TRUE); + when(appConfig.getS3EndpointOverride()).thenReturn(localstack.getEndpoint().toString()); + when(appConfig.getS3AccessKeyId()).thenReturn(localstack.getAccessKey()); + when(appConfig.getS3SecretAccessKey()).thenReturn(localstack.getSecretKey()); + when(appConfig.getS3Bucket()).thenReturn(bucketName); + s3ObjectStore = new S3ObjectStore(appConfig, "location"); + } + @Test + @Order(1) + void testGetS3AsynchClient() { + assertNotNull(s3ObjectStore); + } + + @Test + @Order(2) + void testCreateAndDeleteS3BucketSuccess(CapturedOutput capturedOutput) { + + testSuccess(s3ObjectStore.createDataStore(), actual -> actual.equals(bucketName)); + assertFalse(capturedOutput.getOut().contains("Could not create S3 bucket:")); + + testSuccess(s3ObjectStore.deleteBucket(), actual -> actual.equals(bucketName)); + } + + @Test + @Order(3) + void testWriteAndReadAndDeleteObjectSuccess(CapturedOutput capturedOutput) { + + testSuccess(s3ObjectStore.createDataStore(), actual -> actual.equals(bucketName)); + byte[] fileData = "testData".getBytes(StandardCharsets.UTF_8); + new String(fileData); + testSuccess(s3ObjectStore.writeObject("test", fileData), + actual -> Arrays.equals(actual, fileData)); + assertFalse(capturedOutput.getOut().contains("Failed to store object")); + testSuccess(s3ObjectStore.readObject("test"), + actual -> Arrays.equals(actual, fileData)); + testSuccess(s3ObjectStore.deleteAllObjects(), actual -> actual.equals("OK")); + } + + @Test + @Order(4) + void testListObjectsSuccess() { + + s3ObjectStore.createDataStore().block(); + String objectName = "test"; + byte[] fileData = "testData".getBytes(StandardCharsets.UTF_8); + testSuccess(s3ObjectStore.writeObject(objectName, fileData), + actual -> Arrays.equals(actual, fileData)); + testSuccess(s3ObjectStore.listObjects(""), actual -> actual.equals(objectName)); + } + + @Test + @Order(5) + void testCreateAndDeleteS3BucketError(CapturedOutput capturedOutput) { + + when(appConfig.getS3Bucket()).thenReturn("S3Bucket"); + + testFailure(s3ObjectStore.createDataStore(), actual -> actual.equals("Not Created")); + + testFailure(s3ObjectStore.deleteBucket(), actual -> actual.equals("NOK")); + assertTrue(capturedOutput.getOut().contains("Could not delete bucket:")); + } + + <T> void testSuccess(Publisher<T> publisher, Predicate<T> equalityCheck) { + StepVerifier.create(publisher) + .expectNextMatches(equalityCheck) + .verifyComplete(); + } + + <T> void testFailure(Publisher<T> publisher, Predicate<T> equalityCheck) { + StepVerifier.create(publisher) + .expectNextMatches(equalityCheck) + .verifyComplete(); + } +} |