aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVijay Venkatesh Kumar <vv770d@att.com>2020-02-18 19:09:58 +0000
committerGerrit Code Review <gerrit@onap.org>2020-02-18 19:09:58 +0000
commit29b603a4670706ed58c2348d21ad9d39c2cf4213 (patch)
tree95117af28ad7aeff0d2f14ba0f01bca29617a712
parent9d924537e89a61a85fb7722ec7b5e7208df38937 (diff)
parent193ab871ab8bb06e58946432562faa6cd02293c4 (diff)
Merge "Extract jdbi query"
-rw-r--r--src/main/java/io/swagger/api/impl/DcaeServiceTypeObjectRepository.java107
-rw-r--r--src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java74
-rw-r--r--src/test/java/io/swagger/api/impl/DcaeServiceTypeObjectRepositoryTest.java273
-rw-r--r--src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java25
4 files changed, 392 insertions, 87 deletions
diff --git a/src/main/java/io/swagger/api/impl/DcaeServiceTypeObjectRepository.java b/src/main/java/io/swagger/api/impl/DcaeServiceTypeObjectRepository.java
new file mode 100644
index 0000000..3212fe0
--- /dev/null
+++ b/src/main/java/io/swagger/api/impl/DcaeServiceTypeObjectRepository.java
@@ -0,0 +1,107 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. 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.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.onap.dcae.inventory.daos.InventoryDataAccessManager;
+import org.onap.dcae.inventory.dbthings.mappers.DCAEServiceTypeObjectMapper;
+import org.onap.dcae.inventory.dbthings.models.DCAEServiceTypeObject;
+import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.Query;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.function.Consumer;
+
+class DcaeServiceTypeObjectRepository {
+ private static final Logger metricsLogger = LoggerFactory.getLogger("metricsLogger");
+ private final InventoryDataAccessManager instance;
+
+ public DcaeServiceTypeObjectRepository(InventoryDataAccessManager instance) {
+ this.instance = instance;
+ }
+
+ List<DCAEServiceTypeObject> fetch(String typeName, Boolean onlyLatest, Boolean onlyActive, String vnfType,
+ String serviceId, String serviceLocation, String asdcServiceId,
+ String asdcResourceId, String application, String component, String owner) {
+
+ List<DCAEServiceTypeObject> serviceTypeObjects;
+
+ // TODO: Make this variable also a URL parameter.
+ DateTime createdCutoff = DateTime.now(DateTimeZone.UTC);
+
+ try (Handle jdbiHandle = instance.getHandle()) {
+ final String queryStatement = DcaeServiceTypesQueryStatement.create(typeName, onlyLatest, onlyActive,
+ vnfType, serviceId, serviceLocation, asdcServiceId, asdcResourceId, owner, application, component);
+
+ metricsLogger.info("Query created as: {}." + queryStatement);
+
+ Query<DCAEServiceTypeObject> query = getQuery(jdbiHandle, queryStatement);
+
+ if (typeName != null){
+ typeName = resolveTypeName(typeName);
+ }
+
+ ifNotNullBind(typeName, it -> query.bind("typeName", it));
+ ifNotNullBind(vnfType, it -> query.bind("vnfType", it));
+ ifNotNullBind(serviceId, it -> query.bind("serviceId", it));
+ ifNotNullBind(serviceLocation, it -> query.bind("serviceLocation", it));
+ ifNotNoneBind(asdcServiceId, it -> query.bind("asdcServiceId", it));
+ ifNotNoneBind(asdcResourceId, it -> query.bind("asdcResourceId", it));
+ ifNotNullBind(application, it -> query.bind("application", it));
+ ifNotNullBind(component, it -> query.bind("component", it));
+ ifNotNullBind(owner, it -> query.bind("owner", it));
+ bindCreatedCutoff(createdCutoff, query);
+
+ serviceTypeObjects = query.list();
+ }
+
+ return serviceTypeObjects;
+ }
+
+ private void ifNotNullBind(String value, Consumer<String> bind) {
+ if (value != null) {
+ bind.accept(value);
+ }
+ }
+
+ private void ifNotNoneBind(String value, Consumer<String> bind) {
+ if (value != null && !"NONE".equalsIgnoreCase(value)) {
+ bind.accept(value);
+ }
+ }
+
+ void bindCreatedCutoff(DateTime createdCutoff, Query<DCAEServiceTypeObject> query) {
+ query.bind("createdCutoff", createdCutoff);
+ }
+
+ Query<DCAEServiceTypeObject> getQuery(Handle jdbiHandle, String queryStatement) {
+ return jdbiHandle.createQuery(queryStatement).map(new DCAEServiceTypeObjectMapper());
+ }
+
+ static String resolveTypeName(String typeName){
+ if (typeName.contains("*")) {
+ return typeName.replaceAll("\\*", "%");
+ }
+ return typeName;
+ }
+}
diff --git a/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java
index 3a08daa..9c52ded 100644
--- a/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java
+++ b/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java
@@ -24,7 +24,6 @@ package io.swagger.api.impl;
import org.onap.dcae.inventory.daos.DCAEServiceTypesDAO;
import org.onap.dcae.inventory.daos.DCAEServicesDAO;
import org.onap.dcae.inventory.daos.InventoryDAOManager;
-import org.onap.dcae.inventory.dbthings.mappers.DCAEServiceTypeObjectMapper;
import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject;
import org.onap.dcae.inventory.dbthings.models.DCAEServiceTypeObject;
import io.swagger.api.*;
@@ -33,8 +32,6 @@ import io.swagger.model.*;
import io.swagger.api.NotFoundException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
-import org.skife.jdbi.v2.Handle;
-import org.skife.jdbi.v2.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -83,60 +80,16 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService {
UriInfo uriInfo, SecurityContext securityContext,
String application, String component, String owner)
throws NotFoundException {
- List<DCAEServiceTypeObject> serviceTypeObjects = new ArrayList<>();
- // TODO: Make this variable also a URL parameter.
- DateTime createdCutoff = DateTime.now(DateTimeZone.UTC);
+ DcaeServiceTypeObjectRepository dcaeServiceTypeObjectRepository = new DcaeServiceTypeObjectRepository(
+ InventoryDAOManager.getInstance()
+ );
- try (Handle jdbiHandle = InventoryDAOManager.getInstance().getHandle()) {
- final String queryStatement = DcaeServiceTypesQueryStatement.create(typeName, onlyLatest, onlyActive,
- vnfType, serviceId, serviceLocation, asdcServiceId, asdcResourceId, owner, application, component);
-
- metricsLogger.info("Query created as: {}." + queryStatement);
-
- Query<DCAEServiceTypeObject> query = jdbiHandle.createQuery(queryStatement).map(new DCAEServiceTypeObjectMapper());
-
- if (typeName != null) {
- typeName = resolveTypeName(typeName);
- query.bind("typeName", typeName);
- }
-
- if (vnfType != null) {
- query.bind("vnfType", vnfType);
- }
-
- if (serviceId != null) {
- query.bind("serviceId", serviceId);
- }
-
- if (serviceLocation != null) {
- query.bind("serviceLocation", serviceLocation);
- }
-
- if (asdcServiceId != null && !"NONE".equalsIgnoreCase(asdcServiceId)) {
- query.bind("asdcServiceId", asdcServiceId);
- }
-
- if (asdcResourceId != null && !"NONE".equalsIgnoreCase(asdcResourceId)) {
- query.bind("asdcResourceId", asdcResourceId);
- }
-
- if (application != null) {
- query.bind("application", application);
- }
-
- if (component != null) {
- query.bind("component", component);
- }
-
- if (owner != null) {
- query.bind("owner", owner);
- }
-
- query.bind("createdCutoff", createdCutoff);
-
- serviceTypeObjects = query.list();
- }
+ List<DCAEServiceTypeObject> serviceTypeObjects = dcaeServiceTypeObjectRepository.fetch(
+ typeName, onlyLatest, onlyActive, vnfType,
+ serviceId, serviceLocation, asdcServiceId,
+ asdcResourceId, application, component, owner
+ );
offset = (offset == null) ? 0 : offset;
@@ -161,6 +114,10 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService {
// TODO: MUST UPDATE THIS LINK NAV CODE
+ if (typeName != null){
+ typeName = DcaeServiceTypeObjectRepository.resolveTypeName(typeName);
+ }
+
if (offsetPrev >= 0) {
navigationLinks.setPreviousLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "prev", typeName, onlyLatest,
onlyActive, vnfType, serviceId, serviceLocation, asdcServiceId, asdcResourceId, offsetPrev, application, component, owner));
@@ -193,13 +150,6 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService {
return Response.ok().entity(createDCAEServiceType(serviceTypeObject, uriInfo)).build();
}
- static String resolveTypeName(String typeName){
- if (typeName.contains("*")) {
- return typeName.replaceAll("\\*", "%");
- }
- return typeName;
- }
-
/**
* Create a DCAE service type database object
*
diff --git a/src/test/java/io/swagger/api/impl/DcaeServiceTypeObjectRepositoryTest.java b/src/test/java/io/swagger/api/impl/DcaeServiceTypeObjectRepositoryTest.java
new file mode 100644
index 0000000..60d9a85
--- /dev/null
+++ b/src/test/java/io/swagger/api/impl/DcaeServiceTypeObjectRepositoryTest.java
@@ -0,0 +1,273 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * dcae-inventory
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. 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.joda.time.DateTime;
+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 org.skife.jdbi.v2.Query;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DcaeServiceTypeObjectRepositoryTest {
+
+ @Mock
+ private InventoryDataAccessManager instance;
+ @Mock
+ private Handle handle;
+ @Mock
+ Query query;
+ private DcaeServiceTypeObjectRepository dcaeServiceTypeObjectRepositorySpy;
+
+ @Before
+ public void setUp() {
+ when(instance.getHandle()).thenReturn(handle);
+
+ final DcaeServiceTypeObjectRepository dcaeServiceTypeObjectRepository = new DcaeServiceTypeObjectRepository(instance);
+ this.dcaeServiceTypeObjectRepositorySpy = Mockito.spy(dcaeServiceTypeObjectRepository);
+ Mockito.doReturn(query).when(dcaeServiceTypeObjectRepositorySpy).getQuery(eq(handle), anyString());
+ Mockito.doNothing().when(dcaeServiceTypeObjectRepositorySpy).bindCreatedCutoff(any(DateTime.class), eq(query));
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForTypeNameParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ null, null, null,
+ null, null, null,
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForVnfTypeParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", null, null,
+ null, null, null,
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForServiceIdParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", null,
+ null, null, null,
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForServiceLocationParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", "testServiceLocation",
+ null, null, null,
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).bind(eq("serviceLocation"), eq("testServiceLocation"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForAsdcServiceIdParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", "testServiceLocation",
+ "testAsdcServiceId", null, null,
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).bind(eq("serviceLocation"), eq("testServiceLocation"));
+ verify(query).bind(eq("asdcServiceId"), eq("testAsdcServiceId"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForNoneAsdcServiceIdParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", "testServiceLocation",
+ "NONE", null, null,
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).bind(eq("serviceLocation"), eq("testServiceLocation"));
+ verify(query, never()).bind(eq("asdcServiceId"), anyString());
+ verify(query).list();
+ }
+
+
+ @Test
+ public void shouldConfigureBindQueryForAsdcResourceIdParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", "testServiceLocation",
+ "testAsdcServiceId", "testAsdcResourceId", null,
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).bind(eq("serviceLocation"), eq("testServiceLocation"));
+ verify(query).bind(eq("asdcServiceId"), eq("testAsdcServiceId"));
+ verify(query).bind(eq("asdcResourceId"), eq("testAsdcResourceId"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForNoneAsdcResourceIdParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", "testServiceLocation",
+ "NONE", "NONE", null,
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).bind(eq("serviceLocation"), eq("testServiceLocation"));
+ verify(query, never()).bind(eq("asdcServiceId"), anyString());
+ verify(query, never()).bind(eq("asdcResourceId"), anyString());
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForApplicationParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", "testServiceLocation",
+ "testAsdcServiceId", "testAsdcResourceId", "testApplication",
+ null, null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).bind(eq("serviceLocation"), eq("testServiceLocation"));
+ verify(query).bind(eq("asdcServiceId"), eq("testAsdcServiceId"));
+ verify(query).bind(eq("asdcResourceId"), eq("testAsdcResourceId"));
+ verify(query).bind(eq("application"), eq("testApplication"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForComponentParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", "testServiceLocation",
+ "testAsdcServiceId", "testAsdcResourceId", "testApplication",
+ "testComponent", null
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).bind(eq("serviceLocation"), eq("testServiceLocation"));
+ verify(query).bind(eq("asdcServiceId"), eq("testAsdcServiceId"));
+ verify(query).bind(eq("asdcResourceId"), eq("testAsdcResourceId"));
+ verify(query).bind(eq("component"), eq("testComponent"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldConfigureBindQueryForOwnerParameter() {
+
+ this.dcaeServiceTypeObjectRepositorySpy.fetch("testTypeName", false, false,
+ "testVnfType", "testServiceId", "testServiceLocation",
+ "testAsdcServiceId", "testAsdcResourceId", "testApplication",
+ "testComponent", "testOwner"
+ );
+
+ verify(query).bind(eq("typeName"), eq("testTypeName"));
+ verify(query).bind(eq("vnfType"), eq("testVnfType"));
+ verify(query).bind(eq("serviceId"), eq("testServiceId"));
+ verify(query).bind(eq("serviceLocation"), eq("testServiceLocation"));
+ verify(query).bind(eq("asdcServiceId"), eq("testAsdcServiceId"));
+ verify(query).bind(eq("asdcResourceId"), eq("testAsdcResourceId"));
+ verify(query).bind(eq("component"), eq("testComponent"));
+ verify(query).bind(eq("owner"), eq("testOwner"));
+ verify(query).list();
+ }
+
+ @Test
+ public void shouldReturnOriginalTypeName(){
+ // given
+ String typeName = "abc";
+
+ // when
+ final String actual = DcaeServiceTypeObjectRepository.resolveTypeName(typeName);
+
+ // then
+ assertEquals("abc", actual);
+ }
+
+ @Test
+ public void shouldTransformAsteriskToPercentCharacterInTypeName(){
+ // given
+ String typeName = "abc*d";
+
+ // when
+ final String actual = DcaeServiceTypeObjectRepository.resolveTypeName(typeName);
+
+ // then
+ assertEquals("abc%d", actual);
+ }
+}
diff --git a/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java
index 13dd820..bd13abe 100644
--- a/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java
+++ b/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java
@@ -222,29 +222,4 @@ public class DcaeServiceTypesApiServiceImplTests {
throw new RuntimeException("Unexpected exception: post new 200", e);
}
}
-
- @Test
- public void shouldReturnOriginalTypeName(){
- // given
- String typeName = "abc";
-
- // when
- final String actual = DcaeServiceTypesApiServiceImpl.resolveTypeName(typeName);
-
- // then
- assertEquals("abc", actual);
- }
-
- @Test
- public void shouldTransformAsteriskToPercentCharacterInTypeName(){
- // given
- String typeName = "abc*d";
-
- // when
- final String actual = DcaeServiceTypesApiServiceImpl.resolveTypeName(typeName);
-
- // then
- assertEquals("abc%d", actual);
- }
-
}