Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / propertytopolicydeclarators / ComponentInstancePropertyToPolicyDeclaratorTest.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.propertytopolicydeclarators;
22
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;
27
28 import fj.data.Either;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
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;
52
53 public class ComponentInstancePropertyToPolicyDeclaratorTest {
54
55     @Mock
56     private ToscaOperationFacade toscaOperationFacade;
57     @Mock
58     private PropertyBusinessLogic propertyBl;
59     @Mock
60     private ComponentInstanceBusinessLogic componentInstanceBl;
61     @InjectMocks
62     private ComponentInstancePropertyToPolicyDeclarator declarator;
63     @Captor
64     private ArgumentCaptor<ComponentInstanceProperty> captor;
65
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";
73
74
75     @Before
76     public void setUp() {
77         MockitoAnnotations.initMocks(this);
78
79         service = new Service();
80         componentInstance = new ComponentInstance();
81         componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
82         service.setComponentInstances(Collections.singletonList(componentInstance));
83
84         prop1 = new PolicyDataDefinition();
85         prop1.setName(PROP_1_NAME);
86     }
87
88     @Test
89     public void testCreateDeclaredProperty_success() {
90         ComponentInstanceProperty declaredProperty = declarator.createDeclaredProperty(prop1);
91         Assert.assertEquals(prop1.getName(), declaredProperty.getName());
92     }
93
94     @Test
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);
99
100         when(toscaOperationFacade
101                      .addComponentInstancePropertiesToComponent(eq(service), eq(expectedInstanceProperties)))
102                 .thenReturn(Either.left(expectedInstanceProperties));
103
104
105         Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> updateEither =
106                 (Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus>) declarator
107                                                                                                        .updatePropertiesValues(
108                                                                                                                service,
109                                                                                                                COMPONENT_INSTANCE_ID,
110                                                                                                                properties);
111
112         Assert.assertTrue(updateEither.isLeft());
113         Map<String, List<ComponentInstanceProperty>> actualInstanceProperties = updateEither.left().value();
114         validateUpdateResult(properties, expectedInstanceProperties, actualInstanceProperties);
115     }
116
117     @Test
118     public void testResolvePropertiesOwner_success() {
119         Optional<ComponentInstance> componentInstanceCandidate =
120                 declarator.resolvePropertiesOwner(service, COMPONENT_INSTANCE_ID);
121
122         Assert.assertTrue(componentInstanceCandidate.isPresent());
123         Assert.assertEquals(componentInstanceCandidate.get(), componentInstance);
124     }
125
126     @Test
127     public void testResolvePropertiesOwner_failure() {
128         Optional<ComponentInstance> componentInstanceCandidate =
129                 declarator.resolvePropertiesOwner(service, NON_EXIST_ID);
130
131         Assert.assertFalse(componentInstanceCandidate.isPresent());
132     }
133
134     @Test
135     public void testUnDeclarePropertiesAsPolicies_success() {
136         PolicyDefinition policyDefinition = new PolicyDefinition();
137         policyDefinition.setName(PROP_1_NAME);
138
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);
144
145         StorageOperationStatus status =
146                 declarator.unDeclarePropertiesAsPolicies(service, policyDefinition);
147
148         Assert.assertEquals(status, StorageOperationStatus.OK);
149
150         ComponentInstanceProperty actualProperty = captor.getValue();
151         Assert.assertEquals(prop1.getName(), actualProperty.getName());
152     }
153
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());
160
161         List<ComponentInstanceProperty> actualComponentInstanceProperties =
162                 actualInstanceProperties.get(COMPONENT_INSTANCE_ID);
163
164         Assert.assertEquals(properties, actualComponentInstanceProperties);
165     }
166 }