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