From cfdf0dbbcd208a669c9919f23f1c30b2ec16b4d8 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Fri, 6 Dec 2019 08:37:39 +0100 Subject: Improve code coverage Issue-ID: DCAEGEN2-1980 Signed-off-by: Zebek Bogumil Change-Id: I062a4a7f6508a6a3b2c212793bdad019ab46f7e2 --- .../DcaeServicesGroupbyApiServiceFactory.java | 7 +- .../impl/DcaeServicesGroupByApiServiceImpl.java | 168 +++++++++++++++++++++ .../impl/DcaeServicesGroupbyApiServiceImpl.java | 103 ------------- .../dcae/inventory/daos/InventoryDAOManager.java | 9 +- .../inventory/daos/InventoryDataAccessManager.java | 34 +++++ 5 files changed, 214 insertions(+), 107 deletions(-) create mode 100644 src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java delete mode 100644 src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/InventoryDataAccessManager.java (limited to 'src/main') diff --git a/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java b/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java index 509ac2a..a43be5a 100644 --- a/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java +++ b/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java @@ -21,12 +21,15 @@ package io.swagger.api.factories; import io.swagger.api.DcaeServicesGroupbyApiService; -import io.swagger.api.impl.DcaeServicesGroupbyApiServiceImpl; +import io.swagger.api.impl.DcaeServicesGroupByApiServiceImpl; +import org.onap.dcae.inventory.daos.InventoryDAOManager; @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-04-18T20:16:22.119Z") public final class DcaeServicesGroupbyApiServiceFactory { - private static final DcaeServicesGroupbyApiService service = new DcaeServicesGroupbyApiServiceImpl(); + private static final DcaeServicesGroupbyApiService service = new DcaeServicesGroupByApiServiceImpl( + new DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider(InventoryDAOManager.getInstance()), + new DcaeServicesGroupByApiServiceImpl.DcaeServicesLinkResolver()); //Utility classes, which are a collection of static members, are not meant to be instantiated. private DcaeServicesGroupbyApiServiceFactory(){ diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java new file mode 100644 index 0000000..f879bff --- /dev/null +++ b/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java @@ -0,0 +1,168 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2019 AT&T and 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.DcaeServicesApi; +import io.swagger.api.DcaeServicesGroupbyApiService; +import org.onap.dcae.inventory.daos.InventoryDataAccessManager; +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 javax.ws.rs.core.Link; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.core.UriInfo; +import java.util.List; +import java.util.Map; + +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-04-18T20:16:22.119Z") +public class DcaeServicesGroupByApiServiceImpl extends DcaeServicesGroupbyApiService { + + public static final String PROPERTY_NAME_TYPE = "type"; + public static final String PROPERTY_NAME_VNF_LOCATION = "vnfLocation"; + public static final String PROPERTY_NAME_VNF_TYPE = "vnfType"; + private static final String UNKNOWN_PROPERTY_NAME = "n/a"; + + private final RunningDcaeServicesProvider runningDcaeServicesProvider; + private final DcaeServicesLinkResolver dcaeServicesLinkResolver; + + public DcaeServicesGroupByApiServiceImpl( + RunningDcaeServicesProvider runningDcaeServicesProvider, + DcaeServicesLinkResolver dcaeServicesLinkResolver) { + this.runningDcaeServicesProvider = runningDcaeServicesProvider; + this.dcaeServicesLinkResolver = dcaeServicesLinkResolver; + } + + @Override + public Response dcaeServicesGroupbyPropertyNameGet( + String propertyName, UriInfo uriInfo, SecurityContext securityContext) { + + String columnName = resolveColumnName(propertyName); + if(columnName.equals(UNKNOWN_PROPERTY_NAME)) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + + List> runningDcaeServices = runningDcaeServicesProvider.fetch(columnName); + DCAEServiceGroupByResults response = generateResponse(propertyName, uriInfo, columnName, runningDcaeServices); + + return Response.ok().entity(response).build(); + } + + DCAEServiceGroupByResults generateResponse( + String propertyName, UriInfo uriInfo, String columnName, List> runningDcaeServices) { + + DCAEServiceGroupByResults response = new DCAEServiceGroupByResults(); + response.setPropertyName(propertyName); + + for (Map result : runningDcaeServices) { + DCAEServiceGroupByResultsPropertyValues value = new DCAEServiceGroupByResultsPropertyValues(); + value.setCount(((Long) result.get("num")).intValue()); + + String propertyValue = (String) result.get(columnName); + value.setPropertyValue(propertyValue); + + Link dcaeServicesLink = dcaeServicesLinkResolver.resolveLink(propertyName, uriInfo, propertyValue); + value.setDcaeServiceQueryLink(dcaeServicesLink); + + response.getPropertyValues().add(value); + } + return response; + } + + String resolveColumnName(String propertyName) { + String columnName; + + switch (propertyName) { + case PROPERTY_NAME_TYPE: + columnName = "type_id"; + break; + case PROPERTY_NAME_VNF_LOCATION: + columnName = "vnf_location"; + break; + case PROPERTY_NAME_VNF_TYPE: + columnName = "vnf_type"; + break; + default: + columnName = UNKNOWN_PROPERTY_NAME; + } + return columnName; + } + + public static class DcaeServicesLinkResolver { + + private static final String RELATION = "dcae_services"; + + Link resolveLink(String propertyName, UriInfo uriInfo, String propertyValue) { + Link dcaeServices = null; + switch (propertyName) { + case PROPERTY_NAME_TYPE: + dcaeServices = DcaeServicesApi.buildLinkForGetByTypeId(uriInfo, RELATION, + propertyValue); + break; + case PROPERTY_NAME_VNF_LOCATION: + dcaeServices = DcaeServicesApi.buildLinkForGetByVnfLocation(uriInfo, RELATION, + propertyValue); + break; + case PROPERTY_NAME_VNF_TYPE: + dcaeServices = DcaeServicesApi.buildLinkForGetByVnfType(uriInfo, RELATION, + propertyValue); + break; + default: + throw new UnsupportedOperationException(String.format("Unsupported '%s' property name!", propertyName)); + } + return dcaeServices; + } + } + + + public static class RunningDcaeServicesProvider { + + private static final String SERVICE_STATUS_PLACE_HOLDER = "serviceStatus"; + + public RunningDcaeServicesProvider(InventoryDataAccessManager inventoryDataAccessManager) { + this.inventoryDataAccessManager = inventoryDataAccessManager; + } + + private final InventoryDataAccessManager inventoryDataAccessManager; + + public List> fetch(String columnName) { + + 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(); + } + } + + static String createQuery(String columnName) { + StringBuilder sb = new StringBuilder(); + sb.append(String.format("select %s, count(1) as num ", columnName)); + sb.append(String.format(" from dcae_services where status = :%s",SERVICE_STATUS_PLACE_HOLDER)); + sb.append(String.format(" group by %s order by count(1) desc", columnName)); + return sb.toString(); + } + } +} diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java deleted file mode 100644 index 34fba7d..0000000 --- a/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java +++ /dev/null @@ -1,103 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * dcae-inventory - * ================================================================================ - * Copyright (C) 2017 AT&T 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.onap.dcae.inventory.daos.InventoryDAOManager; -import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; -import io.swagger.api.*; -import io.swagger.model.DCAEServiceGroupByResults; -import io.swagger.model.DCAEServiceGroupByResultsPropertyValues; -import org.skife.jdbi.v2.Handle; - -import javax.ws.rs.core.Response; -import javax.ws.rs.core.SecurityContext; -import javax.ws.rs.core.UriInfo; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-04-18T20:16:22.119Z") -public class DcaeServicesGroupbyApiServiceImpl extends DcaeServicesGroupbyApiService { - - @Override - public Response dcaeServicesGroupbyPropertyNameGet(String propertyName, UriInfo uriInfo, SecurityContext securityContext) { - String columnName; - - switch (propertyName) { - case "type": - columnName = "type_id"; - break; - case "vnfLocation": - columnName = "vnf_location"; - break; - case "vnfType": - columnName = "vnf_type"; - break; - default: - return Response.status(Response.Status.BAD_REQUEST).build(); - } - - List> results = new ArrayList<>(); - - try (Handle jdbiHandle = InventoryDAOManager.getInstance().getHandle()) { - StringBuilder sb = new StringBuilder(); - sb.append(String.format("select %s, count(1) as num ", columnName)); - sb.append(" from dcae_services where status = :serviceStatus"); - sb.append(String.format(" group by %s order by count(1) desc", columnName)); - String queryString = sb.toString(); - - // NOTE: This is hardcoded because service status is only used internally. - results = jdbiHandle.createQuery(queryString).bind("serviceStatus", DCAEServiceObject.DCAEServiceStatus.RUNNING) - .list(); - } - - DCAEServiceGroupByResults response = new DCAEServiceGroupByResults(); - response.setPropertyName(propertyName); - - for (Map result : results) { - DCAEServiceGroupByResultsPropertyValues value = new DCAEServiceGroupByResultsPropertyValues(); - value.setCount(((Long) result.get("num")).intValue()); - String propertyValue = (String) result.get(columnName); - value.setPropertyValue(propertyValue); - - switch (propertyName) { - case "type": - value.setDcaeServiceQueryLink(DcaeServicesApi.buildLinkForGetByTypeId(uriInfo, "dcae_services", - propertyValue)); - break; - case "vnfLocation": - value.setDcaeServiceQueryLink(DcaeServicesApi.buildLinkForGetByVnfLocation(uriInfo, "dcae_services", - propertyValue)); - break; - case "vnfType": - value.setDcaeServiceQueryLink(DcaeServicesApi.buildLinkForGetByVnfType(uriInfo, "dcae_services", - propertyValue)); - break; - default: - return Response.status(Response.Status.BAD_REQUEST).build(); - } - - response.getPropertyValues().add(value); - } - - return Response.ok().entity(response).build(); - } -} diff --git a/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java b/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java index 5bdecfe..7039700 100644 --- a/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java +++ b/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java @@ -40,11 +40,11 @@ import java.util.List; * * Created by mhwang on 4/19/16. */ -public final class InventoryDAOManager { +public final class InventoryDAOManager implements InventoryDataAccessManager { private static InventoryDAOManager instance; - public static InventoryDAOManager getInstance() { + public static synchronized InventoryDAOManager getInstance() { if (instance == null) { instance = new InventoryDAOManager(); } @@ -177,10 +177,12 @@ public final class InventoryDAOManager { return jdbi.onDemand(klass); } + @Override public DCAEServicesDAO getDCAEServicesDAO() { return (DCAEServicesDAO) this.getDAO(DCAEServicesDAO.class); } + @Override public DCAEServiceComponentsDAO getDCAEServiceComponentsDAO() { return (DCAEServiceComponentsDAO) this.getDAO(DCAEServiceComponentsDAO.class); } @@ -189,10 +191,12 @@ public final class InventoryDAOManager { return (DCAEServicesComponentsMapsDAO) this.getDAO(DCAEServicesComponentsMapsDAO.class); } + @Override public DCAEServiceTransactionDAO getDCAEServiceTransactionDAO() { return jdbi.onDemand(DCAEServiceTransactionDAO.class); } + @Override public DCAEServiceTypesDAO getDCAEServiceTypesDAO() { return (DCAEServiceTypesDAO) this.getDAO(DCAEServiceTypesDAO.class); } @@ -202,6 +206,7 @@ public final class InventoryDAOManager { * * @return */ + @Override public Handle getHandle() { return this.jdbi.open(); } diff --git a/src/main/java/org/onap/dcae/inventory/daos/InventoryDataAccessManager.java b/src/main/java/org/onap/dcae/inventory/daos/InventoryDataAccessManager.java new file mode 100644 index 0000000..ef4375b --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/InventoryDataAccessManager.java @@ -0,0 +1,34 @@ +/*- + * ============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 org.onap.dcae.inventory.daos; + +import org.skife.jdbi.v2.Handle; + +public interface InventoryDataAccessManager { + DCAEServicesDAO getDCAEServicesDAO(); + + DCAEServiceComponentsDAO getDCAEServiceComponentsDAO(); + + DCAEServiceTransactionDAO getDCAEServiceTransactionDAO(); + + DCAEServiceTypesDAO getDCAEServiceTypesDAO(); + + Handle getHandle(); +} -- cgit 1.2.3-korg