1 package org.openecomp.sdc.enrichment.impl.external.artifact;
4 import org.openecomp.core.enrichment.types.ArtifactCategory;
5 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
6 import org.openecomp.core.model.types.ServiceArtifact;
7 import org.openecomp.core.utilities.file.FileUtils;
8 import org.openecomp.sdc.enrichment.EnrichmentInfo;
9 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessType;
14 import org.openecomp.sdc.versioning.dao.types.Version;
15 import org.testng.Assert;
16 import org.testng.annotations.BeforeMethod;
17 import org.testng.annotations.Test;
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 import java.util.Collection;
24 import static org.mockito.Matchers.anyObject;
25 import static org.mockito.Mockito.*;
27 public class ProcessArtifactEnricherTest {
29 ProcessDao processDaoMock;
31 EnrichedServiceModelDao enrichedServiceModelDaoMock;
33 ComponentDao componentDaoMock;
35 ProcessArtifactEnricher processArtifactEnricher;
37 @BeforeMethod(alwaysRun = true)
38 public void injectDoubles() {
39 MockitoAnnotations.initMocks(this);
43 public void testEnrichComponent() throws Exception {
45 String componentId = "1111111111";
46 Version version = new Version();
50 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
52 ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
53 ProcessEntity processEntity = new ProcessEntity();
54 processEntity.setType(ProcessType.Lifecycle_Operations);
55 processEntity.setVspId(vspId);
56 processEntity.setVersion(version);
57 processEntity.setComponentId(componentId);
58 processEntity.setArtifactName("artifact_1kb.txt");
59 processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
61 Collection<ComponentEntity> componentList = new ArrayList<>();
62 componentList.add(componentEntity);
63 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
65 Collection<ProcessEntity> list = new ArrayList<>();
66 list.add(processEntity);
67 when(processDaoMock.list(entity)).thenReturn(list);
69 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
71 EnrichmentInfo info = new EnrichmentInfo();
72 info.setVersion(version);
74 processArtifactEnricher.enrich(info, null);
76 String componentName = componentEntity.getComponentCompositionData().getName();
78 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
79 ArgumentCaptor.forClass(ServiceArtifact.class);
80 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
81 .storeExternalArtifact(expectedServiceArtifact.capture());
83 .assertEquals(expectedServiceArtifact.getValue().getName().startsWith(componentName), true);
84 Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
85 componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
86 File.separator + "Lifecycle Operations" + File.separator + "artifact_1kb.txt");
91 public void testEnrichComponentArtifactNameIsNull() throws Exception {
93 String componentId = "1111111111";
94 Version version = new Version();
98 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
100 ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
101 ProcessEntity processEntity = new ProcessEntity();
102 processEntity.setType(ProcessType.Other);
103 processEntity.setVspId(vspId);
104 processEntity.setVersion(version);
105 processEntity.setComponentId(componentId);
106 processEntity.setArtifactName("artifact_1kb.txt");
107 processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
109 Collection<ComponentEntity> componentList = new ArrayList<>();
110 componentList.add(componentEntity);
111 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
113 Collection<ProcessEntity> list = new ArrayList<>();
114 list.add(processEntity);
115 when(processDaoMock.list(entity)).thenReturn(list);
117 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
119 EnrichmentInfo info = new EnrichmentInfo();
120 info.setVersion(version);
122 processArtifactEnricher.enrich(info, null);
124 String componentName = componentEntity.getComponentCompositionData().getName();
126 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
127 ArgumentCaptor.forClass(ServiceArtifact.class);
128 Mockito.verify(enrichedServiceModelDaoMock, never())
129 .storeExternalArtifact(expectedServiceArtifact.capture());
133 public void testEnrichComponentProcessEntityIsNull() throws Exception {
134 String vspId = "123";
135 String componentId = "1111111111";
136 Version version = new Version();
140 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
142 ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
143 ProcessEntity processEntity = new ProcessEntity();
144 processEntity.setType(ProcessType.Other);
145 processEntity.setVspId(vspId);
146 processEntity.setVersion(version);
147 processEntity.setComponentId(componentId);
148 processEntity.setArtifactName("artifact_1kb.txt");
149 processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
151 Collection<ComponentEntity> componentList = new ArrayList<>();
152 componentList.add(componentEntity);
153 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
155 Collection<ProcessEntity> list = new ArrayList<>();
156 list.add(processEntity);
157 when(processDaoMock.list(entity)).thenReturn(list);
159 when(processDaoMock.getArtifact(anyObject())).thenReturn(null);
161 EnrichmentInfo info = new EnrichmentInfo();
162 info.setVersion(version);
164 processArtifactEnricher.enrich(info, null);
166 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
167 ArgumentCaptor.forClass(ServiceArtifact.class);
168 Mockito.verify(enrichedServiceModelDaoMock, never())
169 .storeExternalArtifact(expectedServiceArtifact.capture());
173 public void testEnrichComponentNotALifecycleOperations() throws Exception {
174 String vspId = "123";
175 String componentId = "1111111111";
176 Version version = new Version();
180 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
182 ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null);
183 ProcessEntity processEntity = new ProcessEntity();
184 processEntity.setType(ProcessType.Lifecycle_Operations);
185 processEntity.setVspId(vspId);
186 processEntity.setVersion(version);
187 processEntity.setComponentId(componentId);
188 processEntity.setArtifactName(null);
189 processEntity.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt"));
191 Collection<ComponentEntity> componentList = new ArrayList<>();
192 componentList.add(componentEntity);
193 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
195 Collection<ProcessEntity> list = new ArrayList<>();
196 list.add(processEntity);
197 when(processDaoMock.list(entity)).thenReturn(list);
199 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
201 EnrichmentInfo info = new EnrichmentInfo();
202 info.setVersion(version);
204 processArtifactEnricher.enrich(info, null);
206 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
207 ArgumentCaptor.forClass(ServiceArtifact.class);
208 Mockito.verify(enrichedServiceModelDaoMock, never())
209 .storeExternalArtifact(expectedServiceArtifact.capture());
212 private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) {
213 ComponentEntity componentEntity = new ComponentEntity();
214 componentEntity.setId(componentId);
215 componentEntity.setVspId(vspId);
216 componentEntity.setVersion(version);
218 String componentName = vspId + "enrichMib_server";
219 String compositionData = "{\n" +
220 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
221 " \"displayName\": \"" + componentName + "\"\n" +
223 componentEntity.setCompositionData(compositionData);
224 return componentEntity;
227 private ByteBuffer getMibByteBuffer(String fileName) {
228 byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
229 stream -> FileUtils.toByteArray(stream));
230 return ByteBuffer.wrap(mibBytes);