Added oparent to sdc main
[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 static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.when;
26
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;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class ComponentPropertyToPolicyDeclaratorTest extends PropertyDeclaratorTestBase {
50
51     @InjectMocks
52     ComponentPropertyToPolicyDeclarator declarator;
53     @Mock
54     private ToscaOperationFacade toscaOperationFacade;
55     @Captor
56     ArgumentCaptor<PropertyDefinition> propertyCaptor;
57
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";
63
64
65     @Test
66     public void testDeclarePropertyToPolicy_success() {
67         Service service = new Service();
68         service.setUniqueId(SERVICE_ID);
69
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)));
73
74         Assert.assertTrue(declareEither.isLeft());
75     }
76
77     @Test
78     public void testUndeclarePolicy_success() {
79         Service service = new Service();
80         service.setUniqueId(SERVICE_ID);
81
82         PolicyDefinition policyDefinition = createPolicyDefinition(PROPERTY_ID);
83
84         PropertyDefinition expectedProperty = createPropertyWithDeclaredPolicy(getPolicyId(PROPERTY_ID));
85         service.addProperty(expectedProperty);
86
87         when(toscaOperationFacade.updatePropertyOfComponent(eq(service), propertyCaptor.capture())).thenReturn(Either.left(new PropertyDefinition()));
88
89         StorageOperationStatus storageOperationStatus =
90                 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
91
92         PropertyDefinition actualProperty = propertyCaptor.getValue();
93
94         Assert.assertEquals(storageOperationStatus, storageOperationStatus.OK);
95         Assert.assertEquals(expectedProperty, actualProperty);
96
97     }
98
99     @Test
100     public void shouldReturnOriginalPropertyValueAfterUndeclaring() {
101         Service service = new Service();
102         service.setUniqueId(SERVICE_ID);
103
104         PropertyDefinition expectedProperty = new PropertyDefinition(prop1);
105         addGetPolicyValueToProperty(getPolicyId(prop1.getUniqueId()), expectedProperty);
106         service.addProperty(expectedProperty);
107
108         when(toscaOperationFacade.updatePropertyOfComponent(eq(service), propertyCaptor.capture())).thenReturn(Either.left(new PropertyDefinition()));
109
110         Either<List<PolicyDefinition>, StorageOperationStatus> declareEither = declarator.declarePropertiesAsPolicies(
111                 service, OWNER_ID, createInstancePropInputList(Collections.singletonList(prop1)));
112
113         Assert.assertTrue(declareEither.isLeft());
114
115         PolicyDefinition policyDefinition = createPolicyDefinition(prop1.getUniqueId());
116         StorageOperationStatus storageOperationStatus =
117                 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
118
119         List<PropertyDefinition> actualProperties = propertyCaptor.getAllValues();
120
121         Assert.assertEquals(storageOperationStatus, storageOperationStatus.OK);
122         Assert.assertEquals(actualProperties.size(), 2);
123         Assert.assertEquals(prop1.getValue(), actualProperties.get(1).getValue());
124     }
125
126     private PropertyDefinition createPropertyWithDeclaredPolicy(String policyId) {
127         PropertyDefinition propertyDefinition = new PropertyDefinition();
128         propertyDefinition.setUniqueId(PROPERTY_ID);
129
130         addGetPolicyValueToProperty(policyId, propertyDefinition);
131         return propertyDefinition;
132     }
133
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());
139
140         List<GetPolicyValueDataDefinition> getPolicyList = new ArrayList<>();
141         getPolicyList.add(getPolicyValueDataDefinition);
142         propertyDefinition.setGetPolicyValues(getPolicyList);
143     }
144
145     private PolicyDefinition createPolicyDefinition(String propertyId) {
146         PolicyDefinition policyDefinition = new PolicyDefinition();
147         String policyId = getPolicyId(propertyId);
148         policyDefinition.setUniqueId(policyId);
149
150         return policyDefinition;
151     }
152
153     private String getPolicyId(String propertyId) {
154         return SERVICE_ID + "." + propertyId + "." + POLICY;
155     }
156
157 }