X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-adaptor-java%2Fsrc%2Ftest%2Fjava%2Forg%2Foran%2Fdmaapadapter%2FApplicationTest.java;h=b1c1780a054a79c17f787e1c8a94304a207163f8;hb=960e66a1728c1c332f6b74320bbd086a442ba5ea;hp=2a82ff8fe862c499a2c988963b99142809b5aa90;hpb=e702c19d34c342e75993e169c261f9087cc0643f;p=nonrtric.git diff --git a/dmaap-adaptor-java/src/test/java/org/oran/dmaapadapter/ApplicationTest.java b/dmaap-adaptor-java/src/test/java/org/oran/dmaapadapter/ApplicationTest.java index 2a82ff8f..b1c1780a 100644 --- a/dmaap-adaptor-java/src/test/java/org/oran/dmaapadapter/ApplicationTest.java +++ b/dmaap-adaptor-java/src/test/java/org/oran/dmaapadapter/ApplicationTest.java @@ -22,9 +22,8 @@ package org.oran.dmaapadapter; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertTrue; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonParser; import java.io.FileOutputStream; @@ -44,13 +43,13 @@ import org.oran.dmaapadapter.configuration.ImmutableHttpProxyConfig; import org.oran.dmaapadapter.configuration.ImmutableWebClientConfig; import org.oran.dmaapadapter.configuration.WebClientConfig; import org.oran.dmaapadapter.configuration.WebClientConfig.HttpProxyConfig; +import org.oran.dmaapadapter.controllers.ProducerCallbacksController; import org.oran.dmaapadapter.r1.ConsumerJobInfo; +import org.oran.dmaapadapter.r1.ProducerJobInfo; import org.oran.dmaapadapter.repository.InfoType; import org.oran.dmaapadapter.repository.InfoTypes; import org.oran.dmaapadapter.repository.Jobs; import org.oran.dmaapadapter.tasks.ProducerRegstrationTask; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -60,9 +59,14 @@ import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.web.reactive.function.client.WebClientResponseException; + +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) @@ -72,7 +76,6 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; "app.configuration-filepath=./src/test/resources/test_application_configuration.json"// }) class ApplicationTest { - private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class); @Autowired private ApplicationConfig applicationConfig; @@ -92,11 +95,11 @@ class ApplicationTest { @Autowired private EcsSimulatorController ecsSimulatorController; + private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create(); + @LocalServerPort int localServerHttpPort; - private static Gson gson = new GsonBuilder().create(); - static class TestApplicationConfig extends ApplicationConfig { @Override public String getEcsBaseUrl() { @@ -144,7 +147,6 @@ class ApplicationTest { this.consumerController.testResults.reset(); this.ecsSimulatorController.testResults.reset(); this.jobs.clear(); - this.types.clear(); } private AsyncRestClient restClient(boolean useTrustValidation) { @@ -217,21 +219,92 @@ class ApplicationTest { } } + @Test + void testResponseCodes() throws Exception { + String supervisionUrl = baseUrl() + ProducerCallbacksController.SUPERVISION_URL; + ResponseEntity resp = restClient().getForEntity(supervisionUrl).block(); + assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK); + + String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL; + resp = restClient().deleteForEntity(jobUrl + "/junk").block(); + assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK); + + ProducerJobInfo info = new ProducerJobInfo(null, "id", "typeId", "targetUri", "owner", "lastUpdated"); + String body = gson.toJson(info); + testErrorCode(restClient().post(jobUrl, body), HttpStatus.NOT_FOUND, "Could not find type"); + } + @Test void testWholeChain() throws Exception { - await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInEcs()).isTrue()); + final String JOB_ID = "ID"; - this.ecsSimulatorController.addJob(consumerJobInfo(), restClient()); + // Register producer, Register types + await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull()); + assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(1); + // Create a job + this.ecsSimulatorController.addJob(consumerJobInfo(), JOB_ID, restClient()); await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1)); + // Return two messages from DMAAP and verify that these are sent to the owner of + // the job (consumer) DmaapSimulatorController.dmaapResponses.add("DmaapResponse1"); DmaapSimulatorController.dmaapResponses.add("DmaapResponse2"); - ConsumerController.TestResults consumer = this.consumerController.testResults; await().untilAsserted(() -> assertThat(consumer.receivedBodies.size()).isEqualTo(2)); assertThat(consumer.receivedBodies.get(0)).isEqualTo("DmaapResponse1"); + String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL; + String jobs = restClient().get(jobUrl).block(); + assertThat(jobs).contains("ExampleInformationType"); + + // Delete the job + this.ecsSimulatorController.deleteJob(JOB_ID, restClient()); + await().untilAsserted(() -> assertThat(this.jobs.size()).isZero()); + + } + + @Test + void testReRegister() throws Exception { + // Wait foir register types and producer + await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull()); + assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(1); + + // Clear the registration, should trigger a re-register + ecsSimulatorController.testResults.reset(); + await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull()); + assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(1); + + // Just clear the registerred types, should trigger a re-register + ecsSimulatorController.testResults.types.clear(); + await().untilAsserted( + () -> assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(1)); + + } + + private void testErrorCode(Mono request, HttpStatus expStatus, String responseContains) { + testErrorCode(request, expStatus, responseContains, true); + } + + private void testErrorCode(Mono request, HttpStatus expStatus, String responseContains, + boolean expectApplicationProblemJsonMediaType) { + StepVerifier.create(request) // + .expectSubscription() // + .expectErrorMatches( + t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) // + .verify(); + } + + private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains, + boolean expectApplicationProblemJsonMediaType) { + assertTrue(throwable instanceof WebClientResponseException); + WebClientResponseException responseException = (WebClientResponseException) throwable; + assertThat(responseException.getStatusCode()).isEqualTo(expStatus); + assertThat(responseException.getResponseBodyAsString()).contains(responseContains); + if (expectApplicationProblemJsonMediaType) { + assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON); + } + return true; } }