summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/input/ComponentInputsMergeBLTest.java
blob: 26b6782918612e1292eae40bd0120d10417e7944 (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
package org.openecomp.sdc.be.components.merge.input;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.collections.ListUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.utils.ObjectGenerator;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;

import fj.data.Either;

public class ComponentInputsMergeBLTest {

    @InjectMocks
    private ComponentInputsMergeBL testInstance;

    @Mock
    private InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogicMock;

    @Mock
    private ToscaOperationFacade toscaOperationFacade;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void mergeComponentInputs() {
        Resource oldResource = new ResourceBuilder()
                .addInput("input1")
                .addInput("input2")
                .build();

        Resource newResource = new Resource();

        List<InputDefinition> inputsToMerge = ObjectGenerator.buildInputs("input1", "input2", "input3");

        when(toscaOperationFacade.updateInputsToComponent(inputsToMerge, newResource.getUniqueId())).thenReturn(Either.left(inputsToMerge));
        ActionStatus actionStatus = testInstance.mergeComponentInputs(oldResource, newResource, inputsToMerge);
        assertEquals(ActionStatus.OK, actionStatus);
        verifyCallToMergeComponentInputs(oldResource, inputsToMerge);
    }

    @SuppressWarnings("unchecked")
    @Test
    public void mergeAndRedeclareComponentInputs() throws Exception {
        Resource oldResource = new ResourceBuilder()
                .addInput("input1")
                .addInput("input2")
                .build();

        Resource newResource = ObjectGenerator.buildBasicResource();
        List<InputDefinition> inputsToMerge = ObjectGenerator.buildInputs("input1", "input2", "input3");
        List<InputDefinition> inputsToRedeclare = ObjectGenerator.buildInputs("input4");
        List<InputDefinition> expectedInputsToUpdate = ListUtils.union(inputsToMerge, inputsToRedeclare);
        when(inputsValuesMergingBusinessLogicMock.getPreviouslyDeclaredInputsToMerge(oldResource, newResource)).thenReturn(inputsToRedeclare);
        when(toscaOperationFacade.updateInputsToComponent(expectedInputsToUpdate, newResource.getUniqueId())).thenReturn(Either.left(inputsToMerge));
        ActionStatus actionStatus = testInstance.mergeAndRedeclareComponentInputs(oldResource, newResource, inputsToMerge);
        assertEquals(ActionStatus.OK, actionStatus);
    }

    @Test
    public void redeclareResourceInputsForInstance() throws Exception {
        List<InputDefinition> oldInputs = ObjectGenerator.buildInputs("input1", "input2");
        Resource newResource = ObjectGenerator.buildBasicResource();
        List<InputDefinition> inputsToRedeclare = ObjectGenerator.buildInputs("input1");
        when(inputsValuesMergingBusinessLogicMock.getPreviouslyDeclaredInputsToMerge(oldInputs, newResource, "inst1")).thenReturn(inputsToRedeclare);
        when(toscaOperationFacade.updateInputsToComponent(inputsToRedeclare, newResource.getUniqueId())).thenReturn(Either.left(inputsToRedeclare));
        ActionStatus actionStatus = testInstance.redeclareComponentInputsForInstance(oldInputs, newResource, "inst1");
    }

    private void verifyCallToMergeComponentInputs(Resource oldResource, List<InputDefinition> inputsToMerge) {
        Map<String, InputDefinition> oldInputsByName = oldResource.getInputs().stream().collect(Collectors.toMap(InputDefinition::getName, Function.identity()));
        Map<String, InputDefinition> inputsToMergeByName = inputsToMerge.stream().collect(Collectors.toMap(InputDefinition::getName, Function.identity()));
        verify(inputsValuesMergingBusinessLogicMock).mergeComponentInputs(oldInputsByName, inputsToMergeByName);
    }

}