From 643fd67cf618f518b702322fba9ae0e37a7878ba Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Fri, 9 Aug 2019 10:21:45 +0200 Subject: Improved the DeployTemplate unit tests Improved test and refactor the class: - checkstyle - correct proper log message - remove code duplication - remove useless variable checkedoutVfcmt - used string join instead stream collect Issue-ID: SDC-2327 Signed-off-by: Krystian Kedron Change-Id: Iea65f8ef0317688ac1eb506f894a8eb3c3d3ce13 --- .../src/main/java/tools/DeployTemplate.java | 115 ++++++++--------- .../src/main/java/utilities/DcaeRestClient.java | 2 +- dcaedt_tools/src/test/java/DeployTemplateTest.java | 138 ++++++++++++++++++--- 3 files changed, 178 insertions(+), 77 deletions(-) diff --git a/dcaedt_tools/src/main/java/tools/DeployTemplate.java b/dcaedt_tools/src/main/java/tools/DeployTemplate.java index 4b222e6..f6512b6 100644 --- a/dcaedt_tools/src/main/java/tools/DeployTemplate.java +++ b/dcaedt_tools/src/main/java/tools/DeployTemplate.java @@ -3,13 +3,14 @@ * SDC * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (c) 2019 Samsung * ================================================================================ * 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. @@ -19,23 +20,25 @@ */ package tools; -import com.google.gson.JsonObject; -import json.templateInfo.TemplateInfo; -import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest; -import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed; -import org.onap.sdc.dcae.composition.util.DcaeBeConstants; -import org.springframework.web.client.HttpServerErrorException; -import utilities.IDcaeRestClient; -import utilities.IReport; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; +import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest; +import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed; +import org.onap.sdc.dcae.composition.util.DcaeBeConstants; + +import com.google.gson.JsonObject; + +import json.templateInfo.TemplateInfo; +import utilities.IDcaeRestClient; +import utilities.IReport; public class DeployTemplate { private static final String FAILED_UPDATE_VFCMT = "Failed update vfcmt: "; + private static final String FAILED_CREATE_VFCMT = "Failed create vfcmt: "; private static final String FAILED = "failed"; private final IReport report; private final IDcaeRestClient dcaeRestClient; @@ -49,25 +52,19 @@ public class DeployTemplate { } public void deploy(Map templateInfoToJsonObjectMap) { - ArrayList vfcmtList = new ArrayList<>(); - List regularVfcmtList = dcaeRestClient.getAllVfcmts(); - if (regularVfcmtList != null) { - vfcmtList.addAll(regularVfcmtList); - } - List baseVfcmtList = dcaeRestClient.getAllBaseVfcmts(); - if (baseVfcmtList != null) { - vfcmtList.addAll(baseVfcmtList); - } + ArrayList vfcmtList = getVfcmtList(); List updatedTemplateInfos = new ArrayList<>(); - vfcmtList.forEach(vfcmt -> - templateInfoToJsonObjectMap.keySet().stream().filter(templateInfo -> templateInfo.getName().equalsIgnoreCase(vfcmt.getName())).forEach(templateInfo -> { - update(vfcmt, templateInfo, templateInfoToJsonObjectMap.get(templateInfo)); - updatedTemplateInfos.add(templateInfo); - })); + vfcmtList.forEach(vfcmt -> templateInfoToJsonObjectMap.keySet().stream() + .filter(templateInfo -> templateInfo.getName().equalsIgnoreCase(vfcmt.getName())) + .forEach(templateInfo -> { + update(vfcmt, templateInfo, templateInfoToJsonObjectMap.get(templateInfo)); + updatedTemplateInfos.add(templateInfo); + })); templateInfoToJsonObjectMap.keySet().stream() - .filter(templateInfo -> !updatedTemplateInfos.contains(templateInfo)) - .forEach(templateInfo -> createNew(templateInfo, templateInfoToJsonObjectMap.get(templateInfo))); + .filter(templateInfo -> !updatedTemplateInfos.contains(templateInfo)) + .forEach(templateInfo -> createNew(templateInfo, + templateInfoToJsonObjectMap.get(templateInfo))); verify(templateInfoToJsonObjectMap); } @@ -75,26 +72,18 @@ public class DeployTemplate { private void verify(Map templateInfoToJsonObjectMap) { AtomicInteger foundCount = new AtomicInteger(); debugLogger.log("Starting verify deployment"); - ArrayList vfcmtList = new ArrayList<>(); - List regularVfcmtList = dcaeRestClient.getAllVfcmts(); - if (regularVfcmtList != null) { - vfcmtList.addAll(regularVfcmtList); - } - List baseVfcmtList = dcaeRestClient.getAllBaseVfcmts(); - if (baseVfcmtList != null) { - vfcmtList.addAll(baseVfcmtList); - } + ArrayList vfcmtList = getVfcmtList(); templateInfoToJsonObjectMap.keySet() - .forEach(templateInfo -> vfcmtList.stream() - .filter(vfcmt -> vfcmt.getName().equalsIgnoreCase(templateInfo.getName())) - .forEach(vfcmt -> foundCount.getAndIncrement())); + .forEach(templateInfo -> vfcmtList.stream() + .filter(vfcmt -> vfcmt.getName().equalsIgnoreCase(templateInfo.getName())) + .forEach(vfcmt -> foundCount.getAndIncrement())); if (foundCount.get() == templateInfoToJsonObjectMap.size()) { debugLogger.log("Deployment verify finished successfully"); } else { - errLogger.log("Deployment verify finished successfully"); - String msg = "Deployment verify finished with errors, found only: " + - foundCount.get() + " of " + templateInfoToJsonObjectMap.size() + " vfcmts"; + errLogger.log("Deployment verify finished unsuccessfully"); + String msg = "Deployment verify finished with errors, found only: " + foundCount.get() + + " of " + templateInfoToJsonObjectMap.size() + " vfcmts"; report.addErrorMessage(msg); errLogger.log(msg); } @@ -112,7 +101,7 @@ public class DeployTemplate { saveAndCertify(jsonObject, vfcmt); } catch (Exception e) { - String msg = FAILED_UPDATE_VFCMT + templateInfo.getName() + ", With general message: " + e.getMessage(); + String msg = FAILED_CREATE_VFCMT + templateInfo.getName() + ", With general message: " + e.getMessage(); report.addErrorMessage(msg); errLogger.log(msg + " " + e); report.setStatusCode(2); @@ -120,23 +109,22 @@ public class DeployTemplate { } private void update(ResourceDetailed vfcmt, TemplateInfo templateInfo, JsonObject jsonObject) { - ResourceDetailed checkedoutVfcmt = vfcmt; try { boolean vfcmtIsCheckedOut = isCheckedOut(vfcmt); - if (vfcmtIsCheckedOut && differentUserCannotCheckout(dcaeRestClient.getUserId(), vfcmt)){ + if (vfcmtIsCheckedOut&& differentUserCannotCheckout(dcaeRestClient.getUserId(), vfcmt)) { report.addErrorMessage(FAILED_UPDATE_VFCMT + vfcmt.getName() + ", cannot checkout vfcmt"); return; } if (templateInfo.getUpdateIfExist()) { if (!vfcmtIsCheckedOut) { - checkedoutVfcmt = dcaeRestClient.checkoutVfcmt(vfcmt.getUuid()); + vfcmt = dcaeRestClient.checkoutVfcmt(vfcmt.getUuid()); } - if (checkedoutVfcmt != null) { - checkedoutVfcmt.setSubCategory(templateInfo.getSubCategory()); - checkedoutVfcmt.setCategory(templateInfo.getCategory()); - checkedoutVfcmt.setDescription(templateInfo.getDescription()); - dcaeRestClient.updateResource(checkedoutVfcmt); - saveAndCertify(jsonObject, checkedoutVfcmt); + if (vfcmt != null) { + vfcmt.setSubCategory(templateInfo.getSubCategory()); + vfcmt.setCategory(templateInfo.getCategory()); + vfcmt.setDescription(templateInfo.getDescription()); + dcaeRestClient.updateResource(vfcmt); + saveAndCertify(jsonObject, vfcmt); } } else { report.addNotUpdatedMessage("vfcmt: " + vfcmt.getName() + " found, but didn't update."); @@ -144,13 +132,13 @@ public class DeployTemplate { } catch (Exception e) { String msg = FAILED_UPDATE_VFCMT + vfcmt.getName() + ", With general message: " + e.getMessage(); report.addErrorMessage(msg); - errLogger.log( msg + " " + e); - report.setStatusCode(2); + errLogger.log(msg + " " + e); + report.setStatusCode(2); } } private void saveAndCertify(JsonObject jsonObject, ResourceDetailed checkedoutVfcmt) { - jsonObject.addProperty("cid", checkedoutVfcmt.getUuid()); + jsonObject.addProperty("cid", checkedoutVfcmt.getUuid()); if (saveCompositionAndCertify(checkedoutVfcmt, jsonObject)) { report.addUpdatedMessage("vfcmt: " + checkedoutVfcmt.getName() + " updated successfully"); } else { @@ -178,13 +166,15 @@ public class DeployTemplate { } private boolean isCheckedOut(ResourceDetailed asset) { - return DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == DcaeBeConstants.LifecycleStateEnum.findState(asset.getLifecycleState()); + return DcaeBeConstants.LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == DcaeBeConstants.LifecycleStateEnum + .findState(asset.getLifecycleState()); } private Boolean differentUserCannotCheckout(String userId, ResourceDetailed asset) { String lastUpdaterUserId = asset.getLastUpdaterUserId(); if (lastUpdaterUserId != null && !lastUpdaterUserId.equals(userId)) { - String msg = "User conflicts. Operation not allowed for user "+userId+" on resource checked out by "+lastUpdaterUserId; + String msg = "User conflicts. Operation not allowed for user " + userId + + " on resource checked out by " + lastUpdaterUserId; report.addErrorMessage(msg); errLogger.log(msg); return true; @@ -192,4 +182,17 @@ public class DeployTemplate { return false; } } + + private ArrayList getVfcmtList() { + ArrayList vfcmtList = new ArrayList<>(); + List regularVfcmtList = dcaeRestClient.getAllVfcmts(); + if (regularVfcmtList != null) { + vfcmtList.addAll(regularVfcmtList); + } + List baseVfcmtList = dcaeRestClient.getAllBaseVfcmts(); + if (baseVfcmtList != null) { + vfcmtList.addAll(baseVfcmtList); + } + return vfcmtList; + } } diff --git a/dcaedt_tools/src/main/java/utilities/DcaeRestClient.java b/dcaedt_tools/src/main/java/utilities/DcaeRestClient.java index 566fb1e..f786671 100644 --- a/dcaedt_tools/src/main/java/utilities/DcaeRestClient.java +++ b/dcaedt_tools/src/main/java/utilities/DcaeRestClient.java @@ -167,7 +167,7 @@ public class DcaeRestClient implements IDcaeRestClient { } private String buildRequestPath(String... args){ - String url = uri + Stream.of(args).collect(Collectors.joining()); + String url = uri + String.join("", args); debugLogger.log("Sending request: " + url); return url; } diff --git a/dcaedt_tools/src/test/java/DeployTemplateTest.java b/dcaedt_tools/src/test/java/DeployTemplateTest.java index f46375a..f1c3d3b 100644 --- a/dcaedt_tools/src/test/java/DeployTemplateTest.java +++ b/dcaedt_tools/src/test/java/DeployTemplateTest.java @@ -3,13 +3,14 @@ * SDC * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (c) 2019 Samsung * ================================================================================ * 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. @@ -18,27 +19,57 @@ * ============LICENSE_END========================================================= */ -import com.google.gson.JsonObject; -import json.templateInfo.TemplateInfo; -import org.junit.Before; -import org.junit.Test; -import org.mockito.InjectMocks; -import tools.DeployTemplate; - -import java.util.*; - import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; + import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest; +import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed; +import org.springframework.web.client.ResourceAccessException; + +import com.google.gson.JsonObject; + +import json.templateInfo.TemplateInfo; +import tools.DeployTemplate; + public class DeployTemplateTest extends BaseTest { @InjectMocks DeployTemplate deployTemplate; private Map templateInfoToJsonObjectMap; + private TemplateInfo vfcmtTemplateInfo; + + private static final String VERIFY_FAILED_MESSAGE = + "Deployment verify finished with errors, found only: 1 of 2 vfcmts"; + private static final String RESOURCE_ACCESS_EXCEPTION_MESSAGE = "Permission denny!"; + private static final String CREATE_RESOURCE_FAILED_MESSAGE = + String.format("Failed create vfcmt: %s, With general message: %s", TEMPLATE_INFO_NAME, + RESOURCE_ACCESS_EXCEPTION_MESSAGE); + private static final String UPDATE_RESOURCE_FAILED_MESSAGE = + String.format("Failed update vfcmt: %s, With general message: %s", VFCMT_NAME1, + RESOURCE_ACCESS_EXCEPTION_MESSAGE); + private static final String NOT_UPDATE_RESOURCE_MESSAGE = + String.format("vfcmt: %s found, but didn't update.", VFCMT_NAME1); + private final ResourceAccessException createResourceException = + new ResourceAccessException(RESOURCE_ACCESS_EXCEPTION_MESSAGE); + private static final String BAD_USER_ID = "badUserId"; + private static final String OPERATION_NOT_ALLOWED_FOR_USER_MESSAGE = String.format( + "User conflicts. Operation not allowed for user %s on resource checked out by %s", + BAD_USER_ID, USER_ID); + private static final String UPDATE_FAILED_MESSAGE = + String.format("Failed update vfcmt: %s, cannot checkout vfcmt", VFCMT_NAME1); @Before @Override @@ -50,15 +81,15 @@ public class DeployTemplateTest extends BaseTest { when(dcaeRestClient.saveComposition(any(), any())).thenReturn("Composition Created"); templateInfoToJsonObjectMap = new HashMap<>(); + vfcmtTemplateInfo = new TemplateInfo(); + vfcmtTemplateInfo.setName(VFCMT_NAME1); + vfcmtTemplateInfo.setFlowType(TEMPLATE_INFO_FLOWTYPE); + vfcmtTemplateInfo.setCategory("category"); + vfcmtTemplateInfo.setSubCategory("subCategory"); + vfcmtTemplateInfo.setDescription("description"); + vfcmtTemplateInfo.setUpdateIfExist(true); + templateInfoToJsonObjectMap.put(vfcmtTemplateInfo, new JsonObject()); TemplateInfo templateInfo = new TemplateInfo(); - templateInfo.setName(VFCMT_NAME1); - templateInfo.setFlowType(TEMPLATE_INFO_FLOWTYPE); - templateInfo.setCategory("category"); - templateInfo.setSubCategory("subCategory"); - templateInfo.setDescription("description"); - templateInfo.setUpdateIfExist(true); - templateInfoToJsonObjectMap.put(templateInfo, new JsonObject()); - templateInfo = new TemplateInfo(); templateInfo.setName(TEMPLATE_INFO_NAME); templateInfo.setFlowType(TEMPLATE_INFO_FLOWTYPE); templateInfo.setCategory("category"); @@ -74,9 +105,76 @@ public class DeployTemplateTest extends BaseTest { } @Test - public void deploy_failedSaving_failedVerify() { + public void deployFailedSaving() { when(dcaeRestClient.saveComposition(anyString(), anyString())).thenReturn("failed"); deployTemplate.deploy(templateInfoToJsonObjectMap); verify(report, times(4)).addErrorMessage(anyString()); } + + @Test + public void deployFailedVerify() { + mockGetAllVfcmtWithBadResources(); + deployTemplate.deploy(templateInfoToJsonObjectMap); + verify(report, times(1)).addErrorMessage(VERIFY_FAILED_MESSAGE); + } + + @Test + public void deployFailedCreateResource() { + when(dcaeRestClient.createResource(any(CreateVFCMTRequest.class))) + .thenThrow(createResourceException); + deployTemplate.deploy(templateInfoToJsonObjectMap); + verify(report, times(1)).addErrorMessage(CREATE_RESOURCE_FAILED_MESSAGE); + verify(report, times(1)).setStatusCode(2); + } + + @Test + public void deployButOperationNotAllowForTheUser() { + when(dcaeRestClient.getUserId()).thenReturn(BAD_USER_ID); + deployTemplate.deploy(templateInfoToJsonObjectMap); + verify(report, times(1)).addErrorMessage(OPERATION_NOT_ALLOWED_FOR_USER_MESSAGE); + verify(report, times(1)).addErrorMessage(UPDATE_FAILED_MESSAGE); + } + + @Test + public void deployAndCheckVfcmt() { + mockGetAllVfcmtWithCertifiedResource(); + deployTemplate.deploy(templateInfoToJsonObjectMap); + } + + @Test + public void deployAndCheckVfcmtFaild() { + mockGetAllVfcmtWithCertifiedResource(); + when(dcaeRestClient.checkoutVfcmt(UUID1)).thenThrow(createResourceException); + deployTemplate.deploy(templateInfoToJsonObjectMap); + verify(report, times(1)).addErrorMessage(UPDATE_RESOURCE_FAILED_MESSAGE); + verify(report, times(1)).setStatusCode(2); + } + + @Test + public void deployAndNotUpdate() { + vfcmtTemplateInfo.setUpdateIfExist(false); + deployTemplate.deploy(templateInfoToJsonObjectMap); + verify(report, times(1)).addNotUpdatedMessage(NOT_UPDATE_RESOURCE_MESSAGE); + } + + private void mockGetAllVfcmtWithBadResources() { + List resourceDetaileds = new ArrayList<>(); + resourceDetaileds.add(createResource("NOT_CERTIFIED_CHECKOUT")); + when(dcaeRestClient.getAllVfcmts()).thenReturn(resourceDetaileds); + } + + private void mockGetAllVfcmtWithCertifiedResource() { + List resourceDetaileds = new ArrayList<>(); + resourceDetaileds.add(createResource("READY_FOR_CERTIFICATION")); + when(dcaeRestClient.getAllVfcmts()).thenReturn(resourceDetaileds); + } + + private ResourceDetailed createResource(String status) { + ResourceDetailed resourceDetailed = new ResourceDetailed(); + resourceDetailed.setName(VFCMT_NAME1); + resourceDetailed.setUuid(UUID1); + resourceDetailed.setLifecycleState(status); + resourceDetailed.setLastUpdaterUserId(USER_ID); + return resourceDetailed; + } } -- cgit 1.2.3-korg