Catalog alignment
[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.merge.heat.HeatEnvArtifactsMergeBusinessLogic;
32 import org.openecomp.sdc.be.components.utils.ArtifactBuilder;
33 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
34 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.model.ArtifactDefinition;
37 import org.openecomp.sdc.be.model.Resource;
38 import org.openecomp.sdc.be.model.User;
39 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
40 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
41
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.stream.Collectors;
46 import java.util.stream.Stream;
47
48 import static org.mockito.ArgumentMatchers.isNull;
49 import static org.mockito.Mockito.when;
50
51 public class ComponentInstanceHeatEnvMergeTest {
52
53     @InjectMocks
54     private ComponentInstanceHeatEnvMerge testInstance;
55
56     @Mock
57     private ArtifactsBusinessLogic artifactsBusinessLogicMock;
58
59     @Mock
60     private HeatEnvArtifactsMergeBusinessLogic heatEnvArtifactsMergeBusinessLogicMock;
61
62     @Mock
63     private ComponentsUtils componentsUtils;
64
65     private static final User USER = new User();
66
67     @Before
68     public void setUp() throws Exception {
69         MockitoAnnotations.initMocks(this);
70     }
71
72     @Test
73     public void mergeDataAfterCreate_mergeAndPersistArtifacts() throws Exception {
74         Map<String, ArtifactDefinition> nodeTypeArtifactsByName = buildMapOfHeatArtifacts("artifact1", "artifact2");
75         DataForMergeHolder dataHolder = new DataForMergeHolder();
76         dataHolder.setOrigComponentDeploymentArtifactsCreatedOnTheInstance(nodeTypeArtifactsByName);
77         String instanceId = "instance1";
78         Resource resource = buildResourceWithHeatArtifacts(instanceId, "heatArtifact1", "heatArtifact2");
79         List<ArtifactDefinition> mergedArtifacts = buildListOfArtifacts("artifact1, heatArtifact1");
80         when(heatEnvArtifactsMergeBusinessLogicMock.mergeInstanceHeatEnvArtifacts(dataHolder.getOrigComponentInstanceHeatEnvArtifacts(), resource.safeGetComponentInstanceHeatArtifacts(instanceId)))
81                                                    .thenReturn(mergedArtifacts);
82         expectMergedArtifactsToBePersisted(mergedArtifacts, instanceId, resource);
83         testInstance.mergeDataAfterCreate(USER, dataHolder, resource, instanceId);
84     }
85
86     private void expectMergedArtifactsToBePersisted(List<ArtifactDefinition> mergedArtifacts, String instanceId, Resource resource) {
87         for (ArtifactDefinition mergedArtifact : mergedArtifacts) {
88             Map<String, Object> json = new HashMap<>();
89             when(artifactsBusinessLogicMock.buildJsonForUpdateArtifact(mergedArtifact, ArtifactGroupTypeEnum.DEPLOYMENT, null)).thenReturn(json);
90             ArtifactsBusinessLogic.ArtifactOperationInfo artifactUpdateOperation = artifactsBusinessLogicMock.new ArtifactOperationInfo(false, false, ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE);
91             when(artifactsBusinessLogicMock.updateResourceInstanceArtifactNoContent(Mockito.eq(instanceId), Mockito.eq(resource),
92                                                                                     Mockito.eq(USER), Mockito.eq(json),
93                                                                                     Mockito.refEq(artifactUpdateOperation),
94                                                                                     isNull()))
95                                            .thenReturn(Either.left(new ArtifactDefinition()));
96         }
97     }
98
99     private Resource buildResourceWithHeatArtifacts(String instanceId, String ... artifacts) {
100         ComponentInstanceBuilder componentInstanceBuilder = new ComponentInstanceBuilder().setId(instanceId);
101         for (String artifact : artifacts) {
102             ArtifactDefinition heatArtifact = new ArtifactBuilder().setType(ArtifactTypeEnum.HEAT_ARTIFACT.getType()).setName(artifact).build();
103             componentInstanceBuilder.addDeploymentArtifact(heatArtifact);
104         }
105         return new ResourceBuilder().addComponentInstance(componentInstanceBuilder.build()).build();
106     }
107
108     private Map<String, ArtifactDefinition> buildMapOfHeatArtifacts(String ... artifacts) {
109         Map<String, ArtifactDefinition> artifactsByName = new HashMap<>();
110         for (String artifact : artifacts) {
111             ArtifactDefinition heatArtifact = new ArtifactBuilder().setType(ArtifactTypeEnum.HEAT_ARTIFACT.getType()).setName(artifact).build();
112             artifactsByName.put(artifact, heatArtifact);
113         }
114         return artifactsByName;
115     }
116
117     private List<ArtifactDefinition> buildListOfArtifacts(String ... artifacts) {
118         return Stream.of(artifacts).map(artifact -> new ArtifactBuilder().setName(artifact).build()).collect(Collectors.toList());
119     }
120
121 }