Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / ComponentPropertyToPolicyDeclaratorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.property;
22
23
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;
41
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
45
46 import static org.mockito.ArgumentMatchers.eq;
47 import static org.mockito.Mockito.when;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class ComponentPropertyToPolicyDeclaratorTest extends PropertyDeclaratorTestBase {
51
52     @InjectMocks
53     ComponentPropertyToPolicyDeclarator declarator;
54     @Mock
55     private ToscaOperationFacade toscaOperationFacade;
56     @Captor
57     ArgumentCaptor<PropertyDefinition> propertyCaptor;
58
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";
64
65
66     @Test
67     public void testDeclarePropertyToPolicy_success() {
68         Service service = new Service();
69         service.setUniqueId(SERVICE_ID);
70
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)));
74
75         Assert.assertTrue(declareEither.isLeft());
76     }
77
78     @Test
79     public void testUndeclarePolicy_success() {
80         Service service = new Service();
81         service.setUniqueId(SERVICE_ID);
82
83         PolicyDefinition policyDefinition = createPolicyDefinition(PROPERTY_ID);
84
85         PropertyDefinition expectedProperty = createPropertyWithDeclaredPolicy(getPolicyId(PROPERTY_ID));
86         service.addProperty(expectedProperty);
87
88         when(toscaOperationFacade.updatePropertyOfComponent(eq(service), propertyCaptor.capture())).thenReturn(Either.left(new PropertyDefinition()));
89
90         StorageOperationStatus storageOperationStatus =
91                 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
92
93         PropertyDefinition actualProperty = propertyCaptor.getValue();
94
95         Assert.assertEquals(storageOperationStatus, storageOperationStatus.OK);
96         Assert.assertEquals(expectedProperty, actualProperty);
97
98     }
99
100     @Test
101     public void shouldReturnOriginalPropertyValueAfterUndeclaring() {
102         Service service = new Service();
103         service.setUniqueId(SERVICE_ID);
104
105         PropertyDefinition expectedProperty = new PropertyDefinition(prop1);
106         addGetPolicyValueToProperty(getPolicyId(prop1.getUniqueId()), expectedProperty);
107         service.addProperty(expectedProperty);
108
109         when(toscaOperationFacade.updatePropertyOfComponent(eq(service), propertyCaptor.capture())).thenReturn(Either.left(new PropertyDefinition()));
110
111         Either<List<PolicyDefinition>, StorageOperationStatus> declareEither = declarator.declarePropertiesAsPolicies(
112                 service, OWNER_ID, createInstancePropInputList(Collections.singletonList(prop1)));
113
114         Assert.assertTrue(declareEither.isLeft());
115
116         PolicyDefinition policyDefinition = createPolicyDefinition(prop1.getUniqueId());
117         StorageOperationStatus storageOperationStatus =
118                 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
119
120         List<PropertyDefinition> actualProperties = propertyCaptor.getAllValues();
121
122         Assert.assertEquals(storageOperationStatus, storageOperationStatus.OK);
123         Assert.assertEquals(actualProperties.size(), 2);
124         Assert.assertEquals(prop1.getValue(), actualProperties.get(1).getValue());
125     }
126
127     private PropertyDefinition createPropertyWithDeclaredPolicy(String policyId) {
128         PropertyDefinition propertyDefinition = new PropertyDefinition();
129         propertyDefinition.setUniqueId(PROPERTY_ID);
130
131         addGetPolicyValueToProperty(policyId, propertyDefinition);
132         return propertyDefinition;
133     }
134
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());
140
141         List<GetPolicyValueDataDefinition> getPolicyList = new ArrayList<>();
142         getPolicyList.add(getPolicyValueDataDefinition);
143         propertyDefinition.setGetPolicyValues(getPolicyList);
144     }
145
146     private PolicyDefinition createPolicyDefinition(String propertyId) {
147         PolicyDefinition policyDefinition = new PolicyDefinition();
148         String policyId = getPolicyId(propertyId);
149         policyDefinition.setUniqueId(policyId);
150
151         return policyDefinition;
152     }
153
154     private String getPolicyId(String propertyId) {
155         return SERVICE_ID + "." + propertyId + "." + POLICY;
156     }
157
158 }