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 fj.data.Either;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Captor;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator;
35 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
36 import org.openecomp.sdc.be.model.PolicyDefinition;
37 import org.openecomp.sdc.be.model.PropertyDefinition;
38 import org.openecomp.sdc.be.model.Service;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
46 import static org.mockito.ArgumentMatchers.eq;
47 import static org.mockito.Mockito.when;
49 @RunWith(MockitoJUnitRunner.class)
50 public class ComponentPropertyToPolicyDeclaratorTest extends PropertyDeclaratorTestBase {
53 ComponentPropertyToPolicyDeclarator declarator;
55 private ToscaOperationFacade toscaOperationFacade;
57 ArgumentCaptor<PropertyDefinition> propertyCaptor;
59 private static final String OWNER_ID = "ownerId";
60 private static final String SERVICE_ID = "serviceId";
61 private static final String PROPERTY_ID = "propertyId";
62 private static final String POLICY = "policy";
63 private static final String TEST_VALUE = "testValue";
67 public void testDeclarePropertyToPolicy_success() {
68 Service service = new Service();
69 service.setUniqueId(SERVICE_ID);
71 when(toscaOperationFacade.updatePropertyOfComponent(eq(service), Mockito.any())).thenReturn(Either.left(new PropertyDefinition()));
72 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither = declarator.declarePropertiesAsPolicies(
73 service, OWNER_ID, createInstancePropInputList(Collections.singletonList(prop1)));
75 Assert.assertTrue(declareEither.isLeft());
79 public void testUndeclarePolicy_success() {
80 Service service = new Service();
81 service.setUniqueId(SERVICE_ID);
83 PolicyDefinition policyDefinition = createPolicyDefinition(PROPERTY_ID);
85 PropertyDefinition expectedProperty = createPropertyWithDeclaredPolicy(getPolicyId(PROPERTY_ID));
86 service.addProperty(expectedProperty);
88 when(toscaOperationFacade.updatePropertyOfComponent(eq(service), propertyCaptor.capture())).thenReturn(Either.left(new PropertyDefinition()));
90 StorageOperationStatus storageOperationStatus =
91 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
93 PropertyDefinition actualProperty = propertyCaptor.getValue();
95 Assert.assertEquals(storageOperationStatus, storageOperationStatus.OK);
96 Assert.assertEquals(expectedProperty, actualProperty);
101 public void shouldReturnOriginalPropertyValueAfterUndeclaring() {
102 Service service = new Service();
103 service.setUniqueId(SERVICE_ID);
105 PropertyDefinition expectedProperty = new PropertyDefinition(prop1);
106 addGetPolicyValueToProperty(getPolicyId(prop1.getUniqueId()), expectedProperty);
107 service.addProperty(expectedProperty);
109 when(toscaOperationFacade.updatePropertyOfComponent(eq(service), propertyCaptor.capture())).thenReturn(Either.left(new PropertyDefinition()));
111 Either<List<PolicyDefinition>, StorageOperationStatus> declareEither = declarator.declarePropertiesAsPolicies(
112 service, OWNER_ID, createInstancePropInputList(Collections.singletonList(prop1)));
114 Assert.assertTrue(declareEither.isLeft());
116 PolicyDefinition policyDefinition = createPolicyDefinition(prop1.getUniqueId());
117 StorageOperationStatus storageOperationStatus =
118 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
120 List<PropertyDefinition> actualProperties = propertyCaptor.getAllValues();
122 Assert.assertEquals(storageOperationStatus, storageOperationStatus.OK);
123 Assert.assertEquals(actualProperties.size(), 2);
124 Assert.assertEquals(prop1.getValue(), actualProperties.get(1).getValue());
127 private PropertyDefinition createPropertyWithDeclaredPolicy(String policyId) {
128 PropertyDefinition propertyDefinition = new PropertyDefinition();
129 propertyDefinition.setUniqueId(PROPERTY_ID);
131 addGetPolicyValueToProperty(policyId, propertyDefinition);
132 return propertyDefinition;
135 private void addGetPolicyValueToProperty(String policyId, PropertyDefinition propertyDefinition) {
136 GetPolicyValueDataDefinition getPolicyValueDataDefinition = new GetPolicyValueDataDefinition();
137 getPolicyValueDataDefinition.setPolicyId(policyId);
138 getPolicyValueDataDefinition.setPropertyName(propertyDefinition.getUniqueId());
139 getPolicyValueDataDefinition.setOrigPropertyValue(propertyDefinition.getValue());
141 List<GetPolicyValueDataDefinition> getPolicyList = new ArrayList<>();
142 getPolicyList.add(getPolicyValueDataDefinition);
143 propertyDefinition.setGetPolicyValues(getPolicyList);
146 private PolicyDefinition createPolicyDefinition(String propertyId) {
147 PolicyDefinition policyDefinition = new PolicyDefinition();
148 String policyId = getPolicyId(propertyId);
149 policyDefinition.setUniqueId(policyId);
151 return policyDefinition;
154 private String getPolicyId(String propertyId) {
155 return SERVICE_ID + "." + propertyId + "." + POLICY;