Sorted out unit-test libraries in onboarding
[sdc.git] / openecomp-be / lib / openecomp-sdc-enrichment-lib / openecomp-sdc-enrichment-impl / src / test / java / org / openecomp / sdc / enrichment / impl / external / artifact / ProcessArtifactEnricherTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.enrichment.impl.external.artifact;
18
19 import static org.mockito.Matchers.anyObject;
20 import static org.mockito.Mockito.atLeastOnce;
21 import static org.mockito.Mockito.never;
22 import static org.mockito.Mockito.when;
23
24 import java.io.File;
25 import java.nio.ByteBuffer;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.openecomp.core.enrichment.types.ArtifactCategory;
37 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
38 import org.openecomp.core.model.types.ServiceArtifact;
39 import org.openecomp.core.utilities.file.FileUtils;
40 import org.openecomp.sdc.enrichment.EnrichmentInfo;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessType;
46 import org.openecomp.sdc.versioning.dao.types.Version;
47
48 public class ProcessArtifactEnricherTest {
49   @Mock
50   ProcessDao processDaoMock;
51   @Mock
52   EnrichedServiceModelDao enrichedServiceModelDaoMock;
53   @Mock
54   ComponentDao componentDaoMock;
55   @InjectMocks
56   ProcessArtifactEnricher processArtifactEnricher;
57
58   @Before
59   public void injectDoubles() {
60     MockitoAnnotations.initMocks(this);
61   }
62
63   @Test
64   public void testEnrichComponent() throws Exception {
65     String vspId = "123";
66     String componentId = "1111111111";
67     Version version = new Version();
68     version.setMajor(1);
69     version.setMinor(0);
70
71     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
72
73     ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
74     ProcessEntity processEntity = new ProcessEntity();
75     processEntity.setType(ProcessType.Lifecycle_Operations);
76     processEntity.setVspId(vspId);
77     processEntity.setVersion(version);
78     processEntity.setComponentId(componentId);
79     processEntity.setArtifactName("artifact_1kb.txt");
80     processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
81
82     Collection<ComponentEntity> componentList = new ArrayList<>();
83     componentList.add(componentEntity);
84     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
85
86     Collection<ProcessEntity> list = new ArrayList<>();
87     list.add(processEntity);
88     when(processDaoMock.list(entity)).thenReturn(list);
89
90     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
91
92     EnrichmentInfo info = new EnrichmentInfo();
93     info.setVersion(version);
94     info.setKey(vspId);
95     processArtifactEnricher.enrich(info, null);
96
97     String componentName = componentEntity.getComponentCompositionData().getName();
98
99     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
100         ArgumentCaptor.forClass(ServiceArtifact.class);
101     Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
102         .storeExternalArtifact(expectedServiceArtifact.capture());
103     Assert
104         .assertEquals(expectedServiceArtifact.getValue().getName().startsWith(componentName), true);
105     Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
106         componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
107             File.separator + "Lifecycle Operations" + File.separator + "artifact_1kb.txt");
108
109   }
110
111   @Test
112   public void testEnrichComponentArtifactNameIsNull() throws Exception {
113     String vspId = "123";
114     String componentId = "1111111111";
115     Version version = new Version();
116     version.setMajor(1);
117     version.setMinor(0);
118
119     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
120
121     ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
122     ProcessEntity processEntity = new ProcessEntity();
123     processEntity.setType(ProcessType.Other);
124     processEntity.setVspId(vspId);
125     processEntity.setVersion(version);
126     processEntity.setComponentId(componentId);
127     processEntity.setArtifactName("artifact_1kb.txt");
128     processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
129
130     Collection<ComponentEntity> componentList = new ArrayList<>();
131     componentList.add(componentEntity);
132     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
133
134     Collection<ProcessEntity> list = new ArrayList<>();
135     list.add(processEntity);
136     when(processDaoMock.list(entity)).thenReturn(list);
137
138     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
139
140     EnrichmentInfo info = new EnrichmentInfo();
141     info.setVersion(version);
142     info.setKey(vspId);
143     processArtifactEnricher.enrich(info, null);
144
145     String componentName = componentEntity.getComponentCompositionData().getName();
146
147     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
148         ArgumentCaptor.forClass(ServiceArtifact.class);
149     Mockito.verify(enrichedServiceModelDaoMock, never())
150         .storeExternalArtifact(expectedServiceArtifact.capture());
151   }
152
153   @Test
154   public void testEnrichComponentProcessEntityIsNull() throws Exception {
155     String vspId = "123";
156     String componentId = "1111111111";
157     Version version = new Version();
158     version.setMajor(1);
159     version.setMinor(0);
160
161     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
162
163     ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
164     ProcessEntity processEntity = new ProcessEntity();
165     processEntity.setType(ProcessType.Other);
166     processEntity.setVspId(vspId);
167     processEntity.setVersion(version);
168     processEntity.setComponentId(componentId);
169     processEntity.setArtifactName("artifact_1kb.txt");
170     processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
171
172     Collection<ComponentEntity> componentList = new ArrayList<>();
173     componentList.add(componentEntity);
174     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
175
176     Collection<ProcessEntity> list = new ArrayList<>();
177     list.add(processEntity);
178     when(processDaoMock.list(entity)).thenReturn(list);
179
180     when(processDaoMock.getArtifact(anyObject())).thenReturn(null);
181
182     EnrichmentInfo info = new EnrichmentInfo();
183     info.setVersion(version);
184     info.setKey(vspId);
185     processArtifactEnricher.enrich(info, null);
186
187     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
188         ArgumentCaptor.forClass(ServiceArtifact.class);
189     Mockito.verify(enrichedServiceModelDaoMock, never())
190         .storeExternalArtifact(expectedServiceArtifact.capture());
191   }
192
193   @Test
194   public void testEnrichComponentNotALifecycleOperations() throws Exception {
195     String vspId = "123";
196     String componentId = "1111111111";
197     Version version = new Version();
198     version.setMajor(1);
199     version.setMinor(0);
200
201     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
202
203     ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
204     ProcessEntity processEntity = new ProcessEntity();
205     processEntity.setType(ProcessType.Lifecycle_Operations);
206     processEntity.setVspId(vspId);
207     processEntity.setVersion(version);
208     processEntity.setComponentId(componentId);
209     processEntity.setArtifactName(null);
210     processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
211
212     Collection<ComponentEntity> componentList = new ArrayList<>();
213     componentList.add(componentEntity);
214     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
215
216     Collection<ProcessEntity> list = new ArrayList<>();
217     list.add(processEntity);
218     when(processDaoMock.list(entity)).thenReturn(list);
219
220     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
221
222     EnrichmentInfo info = new EnrichmentInfo();
223     info.setVersion(version);
224     info.setKey(vspId);
225     processArtifactEnricher.enrich(info, null);
226
227     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
228         ArgumentCaptor.forClass(ServiceArtifact.class);
229     Mockito.verify(enrichedServiceModelDaoMock, never())
230         .storeExternalArtifact(expectedServiceArtifact.capture());
231   }
232
233   private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) {
234     ComponentEntity componentEntity = new ComponentEntity();
235     componentEntity.setId(componentId);
236     componentEntity.setVspId(vspId);
237     componentEntity.setVersion(version);
238
239     String componentName = vspId + "enrichMib_server";
240     String compositionData = "{\n" +
241         "  \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
242         "  \"displayName\": \"" + componentName + "\"\n" +
243         "}";
244     componentEntity.setCompositionData(compositionData);
245     return componentEntity;
246   }
247
248   private ByteBuffer getMibByteBuffer(String fileName) {
249     byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
250         stream -> FileUtils.toByteArray(stream));
251     return ByteBuffer.wrap(mibBytes);
252   }
253 }