2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.property.propertytopolicydeclarators;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import fj.data.Either;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
33 import java.util.Optional;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Captor;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
43 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
44 import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
46 import org.openecomp.sdc.be.model.ComponentInstance;
47 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
48 import org.openecomp.sdc.be.model.PolicyDefinition;
49 import org.openecomp.sdc.be.model.Service;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
51 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
53 public class ComponentInstancePropertyToPolicyDeclaratorTest {
56 private ToscaOperationFacade toscaOperationFacade;
58 private PropertyBusinessLogic propertyBl;
60 private ComponentInstanceBusinessLogic componentInstanceBl;
62 private ComponentInstancePropertyToPolicyDeclarator declarator;
64 private ArgumentCaptor<ComponentInstanceProperty> captor;
66 private static Service service;
67 private static ComponentInstance componentInstance;
68 private static PropertyDataDefinition prop1;
69 private static final String PROP_1_NAME = "prop1";
70 private static final String NON_EXIST_PROP_NAME = "prop1";
71 private static final String COMPONENT_INSTANCE_ID = "ciId";
72 private static final String NON_EXIST_ID = "nonExistId";
77 MockitoAnnotations.initMocks(this);
79 service = new Service();
80 componentInstance = new ComponentInstance();
81 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
82 service.setComponentInstances(Collections.singletonList(componentInstance));
84 prop1 = new PolicyDataDefinition();
85 prop1.setName(PROP_1_NAME);
89 public void testCreateDeclaredProperty_success() {
90 ComponentInstanceProperty declaredProperty = declarator.createDeclaredProperty(prop1);
91 Assert.assertEquals(prop1.getName(), declaredProperty.getName());
95 public void testUpdatePropertiesValues_success() {
96 List<ComponentInstanceProperty> properties = Arrays.asList(new ComponentInstanceProperty(prop1));
97 Map<String, List<ComponentInstanceProperty>> expectedInstanceProperties =
98 Collections.singletonMap(COMPONENT_INSTANCE_ID, properties);
100 when(toscaOperationFacade
101 .addComponentInstancePropertiesToComponent(eq(service), eq(expectedInstanceProperties)))
102 .thenReturn(Either.left(expectedInstanceProperties));
105 Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> updateEither =
106 (Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus>) declarator
107 .updatePropertiesValues(
109 COMPONENT_INSTANCE_ID,
112 Assert.assertTrue(updateEither.isLeft());
113 Map<String, List<ComponentInstanceProperty>> actualInstanceProperties = updateEither.left().value();
114 validateUpdateResult(properties, expectedInstanceProperties, actualInstanceProperties);
118 public void testResolvePropertiesOwner_success() {
119 Optional<ComponentInstance> componentInstanceCandidate =
120 declarator.resolvePropertiesOwner(service, COMPONENT_INSTANCE_ID);
122 Assert.assertTrue(componentInstanceCandidate.isPresent());
123 Assert.assertEquals(componentInstanceCandidate.get(), componentInstance);
127 public void testResolvePropertiesOwner_failure() {
128 Optional<ComponentInstance> componentInstanceCandidate =
129 declarator.resolvePropertiesOwner(service, NON_EXIST_ID);
131 Assert.assertFalse(componentInstanceCandidate.isPresent());
135 public void testUnDeclarePropertiesAsPolicies_success() {
136 PolicyDefinition policyDefinition = new PolicyDefinition();
137 policyDefinition.setName(PROP_1_NAME);
139 when(componentInstanceBl.getComponentInstancePropertyByPolicyId(eq(service), eq(policyDefinition)))
140 .thenReturn(Optional.of(new ComponentInstanceProperty(prop1)));
141 when(toscaOperationFacade
142 .updateComponentInstanceProperty(any(), any(), captor.capture()))
143 .thenReturn(StorageOperationStatus.OK);
145 StorageOperationStatus status =
146 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
148 Assert.assertEquals(status, StorageOperationStatus.OK);
150 ComponentInstanceProperty actualProperty = captor.getValue();
151 Assert.assertEquals(prop1.getName(), actualProperty.getName());
154 private void validateUpdateResult(List<ComponentInstanceProperty> properties,
155 Map<String, List<ComponentInstanceProperty>> expectedInstanceProperties,
156 Map<String, List<ComponentInstanceProperty>> actualInstanceProperties) {
157 Assert.assertEquals(expectedInstanceProperties.size(), actualInstanceProperties.size());
158 Assert.assertEquals(1, actualInstanceProperties.size());
159 Assert.assertEquals(expectedInstanceProperties.keySet(), actualInstanceProperties.keySet());
161 List<ComponentInstanceProperty> actualComponentInstanceProperties =
162 actualInstanceProperties.get(COMPONENT_INSTANCE_ID);
164 Assert.assertEquals(properties, actualComponentInstanceProperties);