Improve code coverage 59/99659/1
authorBogumil Zebek <bogumil.zebek@nokia.com>
Mon, 16 Dec 2019 08:34:21 +0000 (09:34 +0100)
committerZebek Bogumil <bogumil.zebek@nokia.com>
Mon, 16 Dec 2019 08:35:57 +0000 (09:35 +0100)
Issue-ID: DCAEGEN2-1992
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Change-Id: I3e57f34fb8d3fcec66a5c937aa352032dc84b1e8

src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java
src/test/java/io/swagger/api/FakeUriInfoTestDataFactory.java [moved from src/test/java/io/swagger/api/Util.java with 94% similarity]
src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java
src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java
src/test/java/io/swagger/api/impl/DcaeServicesLinkResolverTest.java [new file with mode: 0644]
src/test/java/io/swagger/api/impl/RunningDcaeServicesProviderTest.java [new file with mode: 0644]

index f879bff..fff2bd4 100644 (file)
@@ -27,6 +27,7 @@ import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject;
 import io.swagger.model.DCAEServiceGroupByResults;
 import io.swagger.model.DCAEServiceGroupByResultsPropertyValues;
 import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.Query;
 
 import javax.ws.rs.core.Link;
 import javax.ws.rs.core.Response;
@@ -149,14 +150,18 @@ public class DcaeServicesGroupByApiServiceImpl extends DcaeServicesGroupbyApiSer
 
             String queryString = createQuery(columnName);
             try (Handle jdbiHandle = inventoryDataAccessManager.getHandle()) {
-
-                // NOTE: This is hardcoded because service status is only used internally.
-                return jdbiHandle.createQuery(queryString)
-                        .bind(SERVICE_STATUS_PLACE_HOLDER, DCAEServiceObject.DCAEServiceStatus.RUNNING)
-                        .list();
+                return executeQuery(queryString, jdbiHandle);
             }
         }
 
