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
7 * http://www.apache.org/licenses/LICENSE-2.0
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===============================================================================================================
14 package org.openecomp.sdc.be.components.property;
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;
24 import fj.data.Either;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
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;
52 @RunWith(MockitoJUnitRunner.class)
53 public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {
56 private ComponentInstanceInputPropertyDeclarator testInstance;
59 private ToscaOperationFacade toscaOperationFacade;
62 private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;
64 private Annotation annotation1;
66 private Annotation annotation2;
70 public void setUp() throws Exception {
72 testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), null,
73 toscaOperationFacade, null, mockExceptionUtils());
74 annotation1 = AnnotationBuilder.create()
75 .setType("annotationType1")
76 .setName("annotation1")
81 annotation2 = AnnotationBuilder.create()
82 .setType("annotationType2")
83 .setName("annotation2")
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();
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));
111 public void testCreateDeclaredProperty() {
112 ComponentInstanceInput declaredProperty = testInstance.createDeclaredProperty(prop1);
113 assertThat(declaredProperty.getUniqueId()).isEqualTo(prop1.getUniqueId());
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);
122 when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap()))
123 .thenReturn(Either.left(expectedMap));
125 Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> updateEither =
126 (Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus>) testInstance.updatePropertiesValues(resource, resource.getUniqueId(), expectedProperties);
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);
135 public void testResolvePropertiesOwner() {
136 Optional<ComponentInstance> componentInstance = testInstance.resolvePropertiesOwner(resource, INSTANCE_ID);
138 assertThat(componentInstance.isPresent());
139 assertThat(componentInstance.get().getUniqueId()).isEqualTo(INSTANCE_ID);
142 private void verifyInputAnnotations(InputDefinition inputDefinition) {
143 List<Annotation> annotations = inputDefinition.getAnnotations();
144 assertThat(annotations)
145 .containsExactlyInAnyOrder(annotation1, annotation2);
148 private Component createComponentWithInputAndAnnotation(String inputName) {
149 InputDefinition input = InputsBuilder.create()
151 .addAnnotation(annotation1)
152 .addAnnotation(annotation2)
154 return new ResourceBuilder()