From 0bfa9b4dfe33aa70fa921aa5e5c684790fd030d6 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 1 Oct 2018 15:05:58 +0100 Subject: Refactor monitoring REST tests to remove PowerMock Removed PowerMock from tests becasue coverage of PowerMock is not counted by JaCoCO. It was easier to remove PowerMock than to try and hack the build get PowerMock coverage counted. Issue-ID: POLICY-1034 Change-Id: Ifc3e1ff93b2bfcb5619a5af6dec320d2de992f87 Signed-off-by: liamfallon --- .../client/monitoring/rest/RestResourceTest.java | 189 ++++++++++++++++++--- 1 file changed, 163 insertions(+), 26 deletions(-) (limited to 'client/client-monitoring/src/test/java/org') 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 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 slidingWindowList1 = restResource.new SlidingWindowList<>( + 2); + ApexMonitoringRestResource.SlidingWindowList slidingWindowList2 = restResource.new SlidingWindowList<>( + 2); + assertFalse(slidingWindowList0.equals(slidingWindowList1)); + assertFalse(slidingWindowList0.equals(slidingWindowList2)); + assertTrue(slidingWindowList1.equals(slidingWindowList2)); + ApexMonitoringRestResource.SlidingWindowList slidingWindowList3 = restResource.new SlidingWindowList<>( + 3); + assertFalse(slidingWindowList1.equals(slidingWindowList3)); + ApexMonitoringRestResource.SlidingWindowList 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)); + } } -- cgit 1.2.3-korg