Improve testing stability
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceArtifactsMergeTest.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.model.ArtifactDefinition;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.ComponentInstance;
35 import org.openecomp.sdc.be.model.Operation;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
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.LinkedList;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.stream.Collectors;
47
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.mockito.Mockito.when;
50
51 public class ComponentInstanceArtifactsMergeTest {
52
53         @InjectMocks
54         ComponentInstanceArtifactsMerge testInstance;
55
56         @Mock
57         ArtifactsBusinessLogic artifactsBusinessLogicMock;
58         
59         @Mock
60         ToscaOperationFacade toscaOperationFacadeMock;
61         
62         @Before
63         public void setUp() throws Exception {
64                 MockitoAnnotations.openMocks(this);
65         }
66
67         @Test
68         public void testDeploymentArtifactSaveData() throws Exception {
69
70                 Component containerComponent = new Resource();
71                 Component originComponent = buildOriginalComponentWithOneArtifact();
72         ComponentInstance componentInstance = buildComponentInstanceWithTwoArtifactsAndVfModuleFile();
73
74                 DataForMergeHolder dataForMergeHolder = new DataForMergeHolder();
75                 testInstance.saveDataBeforeMerge(dataForMergeHolder, containerComponent, componentInstance, originComponent);
76                 Map<String, ArtifactDefinition> originalComponentDeploymentArtifactsCreatedOnTheInstance = dataForMergeHolder
77                                 .getOrigComponentDeploymentArtifactsCreatedOnTheInstance();
78                 Map<String, Integer> componentInstanceDeploymentArtifactsTimeOut = dataForMergeHolder
79                                 .getComponentInstanceDeploymentArtifactsTimeOut();
80         assertThat(originalComponentDeploymentArtifactsCreatedOnTheInstance.size()).isEqualTo(1);
81                 assertThat(componentInstanceDeploymentArtifactsTimeOut.size()).isEqualTo(3);
82                 assertThat(originalComponentDeploymentArtifactsCreatedOnTheInstance.containsKey("artifactTwo")).isTrue();
83         }
84
85         @Test
86         public void testInformationalArtifactSaveData() throws Exception {
87
88                 Component containerComponent = new Resource();
89                 Component originComponent = buildOriginalComponentWithOneArtifact();
90                 ComponentInstance componentInstance = buildComponentInstanceWithTwoArtifacts();
91
92                 DataForMergeHolder dataForMergeHolder = new DataForMergeHolder();
93                 testInstance.saveDataBeforeMerge(dataForMergeHolder, containerComponent, componentInstance, originComponent);
94                 Map<String, ArtifactDefinition> originalComponentInformationalArtifactsCreatedOnTheInstance = dataForMergeHolder
95                                 .getOrigComponentInformationalArtifactsCreatedOnTheInstance();
96
97         assertThat(originalComponentInformationalArtifactsCreatedOnTheInstance.size()).isEqualTo(1);
98                 assert (originalComponentInformationalArtifactsCreatedOnTheInstance.containsKey("artifactTwo"));
99         }
100
101         @Test
102         public void testMergeDataAfterCreate() throws Exception {
103
104                 Component originComponent = buildOriginalComponentWithOneArtifact();
105                 List<ComponentInstance> resourceInstances = new LinkedList<>();
106                 ComponentInstance ci = new ComponentInstance();
107                 ci.setUniqueId("mock");
108                 Map<String, ArtifactDefinition> currentDeploymentArtifacts = buildDeploymentArtifacts();
109                 ci.setDeploymentArtifacts(currentDeploymentArtifacts);
110                 resourceInstances.add(ci);
111                 originComponent.setComponentInstances(resourceInstances);
112                 DataForMergeHolder dataForMergeHolder = new DataForMergeHolder();
113                 Map<String, ArtifactDefinition> origDeploymentArtifacts = new HashMap<>();
114                 ArtifactDefinition currentArtifactDefinition = new ArtifactDefinition();
115                 origDeploymentArtifacts.put("mock", currentArtifactDefinition);
116                 dataForMergeHolder.setOrigComponentDeploymentArtifactsCreatedOnTheInstance(origDeploymentArtifacts);
117                 Map<String, ArtifactDefinition> updateDeploymentArtifacts = buildDeploymentArtifacts();
118                 updateDeploymentArtifacts.get("artifactOne").setTimeout(55);
119                 dataForMergeHolder.setComponentInstanceDeploymentArtifactsTimeOut(updateDeploymentArtifacts.entrySet().stream()
120                                 .collect(Collectors.toMap(Map.Entry::getKey, artifact -> artifact.getValue().getTimeout())));
121                 when(artifactsBusinessLogicMock.buildJsonForUpdateArtifact(Mockito.anyString(), 
122                                 Mockito.anyString(),
123                                 Mockito.anyString(), 
124                                 Mockito.any(ArtifactGroupTypeEnum.class), 
125                                 Mockito.anyString(), 
126                                 Mockito.anyString(),
127                                 Mockito.anyString(), 
128                                 Mockito.any(byte[].class), 
129                                 Mockito.any(), 
130                                 Mockito.any(List.class)))
131                                                 .thenReturn(new HashMap<>());
132                 
133                 Either<ArtifactDefinition, Operation> left = Either.left(new ArtifactDefinition());
134
135                 User user = new User();
136
137                 when(artifactsBusinessLogicMock.updateResourceInstanceArtifactNoContent("mock", originComponent, user,
138                                 new HashMap<>(), new ArtifactOperationInfo(false, false,
139                                                 ArtifactsBusinessLogic.ArtifactOperationEnum.LINK), currentDeploymentArtifacts.get("artifactOne"))).thenReturn(left);
140
141                 when(artifactsBusinessLogicMock.updateResourceInstanceArtifactNoContent(Mockito.anyString(), Mockito.any(Component.class), Mockito.any(User.class),
142                                 Mockito.any(Map.class), Mockito.any(ArtifactOperationInfo.class), Mockito.any(ArtifactDefinition.class))).thenReturn(left);
143
144                 Component result = testInstance.mergeDataAfterCreate(user, dataForMergeHolder, originComponent, "mock");
145                 assertThat(result.getDeploymentArtifacts()).isEqualTo(originComponent.getDeploymentArtifacts());
146         }
147
148         private Map<String, ArtifactDefinition> buildDeploymentArtifacts() {
149                 ArtifactDefinition artifactFromTheOriginalResource = new ArtifactDefinition();
150                 artifactFromTheOriginalResource.setArtifactLabel("artifactOne");
151                 artifactFromTheOriginalResource.setTimeout(30);
152                 ArtifactDefinition artifactCreatedOnTheInstance = new ArtifactDefinition();
153                 artifactCreatedOnTheInstance.setArtifactLabel("artifactTwo");
154                 artifactCreatedOnTheInstance.setTimeout(30);
155                 ArtifactDefinition artifactGeneratedBySubmitForTesting = new ArtifactDefinition();
156                 artifactGeneratedBySubmitForTesting.setArtifactLabel("artifactThree");
157                 artifactGeneratedBySubmitForTesting.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType());
158                 artifactGeneratedBySubmitForTesting.setTimeout(30);
159                 Map<String, ArtifactDefinition> componentInstanceArtifacts = new HashMap<>();
160                 componentInstanceArtifacts.put(artifactFromTheOriginalResource.getArtifactLabel(), artifactFromTheOriginalResource);
161                 componentInstanceArtifacts.put(artifactCreatedOnTheInstance.getArtifactLabel(), artifactCreatedOnTheInstance);
162                 componentInstanceArtifacts.put(artifactGeneratedBySubmitForTesting.getArtifactLabel(), artifactGeneratedBySubmitForTesting);
163                 return componentInstanceArtifacts;
164         }
165
166         private ComponentInstance buildComponentInstanceWithTwoArtifactsAndVfModuleFile(){
167
168                 Map<String, ArtifactDefinition> componentInstanceArtifacts = buildDeploymentArtifacts();
169                 ComponentInstance componentInstance = new ComponentInstance();
170                 componentInstance.setArtifacts(componentInstanceArtifacts);
171                 componentInstance.setDeploymentArtifacts(componentInstanceArtifacts);
172                 return componentInstance;
173         }
174
175         private ComponentInstance buildComponentInstanceWithTwoArtifacts() {
176                 ArtifactDefinition artifactFromTheOriginalResource = new ArtifactDefinition();
177                 artifactFromTheOriginalResource.setArtifactLabel("artifactOne");
178                 artifactFromTheOriginalResource.setTimeout(30);
179                 ArtifactDefinition artifactCreatedOnTheInstance = new ArtifactDefinition();
180                 artifactCreatedOnTheInstance.setArtifactLabel("artifactTwo");
181                 artifactCreatedOnTheInstance.setTimeout(30);
182
183                 Map<String, ArtifactDefinition> componentInstanceArtifacts = new HashMap<>();
184                 componentInstanceArtifacts.put(artifactFromTheOriginalResource.getArtifactLabel(),
185                                 artifactFromTheOriginalResource);
186                 componentInstanceArtifacts.put(artifactCreatedOnTheInstance.getArtifactLabel(), artifactCreatedOnTheInstance);
187
188                 ComponentInstance componentInstance = new ComponentInstance();
189                 componentInstance.setDeploymentArtifacts(componentInstanceArtifacts);
190                 componentInstance.setArtifacts(componentInstanceArtifacts);
191                 return componentInstance;
192         }
193
194         private Component buildOriginalComponentWithOneArtifact() {
195                 ArtifactDefinition artifactFromTheOriginalResource = new ArtifactDefinition();
196                 artifactFromTheOriginalResource.setArtifactLabel("artifactOne");
197                 artifactFromTheOriginalResource.setTimeout(30);
198
199                 Map<String, ArtifactDefinition> originComponentArtifacts = new HashMap<>();
200                 originComponentArtifacts.put(artifactFromTheOriginalResource.getArtifactLabel(),
201                                 artifactFromTheOriginalResource);
202                 Component originComponent = new Resource();
203                 originComponent.setDeploymentArtifacts(originComponentArtifacts);
204                 originComponent.setArtifacts(originComponentArtifacts);
205                 return originComponent;
206         }
207
208 }