84050dce49898b89de76980d3825d29ba65c1be0
[sdc.git] /
1 package org.openecomp.sdc.enrichment.impl.external.artifact;
2
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;
23
24 import java.io.File;
25 import java.nio.ByteBuffer;
26 import java.util.ArrayList;
27
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;
32
33 public class ProcessArtifactEnricherTest {
34   @Mock
35   ProcessDao processDaoMock;
36   @Mock
37   EnrichedServiceModelDao enrichedServiceModelDaoMock;
38   @Mock
39   ComponentDao componentDaoMock;
40   @InjectMocks
41   ProcessArtifactEnricher processArtifactEnricher;
42
43   @BeforeMethod(alwaysRun = true)
44   public void injectDoubles() {
45     MockitoAnnotations.initMocks(this);
46   }
47
48   @Test
49   public void testEnrichComponent() throws Exception {
50     String vspId = "123";
51     String componentId = "1111111111";
52     Version version = new Version();
53     version.setMajor(1);
54     version.setMinor(0);
55
56     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
57
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"));
66
67     Collection<ComponentEntity> componentList = new ArrayList<>();
68     componentList.add(componentEntity);
69     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
70
71     Collection<ProcessEntity> list = new ArrayList<>();
72     list.add(processEntity);
73     when(processDaoMock.list(entity)).thenReturn(list);
74
75     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
76
77     EnrichmentInfo info = new EnrichmentInfo();
78     info.setVersion(version);
79     info.setKey(vspId);
80     processArtifactEnricher.enrich(info, null);
81
82     String componentName = componentEntity.getComponentCompositionData().getName();
83
84     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
85         ArgumentCaptor.forClass(ServiceArtifact.class);
86     Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
87         .storeExternalArtifact(expectedServiceArtifact.capture());
88     Assert
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");
93
94   }
95
96   @Test
97   public void testEnrichComponentArtifactNameIsNull() throws Exception {
98     String vspId = "123";
99     String componentId = "1111111111";
100     Version version = new Version();
101     version.setMajor(1);
102     version.setMinor(0);
103
104     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
105
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"));
114
115     Collection<ComponentEntity> componentList = new ArrayList<>();
116     componentList.add(componentEntity);
117     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
118
119     Collection<ProcessEntity> list = new ArrayList<>();
120     list.add(processEntity);
121     when(processDaoMock.list(entity)).thenReturn(list);
122
123     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
124
125     EnrichmentInfo info = new EnrichmentInfo();
126     info.setVersion(version);
127     info.setKey(vspId);
128     processArtifactEnricher.enrich(info, null);
129
130     String componentName = componentEntity.getComponentCompositionData().getName();
131
132     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
133         ArgumentCaptor.forClass(ServiceArtifact.class);
134     Mockito.verify(enrichedServiceModelDaoMock, never())
135         .storeExternalArtifact(expectedServiceArtifact.capture());
136   }
137
138   @Test
139   public void testEnrichComponentProcessEntityIsNull() throws Exception {
140     String vspId = "123";
141     String componentId = "1111111111";
142     Version version = new Version();
143     version.setMajor(1);
144     version.setMinor(0);
145
146     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
147
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"));
156
157     Collection<ComponentEntity> componentList = new ArrayList<>();
158     componentList.add(componentEntity);
159     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
160
161     Collection<ProcessEntity> list = new ArrayList<>();
162     list.add(processEntity);
163     when(processDaoMock.list(entity)).thenReturn(list);
164
165     when(processDaoMock.getArtifact(anyObject())).thenReturn(null);
166
167     EnrichmentInfo info = new EnrichmentInfo();
168     info.setVersion(version);
169     info.setKey(vspId);
170     processArtifactEnricher.enrich(info, null);
171
172     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
173         ArgumentCaptor.forClass(ServiceArtifact.class);
174     Mockito.verify(enrichedServiceModelDaoMock, never())
175         .storeExternalArtifact(expectedServiceArtifact.capture());
176   }
177
178   @Test
179   public void testEnrichComponentNotALifecycleOperations() throws Exception {
180     String vspId = "123";
181     String componentId = "1111111111";
182     Version version = new Version();
183     version.setMajor(1);
184     version.setMinor(0);
185
186     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
187
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"));
196
197     Collection<ComponentEntity> componentList = new ArrayList<>();
198     componentList.add(componentEntity);
199     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
200
201     Collection<ProcessEntity> list = new ArrayList<>();
202     list.add(processEntity);
203     when(processDaoMock.list(entity)).thenReturn(list);
204
205     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
206
207     EnrichmentInfo info = new EnrichmentInfo();
208     info.setVersion(version);
209     info.setKey(vspId);
210     processArtifactEnricher.enrich(info, null);
211
212     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
213         ArgumentCaptor.forClass(ServiceArtifact.class);
214     Mockito.verify(enrichedServiceModelDaoMock, never())
215         .storeExternalArtifact(expectedServiceArtifact.capture());
216   }
217
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);
223
224     String componentName = vspId + "enrichMib_server";
225     String compositionData = "{\n" +
226         "  \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
227         "  \"displayName\": \"" + componentName + "\"\n" +
228         "}";
229     componentEntity.setCompositionData(compositionData);
230     return componentEntity;
231   }
232
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);
237   }
238 }