+        List<Map<String, Object>> executeQuery(String queryString, Handle jdbiHandle) {
+            // NOTE: This is hardcoded because service status is only used internally.
+            final Query<Map<String, Object>> query = jdbiHandle.createQuery(queryString);
+            final Query<Map<String, Object>> bind = query
+                    .bind(SERVICE_STATUS_PLACE_HOLDER, DCAEServiceObject.DCAEServiceStatus.RUNNING);
+            return bind.list();
+        }
+
         static String createQuery(String columnName) {
             StringBuilder sb = new StringBuilder();
             sb.append(String.format("select %s, count(1) as num ", columnName));
@@ -29,9 +29,13 @@ import javax.ws.rs.core.UriInfo;
 /**
  * Created by mhwang on 9/25/17.
  */
-public class Util {
+public class FakeUriInfoTestDataFactory {
 
-    public static class FakeUriInfo implements UriInfo {
+    public static UriInfo givenFakeUriInfo(){
+        return new FakeUriInfo();
+    }
+
+    private static class FakeUriInfo implements UriInfo {
 
         @Override
         public String getPath() {
index cbdff7d..32cc664 100644 (file)
@@ -28,6 +28,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.SecurityContext;
 import javax.ws.rs.core.UriInfo;
 
+import org.eclipse.jetty.http.HttpStatus;
 import org.joda.time.DateTime;
 import org.junit.Before;
 import org.junit.Test;
@@ -51,7 +52,7 @@ import io.dropwizard.db.DataSourceFactory;
 import io.dropwizard.jackson.Jackson;
 import io.dropwizard.jdbi.DBIFactory;
 import io.dropwizard.setup.Environment;
-import io.swagger.api.Util;
+import io.swagger.api.FakeUriInfoTestDataFactory;
 import io.swagger.model.DCAEServiceRequest;
 
 /**
@@ -126,11 +127,11 @@ public class DcaeServicesApiServiceImplH2Tests {
         Boolean shareable = Boolean.TRUE;
         DateTime created = null;
         Integer offset = 0;
-        UriInfo uriInfo = new Util.FakeUriInfo();
+        UriInfo uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo();
         SecurityContext securityContext = null;
         Response response = api.dcaeServicesGet(typeId, vnfId, vnfType, vnfLocation, componentType, shareable, created,
                 offset, uriInfo, securityContext);
-        assertEquals(response.getStatus(), 200);
+        assertEquals(HttpStatus.OK_200, response.getStatus());
     }
 
 }
index 0f5a0a8..9599ca5 100644 (file)
@@ -31,6 +31,7 @@ import java.util.List;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
+import org.eclipse.jetty.http.HttpStatus;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -48,7 +49,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import io.swagger.api.NotFoundException;
-import io.swagger.api.Util;
+import io.swagger.api.FakeUriInfoTestDataFactory;
 import io.swagger.model.DCAEService;
 import io.swagger.model.DCAEServiceRequest;
 
@@ -102,8 +103,8 @@ public class DcaeServicesApiServiceImplTests {
         serviceRequest.setVnfId("vnf-id-def");
         String serviceId = "service-id-123";
         DCAEServiceObject serviceObject = new DCAEServiceObject(serviceId, serviceRequest);
-        List<DCAEServiceComponentObject> components = new ArrayList<DCAEServiceComponentObject>();
-        UriInfo uriInfo = new Util.FakeUriInfo();
+        List<DCAEServiceComponentObject> components = new ArrayList<>();
+        UriInfo uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo();
 
         try {
             DCAEService service = (DCAEService) createDCAEService.invoke(api, serviceObject, components, uriInfo);
@@ -121,15 +122,15 @@ public class DcaeServicesApiServiceImplTests {
         serviceRequest.setVnfId("vnf-id-def");
         DCAEServiceObject serviceObject = new DCAEServiceObject(serviceId, serviceRequest);
         when(mockServicesDao.getByServiceId(DCAEServiceObject.DCAEServiceStatus.RUNNING, serviceId)).thenReturn(serviceObject);
-        when(mockComponentsDao.getByServiceId(serviceId)).thenReturn(new ArrayList<DCAEServiceComponentObject>());
+        when(mockComponentsDao.getByServiceId(serviceId)).thenReturn(new ArrayList<>());
 
         DatabusControllerClient dbcc = mock(DatabusControllerClient.class);
         DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc);
-        UriInfo uriInfo = new Util.FakeUriInfo();
+        UriInfo uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo();
 
         try {
             Response response = api.dcaeServicesServiceIdGet(serviceId, uriInfo, null);
-            assertEquals(response.getStatus(), 200);
+            assertEquals(HttpStatus.OK_200, response.getStatus());
         } catch (NotFoundException e) {
             fail("Service should have been found");
         }
@@ -183,11 +184,11 @@ public class DcaeServicesApiServiceImplTests {
 
         DatabusControllerClient dbcc = mock(DatabusControllerClient.class);
         DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc);
-        UriInfo uriInfo = new Util.FakeUriInfo();
+        UriInfo uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo();
 
         String serviceId = "service-id-123";
         Response response = api.dcaeServicesServiceIdPut(serviceId, serviceRequest, uriInfo, null);
-        assertEquals(response.getStatus(), 422);
+        assertEquals(HttpStatus.UNPROCESSABLE_ENTITY_422, response.getStatus());
     }
 
     @Test
@@ -201,11 +202,10 @@ public class DcaeServicesApiServiceImplTests {
 
         DatabusControllerClient dbcc = mock(DatabusControllerClient.class);
         DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc);
-        UriInfo uriInfo = new Util.FakeUriInfo();
 
         try {
             Response response = api.dcaeServicesServiceIdDelete(serviceId, null);
-            assertEquals(response.getStatus(), 200);
+            assertEquals(HttpStatus.OK_200, response.getStatus());
         } catch (NotFoundException e) {
             fail("Should have NOT thrown a NotFoundException");
         } catch (Exception e) {
@@ -221,7 +221,6 @@ public class DcaeServicesApiServiceImplTests {
 
         DatabusControllerClient dbcc = mock(DatabusControllerClient.class);
         DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc);
-        UriInfo uriInfo = new Util.FakeUriInfo();
 
         try {
             api.dcaeServicesServiceIdDelete(serviceId, null);
diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesLinkResolverTest.java b/src/test/java/io/swagger/api/impl/DcaeServicesLinkResolverTest.java
new file mode 100644 (file)
index 0000000..3d19536
--- /dev/null
@@ -0,0 +1,99 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * 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 io.swagger.api.impl;
+
+import io.swagger.api.FakeUriInfoTestDataFactory;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import javax.ws.rs.core.Link;
+import javax.ws.rs.core.UriInfo;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DcaeServicesLinkResolverTest {
+    private DcaeServicesGroupByApiServiceImpl.DcaeServicesLinkResolver dcaeServicesLinkResolver;
+    private UriInfo uriInfo;
+
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
+    @Before
+    public void setUp(){
+        dcaeServicesLinkResolver = new DcaeServicesGroupByApiServiceImpl.DcaeServicesLinkResolver();
+        uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo();
+    }
+
+    @Test
+    public void shouldReturnDcaeServiceLinkForTypeParameter(){
+        // when
+        final Link link = dcaeServicesLinkResolver.resolveLink(
+                DcaeServicesGroupByApiServiceImpl.PROPERTY_NAME_TYPE,
+                uriInfo,
+                "test"
+        );
+
+        // then
+        assertThat(link.getRel()).isEqualTo("dcae_services");
+        assertThat(link.getUri().toString()).isEqualTo("http://some-fake-base-uri/dcae-services/?typeId=test");
+    }
+
+    @Test
+    public void shouldReturnDcaeServiceLinkForVnfLocationParameter(){
+        // when
+        final Link link = dcaeServicesLinkResolver.resolveLink(
+                DcaeServicesGroupByApiServiceImpl.PROPERTY_NAME_VNF_LOCATION,
+                uriInfo,
+                "test"
+        );
+
+        // then
+        assertThat(link.getRel()).isEqualTo("dcae_services");
+        assertThat(link.getUri().toString()).isEqualTo("http://some-fake-base-uri/dcae-services/?vnfLocation=test");
+    }
+
+    @Test
+    public void shouldReturnDcaeServiceLinkForVnfTypeParameter(){
+        // when
+        final Link link = dcaeServicesLinkResolver.resolveLink(
+                DcaeServicesGroupByApiServiceImpl.PROPERTY_NAME_VNF_TYPE,
+                uriInfo,
+                "test"
+        );
+
+        // then
+        assertThat(link.getRel()).isEqualTo("dcae_services");
+        assertThat(link.getUri().toString()).isEqualTo("http://some-fake-base-uri/dcae-services/?vnfType=test");
+    }
+
+
+    @Test
+    public void shouldReportAnErrorForUnknownParameter(){
+        // arrange
+        thrown.expect(UnsupportedOperationException.class);
+        thrown.expectMessage("Unsupported 'unknown' property name!");
+
+        // when
+        dcaeServicesLinkResolver.resolveLink("unknown", uriInfo, "test");
+
+    }
+}
diff --git a/src/test/java/io/swagger/api/impl/RunningDcaeServicesProviderTest.java b/src/test/java/io/swagger/api/impl/RunningDcaeServicesProviderTest.java
new file mode 100644 (file)
index 0000000..18e2da9
--- /dev/null
@@ -0,0 +1,68 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * 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 io.swagger.api.impl;
+
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.dcae.inventory.daos.InventoryDataAccessManager;
+import org.skife.jdbi.v2.Handle;
+
+import java.util.ArrayList;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RunningDcaeServicesProviderTest {
+
+    @Mock
+    private InventoryDataAccessManager inventoryDataAccessManager;
+    @Mock
+    private Handle handle;
+
+    private DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider runningDcaeServicesProvider;
+
+
+    @Before
+    public void setUp() {
+        runningDcaeServicesProvider = Mockito.spy(new DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider(inventoryDataAccessManager));
+    }
+
+    @Test
+    public void shouldInvokeQueryForRunningDcaeServices() {
+        // given
+        when(inventoryDataAccessManager.getHandle()).thenReturn(handle);
+        Mockito.doReturn(new ArrayList<>()).when(runningDcaeServicesProvider).executeQuery(any(), any());
+
+        // when
+        runningDcaeServicesProvider.fetch("testColumnName");
+
+        // then
+        verify(runningDcaeServicesProvider, times(1)).executeQuery(any(), any());
+
+    }
+}