diff options
Diffstat (limited to 'client/client-monitoring')
3 files changed, 180 insertions, 42 deletions
diff --git a/client/client-monitoring/pom.xml b/client/client-monitoring/pom.xml index fb150cb08..55e1c0649 100644 --- a/client/client-monitoring/pom.xml +++ b/client/client-monitoring/pom.xml @@ -64,19 +64,8 @@ <artifactId>commons-cli</artifactId> </dependency> <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-module-junit4</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-api-mockito</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-module-junit4-rule-agent</artifactId> - <version>${version.powermock}</version> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> <scope>test</scope> </dependency> </dependencies> diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java index 07e2efd13..c5c4b1291 100644 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java +++ b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java @@ -89,7 +89,7 @@ public class ApexMonitoringRestResource { public Response createSession(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port) { final Gson gson = new Gson(); final String host = hostName + ":" + port; - final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port); + final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port); try { engineServiceFacade.init(); @@ -175,7 +175,7 @@ public class ApexMonitoringRestResource { @Path("startstop/") public Response startStop(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port, @QueryParam("engineId") final String engineId, @QueryParam("startstop") final String startStop) { - final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port); + final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port); try { engineServiceFacade.init(); @@ -227,7 +227,7 @@ public class ApexMonitoringRestResource { public Response periodiceventStartStop(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port, @QueryParam("engineId") final String engineId, @QueryParam("startstop") final String startStop, @QueryParam("period") final long period) { - final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port); + final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port); final String host = hostName + ":" + port; try { engineServiceFacade.init(); @@ -312,6 +312,18 @@ public class ApexMonitoringRestResource { return valueList; } + + /** + * Get an engine service facade for sending REST requests. This method is package because it is used by unit test. + * + * @param hostName the host name of the Apex engine + * @param port the port of the Apex engine + * @return the engine service facade + */ + protected EngineServiceFacade getEngineServiceFacade(final String hostName, final int port) { + return new EngineServiceFacade(hostName, port); + } + /** * A list of values that uses a FIFO sliding window of a fixed size. */ diff --git a/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/RestResourceTest.java b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/RestResourceTest.java index d63f6bd8c..7345dece0 100644 --- a/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/RestResourceTest.java +++ b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/RestResourceTest.java @@ -21,67 +21,127 @@ package org.onap.policy.apex.client.monitoring.rest; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import javax.ws.rs.core.Response; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.core.deployment.ApexDeploymentException; import org.onap.policy.apex.core.deployment.EngineServiceFacade; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; /** * Test the monitoring rest resource. */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(ApexMonitoringRestResource.class) public class RestResourceTest { @Mock - EngineServiceFacade engineServiceFacadeMock; + private EngineServiceFacade engineServiceFacadeMock; + private ApexMonitoringRestResource restResource; /** - * Set up the mocking for this test. + * Set up mocking of the engine service facade. * - * @throws Exception on mock setup failures + * @throws ApexException on engine service facade setup errors */ @Before - public void setupFacade() throws Exception { + public void initializeMocking() throws ApexException { MockitoAnnotations.initMocks(this); - PowerMockito.whenNew(EngineServiceFacade.class).withAnyArguments().thenReturn(engineServiceFacadeMock); - } - @Test - public void testRestResourceCreateSession() throws ApexException { final AxArtifactKey engineServiceKey = new AxArtifactKey("EngineServiceKey", "0.0.1"); final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); final AxArtifactKey[] engineServiceKeyArray = { engineKey }; final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]); - Mockito.when(engineServiceFacadeMock.getKey()).thenReturn(engineServiceKey); - Mockito.when(engineServiceFacadeMock.getEngineKeyArray()).thenReturn(engineServiceKeyArray); - Mockito.when(engineServiceFacadeMock.getEngineStatus(engineKey)).thenReturn(engineModel); + restResource = Mockito.spy(new ApexMonitoringRestResource()); + Mockito.doReturn(engineServiceFacadeMock).when(restResource).getEngineServiceFacade("apexServer", 12345); + + Mockito.doReturn(engineServiceKey).when(engineServiceFacadeMock).getKey(); + Mockito.doReturn(engineServiceKeyArray).when(engineServiceFacadeMock).getEngineKeyArray(); + Mockito.doReturn(engineModel).when(engineServiceFacadeMock).getEngineStatus(engineKey); + } + + @Test + public void testRestResourceCreateSession() throws ApexException { + Response response = restResource.createSession("apexServer", 12345); + assertEquals(200, response.getStatus()); + assertTrue(((String) response.getEntity()).contains("Engine0:0.0.1")); + } + + @Test + public void testRestResourceCreateSessionWithApexModelKey() throws ApexException { + Mockito.doReturn(new AxArtifactKey("ModelKey:0.0.1")).when(engineServiceFacadeMock).getApexModelKey(); + + Response response = restResource.createSession("apexServer", 12345); + assertEquals(200, response.getStatus()); + assertTrue(((String) response.getEntity()).contains("Engine0:0.0.1")); + } + + @Test + public void testRestResourceCreateSessionConnectException() throws ApexException { + Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init(); + + Response response = restResource.createSession("apexServer", 12345); + assertEquals(500, response.getStatus()); + assertTrue(((String) response.getEntity()).contains("Error connecting to Apex Engine Service")); + } + + @Test + public void testRestResourceCreateSessionGetException() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + Mockito.doThrow(new ApexException("Exception on get")).when(engineServiceFacadeMock).getEngineStatus(engineKey); + + Response response = restResource.createSession("apexServer", 12345); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceCreateSessionInfo() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + Mockito.doReturn("{}").when(engineServiceFacadeMock).getEngineInfo(engineKey); + + Response response = restResource.createSession("apexServer", 12345); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceCreateSessionNullInfo() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + Mockito.doReturn(null).when(engineServiceFacadeMock).getEngineInfo(engineKey); + + Response response = restResource.createSession("apexServer", 12345); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceCreateSessionEmptyInfo() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + Mockito.doReturn(" ").when(engineServiceFacadeMock).getEngineInfo(engineKey); + + Response response = restResource.createSession("apexServer", 12345); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceCreateSessionExceptionInfo() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + Mockito.doThrow(new ApexException("Exception on info")).when(engineServiceFacadeMock).getEngineInfo(engineKey); - ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); Response response = restResource.createSession("apexServer", 12345); assertEquals(200, response.getStatus()); - assertTrue(((String) response.getEntity()).contains(engineKey.getId())); } @Test public void testRestResourceStartEngine() throws ApexException { final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); - ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start"); assertEquals(200, response.getStatus()); } @@ -90,36 +150,113 @@ public class RestResourceTest { public void testRestResourceStopEngine() throws ApexException { final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); - ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Stop"); assertEquals(200, response.getStatus()); } @Test + public void testRestResourceNotStartStopEngine() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + + Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello"); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceInitExceptionStartStopEngine() throws ApexException { + Mockito.doThrow(new ApexDeploymentException("Exception on init")).when(engineServiceFacadeMock).init(); + + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + + Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello"); + assertEquals(500, response.getStatus()); + } + + @Test + public void testRestResourceExceptionStartStopEngine() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + Mockito.doThrow(new ApexDeploymentException("Exception on Start/Stop")).when(engineServiceFacadeMock) + .startEngine(engineKey); + + Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start"); + assertEquals(500, response.getStatus()); + } + + @Test public void testRestResourceStartPeriodicEvents() throws ApexException { final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); - ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Start", 1000); assertEquals(200, response.getStatus()); } @Test - public void testRestResourceStopEPeriodicEvents() throws ApexException { + public void testRestResourceStopPeriodicEvents() throws ApexException { final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); - ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000); assertEquals(200, response.getStatus()); } @Test - public void testCounter() { - ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); + public void testRestResourceNotStartStopPeriodicEvents() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + + Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Hello", 1000); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceExceptionPeriodicEvents() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + Mockito.doThrow(new ApexDeploymentException("Exception on Periodic Events")).when(engineServiceFacadeMock) + .stopPerioidicEvents(engineKey); + Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000); + assertEquals(500, response.getStatus()); + } + + @Test + public void testCounter() { ApexMonitoringRestResource.Counter counter = restResource.new Counter(1538338576, 1538338592); assertEquals(1538338576, counter.getTimestamp()); assertEquals(1538338592, counter.getValue()); } + + @Test + public void testSlidingWindow() { + ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList0 = restResource.new SlidingWindowList<>( + 2); + + assertFalse(slidingWindowList0.hashCode() == 0); + + assertTrue(slidingWindowList0.add("Hello")); + assertTrue(slidingWindowList0.add("Hi")); + assertTrue(slidingWindowList0.add("Howdy")); + + assertFalse(slidingWindowList0.equals(null)); + assertTrue(slidingWindowList0.equals(slidingWindowList0)); + + ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList1 = restResource.new SlidingWindowList<>( + 2); + ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList2 = restResource.new SlidingWindowList<>( + 2); + assertFalse(slidingWindowList0.equals(slidingWindowList1)); + assertFalse(slidingWindowList0.equals(slidingWindowList2)); + assertTrue(slidingWindowList1.equals(slidingWindowList2)); + ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList3 = restResource.new SlidingWindowList<>( + 3); + assertFalse(slidingWindowList1.equals(slidingWindowList3)); + ApexMonitoringRestResource.SlidingWindowList<Integer> slidingWindowList4 = restResource.new SlidingWindowList<>( + 3); + assertTrue(slidingWindowList3.add("Hello")); + assertTrue(slidingWindowList4.add(10)); + assertFalse(slidingWindowList3.equals(slidingWindowList4)); + } + + @Test + public void mopUp() { + assertEquals(engineServiceFacadeMock, restResource.getEngineServiceFacade("apexServer", 12345)); + } } |