a23508f797e495bf6d9e6370c976e7fb1143cab1
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceHeatEnvMergeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.instance;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
31 import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
32 import org.openecomp.sdc.be.components.merge.heat.HeatEnvArtifactsMergeBusinessLogic;
33 import org.openecomp.sdc.be.components.utils.ArtifactBuilder;
34 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
35 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
36 import org.openecomp.sdc.be.impl.ComponentsUtils;
37 import org.openecomp.sdc.be.model.ArtifactDefinition;
38 import org.openecomp.sdc.be.model.Component;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.User;
41 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
42 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
43
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.stream.Collectors;
48 import java.util.stream.Stream;
49
50 import static org.assertj.core.api.Assertions.assertThat;
51 import static org.mockito.ArgumentMatchers.isNull;
52 import static org.mockito.Mockito.when;
53
54 public class ComponentInstanceHeatEnvMergeTest {
55
56     @InjectMocks
57     private ComponentInstanceHeatEnvMerge testInstance;
58
59     @Mock
60     private ArtifactsBusinessLogic artifactsBusinessLogicMock;
61
62     @Mock
63     private HeatEnvArtifactsMergeBusinessLogic heatEnvArtifactsMergeBusinessLogicMock;
64
65     @Mock
66     private ComponentsUtils componentsUtils;
67
68     private static final User USER = new User();
69
70     @Before
71     public void setUp() throws Exception {
72         MockitoAnnotations.initMocks(this);
73     }
74
75     @Test
76     public void mergeDataAfterCreate_mergeAndPersistArtifacts() throws Exception {
77         Map<String, ArtifactDefinition> nodeTypeArtifactsByName = buildMapOfHeatArtifacts("artifact1", "artifact2");
78         DataForMergeHolder dataHolder = new DataForMergeHolder();
79         dataHolder.setOrigComponentDeploymentArtifactsCreatedOnTheInstance(nodeTypeArtifactsByName);
80         String instanceId = "instance1";
81         Resource resource = buildResourceWithHeatArtifacts(instanceId, "heatArtifact1", "heatArtifact2");
82         List<ArtifactDefinition> mergedArtifacts = buildListOfArtifacts("artifact1, heatArtifact1");
83         when(heatEnvArtifactsMergeBusinessLogicMock.mergeInstanceHeatEnvArtifacts(dataHolder.getOrigComponentInstanceHeatEnvArtifacts(), resource.safeGetComponentInstanceHeatArtifacts(instanceId)))
84                                                    .thenReturn(mergedArtifacts);
85         expectMergedArtifactsToBePersisted(mergedArtifacts, instanceId, resource);
86         Component result = testInstance.mergeDataAfterCreate(USER, dataHolder, resource, instanceId);
87         assertThat(result.getDeploymentArtifacts()).isEqualTo(resource.getDeploymentArtifacts());
88     }
89
90     private void expectMergedArtifactsToBePersisted(List<ArtifactDefinition> mergedArtifacts, String instanceId, Resource resource) {
91         for (ArtifactDefinition mergedArtifact : mergedArtifacts) {
92             Map<String, Object> json = new HashMap<>();
93             when(artifactsBusinessLogicMock.buildJsonForUpdateArtifact(mergedArtifact, ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(json);
94             ArtifactOperationInfo artifactUpdateOperation = new ArtifactOperationInfo(false, false, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE);
95             when(artifactsBusinessLogicMock.updateResourceInstanceArtifactNoContent(Mockito.eq(instanceId), Mockito.eq(resource),
96                                                                                     Mockito.eq(USER), Mockito.eq(json),
97                                                                                     Mockito.refEq(artifactUpdateOperation),
98                                                                                     isNull()))
99                                            .thenReturn(Either.left(new ArtifactDefinition()));
100         }
101     }
102
103     private Resource buildResourceWithHeatArtifacts(String instanceId, String ... artifacts) {
104         ComponentInstanceBuilder componentInstanceBuilder = new ComponentInstanceBuilder().setId(instanceId);
105         for (String artifact : artifacts) {
106             ArtifactDefinition heatArtifact = new ArtifactBuilder().setType(ArtifactTypeEnum.HEAT_ARTIFACT.getType()).setName(artifact).build();
107             componentInstanceBuilder.addDeploymentArtifact(heatArtifact);
108         }
109         return new ResourceBuilder().addComponentInstance(componentInstanceBuilder.build()).build();
110     }
111
112     private Map<String, ArtifactDefinition> buildMapOfHeatArtifacts(String ... artifacts) {
113         Map<String, ArtifactDefinition> artifactsByName = new HashMap<>();
114         for (String artifact : artifacts) {
115             ArtifactDefinition heatArtifact = new ArtifactBuilder().setType(ArtifactTypeEnum.HEAT_ARTIFACT.getType()).setName(artifact).build();
116             artifactsByName.put(artifact, heatArtifact);
117         }
118         return artifactsByName;
119     }
120
121     private List<ArtifactDefinition> buildListOfArtifacts(String ... artifacts) {
122         return Stream.of(artifacts).map(artifact -> new ArtifactBuilder().setName(artifact).build()).collect(Collectors.toList());
123     }
124
125 }