diff options
author | andrzejszukuc <andrzej.szukuc@nokia.com> | 2019-03-28 16:48:55 +0100 |
---|---|---|
committer | andrzejszukuc <andrzej.szukuc@nokia.com> | 2019-03-29 18:14:06 +0100 |
commit | a9dfad25c62ec8b4ed1bd04f4b51a59581431a85 (patch) | |
tree | 07da8b640412e2fa822d08db39bd2a70936b4057 /prh-app-server/src/test/java | |
parent | 3a3f19a74d62567a703d2b9cf01de0cde463a557 (diff) |
PNF re-registration is supported
Change-Id: I3a70c610e075bcfbab8cee62ae229ce06cfc5e5d
Signed-off-by: andrzejszukuc <andrzej.szukuc@nokia.com>
Issue-ID: DCAEGEN2-1059
Diffstat (limited to 'prh-app-server/src/test/java')
9 files changed, 459 insertions, 65 deletions
diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/TestAppConfiguration.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/TestAppConfiguration.java index cdfffbd1..754bdba9 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/TestAppConfiguration.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/TestAppConfiguration.java @@ -74,6 +74,7 @@ public class TestAppConfiguration { .aaiIgnoreSslCertificateErrors(true) .aaiBasePath("/aai/v12") .aaiPnfPath("/network/pnfs/pnf") + .aaiServiceInstancePath("/business/customers/customer/${customer}/service-subscriptions/service-subscription/${serviceType}/service-instances/service-instance/${serviceInstanceId}") .trustStorePath("/opt/app/prh/local/org.onap.prh.trust.jks") .trustStorePasswordPath("change_it") .keyStorePath("/opt/app/prh/local/org.onap.prh.p12") diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImplTest.java index 419e9144..1160f77e 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiProducerTaskImplTest.java @@ -20,19 +20,8 @@ package org.onap.dcaegen2.services.prh.tasks; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - - import com.google.gson.JsonObject; -import javax.net.ssl.SSLException; - +import io.netty.handler.codec.http.HttpResponseStatus; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -44,12 +33,15 @@ import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.patch.AaiHttpPatchClient; - -import reactor.netty.http.client.HttpClientResponse; -import io.netty.handler.codec.http.HttpResponseStatus; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import javax.net.ssl.SSLException; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/14/18 */ @@ -60,11 +52,11 @@ class AaiProducerTaskImplTest { private AaiClientConfiguration aaiClientConfiguration; private AaiHttpPatchClient aaiReactiveHttpPatchClient; private AppConfig appConfig; - private HttpClientResponse clientResponse; + private HttpResponse clientResponse; @BeforeEach void setUp() { - clientResponse = mock(HttpClientResponse.class); + clientResponse = mock(HttpResponse.class); aaiClientConfiguration = TestAppConfiguration.createDefaultAaiClientConfiguration(); consumerDmaapModel = ImmutableConsumerDmaapModel.builder() .ipv4("10.16.123.234") @@ -86,7 +78,7 @@ class AaiProducerTaskImplTest { void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { //given/when/ when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiProducerTask = new AaiProducerTaskImpl(appConfig); + aaiProducerTask = new AaiProducerTaskImpl(aaiReactiveHttpPatchClient); Executable executableCode = () -> aaiProducerTask.execute(null); //then @@ -120,14 +112,12 @@ class AaiProducerTaskImplTest { private void getAaiProducerTask_whenMockingResponseObject(HttpResponseStatus httpResponseStatus) throws SSLException { //given - doReturn(httpResponseStatus).when(clientResponse).status(); - Mono<HttpClientResponse> clientResponseMono = Mono.just(clientResponse); + doReturn(httpResponseStatus.code()).when(clientResponse).statusCode(); + Mono<HttpResponse> clientResponseMono = Mono.just(clientResponse); aaiReactiveHttpPatchClient = mock(AaiHttpPatchClient.class); when(aaiReactiveHttpPatchClient.getAaiResponse(any())) .thenReturn(clientResponseMono); when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiProducerTask = spy(new AaiProducerTaskImpl(appConfig)); - when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiReactiveHttpPatchClient).when(aaiProducerTask).resolveClient(); + aaiProducerTask = spy(new AaiProducerTaskImpl(aaiReactiveHttpPatchClient)); } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiPublisherTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiPublisherTaskSpy.java index a446c353..ae770fc6 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiPublisherTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiPublisherTaskSpy.java @@ -52,10 +52,9 @@ public class AaiPublisherTaskSpy { AppConfig appConfig = spy(AppConfig.class); ConsumerDmaapModel consumerDmaapModel = spy(ConsumerDmaapModel.class); doReturn(mock(AaiClientConfiguration.class)).when(appConfig).getAaiClientConfiguration(); - AaiProducerTaskImpl aaiProducerTask = spy(new AaiProducerTaskImpl(appConfig)); AaiHttpPatchClient aaiReactiveHttpPatchClient = mock(AaiHttpPatchClient.class); - doReturn(mock(AaiClientConfiguration.class)).when(aaiProducerTask).resolveConfiguration(); - doReturn(aaiReactiveHttpPatchClient).when(aaiProducerTask).resolveClient(); + AaiProducerTaskImpl aaiProducerTask = spy(new AaiProducerTaskImpl(aaiReactiveHttpPatchClient)); + return aaiProducerTask; } } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiQueryTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiQueryTaskImplTest.java new file mode 100644 index 00000000..4b6d7a61 --- /dev/null +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AaiQueryTaskImplTest.java @@ -0,0 +1,207 @@ +/* + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.tasks; + +import org.assertj.core.util.Lists; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.onap.dcaegen2.services.prh.integration.junit5.mockito.MockitoExtension; +import org.onap.dcaegen2.services.prh.model.*; +import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.AaiHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.model.AaiModel; +import org.onap.dcaegen2.services.sdk.rest.services.model.AaiServiceInstanceQueryModel; +import reactor.core.publisher.Mono; + +import java.util.Collections; +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; + +@ExtendWith(MockitoExtension.class) +public class AaiQueryTaskImplTest { + @Mock + private AaiHttpClient<AaiModel, AaiPnfResultModel> getPnfModelClient; + + @Mock + private AaiHttpClient<AaiServiceInstanceQueryModel, AaiServiceInstanceResultModel> getServiceClient; + + @Mock + private AaiPnfResultModel pnfResultModel; + + @Mock + private Relationship pnfRelationships; + + @Mock + private RelationshipDict pnfRelation; + + @Mock + private AaiServiceInstanceResultModel serviceModel; + + private final RelationshipData customer = new RelationshipData(); + private final RelationshipData serviceType = new RelationshipData(); + private final RelationshipData serviceInstanceId = new RelationshipData(); + + private List<RelationshipData> allRelationData; + + private AaiQueryTask sut; + + private final AaiModel aaiModel = () -> "SomePNF"; + + @BeforeEach + void setUp() { + customer.setRelationshipKey(AaiQueryTaskImpl.CUSTOMER); + customer.setRelationshipValue("Foo"); + + serviceType.setRelationshipKey(AaiQueryTaskImpl.SERVICE_TYPE); + serviceType.setRelationshipValue("Bar"); + + serviceInstanceId.setRelationshipKey(AaiQueryTaskImpl.SERVICE_INSTANCE_ID); + serviceInstanceId.setRelationshipValue("Baz"); + + allRelationData = Lists.list(customer, serviceType, serviceInstanceId); + + sut = new AaiQueryTaskImpl(getPnfModelClient, getServiceClient); + } + + @Test + void whenPnfIsUnavailable_ShouldThrowException() { + //given + given(getPnfModelClient.getAaiResponse(aaiModel)).willReturn(Mono.error(new Exception("404"))); + + //when + final Mono<Boolean> task = sut.execute(aaiModel); + + //then + Assertions.assertThrows(Exception.class, task::block); + } + + @Test + void whenPnfIsAvailableButRelationshipIsNull_ShouldReturnFalse() { + //given + given(pnfResultModel.getRelationshipList()).willReturn(null); + + configurePnfClient(aaiModel, pnfResultModel); + + //when + final Mono<Boolean> task = sut.execute(aaiModel); + + //then + Assertions.assertFalse(task::block); + } + + @Test + void whenPnfIsAvailableButRelationshipIsEmpty_ShouldReturnFalse() { + //given + given(pnfRelationships.getRelationship()).willReturn(Collections.emptyList()); + given(pnfResultModel.getRelationshipList()).willReturn(pnfRelationships); + configurePnfClient(aaiModel, pnfResultModel); + + //when + final Mono<Boolean> task = sut.execute(aaiModel); + + //then + Assertions.assertFalse(task::block); + } + + @Test + void whenPnfIsAvailableButServiceRelationIsMissing_ShouldReturnFalse() { + //given + given(pnfRelation.getRelatedTo()).willReturn("some-other-relation"); + given(pnfRelationships.getRelationship()).willReturn(Collections.singletonList(pnfRelation)); + given(pnfResultModel.getRelationshipList()).willReturn(pnfRelationships); + + configurePnfClient(aaiModel, pnfResultModel); + + //when + final Mono<Boolean> task = sut.execute(aaiModel); + + //then + Assertions.assertFalse(task::block); + } + + @Test + void whenPnfIsAvailableButServiceRelationIsMissingRequiredKey_ShouldReturnFalse() { + //given + Collections.shuffle(allRelationData); + allRelationData.remove(0); + + given(pnfRelation.getRelatedTo()).willReturn(AaiQueryTaskImpl.RELATED_TO); + given(pnfRelation.getRelationshipData()).willReturn(allRelationData); + given(pnfRelationships.getRelationship()).willReturn(Collections.singletonList(pnfRelation)); + given(pnfResultModel.getRelationshipList()).willReturn(pnfRelationships); + + configurePnfClient(aaiModel, pnfResultModel); + + //when + final Mono<Boolean> task = sut.execute(aaiModel); + + //then + Assertions.assertFalse(task::block); + } + + @Test + void whenPnfIsAvailableAndServiceRelationIsCompleteButServiceIsInactive_ShouldReturnFalse() { + //given + given(serviceModel.getOrchestrationStatus()).willReturn("Inactive"); + given(getServiceClient.getAaiResponse(any())).willReturn(Mono.just(serviceModel)); + + given(pnfRelation.getRelatedTo()).willReturn(AaiQueryTaskImpl.RELATED_TO); + given(pnfRelation.getRelationshipData()).willReturn(allRelationData); + given(pnfRelationships.getRelationship()).willReturn(Collections.singletonList(pnfRelation)); + given(pnfResultModel.getRelationshipList()).willReturn(pnfRelationships); + + configurePnfClient(aaiModel, pnfResultModel); + + //when + final Mono<Boolean> task = sut.execute(aaiModel); + + //then + Assertions.assertFalse(task::block); + } + + @Test + void whenPnfIsAvailableAndServiceRelationIsCompleteButServiceIsActive_ShouldReturnFalse() { + //given + given(serviceModel.getOrchestrationStatus()).willReturn("Active"); + given(getServiceClient.getAaiResponse(any())).willReturn(Mono.just(serviceModel)); + + given(pnfRelation.getRelatedTo()).willReturn(AaiQueryTaskImpl.RELATED_TO); + given(pnfRelation.getRelationshipData()).willReturn(allRelationData); + given(pnfRelationships.getRelationship()).willReturn(Collections.singletonList(pnfRelation)); + given(pnfResultModel.getRelationshipList()).willReturn(pnfRelationships); + + configurePnfClient(aaiModel, pnfResultModel); + + //when + final Mono<Boolean> task = sut.execute(aaiModel); + + //then + Assertions.assertTrue(task::block); + } + + private void configurePnfClient(final AaiModel aaiModel, final AaiPnfResultModel pnfResultModel) { + given(getPnfModelClient.getAaiResponse(aaiModel)).willReturn(Mono.just(pnfResultModel)); + } +} diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskTest.java index 197d9a8c..ae3af7b9 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskTest.java @@ -68,7 +68,7 @@ class BbsActionsTaskTest { ConsumerDmaapModel consumerDmaapModel = buildConsumerDmaapModel(null); // when - ConsumerDmaapModel result = new BbsActionsTask(appConfig, httpClient).execute(consumerDmaapModel).block(); + ConsumerDmaapModel result = new BbsActionsTaskImpl(appConfig, httpClient).execute(consumerDmaapModel).block(); // then verifyZeroInteractions(httpClient); @@ -85,7 +85,7 @@ class BbsActionsTaskTest { ConsumerDmaapModel consumerDmaapModel = buildConsumerDmaapModel(additionalFields); // when - ConsumerDmaapModel result = new BbsActionsTask(appConfig, httpClient).execute(consumerDmaapModel).block(); + ConsumerDmaapModel result = new BbsActionsTaskImpl(appConfig, httpClient).execute(consumerDmaapModel).block(); // then verifyZeroInteractions(httpClient); @@ -104,7 +104,7 @@ class BbsActionsTaskTest { given(httpClient.call(any())).willReturn(Mono.just(buildAaiResponse(HttpResponseStatus.OK))); // when - Mono<ConsumerDmaapModel> response = new BbsActionsTask(appConfig, httpClient).execute(consumerDmaapModel); + Mono<ConsumerDmaapModel> response = new BbsActionsTaskImpl(appConfig, httpClient).execute(consumerDmaapModel); // then ArgumentCaptor<HttpRequest> captor = ArgumentCaptor.forClass(HttpRequest.class); @@ -115,7 +115,7 @@ class BbsActionsTaskTest { assertThat(request.url()).isEqualTo( "https://aai.onap.svc.cluster.local:8443/aai/v12/network/logical-links/logical-link/some-link"); assertJsonEquals(request.body(), CORRECT_LOGICAL_LINK_JSON); - assertThat(request.headers().toJavaMap()).containsOnlyKeys("X-InvocationID", "X-RequestID"); + assertThat(request.headers().toJavaMap()).containsOnlyKeys("X-InvocationID", "X-RequestID", "Content-Type"); assertEquals(consumerDmaapModel, response.block()); } @@ -131,7 +131,7 @@ class BbsActionsTaskTest { given(httpClient.call(any())).willReturn(Mono.just(buildAaiResponse(HttpResponseStatus.INTERNAL_SERVER_ERROR))); // when - Mono<ConsumerDmaapModel> response = new BbsActionsTask(appConfig, httpClient).execute(consumerDmaapModel); + Mono<ConsumerDmaapModel> response = new BbsActionsTaskImpl(appConfig, httpClient).execute(consumerDmaapModel); // then ArgumentCaptor<HttpRequest> captor = ArgumentCaptor.forClass(HttpRequest.class); @@ -141,7 +141,7 @@ class BbsActionsTaskTest { HttpRequest request = captor.getValue(); assertThat(request.url()).isEqualTo(AAI_URL); assertJsonEquals(request.body(), CORRECT_LOGICAL_LINK_JSON); - assertThat(request.headers().toJavaMap()).containsOnlyKeys("X-InvocationID", "X-RequestID"); + assertThat(request.headers().toJavaMap()).containsOnlyKeys("X-InvocationID", "X-RequestID", "Content-Type"); assertThatThrownBy(response::block).hasCauseInstanceOf(AaiFailureException.class); } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapProducerTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapProducerTaskSpy.java index 57d28488..08b10d98 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapProducerTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapProducerTaskSpy.java @@ -32,6 +32,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import java.util.function.Supplier; + /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18 */ @@ -46,10 +48,11 @@ public class DmaapProducerTaskSpy { @Bean @Primary public DmaapPublisherTask registerSimpleDmaapPublisherTask() throws SSLException { - AppConfig appConfig = spy(AppConfig.class); + final AppConfig appConfig = spy(AppConfig.class); + final Supplier<DmaapPublisherConfiguration> configSupplier = () -> appConfig.getDmaapPublisherConfiguration(); doReturn(mock(DmaapPublisherConfiguration.class)).when(appConfig).getDmaapPublisherConfiguration(); - DmaapPublisherTaskImpl dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig)); - DMaaPPublisherReactiveHttpClient extendedDmaapProducerHttpClient = mock( + final DmaapPublisherTaskImpl dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(configSupplier)); + final DMaaPPublisherReactiveHttpClient extendedDmaapProducerHttpClient = mock( DMaaPPublisherReactiveHttpClient.class); doReturn(extendedDmaapProducerHttpClient).when(dmaapPublisherTask).resolveClient(); return dmaapPublisherTask; diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java index 605af595..6bcf6737 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java @@ -20,18 +20,7 @@ package org.onap.dcaegen2.services.prh.tasks; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; -import static org.onap.dcaegen2.services.prh.TestAppConfiguration.createDefaultDmaapPublisherConfiguration; - import io.netty.handler.codec.http.HttpResponseStatus; -import java.util.Optional; -import javax.net.ssl.SSLException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; @@ -39,16 +28,23 @@ import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.DMaaPPublisherReactiveHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.PublisherReactiveHttpClientFactory; import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import reactor.core.publisher.Mono; -import reactor.netty.http.client.HttpClientResponse; import reactor.test.StepVerifier; -; +import javax.net.ssl.SSLException; +import java.util.Optional; +import java.util.function.Supplier; + +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; +import static org.onap.dcaegen2.services.prh.TestAppConfiguration.createDefaultDmaapPublisherConfiguration; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/17/18 @@ -63,6 +59,7 @@ class DmaapPublisherTaskImplTest { private Optional<RequestDiagnosticContext> requestDiagnosticContextOptionalMock; private DmaapModel dmaapModel; private PublisherReactiveHttpClientFactory publisherReactiveHttpClientFactory; + private Supplier<DmaapPublisherConfiguration> configSupplier; @BeforeEach public void beforeEach() throws SSLException { @@ -76,12 +73,13 @@ class DmaapPublisherTaskImplTest { when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); when(publisherReactiveHttpClientFactory.create(dmaapPublisherConfiguration)) .thenReturn(dMaaPPublisherReactiveHttpClient); + configSupplier = () -> appConfig.getDmaapPublisherConfiguration(); } @Test void execute_whenPassedObjectDoesntFit_ThrowsPrhTaskException() { //given - dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig); + dmaapPublisherTask = new DmaapPublisherTaskImpl(configSupplier); //when Executable executableFunction = () -> dmaapPublisherTask.execute(null); //then @@ -93,8 +91,8 @@ class DmaapPublisherTaskImplTest { void execute_whenPassedObjectFits_ReturnsCorrectStatus() throws PrhTaskException, SSLException { //given HttpResponseStatus httpResponseStatus = HttpResponseStatus.OK; - HttpClientResponse httpClientReponse = prepareMocksForTests(httpResponseStatus); - dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig, publisherReactiveHttpClientFactory); + HttpResponse httpClientReponse = prepareMocksForTests(httpResponseStatus); + dmaapPublisherTask = new DmaapPublisherTaskImpl(configSupplier, publisherReactiveHttpClientFactory); //when StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel)).expectSubscription() @@ -111,8 +109,8 @@ class DmaapPublisherTaskImplTest { void execute_whenPassedObjectFits_butIncorrectResponseReturns() throws DmaapNotFoundException, SSLException { //given HttpResponseStatus httpResponseStatus = HttpResponseStatus.UNAUTHORIZED; - HttpClientResponse httpClientReponse = prepareMocksForTests(httpResponseStatus); - dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig, publisherReactiveHttpClientFactory); + HttpResponse httpClientReponse = prepareMocksForTests(httpResponseStatus); + dmaapPublisherTask = new DmaapPublisherTaskImpl(configSupplier, publisherReactiveHttpClientFactory); //when StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel)).expectSubscription() @@ -128,8 +126,8 @@ class DmaapPublisherTaskImplTest { void execute_whenConsumerDmaapModelIsNull() { //given HttpResponseStatus httpResponseStatus = HttpResponseStatus.UNAUTHORIZED; - HttpClientResponse httpClientReponse = prepareMocksForTests(httpResponseStatus); - dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig, publisherReactiveHttpClientFactory); + HttpResponse httpClientReponse = prepareMocksForTests(httpResponseStatus); + dmaapPublisherTask = new DmaapPublisherTaskImpl(configSupplier, publisherReactiveHttpClientFactory); assertThrows(DmaapNotFoundException.class, () -> { dmaapPublisherTask.execute(null); }); @@ -138,16 +136,16 @@ class DmaapPublisherTaskImplTest { @Test public void resolveClient() throws SSLException { //given - dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig, publisherReactiveHttpClientFactory); + dmaapPublisherTask = new DmaapPublisherTaskImpl(configSupplier, publisherReactiveHttpClientFactory); //when DMaaPPublisherReactiveHttpClient dMaaPPublisherReactiveHttpClientResolved = dmaapPublisherTask.resolveClient(); //then assertSame(dMaaPPublisherReactiveHttpClientResolved, dMaaPPublisherReactiveHttpClient); } - private HttpClientResponse prepareMocksForTests(HttpResponseStatus httpResponseStatus) { - HttpClientResponse httpClientResponse = mock(HttpClientResponse.class); - when(httpClientResponse.status()).thenReturn(httpResponseStatus); + private HttpResponse prepareMocksForTests(HttpResponseStatus httpResponseStatus) { + HttpResponse httpClientResponse = mock(HttpResponse.class); + when(httpClientResponse.statusCode()).thenReturn(httpResponseStatus.code()); when( dMaaPPublisherReactiveHttpClient.getDMaaPProducerResponse(dmaapModel, requestDiagnosticContextOptionalMock)) .thenReturn(Mono.just(httpClientResponse)); diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java index b12b3d39..8d41ec63 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java @@ -25,6 +25,7 @@ import static org.mockito.Mockito.spy; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -38,8 +39,16 @@ public class ScheduleControllerSpy { @Autowired private DmaapConsumerTask dmaapConsumerTaskImplSpy; + @Qualifier("ReadyPublisherTask") @Autowired - private DmaapPublisherTask dmaapPublisherTaskImplSpy; + private DmaapPublisherTask dmaapReadyPublisherTaskImplSpy; + + @Qualifier("UpdatePublisherTask") + @Autowired + private DmaapPublisherTask dmaapUpdatePublisherTaskImplSpy; + + @Autowired + private AaiQueryTask aaiQueryTaskImplSpy; @Autowired private AaiProducerTask aaiPublisherTaskImplSpy; @@ -54,10 +63,12 @@ public class ScheduleControllerSpy { @Primary public ScheduledTasks registerSimpleScheduledTask() { return spy(new ScheduledTasks( - dmaapConsumerTaskImplSpy, - dmaapPublisherTaskImplSpy, - aaiPublisherTaskImplSpy, - bbsActionsTaskImplSpy, - mdcContextMap)); + dmaapConsumerTaskImplSpy, + dmaapReadyPublisherTaskImplSpy, + dmaapUpdatePublisherTaskImplSpy, + aaiQueryTaskImplSpy, + aaiPublisherTaskImplSpy, + bbsActionsTaskImplSpy, + mdcContextMap)); } } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasksTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasksTest.java new file mode 100644 index 00000000..9a3099c5 --- /dev/null +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasksTest.java @@ -0,0 +1,185 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.tasks; + +import org.apache.http.HttpResponse; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; +import org.onap.dcaegen2.services.prh.integration.junit5.mockito.MockitoExtension; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import javax.net.ssl.SSLException; +import java.util.Collections; +import java.util.Map; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class ScheduledTasksTest { + private final static ConsumerDmaapModel DMAAP_MODEL = + ImmutableConsumerDmaapModel + .builder() + .correlationId("SomeId") + .ipv4("ipv4") + .ipv6("ipv6") + .build(); + + @Mock + private DmaapPublisherTask readyPublisher; + + @Mock + private DmaapPublisherTask updatePublisher; + + @Mock + private DmaapConsumerTask consumer; + + @Mock + private BbsActionsTask bbsActions; + + @Mock + private AaiQueryTask aaiQuery; + + @Mock + private AaiProducerTask aaiProducer; + + private final Map<String, String> context = Collections.emptyMap(); + + private ScheduledTasks sut; + + @BeforeEach + void setUp() throws PrhTaskException, SSLException { + final Mono<ConsumerDmaapModel> consumerModel = Mono.just(DMAAP_MODEL); + + given(aaiProducer.execute(DMAAP_MODEL)).willReturn(consumerModel); + given(bbsActions.execute(DMAAP_MODEL)).willReturn(consumerModel); + + sut = new ScheduledTasks( + consumer, + readyPublisher, + updatePublisher, + aaiQuery, + aaiProducer, + bbsActions, + context); + } + + @Test + void whenEmptyResultFromDMaaPConsumer_NotActionShouldBePerformed() throws SSLException, PrhTaskException { + //given + given(consumer.execute(anyString())).willReturn(Flux.empty()); + + //when + sut.scheduleMainPrhEventTask(); + + //then + verifyThatPnfUpdateWasNotSentToAai(); + verifyIfLogicalLinkWasNotCreated(); + verifyThatPnfModelWasNotSentDmaapPnfReadyTopic(); + verifyThatPnfModelWasNotSentDmaapPnfUpdateTopic(); + } + + @Test + void whenPnfNotFoundInAai_NotActionShouldBePerformed() throws SSLException, PrhTaskException { + //given + given(consumer.execute(anyString())).willReturn(Flux.just(DMAAP_MODEL)); + given(aaiQuery.execute(any())).willReturn(Mono.error(new PrhTaskException("404 Not Found"))); + + //when + sut.scheduleMainPrhEventTask(); + + verifyThatPnfUpdateWasNotSentToAai(); + verifyIfLogicalLinkWasNotCreated(); + verifyThatPnfModelWasNotSentDmaapPnfReadyTopic(); + verifyThatPnfModelWasNotSentDmaapPnfUpdateTopic(); + } + + @Test + void whenPnfWithoutService_PatchToAaiAndPostToPnfReadyShouldBePerformed() throws SSLException, PrhTaskException { + //given + given(consumer.execute(anyString())).willReturn(Flux.just(DMAAP_MODEL)); + given(aaiQuery.execute(any())).willReturn(Mono.just(false)); + + //when + sut.scheduleMainPrhEventTask(); + + //then + verifyThatPnfUpdateWasSentToAai(); + verifyIfLogicalLinkWasCreated(); + verifyThatPnfModelWasSentDmaapPnfReadyTopic(); + verifyThatPnfModelWasNotSentDmaapPnfUpdateTopic(); + } + + @Test + void whenPnfHasActiveService_OnlyPostToPnfUpdateShouldBePerformed() throws SSLException, PrhTaskException { + //given + given(consumer.execute(anyString())).willReturn(Flux.just(DMAAP_MODEL)); + given(aaiQuery.execute(any())).willReturn(Mono.just(true)); + + //when + sut.scheduleMainPrhEventTask(); + + //then + verifyThatPnfUpdateWasNotSentToAai(); + verifyIfLogicalLinkWasNotCreated(); + verifyThatPnfModelWasNotSentDmaapPnfReadyTopic(); + verifyThatPnfModelWasSentDmaapPnfUpdateTopic(); + } + + private void verifyThatPnfModelWasNotSentDmaapPnfReadyTopic() { + verify(readyPublisher, never()).executeWithApache(DMAAP_MODEL); + } + + private Mono<HttpResponse> verifyThatPnfModelWasNotSentDmaapPnfUpdateTopic() { + return verify(updatePublisher, never()).executeWithApache(DMAAP_MODEL); + } + + private void verifyThatPnfModelWasSentDmaapPnfReadyTopic() { + verify(readyPublisher, atLeastOnce()).executeWithApache(DMAAP_MODEL); + } + + private Mono<HttpResponse> verifyThatPnfModelWasSentDmaapPnfUpdateTopic() { + return verify(updatePublisher, atLeastOnce()).executeWithApache(DMAAP_MODEL); + } + + private void verifyThatPnfUpdateWasNotSentToAai() throws PrhTaskException, SSLException { + verify(aaiProducer, never()).execute(DMAAP_MODEL); + } + + private void verifyThatPnfUpdateWasSentToAai() throws PrhTaskException, SSLException { + verify(aaiProducer, atLeastOnce()).execute(DMAAP_MODEL); + } + + private void verifyIfLogicalLinkWasCreated(){ + verify(bbsActions, atLeastOnce()).execute(DMAAP_MODEL); + } + + private void verifyIfLogicalLinkWasNotCreated(){ + verify(bbsActions, never()).execute(DMAAP_MODEL); + } +}
\ No newline at end of file |