Fix test coverage
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / property / ComponentInstanceInputPropertyDeclaratorTest.java
1 /*
2  * ============LICENSE_START=============================================================================================================
3  * Copyright (c) 2019 <Company or Individual>.
4  * ===================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6  *
7  *        http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
10  * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
11  * ============LICENSE_END===============================================================================================================
12  *
13  */
14 package org.openecomp.sdc.be.components.property;
15
16 import static org.assertj.core.api.Assertions.assertThat;
17 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
18 import static org.mockito.ArgumentMatchers.anyMap;
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.Mockito.when;
21 import static org.openecomp.sdc.be.MockGenerator.mockComponentUtils;
22 import static org.openecomp.sdc.be.MockGenerator.mockExceptionUtils;
23
24 import fj.data.Either;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Captor;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.openecomp.sdc.be.components.utils.AnnotationBuilder;
38 import org.openecomp.sdc.be.components.utils.InputsBuilder;
39 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
40 import org.openecomp.sdc.be.datatypes.elements.Annotation;
41 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
42 import org.openecomp.sdc.be.model.Component;
43 import org.openecomp.sdc.be.model.ComponentInstance;
44 import org.openecomp.sdc.be.model.ComponentInstanceInput;
45 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
46 import org.openecomp.sdc.be.model.ComponentParametersView;
47 import org.openecomp.sdc.be.model.InputDefinition;
48 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
49 import org.openecomp.sdc.be.model.operations.StorageException;
50 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
54
55
56     private ComponentInstanceInputPropertyDeclarator testInstance;
57
58     @Mock
59     private ToscaOperationFacade toscaOperationFacade;
60
61     @Captor
62     private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
63
64     private Annotation annotation1;
65
66     private Annotation annotation2;
67
68     @Override
69     @Before
70     public void setUp() throws Exception {
71         super.setUp();
72         testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), null,
73                 toscaOperationFacade, null, mockExceptionUtils());
74         annotation1 =  AnnotationBuilder.create()
75                 .setType("annotationType1")
76                 .setName("annotation1")
77                 .addProperty("prop1")
78                 .addProperty("prop2")
79                 .build();
80
81         annotation2 =  AnnotationBuilder.create()
82                 .setType("annotationType2")
83                 .setName("annotation2")
84                 .addProperty("prop3")
85                 .build();
86     }
87
88     @Test
89     public void whenDeclaredPropertyOriginalInputContainsAnnotation_createNewInputWithSameAnnotations() {
90         List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
91         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
92         Component originInstanceNodeType = createComponentWithInputAndAnnotation(prop1.getName());
93         when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap())).thenReturn(Either.left(Collections.emptyMap()));
94         when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.left(originInstanceNodeType));
95         Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
96         List<InputDefinition> inputs = createdInputs.left().value();
97         assertThat(inputs).hasSize(1);
98         verifyInputAnnotations(inputs.get(0));
99         assertThat(inputsFilterCaptor.getValue().isIgnoreInputs()).isFalse();
100     }
101
102     @Test
103     public void throwExceptionWhenFailingToGetInstanceOriginType() {
104         List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
105         List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
106         when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
107         assertThatExceptionOfType(StorageException.class).isThrownBy(() -> testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare));
108     }
109
110     @Test
111     public void testCreateDeclaredProperty() {
112         ComponentInstanceInput declaredProperty = testInstance.createDeclaredProperty(prop1);
113         assertThat(declaredProperty.getUniqueId()).isEqualTo(prop1.getUniqueId());
114     }
115
116     @Test
117     public void testUpdateDeclaredProperties() {
118         List<ComponentInstanceInput> expectedProperties = Collections.singletonList(new ComponentInstanceInput(prop1));
119         Map<String, List<ComponentInstanceInput>> expectedMap = new HashMap<>();
120         expectedMap.put(prop1.getName(), expectedProperties);
121
122         when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap()))
123                 .thenReturn(Either.left(expectedMap));
124
125         Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateEither =
126                 (Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus>) testInstance.updatePropertiesValues(resource, resource.getUniqueId(), expectedProperties);
127
128         assertThat(updateEither.isLeft());
129         Map<String, List<ComponentInstanceInput>> actualProperties = updateEither.left().value();
130         assertThat(actualProperties.values().size()).isEqualTo(expectedProperties.size());
131         assertThat(actualProperties.values().iterator().next()).isEqualTo(expectedProperties);
132     }
133
134     @Test
135     public void testResolvePropertiesOwner() {
136         Optional<ComponentInstance> componentInstance = testInstance.resolvePropertiesOwner(resource, INSTANCE_ID);
137
138         assertThat(componentInstance.isPresent());
139         assertThat(componentInstance.get().getUniqueId()).isEqualTo(INSTANCE_ID);
140     }
141
142     private void verifyInputAnnotations(InputDefinition inputDefinition) {
143         List<Annotation> annotations = inputDefinition.getAnnotations();
144         assertThat(annotations)
145                 .containsExactlyInAnyOrder(annotation1, annotation2);
146     }
147
148     private Component createComponentWithInputAndAnnotation(String inputName) {
149         InputDefinition input = InputsBuilder.create()
150                 .setName(inputName)
151                 .addAnnotation(annotation1)
152                 .addAnnotation(annotation2)
153                 .build();
154         return new ResourceBuilder()
155                 .addInput(input)
156                 .build();
157     }
158
159 }