diff options
author | tkogut <tomasz.kogut@nokia.com> | 2021-04-13 10:11:35 +0200 |
---|---|---|
committer | tkogut <tomasz.kogut@nokia.com> | 2021-04-14 17:23:50 +0200 |
commit | f789f254efe024085663bbe04f99f10af4a98fe7 (patch) | |
tree | a6d99beee49fc931bb16d58932c2ec5e9aeb47c5 /src/test/java | |
parent | 0f08018a05f3eff7cacb91d7b3b0fe272f19e269 (diff) |
[DCAE/PM-Mapper] Utilize DMaaP-Client in PM-Mapper
- Bump mockserver libraries
- Use dmaap-client for sending events to dmapp-mr
- Extract Retry/Timeout configs to separate class
- Extract logging utils to separate class
Issue-ID: DCAEGEN2-2732
Change-Id: I5d406e99fe1def078f102ff704df5312f5ae996b
Signed-off-by: tkogut <tomasz.kogut@nokia.com>
Diffstat (limited to 'src/test/java')
6 files changed, 215 insertions, 48 deletions
diff --git a/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java b/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java index e5c5af4..47e09e9 100644 --- a/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java +++ b/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2021 Nokia. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,75 +19,122 @@ * ============LICENSE_END========================================================= */ package org.onap.dcaegen2.pmmapper.messagerouter; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.onap.dcaegen2.services.pmmapper.exceptions.RequestFailure; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import reactor.test.StepVerifier; -import java.util.Arrays; -import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.onap.dcaegen2.services.pmmapper.messagerouter.VESPublisher; -import org.onap.dcaegen2.services.pmmapper.utils.EnvironmentConfig; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; -import org.onap.dcaegen2.services.pmmapper.utils.RequestSender; +import org.onap.dcaegen2.services.pmmapper.utils.DmaapRequestSender; +import org.onap.dcaegen2.services.pmmapper.utils.EnvironmentConfig; +import org.onap.dcaegen2.services.sdk.model.streams.ImmutableAafCredentials; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishResponse; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +import java.util.Arrays; +import java.util.List; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) @PrepareForTest(EnvironmentConfig.class) @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"}) public class VESPublisherTest { - private static String topicURL = "http://mr/topic"; - private static RequestSender sender; - private static MapperConfig config; + private static final String TOPIC_URL = "http://mr/topic"; + private static final String VES = "{}"; + private static final ImmutableAafCredentials AAF_CREDENTIALS = ImmutableAafCredentials.builder() + .username("") + .password("") + .build(); + + private static final MessageRouterPublishResponse SUCCESSFUL = + ImmutableMessageRouterPublishResponse.builder().build(); + private static final MessageRouterPublishResponse FAILED = + ImmutableMessageRouterPublishResponse.builder() + .failReason("failReason") + .build(); + + private DmaapRequestSender sender; private VESPublisher sut; - private String ves = "{}"; @Before - public void before() throws Exception { - config = mock(MapperConfig.class); - sender = mock(RequestSender.class); + public void before() { + MapperConfig config = mock(MapperConfig.class); + when(config.getPublisherTopicUrl()).thenReturn(TOPIC_URL); + when(config.getPublisherUserName()).thenReturn(""); + when(config.getPublisherPassword()).thenReturn(""); + sender = mock(DmaapRequestSender.class); sut = new VESPublisher(config, sender); - when(config.getPublisherTopicUrl()).thenReturn(topicURL); } @Test - public void publish_multiple_success() throws Exception { + public void publish_multiple_success() { + Event event = mock(Event.class); + List<Event> events = Arrays.asList(event, event, event); + when(event.getVes()).thenReturn(VES); + MessageRouterPublishResponse successfulResponse = ImmutableMessageRouterPublishResponse.builder().build(); + when(sender.send(any(), any(), any())).thenReturn(Flux.just(successfulResponse, successfulResponse)); + + Flux<Event> flux = sut.publish(events); + + verify(sender, times(1)).send(anyString(), any(), any()); + StepVerifier.create(flux) + .expectNextMatches(event::equals) + .verifyComplete(); + } + + @Test + public void publish_multiple_fail_sender_exceptions() { + Event event = mock(Event.class); + List<Event> events = Arrays.asList(event, event, event); + when(event.getVes()).thenReturn(VES); + when(sender.send(eq(TOPIC_URL), any(), eq(AAF_CREDENTIALS))) + .thenReturn(Flux.error(new RuntimeException())); + + Flux<Event> flux = sut.publish(events); + + StepVerifier.create(flux) + .verifyComplete(); + } + + @Test + public void publish_multiple_fail_mr_responses_failed() { Event event = mock(Event.class); - List<Event> events = Arrays.asList(event,event,event); - when(event.getVes()).thenReturn(ves); + List<Event> events = Arrays.asList(event, event, event); + when(event.getVes()).thenReturn(VES); + when(sender.send(eq(TOPIC_URL), any(), eq(AAF_CREDENTIALS))) + .thenReturn(Flux.just(FAILED, FAILED, FAILED)); Flux<Event> flux = sut.publish(events); - verify(sender, times(3)).send(Mockito.anyString(),Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); StepVerifier.create(flux) - .expectNextMatches(event::equals) - .expectComplete() - .verify(); + .verifyComplete(); } @Test - public void publish_multiple_fail() throws Exception { + public void publish_multiple_fail_and_multiple_success() { Event event = mock(Event.class); - List<Event> events = Arrays.asList(event,event,event); - when(event.getVes()).thenReturn(ves); - when(sender.send("POST",topicURL,ves,"base64encoded")).thenThrow(RequestFailure.class); + when(event.getVes()).thenReturn(VES); + List<Event> events = Arrays.asList(event, event, event, event); + when(sender.send(eq(TOPIC_URL), any(), eq(AAF_CREDENTIALS))) + .thenReturn(Flux.just(SUCCESSFUL, FAILED, SUCCESSFUL, FAILED)); Flux<Event> flux = sut.publish(events); StepVerifier.create(flux) - .expectNext(events.get(0)) - .verifyComplete(); + .verifyComplete(); } } diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/AppTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/AppTest.java index db45029..617cbd1 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/AppTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/AppTest.java @@ -62,7 +62,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; -import org.mockserver.client.server.MockServerClient; +import org.mockserver.client.MockServerClient; import org.mockserver.integration.ClientAndServer; import org.mockserver.model.HttpRequest; import org.onap.dcaegen2.services.pmmapper.filtering.MeasFilterHandler; diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java index 7ddc929..26cb648 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2021 Nokia. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +43,7 @@ import java.util.List; import org.everit.json.schema.Schema; import org.everit.json.schema.loader.SchemaLoader; +import org.json.JSONException; import org.json.JSONObject; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -79,7 +81,7 @@ class MapperTest { @BeforeAll - static void classSetup() throws IOException { + static void classSetup() throws IOException, JSONException { JSONObject ves = new JSONObject(new String(Files.readAllBytes(schema))); vesSchema = SchemaLoader.load(ves); @@ -97,7 +99,7 @@ class MapperTest { @ParameterizedTest @MethodSource("getValidEvents") - void testValidEvent(Event testEvent) { + void testValidEvent(Event testEvent) throws JSONException { vesSchema.validate(new JSONObject(objUnderTest.map(testEvent))); } diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/DataRouterUtilsTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/DataRouterUtilsTest.java index 0451543..1c6d850 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/DataRouterUtilsTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/DataRouterUtilsTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 - 2020 Nordix Foundation. + * Copyright (C) 2021 Nokia. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,6 +66,7 @@ public class DataRouterUtilsTest { private static MapperConfig validConfig; private SSLContextFactory sslContextFactory; private static final Path validConfigPath = Paths.get("src/test/resources/valid_mapper_config.json"); + private static final String DELETE = "DELETE"; @Test public void processEventSuccessful() throws Exception { @@ -94,7 +96,7 @@ public class DataRouterUtilsTest { Event testEvent = EventUtils.makeMockEvent("", mock(EventMetadata.class), publishIdentity); assertEquals(serviceResponse, DataRouterUtils.processEvent(mockMapperConfig, testEvent)); - verify(mockConnection, times(1)).setRequestMethod(RequestSender.DELETE); + verify(mockConnection, times(1)).setRequestMethod(DELETE); } @Test @@ -117,7 +119,7 @@ public class DataRouterUtilsTest { Event testEvent = EventUtils.makeMockEvent("", mock(EventMetadata.class), publishIdentity); assertEquals(serviceResponse, DataRouterUtils.processEvent(mockMapperConfig, testEvent)); - verify(mockConnection, times(1)).setRequestMethod(RequestSender.DELETE); + verify(mockConnection, times(1)).setRequestMethod(DELETE); } @Test @@ -148,7 +150,7 @@ public class DataRouterUtilsTest { PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL); Event testEvent = EventUtils.makeMockEvent("", mock(EventMetadata.class), publishIdentity); assertEquals(serviceResponse, DataRouterUtils.processEvent(mockMapperConfig, testEvent)); - verify(mockConnection, times(5)).setRequestMethod(RequestSender.DELETE); + verify(mockConnection, times(5)).setRequestMethod(DELETE); } @Test diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/DmaapRequestSenderTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/DmaapRequestSenderTest.java new file mode 100644 index 0000000..50abb5f --- /dev/null +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/DmaapRequestSenderTest.java @@ -0,0 +1,112 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nokia. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.pmmapper.utils; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockserver.client.MockServerClient; +import org.mockserver.integration.ClientAndServer; +import org.mockserver.model.HttpStatusCode; +import org.mockserver.verify.VerificationTimes; +import org.onap.dcaegen2.services.sdk.model.streams.ImmutableAafCredentials; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.DmaapResponse; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse; +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +import java.util.Collections; +import java.util.List; + +import static org.mockserver.integration.ClientAndServer.startClientAndServer; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +public class DmaapRequestSenderTest { + + private static final ImmutableAafCredentials AAF_CREDENTIALS = ImmutableAafCredentials.builder() + .username("") + .password("") + .build(); + private static final List<String> SINGLE = Collections.singletonList("any"); + + private static ClientAndServer mockServer; + private final MockServerClient client = mockClient(); + + @BeforeClass + public static void setup() { + mockServer = startClientAndServer(35454); + } + + @AfterClass + public static void teardown() { + mockServer.stop(); + } + + @Before + public void setUp() { + client.reset(); + } + + @Test + public void send_success() { + client.when(request()).respond(response() + .withStatusCode(HttpStatusCode.OK_200.code()) + .withBody("ResponseBody")); + + Flux<MessageRouterPublishResponse> result = new DmaapRequestSender() + .send("http://127.0.0.1:35454/once", SINGLE, AAF_CREDENTIALS); + + StepVerifier.create(result) + .expectNextMatches(DmaapResponse::successful) + .verifyComplete(); + client.verify(request(), VerificationTimes.once()); + } + + @Test + public void host_unavailable_retry_mechanism() { + client.when(request()) + .respond(response().withStatusCode(HttpStatusCode.SERVICE_UNAVAILABLE_503.code())); + + Flux<MessageRouterPublishResponse> result = new DmaapRequestSender() + .send("http://127.0.0.1:35454/anypath", SINGLE, AAF_CREDENTIALS); + + StepVerifier.create(result) + .expectNextMatches(DmaapResponse::failed) + .verifyComplete(); + client.verify(request(), VerificationTimes.exactly(5)); + } + + @Test + public void host_unknown() { + Flux<MessageRouterPublishResponse> result = new DmaapRequestSender() + .send("http://unknown-host:35454/host-is-unknown", SINGLE, AAF_CREDENTIALS); + + StepVerifier.create(result) + .verifyError(); + client.verify(request(), VerificationTimes.exactly(0)); + } + + private MockServerClient mockClient() { + return new MockServerClient("127.0.0.1", 35454); + } +} diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java index 08faf4a..8541824 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2021 Nokia. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,15 +31,15 @@ import java.net.URL; import java.net.UnknownHostException; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockserver.client.server.MockServerClient; +import org.mockserver.client.MockServerClient; import org.mockserver.integration.ClientAndServer; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpStatusCode; import org.mockserver.verify.VerificationTimes; -import org.onap.dcaegen2.services.pmmapper.utils.RequestSender; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; @@ -53,7 +54,7 @@ import utils.LoggingUtils; @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"}) public class RequestSenderTests { private static ClientAndServer mockServer; - private MockServerClient client = mockClient(); + private final MockServerClient client = mockClient(); @BeforeClass public static void setup() { @@ -65,6 +66,11 @@ public class RequestSenderTests { mockServer.stop(); } + @Before + public void setUp() { + client.reset(); + } + @Test public void send_success() throws Exception { String url = "http://127.0.0.1:35454/once"; @@ -84,11 +90,10 @@ public class RequestSenderTests { assertTrue(logAppender.list.get(1).getMessage().contains("Sending")); assertTrue(logAppender.list.get(2).getMessage().contains("Received")); logAppender.stop(); - client.clear(req); } @Test - public void host_unavailable_retry_mechanism() throws Exception { + public void host_unavailable_retry_mechanism() { PowerMockito.mockStatic(Thread.class); client.when(request()) @@ -99,7 +104,6 @@ public class RequestSenderTests { }); client.verify(request(), VerificationTimes.exactly(5)); - client.clear(request()); } @Test @@ -114,11 +118,10 @@ public class RequestSenderTests { }); client.verify(request(), VerificationTimes.exactly(0)); - client.clear(request()); } private MockServerClient mockClient() { return new MockServerClient("127.0.0.1", 35454); } -}
\ No newline at end of file +} |