aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceHeatEnvMergeTest.java
blob: f5cd9249573ce0fb68d7a1fc308ed3dba729c2a2 (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
package org.openecomp.sdc.be.components.merge.instance;

import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
import org.openecomp.sdc.be.components.merge.heat.HeatEnvArtifactsMergeBusinessLogic;
import org.openecomp.sdc.be.components.utils.ArtifactBuilder;
import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
import org.openecomp.sdc.be.components.utils.ResourceBuilder;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
import org.openecomp.sdc.common.api.ArtifactTypeEnum;

import fj.data.Either;

public class ComponentInstanceHeatEnvMergeTest {

    @InjectMocks
    private ComponentInstanceHeatEnvMerge testInstance;

    @Mock
    private ArtifactsBusinessLogic artifactsBusinessLogicMock;

    @Mock
    private HeatEnvArtifactsMergeBusinessLogic heatEnvArtifactsMergeBusinessLogicMock;

    @Mock
    private ComponentsUtils componentsUtils;

    private static final User USER = new User();

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

    @Test
    public void mergeDataAfterCreate_mergeAndPersistArtifacts() throws Exception {
        Map<String, ArtifactDefinition> nodeTypeArtifactsByName = buildMapOfHeatArtifacts("artifact1", "artifact2");
        DataForMergeHolder dataHolder = new DataForMergeHolder();
        dataHolder.setOrigComponentDeploymentArtifactsCreatedOnTheInstance(nodeTypeArtifactsByName);
        String instanceId = "instance1";
        Resource resource = buildResourceWithHeatArtifacts(instanceId, "heatArtifact1", "heatArtifact2");
        List<ArtifactDefinition> mergedArtifacts = buildListOfArtifacts("artifact1, heatArtifact1");
        when(heatEnvArtifactsMergeBusinessLogicMock.mergeInstanceHeatEnvArtifacts(dataHolder.getOrigComponentInstanceHeatEnvArtifacts(), resource.safeGetComponentInstanceHeatArtifacts(instanceId)))
                                                   .thenReturn(mergedArtifacts);
        expectMergedArtifactsToBePersisted(mergedArtifacts, instanceId, resource);
        testInstance.mergeDataAfterCreate(USER, dataHolder, resource, instanceId);
    }

    private void expectMergedArtifactsToBePersisted(List<ArtifactDefinition> mergedArtifacts, String instanceId, Resource resource) {
        for (ArtifactDefinition mergedArtifact : mergedArtifacts) {
            Map<String, Object> json = new HashMap<>();
            when(artifactsBusinessLogicMock.buildJsonForUpdateArtifact(mergedArtifact, ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(json);
            ArtifactsBusinessLogic.ArtifactOperationInfo artifactUpdateOperation = artifactsBusinessLogicMock.new ArtifactOperationInfo(false, false, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE);
            when(artifactsBusinessLogicMock.updateResourceInstanceArtifactNoContent(Mockito.eq(instanceId), Mockito.eq(resource),
                                                                                    Mockito.eq(USER), Mockito.eq(json),
                                                                                    Mockito.refEq(artifactUpdateOperation),
                                                                                    Mockito.isNull(ArtifactDefinition.class)))
                                           .thenReturn(Either.left(Either.left(new ArtifactDefinition())));
        }
    }

    private Resource buildResourceWithHeatArtifacts(String instanceId, String ... artifacts) {
        ComponentInstanceBuilder componentInstanceBuilder = new ComponentInstanceBuilder().setId(instanceId);
        for (String artifact : artifacts) {
            ArtifactDefinition heatArtifact = new ArtifactBuilder().setType(ArtifactTypeEnum.HEAT_ARTIFACT.getType()).setName(artifact).build();
            componentInstanceBuilder.addDeploymentArtifact(heatArtifact);
        }
        return new ResourceBuilder().addComponentInstance(componentInstanceBuilder.build()).build();
    }

    private Map<String, ArtifactDefinition> buildMapOfHeatArtifacts(String ... artifacts) {
        Map<String, ArtifactDefinition> artifactsByName = new HashMap<>();
        for (String artifact : artifacts) {
            ArtifactDefinition heatArtifact = new ArtifactBuilder().setType(ArtifactTypeEnum.HEAT_ARTIFACT.getType()).setName(artifact).build();
            artifactsByName.put(artifact, heatArtifact);
        }
        return artifactsByName;
    }

    private List<ArtifactDefinition> buildListOfArtifacts(String ... artifacts) {
        return Stream.of(artifacts).map(artifact -> new ArtifactBuilder().setName(artifact).build()).collect(Collectors.toList());
    }

}