1 package org.openecomp.sdc.enrichment.impl.external.artifact;
3 import java.util.Collection;
4 import org.mockito.ArgumentCaptor;
5 import org.mockito.InjectMocks;
6 import org.mockito.Mock;
7 import org.mockito.Mockito;
8 import org.mockito.MockitoAnnotations;
9 import org.openecomp.core.enrichment.types.ArtifactCategory;
10 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
11 import org.openecomp.core.model.types.ServiceArtifact;
12 import org.openecomp.core.utilities.file.FileUtils;
13 import org.openecomp.sdc.enrichment.EnrichmentInfo;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessType;
19 import org.openecomp.sdc.versioning.dao.types.Version;
20 import org.testng.Assert;
21 import org.testng.annotations.BeforeMethod;
22 import org.testng.annotations.Test;
25 import java.nio.ByteBuffer;
26 import java.util.ArrayList;
28 import static org.mockito.Matchers.anyObject;
29 import static org.mockito.Mockito.atLeastOnce;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.when;
33 public class ProcessArtifactEnricherTest {
35 ProcessDao processDaoMock;
37 EnrichedServiceModelDao enrichedServiceModelDaoMock;
39 ComponentDao componentDaoMock;
41 ProcessArtifactEnricher processArtifactEnricher;
43 @BeforeMethod(alwaysRun = true)
44 public void injectDoubles() {
45 MockitoAnnotations.initMocks(this);
49 public void testEnrichComponent() throws Exception {
51 String componentId = "1111111111";
52 Version version = new Version();
56 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
58 ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
59 ProcessEntity processEntity = new ProcessEntity();
60 processEntity.setType(ProcessType.Lifecycle_Operations);
61 processEntity.setVspId(vspId);
62 processEntity.setVersion(version);
63 processEntity.setComponentId(componentId);
64 processEntity.setArtifactName("artifact_1kb.txt");
65 processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
67 Collection<ComponentEntity> componentList = new ArrayList<>();
68 componentList.add(componentEntity);
69 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
71 Collection<ProcessEntity> list = new ArrayList<>();
72 list.add(processEntity);
73 when(processDaoMock.list(entity)).thenReturn(list);
75 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
77 EnrichmentInfo info = new EnrichmentInfo();
78 info.setVersion(version);
80 processArtifactEnricher.enrich(info, null);
82 String componentName = componentEntity.getComponentCompositionData().getName();
84 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
85 ArgumentCaptor.forClass(ServiceArtifact.class);
86 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
87 .storeExternalArtifact(expectedServiceArtifact.capture());
89 .assertEquals(expectedServiceArtifact.getValue().getName().startsWith(componentName), true);
90 Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
91 componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
92 File.separator + "Lifecycle Operations" + File.separator + "artifact_1kb.txt");
97 public void testEnrichComponentArtifactNameIsNull() throws Exception {
99 String componentId = "1111111111";
100 Version version = new Version();
104 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
106 ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
107 ProcessEntity processEntity = new ProcessEntity();
108 processEntity.setType(ProcessType.Other);
109 processEntity.setVspId(vspId);
110 processEntity.setVersion(version);
111 processEntity.setComponentId(componentId);
112 processEntity.setArtifactName("artifact_1kb.txt");
113 processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
115 Collection<ComponentEntity> componentList = new ArrayList<>();
116 componentList.add(componentEntity);
117 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
119 Collection<ProcessEntity> list = new ArrayList<>();
120 list.add(processEntity);
121 when(processDaoMock.list(entity)).thenReturn(list);
123 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
125 EnrichmentInfo info = new EnrichmentInfo();
126 info.setVersion(version);
128 processArtifactEnricher.enrich(info, null);
130 String componentName = componentEntity.getComponentCompositionData().getName();
132 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
133 ArgumentCaptor.forClass(ServiceArtifact.class);
134 Mockito.verify(enrichedServiceModelDaoMock, never())
135 .storeExternalArtifact(expectedServiceArtifact.capture());
139 public void testEnrichComponentProcessEntityIsNull() throws Exception {
140 String vspId = "123";
141 String componentId = "1111111111";
142 Version version = new Version();
146 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
148 ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
149 ProcessEntity processEntity = new ProcessEntity();
150 processEntity.setType(ProcessType.Other);
151 processEntity.setVspId(vspId);
152 processEntity.setVersion(version);
153 processEntity.setComponentId(componentId);
154 processEntity.setArtifactName("artifact_1kb.txt");
155 processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
157 Collection<ComponentEntity> componentList = new ArrayList<>();
158 componentList.add(componentEntity);
159 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
161 Collection<ProcessEntity> list = new ArrayList<>();
162 list.add(processEntity);
163 when(processDaoMock.list(entity)).thenReturn(list);
165 when(processDaoMock.getArtifact(anyObject())).thenReturn(null);
167 EnrichmentInfo info = new EnrichmentInfo();
168 info.setVersion(version);
170 processArtifactEnricher.enrich(info, null);
172 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
173 ArgumentCaptor.forClass(ServiceArtifact.class);
174 Mockito.verify(enrichedServiceModelDaoMock, never())
175 .storeExternalArtifact(expectedServiceArtifact.capture());
179 public void testEnrichComponentNotALifecycleOperations() throws Exception {
180 String vspId = "123";
181 String componentId = "1111111111";
182 Version version = new Version();
186 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
188 ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
189 ProcessEntity processEntity = new ProcessEntity();
190 processEntity.setType(ProcessType.Lifecycle_Operations);
191 processEntity.setVspId(vspId);
192 processEntity.setVersion(version);
193 processEntity.setComponentId(componentId);
194 processEntity.setArtifactName(null);
195 processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
197 Collection<ComponentEntity> componentList = new ArrayList<>();
198 componentList.add(componentEntity);
199 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
201 Collection<ProcessEntity> list = new ArrayList<>();
202 list.add(processEntity);
203 when(processDaoMock.list(entity)).thenReturn(list);
205 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
207 EnrichmentInfo info = new EnrichmentInfo();
208 info.setVersion(version);
210 processArtifactEnricher.enrich(info, null);
212 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
213 ArgumentCaptor.forClass(ServiceArtifact.class);
214 Mockito.verify(enrichedServiceModelDaoMock, never())
215 .storeExternalArtifact(expectedServiceArtifact.capture());
218 private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) {
219 ComponentEntity componentEntity = new ComponentEntity();
220 componentEntity.setId(componentId);
221 componentEntity.setVspId(vspId);
222 componentEntity.setVersion(version);
224 String componentName = vspId + "enrichMib_server";
225 String compositionData = "{\n" +
226 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
227 " \"displayName\": \"" + componentName + "\"\n" +
229 componentEntity.setCompositionData(compositionData);
230 return componentEntity;
233 private ByteBuffer getMibByteBuffer(String fileName) {
234 byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
235 stream -> FileUtils.toByteArray(stream));
236 return ByteBuffer.wrap(mibBytes);