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;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.when;
27 import fj.data.Either;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Captor;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator;
41 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
42 import org.openecomp.sdc.be.model.PolicyDefinition;
43 import org.openecomp.sdc.be.model.PropertyDefinition;
44 import org.openecomp.sdc.be.model.Service;
45 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
48 @RunWith(MockitoJUnitRunner.class)
49 public class ComponentPropertyToPolicyDeclaratorTest extends PropertyDeclaratorTestBase {
52 ComponentPropertyToPolicyDeclarator declarator;
54 private ToscaOperationFacade toscaOperationFacade;
56 ArgumentCaptor<PropertyDefinition> propertyCaptor;
58 private static final String OWNER_ID = "ownerId";
59 private static final String SERVICE_ID = "serviceId";
60 private static final String PROPERTY_ID = "propertyId";
61 private static final String POLICY = "policy";
62 private static final String TEST_VALUE = "testValue";
66 public void testDeclarePropertyToPolicy_success() {
67 Service service = new Service();
68 service.setUniqueId(SERVICE_ID);
70 when(toscaOperationFacade.updatePropertyOfComponent(eq(service), Mockito.any())).thenReturn(Either.left(new PropertyDefinition()));
71 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither = declarator.declarePropertiesAsPolicies(
72 service, OWNER_ID, createInstancePropInputList(Collections.singletonList(prop1)));
74 Assert.assertTrue(declareEither.isLeft());
78 public void testUndeclarePolicy_success() {
79 Service service = new Service();
80 service.setUniqueId(SERVICE_ID);
82 PolicyDefinition policyDefinition = createPolicyDefinition(PROPERTY_ID);
84 PropertyDefinition expectedProperty = createPropertyWithDeclaredPolicy(getPolicyId(PROPERTY_ID));
85 service.addProperty(expectedProperty);
87 when(toscaOperationFacade.updatePropertyOfComponent(eq(service), propertyCaptor.capture())).thenReturn(Either.left(new PropertyDefinition()));
89 StorageOperationStatus storageOperationStatus =
90 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
92 PropertyDefinition actualProperty = propertyCaptor.getValue();
94 Assert.assertEquals(storageOperationStatus, storageOperationStatus.OK);
95 Assert.assertEquals(expectedProperty, actualProperty);
100 public void shouldReturnOriginalPropertyValueAfterUndeclaring() {
101 Service service = new Service();
102 service.setUniqueId(SERVICE_ID);
104 PropertyDefinition expectedProperty = new PropertyDefinition(prop1);
105 addGetPolicyValueToProperty(getPolicyId(prop1.getUniqueId()), expectedProperty);
106 service.addProperty(expectedProperty);
108 when(toscaOperationFacade.updatePropertyOfComponent(eq(service), propertyCaptor.capture())).thenReturn(Either.left(new PropertyDefinition()));
110 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither = declarator.declarePropertiesAsPolicies(
111 service, OWNER_ID, createInstancePropInputList(Collections.singletonList(prop1)));
113 Assert.assertTrue(declareEither.isLeft());
115 PolicyDefinition policyDefinition = createPolicyDefinition(prop1.getUniqueId());
116 StorageOperationStatus storageOperationStatus =
117 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
119 List<PropertyDefinition> actualProperties = propertyCaptor.getAllValues();
121 Assert.assertEquals(storageOperationStatus, storageOperationStatus.OK);
122 Assert.assertEquals(actualProperties.size(), 2);
123 Assert.assertEquals(prop1.getValue(), actualProperties.get(1).getValue());
126 private PropertyDefinition createPropertyWithDeclaredPolicy(String policyId) {
127 PropertyDefinition propertyDefinition = new PropertyDefinition();
128 propertyDefinition.setUniqueId(PROPERTY_ID);
130 addGetPolicyValueToProperty(policyId, propertyDefinition);
131 return propertyDefinition;
134 private void addGetPolicyValueToProperty(String policyId, PropertyDefinition propertyDefinition) {
135 GetPolicyValueDataDefinition getPolicyValueDataDefinition = new GetPolicyValueDataDefinition();
136 getPolicyValueDataDefinition.setPolicyId(policyId);
137 getPolicyValueDataDefinition.setPropertyName(propertyDefinition.getUniqueId());
138 getPolicyValueDataDefinition.setOrigPropertyValue(propertyDefinition.getValue());
140 List<GetPolicyValueDataDefinition> getPolicyList = new ArrayList<>();
141 getPolicyList.add(getPolicyValueDataDefinition);
142 propertyDefinition.setGetPolicyValues(getPolicyList);
145 private PolicyDefinition createPolicyDefinition(String propertyId) {
146 PolicyDefinition policyDefinition = new PolicyDefinition();
147 String policyId = getPolicyId(propertyId);
148 policyDefinition.setUniqueId(policyId);
150 return policyDefinition;
153 private String getPolicyId(String propertyId) {
154 return SERVICE_ID + "." + propertyId + "." + POLICY;