From 3313023cf0e8c3461bbced47b2eb1f485d50dbf8 Mon Sep 17 00:00:00 2001 From: talio Date: Tue, 14 May 2019 14:52:37 +0300 Subject: Fix test coverage Fix test coverage for PropertyDeclarationOrchestrator.java Change-Id: I8f787017713dc2218ecfadbe31aee0831c303e2b Issue-ID: SDC-2298 Signed-off-by: talio --- .../PropertyDecelerationOrchestratorTest.java | 159 ----------- .../PropertyDeclarationOrchestratorTest.java | 295 +++++++++++++++++++++ 2 files changed, 295 insertions(+), 159 deletions(-) delete mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PropertyDecelerationOrchestratorTest.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PropertyDeclarationOrchestratorTest.java (limited to 'catalog-be/src/test/java') diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PropertyDecelerationOrchestratorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PropertyDecelerationOrchestratorTest.java deleted file mode 100644 index ef1d45dbaa..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PropertyDecelerationOrchestratorTest.java +++ /dev/null @@ -1,159 +0,0 @@ -package org.openecomp.sdc.be.components.property; - -import fj.data.Either; -import mockit.Deencapsulation; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Answers; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.ComponentInstInputsMap; -import org.openecomp.sdc.be.model.ComponentInstancePropInput; -import org.openecomp.sdc.be.model.InputDefinition; -import org.openecomp.sdc.be.model.Resource; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class PropertyDecelerationOrchestratorTest { - - @InjectMocks - PropertyDeclarationOrchestrator testSubject; - - @Mock(answer = Answers.CALLS_REAL_METHODS) - List propertyDeceleratorsMock; - - @Mock - private ComponentInstanceInputPropertyDeclarator componentInstanceInputPropertyDecelerator; - @Mock - private ComponentInstancePropertyDeclarator componentInstancePropertyDecelerator; - @Mock - private PolicyPropertyDeclarator policyPropertyDecelerator; - - @Before - public void setUp() throws Exception { - - MockitoAnnotations.initMocks(this); - } - - @Test(expected = IllegalStateException.class) - public void testDeclarePropertiesToInputs() throws Exception { - Component component = new Resource(); - ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); - Either, StorageOperationStatus> result; - - // default test - result = testSubject.declarePropertiesToInputs(component, componentInstInputsMap); - } - - @Test - public void testDeclarePropertiesToListInputs() throws Exception { - Component component = new Resource(); - ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); - Map> componentInstanceInputsMap = new HashMap<>(); - List value = new LinkedList<>(); - componentInstanceInputsMap.put("mock", value); - componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap); - InputDefinition input = new InputDefinition(); - Either result; - - // default test - result = testSubject.declarePropertiesToListInput(component, componentInstInputsMap, input); - } - - @Test - public void testUnDeclarePropertiesAsInputs() throws Exception { - Component component = new Resource(); - InputDefinition inputToDelete = new InputDefinition(); - StorageOperationStatus result; - - Iterator mockIter = Mockito.mock(Iterator.class); - Mockito.when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter); - Mockito.when(mockIter.hasNext()).thenReturn(false); - - // default test - result = testSubject.unDeclarePropertiesAsInputs(component, inputToDelete); - } - - @Test - public void testUnDeclarePropertiesAsListInputs() throws Exception { - Component component = new Resource(); - InputDefinition inputToDelete = new InputDefinition(); - StorageOperationStatus result; - - Iterator mockIter = Mockito.mock(Iterator.class); - Mockito.when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter); - Mockito.when(mockIter.hasNext()).thenReturn(false); - - // default test - result = testSubject.unDeclarePropertiesAsListInputs(component, inputToDelete); - } - - @Test - public void testGetPropOwnerId() throws Exception { - ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); - Map> componentInstanceInputsMap = new HashMap<>(); - List value = new LinkedList<>(); - componentInstanceInputsMap.put("mock", value); - componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap); - String result; - - // default test - result = Deencapsulation.invoke(testSubject, "getPropOwnerId", componentInstInputsMap); - } - - @Test(expected = IllegalStateException.class) - public void testGetPropertyDecelerator() throws Exception { - ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); - PropertyDeclarator result; - - // default test - result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap); - } - - @Test - public void testGetPropertyDeceleratorWithInputsMap() throws Exception { - ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); - Map> componentInstanceInputsMap = new HashMap<>(); - List value = new LinkedList<>(); - componentInstanceInputsMap.put("mock", value); - componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap); - PropertyDeclarator result; - - // default test - result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap); - } - - @Test - public void testGetPropertyDeceleratorWithCIProperties() throws Exception { - ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); - Map> componentInstanceProperties = new HashMap<>(); - List value = new LinkedList<>(); - componentInstanceProperties.put("mock", value); - componentInstInputsMap.setComponentInstanceProperties(componentInstanceProperties); - PropertyDeclarator result; - - // default test - result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap); - } - - @Test - public void testGetPropertyDeceleratorWithCIPolicy() throws Exception { - ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); - Map> policyProperties = new HashMap<>(); - List value = new LinkedList<>(); - policyProperties.put("mock", value); - componentInstInputsMap.setPolicyProperties(policyProperties); - PropertyDeclarator result; - - // default test - result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap); - } -} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PropertyDeclarationOrchestratorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PropertyDeclarationOrchestratorTest.java new file mode 100644 index 0000000000..2fabd25549 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/PropertyDeclarationOrchestratorTest.java @@ -0,0 +1,295 @@ +/* + * ============LICENSE_START============================================================================================================= + * Copyright (c) 2019 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 org.openecomp.sdc.be.components.property; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import fj.data.Either; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import mockit.Deencapsulation; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Answers; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentInstancePropertyToPolicyDeclarator; +import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator; +import org.openecomp.sdc.be.components.utils.ServiceBuilder; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstInputsMap; +import org.openecomp.sdc.be.model.ComponentInstancePropInput; +import org.openecomp.sdc.be.model.InputDefinition; +import org.openecomp.sdc.be.model.PolicyDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; + +public class PropertyDeclarationOrchestratorTest { + + @InjectMocks + PropertyDeclarationOrchestrator testSubject; + + @Mock(answer = Answers.CALLS_REAL_METHODS) + List propertyDeceleratorsMock; + + @Mock + private ComponentInstancePropertyToPolicyDeclarator componentInstancePropertyToPolicyDeclarator; + + @Mock + private ComponentPropertyToPolicyDeclarator componentPropertyToPolicyDeclarator; + + @Mock + private ComponentInstanceInputPropertyDeclarator componentInstanceInputPropertyDecelerator; + + @Mock + private ComponentInstancePropertyDeclarator componentInstancePropertyDecelerator; + + @Mock + private ComponentPropertyDeclarator servicePropertyDeclarator; + + @Mock + private PolicyPropertyDeclarator policyPropertyDecelerator; + + @Mock + private GroupPropertyDeclarator groupPropertyDeclarator;; + + private static final String PROP_UID = "propertyUid"; + private static final String SERVICE_UID = "serviceUid"; + + private Service service; + + @Before + public void setUp() throws Exception { + + MockitoAnnotations.initMocks(this); + + service = new ServiceBuilder().setUniqueId(SERVICE_UID).build(); + } + + @Test(expected = IllegalStateException.class) + public void testDeclarePropertiesToInputs() throws Exception { + Component component = new Resource(); + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + Either, StorageOperationStatus> result; + + // default test + result = testSubject.declarePropertiesToInputs(component, componentInstInputsMap); + } + + @Test + public void testDeclarePropertiesToListInputs() throws Exception { + Component component = new Resource(); + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + Map> componentInstanceInputsMap = getPropertiesMapToDeclare(); + componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap); + InputDefinition input = new InputDefinition(); + input.setUniqueId(PROP_UID); + Either result; + + when(componentInstanceInputPropertyDecelerator.declarePropertiesAsListInput(eq(component), anyString(), anyList(), eq(input))).thenReturn(Either.left(input)); + // default test + result = testSubject.declarePropertiesToListInput(component, componentInstInputsMap, input); + + assertTrue(result.isLeft()); + assertEquals(PROP_UID, result.left().value().getUniqueId()); + } + + @Test + public void testUnDeclarePropertiesAsInputs() throws Exception { + Component component = new Resource(); + InputDefinition inputToDelete = new InputDefinition(); + StorageOperationStatus result; + + Iterator mockIter = Mockito.mock(Iterator.class); + when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter); + when(mockIter.hasNext()).thenReturn(false); + + setInputUndeclarationStubbings(component, inputToDelete); + + // default test + result = testSubject.unDeclarePropertiesAsInputs(component, inputToDelete); + + assertEquals(StorageOperationStatus.OK, result); + } + + @Test + public void testUnDeclarePropertiesAsListInputs() throws Exception { + Component component = new Resource(); + InputDefinition inputToDelete = new InputDefinition(); + StorageOperationStatus result; + + Iterator mockIter = Mockito.mock(Iterator.class); + Mockito.when(propertyDeceleratorsMock.iterator()).thenReturn(mockIter); + Mockito.when(mockIter.hasNext()).thenReturn(false); + + // default test + result = testSubject.unDeclarePropertiesAsListInputs(component, inputToDelete); + } + + @Test + public void testGetPropOwnerId() throws Exception { + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + Map> componentInstanceInputsMap = new HashMap<>(); + List value = new LinkedList<>(); + componentInstanceInputsMap.put("mock", value); + componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap); + String result; + + // default test + result = Deencapsulation.invoke(testSubject, "getPropOwnerId", componentInstInputsMap); + } + + @Test(expected = IllegalStateException.class) + public void testGetPropertyDecelerator() throws Exception { + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + PropertyDeclarator result; + + // default test + result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap); + } + + @Test + public void testGetPropertyDeceleratorWithInputsMap() throws Exception { + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + Map> componentInstanceInputsMap = getPropertiesMapToDeclare(); + componentInstInputsMap.setComponentInstanceInputsMap(componentInstanceInputsMap); + PropertyDeclarator result; + + // default test + result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap); + + assertTrue(result instanceof ComponentInstanceInputPropertyDeclarator); + } + + @Test + public void testGetPropertyDeceleratorWithCIProperties() throws Exception { + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + Map> componentInstanceProperties = new HashMap<>(); + List value = new LinkedList<>(); + componentInstanceProperties.put("mock", value); + componentInstInputsMap.setComponentInstanceProperties(componentInstanceProperties); + PropertyDeclarator result; + + // default test + result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap); + + assertTrue(result instanceof ComponentInstancePropertyDeclarator); + } + + @Test + public void testGetPropertyDeceleratorWithCIPolicy() throws Exception { + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + Map> policyProperties = getPropertiesMapToDeclare(); + componentInstInputsMap.setPolicyProperties(policyProperties); + PropertyDeclarator result; + + // default test + result = Deencapsulation.invoke(testSubject, "getPropertyDeclarator", componentInstInputsMap); + + assertTrue(result instanceof PolicyPropertyDeclarator); + } + + @Test + public void testDeclarePropertiesToPolicies() { + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + Map> policyProperties = getPropertiesMapToDeclare(); + componentInstInputsMap.setComponentInstancePropertiesToPolicies(policyProperties); + + PolicyDefinition declaredPolicy = getDeclaredPolicy(); + + when(componentInstancePropertyToPolicyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn( + Either.left(Collections.singletonList(declaredPolicy))); + + Either, StorageOperationStatus> declareEither = + testSubject.declarePropertiesToPolicies(service, componentInstInputsMap); + + validatePolicyDeclaration(declaredPolicy, declareEither); + } + + @Test + public void testUndeclarePropertiesAsPolicies() { + when(componentInstancePropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK); + when(componentPropertyToPolicyDeclarator.unDeclarePropertiesAsPolicies(any(), any(PolicyDefinition.class))).thenReturn(StorageOperationStatus.OK); + + StorageOperationStatus undeclareStatus = + testSubject.unDeclarePropertiesAsPolicies(service, getDeclaredPolicy()); + + assertEquals(StorageOperationStatus.OK, undeclareStatus); + } + + @Test + public void testDeclareServiceProperties() { + ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap(); + Map> serviceProperties = getPropertiesMapToDeclare(); + componentInstInputsMap.setServiceProperties(serviceProperties); + + PolicyDefinition declaredPolicy = getDeclaredPolicy(); + + when(servicePropertyDeclarator.declarePropertiesAsPolicies(any(), anyString(), anyList())).thenReturn( + Either.left(Collections.singletonList(declaredPolicy))); + + Either, StorageOperationStatus> declareEither = + testSubject.declarePropertiesToPolicies(service, componentInstInputsMap); + + validatePolicyDeclaration(declaredPolicy, declareEither); + } + + private PolicyDefinition getDeclaredPolicy() { + PolicyDefinition declaredPolicy = new PolicyDefinition(); + declaredPolicy.setUniqueId(PROP_UID); + return declaredPolicy; + } + + private Map> getPropertiesMapToDeclare() { + Map> policyProperties = new HashMap<>(); + + ComponentInstancePropInput propertyToDeclare = new ComponentInstancePropInput(); + propertyToDeclare.setUniqueId(PROP_UID); + propertyToDeclare.setPropertiesName(PROP_UID); + + policyProperties.put("mock", Collections.singletonList(propertyToDeclare)); + return policyProperties; + } + + private void validatePolicyDeclaration(PolicyDefinition declaredPolicy, + Either, StorageOperationStatus> declareEither) { + assertTrue(declareEither.isLeft()); + + List declaredPolicies = declareEither.left().value(); + assertEquals(1, declaredPolicies.size()); + assertEquals(declaredPolicy.getUniqueId(), declaredPolicies.get(0).getUniqueId()); + } + + private void setInputUndeclarationStubbings(Component component, InputDefinition inputToDelete) { + when(policyPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn( + StorageOperationStatus.OK); + when(servicePropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK); + when(componentInstancePropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK); + when(componentInstanceInputPropertyDecelerator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK); + when(groupPropertyDeclarator.unDeclarePropertiesAsInputs(eq(component), eq(inputToDelete))).thenReturn(StorageOperationStatus.OK); + } +} -- cgit 1.2.3-korg