diff options
author | JoeOLeary <joseph.o.leary@est.tech> | 2019-01-28 13:44:27 +0000 |
---|---|---|
committer | JoeOLeary <joseph.o.leary@est.tech> | 2019-01-28 13:44:27 +0000 |
commit | becd8eebb1079e8cb970824ef704f5eebfdbdd42 (patch) | |
tree | a9336440fe0fb6df17a2266b0371373a93e9fc8c /src/test/java/org/onap | |
parent | 33103f18d4ffd56a84ee72597d75c26a189da2fd (diff) |
POM Updates
*Added the ONAP parent POM to the project POM
*Introduced support for building and pushing Docker images in the POM
*Added basic Dockerfile
*Updated source to comply with checkstyles in parent POM
Change-Id: Ieabd0a911359b107ea0a5b2e65ca3260f990e3cd
Issue-ID: DCAEGEN2-1123
Signed-off-by: JoeOLeary <joseph.o.leary@est.tech>
Diffstat (limited to 'src/test/java/org/onap')
-rw-r--r-- | src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java | 93 |
1 files changed, 47 insertions, 46 deletions
diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java index 8f73c91..25fb8ae 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java @@ -17,48 +17,46 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ + package org.onap.dcaegen2.services.pmmapper.datarouter; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; +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 com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import io.undertow.io.Receiver; +import io.undertow.io.Sender; +import io.undertow.server.HttpServerExchange; +import io.undertow.util.StatusCodes; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.onap.dcaegen2.services.pmmapper.config.BusControllerConfig; import org.onap.dcaegen2.services.pmmapper.exceptions.TooManyTriesException; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.dcaegen2.services.pmmapper.model.EventMetadata; -import io.undertow.io.Receiver; -import io.undertow.io.Sender; -import io.undertow.server.HttpServerExchange; -import io.undertow.util.StatusCodes; - -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Paths; - -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.anyInt; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doThrow; -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(DataRouterSubscriber.class) @@ -79,22 +77,22 @@ public class DataRouterSubscriberTest { public void testStartTooManyTriesWithResponse() throws IOException { PowerMockito.mockStatic(Thread.class); - URL subURL = mock(URL.class); + URL subEndpoint = mock(URL.class); BusControllerConfig config = new BusControllerConfig(); - config.setDataRouterSubscribeEndpoint(subURL); + config.setDataRouterSubscribeEndpoint(subEndpoint); HttpURLConnection huc = mock(HttpURLConnection.class, RETURNS_DEEP_STUBS); - when(subURL.openConnection()).thenReturn(huc); + when(subEndpoint.openConnection()).thenReturn(huc); when(huc.getResponseCode()).thenReturn(300); Assertions.assertThrows(TooManyTriesException.class, () -> objUnderTest.start(config)); } @Test public void testStartImmediateSuccess() throws IOException, TooManyTriesException, InterruptedException { - URL subURL = mock(URL.class); + URL subEndpoint = mock(URL.class); BusControllerConfig config = new BusControllerConfig(); - config.setDataRouterSubscribeEndpoint(subURL); + config.setDataRouterSubscribeEndpoint(subEndpoint); HttpURLConnection huc = mock(HttpURLConnection.class, RETURNS_DEEP_STUBS); - when(subURL.openConnection()).thenReturn(huc); + when(subEndpoint.openConnection()).thenReturn(huc); when(huc.getResponseCode()).thenReturn(200); objUnderTest.start(config); verify(huc, times(1)).getResponseCode(); @@ -104,11 +102,11 @@ public class DataRouterSubscriberTest { public void testStartDelayedSuccess() throws IOException, TooManyTriesException, InterruptedException { PowerMockito.mockStatic(Thread.class); - URL subURL = mock(URL.class); + URL subEndpoint = mock(URL.class); BusControllerConfig config = new BusControllerConfig(); - config.setDataRouterSubscribeEndpoint(subURL); + config.setDataRouterSubscribeEndpoint(subEndpoint); HttpURLConnection huc = mock(HttpURLConnection.class, RETURNS_DEEP_STUBS); - when(subURL.openConnection()).thenReturn(huc); + when(subEndpoint.openConnection()).thenReturn(huc); doAnswer(new Answer() { boolean forceRetry = true; @@ -129,11 +127,11 @@ public class DataRouterSubscriberTest { public void testStartReadTimeout() throws IOException { PowerMockito.mockStatic(Thread.class); - URL subURL = mock(URL.class); + URL subEndpoint = mock(URL.class); BusControllerConfig config = new BusControllerConfig(); - config.setDataRouterSubscribeEndpoint(subURL); + config.setDataRouterSubscribeEndpoint(subEndpoint); HttpURLConnection huc = mock(HttpURLConnection.class, RETURNS_DEEP_STUBS); - when(subURL.openConnection()).thenReturn(huc); + when(subEndpoint.openConnection()).thenReturn(huc); doThrow(new IOException()).when(huc).getResponseCode(); Assertions.assertThrows(TooManyTriesException.class, () -> objUnderTest.start(config)); } @@ -160,13 +158,13 @@ public class DataRouterSubscriberTest { verify(eventReceiver, times(0)).receive(any()); } - - @Test public void testRequestInboundInvalidMetadata() throws Exception { HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS); - JsonObject metadata = new JsonParser().parse(new String(Files.readAllBytes(Paths.get("src/test/resources/invalid_metadata.json")))).getAsJsonObject(); - when(httpServerExchange.getRequestHeaders().get(any(String.class)).get(anyInt())).thenReturn(metadata.toString()); + JsonObject metadata = new JsonParser().parse(new String(Files + .readAllBytes(Paths.get("src/test/resources/invalid_metadata.json")))).getAsJsonObject(); + when(httpServerExchange.getRequestHeaders().get(any(String.class)).get(anyInt())) + .thenReturn(metadata.toString()); when(httpServerExchange.setStatusCode(anyInt())).thenReturn(httpServerExchange); objUnderTest.handleRequest(httpServerExchange); verify(httpServerExchange, times(1)).setStatusCode(StatusCodes.BAD_REQUEST); @@ -175,7 +173,7 @@ public class DataRouterSubscriberTest { } @Test - public void testRequestInboundNoMetadata() throws Exception{ + public void testRequestInboundNoMetadata() throws Exception { HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS); Receiver receiver = mock(Receiver.class); when(httpServerExchange.getRequestReceiver()).thenReturn(receiver); @@ -203,10 +201,10 @@ public class DataRouterSubscriberTest { Receiver receiver = mock(Receiver.class); when(httpServerExchange.getRequestReceiver()).thenReturn(receiver); String testString = "MESSAGE BODY"; - JsonObject metadata = new JsonParser().parse(new String(Files.readAllBytes(Paths.get("src/test/resources/valid_metadata.json")))).getAsJsonObject(); - EventMetadata metadataObj = new GsonBuilder().create().fromJson(metadata, EventMetadata.class); - - when(httpServerExchange.getRequestHeaders().get(any(String.class)).get(anyInt())).thenReturn(metadata.toString()); + JsonObject metadata = new JsonParser().parse( + new String(Files.readAllBytes(Paths.get("src/test/resources/valid_metadata.json")))).getAsJsonObject(); + when(httpServerExchange.getRequestHeaders().get(any(String.class)).get(anyInt())) + .thenReturn(metadata.toString()); doAnswer((Answer<Void>) invocationOnMock -> { Receiver.FullStringCallback callback = invocationOnMock.getArgument(0); callback.handle(httpServerExchange, testString); @@ -220,6 +218,9 @@ public class DataRouterSubscriberTest { }).when(httpServerExchange).dispatch(any(Runnable.class)); objUnderTest.handleRequest(httpServerExchange); - verify(eventReceiver, times(1)).receive(new Event(httpServerExchange, testString, metadataObj)); + verify(eventReceiver, times(1)) + .receive(new Event(httpServerExchange, testString, + new GsonBuilder().create() + .fromJson(metadata, EventMetadata.class))); } } |