summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/ComponentInstanceInputPropertyDeclaratorTest.java
blob: acfa721e74ba022c0911e581be3375ba7cc85fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.openecomp.sdc.be.components.property;

import fj.data.Either;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.utils.AnnotationBuilder;
import org.openecomp.sdc.be.components.utils.InputsBuilder;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.datatypes.elements.Annotation;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstancePropInput;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.StorageException;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;

import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.openecomp.sdc.be.MockGenerator.mockComponentUtils;
import static org.openecomp.sdc.be.MockGenerator.mockExceptionUtils;

@RunWith(MockitoJUnitRunner.class)
public class ComponentInstanceInputPropertyDeclaratorTest extends PropertyDeclaratorTestBase {


    private ComponentInstanceInputPropertyDeclarator testInstance;

    @Mock
    private ToscaOperationFacade toscaOperationFacade;

    @Captor
    private ArgumentCaptor<ComponentParametersView> inputsFilterCaptor;

    private Annotation annotation1, annotation2;

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
        testInstance = new ComponentInstanceInputPropertyDeclarator(mockComponentUtils(), null,
                toscaOperationFacade, null, mockExceptionUtils());
        annotation1 =  AnnotationBuilder.create()
                .setType("annotationType1")
                .setName("annotation1")
                .addProperty("prop1")
                .addProperty("prop2")
                .build();

        annotation2 =  AnnotationBuilder.create()
                .setType("annotationType2")
                .setName("annotation2")
                .addProperty("prop3")
                .build();
    }

    @Test
    public void whenDeclaredPropertyOriginalInputContainsAnnotation_createNewInputWithSameAnnotations() {
        List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
        List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
        Component originInstanceNodeType = createComponentWithInputAndAnnotation(prop1.getName());
        when(toscaOperationFacade.addComponentInstanceInputsToComponent(eq(resource), anyMap())).thenReturn(Either.left(Collections.emptyMap()));
        when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.left(originInstanceNodeType));
        Either<List<InputDefinition>, StorageOperationStatus> createdInputs = testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare);
        List<InputDefinition> inputs = createdInputs.left().value();
        assertThat(inputs).hasSize(1);
        verifyInputAnnotations(inputs.get(0));
        assertThat(inputsFilterCaptor.getValue().isIgnoreInputs()).isFalse();
    }

    @Test
    public void throwExceptionWhenFailingToGetInstanceOriginType() {
        List<PropertyDataDefinition> properties = Collections.singletonList(prop1);
        List<ComponentInstancePropInput> propsToDeclare = createInstancePropInputList(properties);
        when(toscaOperationFacade.getToscaElement(eq(ORIGIN_INSTANCE_ID), inputsFilterCaptor.capture())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
        assertThatExceptionOfType(StorageException.class).isThrownBy(() -> testInstance.declarePropertiesAsInputs(resource, "inst1", propsToDeclare));
    }

    private void verifyInputAnnotations(InputDefinition inputDefinition) {
        List<Annotation> annotations = inputDefinition.getAnnotations();
        assertThat(annotations)
                .containsExactlyInAnyOrder(annotation1, annotation2);
    }

    private Component createComponentWithInputAndAnnotation(String inputName) {
        InputDefinition input = InputsBuilder.create()
                .setName(inputName)
                .addAnnotation(annotation1)
                .addAnnotation(annotation2)
                .build();
        return new ResourceBuilder()
                .addInput(input)
                .build();
    }

}