re base code
[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 package org.openecomp.sdc.enrichment.impl.external.artifact;
2
3 import org.mockito.*;
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;
18
19 import java.io.File;
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 import java.util.Collection;
23
24 import static org.mockito.Matchers.anyObject;
25 import static org.mockito.Mockito.*;
26
27 public class ProcessArtifactEnricherTest {
28   @Mock
29   ProcessDao processDaoMock;
30   @Mock
31   EnrichedServiceModelDao enrichedServiceModelDaoMock;
32   @Mock
33   ComponentDao componentDaoMock;
34   @InjectMocks
35   ProcessArtifactEnricher processArtifactEnricher;
36
37   @BeforeMethod(alwaysRun = true)
38   public void injectDoubles() {
39     MockitoAnnotations.initMocks(this);
40   }
41
42   @Test
43   public void testEnrichComponent() throws Exception {
44     String vspId = "123";
45     String componentId = "1111111111";
46     Version version = new Version();
47     version.setMajor(1);
48     version.setMinor(0);
49
50     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
51
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"));
60
61     Collection<ComponentEntity> componentList = new ArrayList<>();
62     componentList.add(componentEntity);
63     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
64
65     Collection<ProcessEntity> list = new ArrayList<>();
66     list.add(processEntity);
67     when(processDaoMock.list(entity)).thenReturn(list);
68
69     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
70
71     EnrichmentInfo info = new EnrichmentInfo();
72     info.setVersion(version);
73     info.setKey(vspId);
74     processArtifactEnricher.enrich(info, null);
75
76     String componentName = componentEntity.getComponentCompositionData().getName();
77
78     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
79         ArgumentCaptor.forClass(ServiceArtifact.class);
80     Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
81         .storeExternalArtifact(expectedServiceArtifact.capture());
82     Assert
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");
87
88   }
89
90   @Test
91   public void testEnrichComponentArtifactNameIsNull() throws Exception {
92     String vspId = "123";
93     String componentId = "1111111111";
94     Version version = new Version();
95     version.setMajor(1);
96     version.setMinor(0);
97
98     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
99
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"));
108
109     Collection<ComponentEntity> componentList = new ArrayList<>();
110     componentList.add(componentEntity);
111     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
112
113     Collection<ProcessEntity> list = new ArrayList<>();
114     list.add(processEntity);
115     when(processDaoMock.list(entity)).thenReturn(list);
116
117     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
118
119     EnrichmentInfo info = new EnrichmentInfo();
120     info.setVersion(version);
121     info.setKey(vspId);
122     processArtifactEnricher.enrich(info, null);
123
124     String componentName = componentEntity.getComponentCompositionData().getName();
125
126     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
127         ArgumentCaptor.forClass(ServiceArtifact.class);
128     Mockito.verify(enrichedServiceModelDaoMock, never())
129         .storeExternalArtifact(expectedServiceArtifact.capture());
130   }
131
132   @Test
133   public void testEnrichComponentProcessEntityIsNull() throws Exception {
134     String vspId = "123";
135     String componentId = "1111111111";
136     Version version = new Version();
137     version.setMajor(1);
138     version.setMinor(0);
139
140     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
141
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"));
150
151     Collection<ComponentEntity> componentList = new ArrayList<>();
152     componentList.add(componentEntity);
153     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
154
155     Collection<ProcessEntity> list = new ArrayList<>();
156     list.add(processEntity);
157     when(processDaoMock.list(entity)).thenReturn(list);
158
159     when(processDaoMock.getArtifact(anyObject())).thenReturn(null);
160
161     EnrichmentInfo info = new EnrichmentInfo();
162     info.setVersion(version);
163     info.setKey(vspId);
164     processArtifactEnricher.enrich(info, null);
165
166     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
167         ArgumentCaptor.forClass(ServiceArtifact.class);
168     Mockito.verify(enrichedServiceModelDaoMock, never())
169         .storeExternalArtifact(expectedServiceArtifact.capture());
170   }
171
172   @Test
173   public void testEnrichComponentNotALifecycleOperations() throws Exception {
174     String vspId = "123";
175     String componentId = "1111111111";
176     Version version = new Version();
177     version.setMajor(1);
178     version.setMinor(0);
179
180     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
181
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"));
190
191     Collection<ComponentEntity> componentList = new ArrayList<>();
192     componentList.add(componentEntity);
193     when(componentDaoMock.list(anyObject())).thenReturn(componentList);
194
195     Collection<ProcessEntity> list = new ArrayList<>();
196     list.add(processEntity);
197     when(processDaoMock.list(entity)).thenReturn(list);
198
199     when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
200
201     EnrichmentInfo info = new EnrichmentInfo();
202     info.setVersion(version);
203     info.setKey(vspId);
204     processArtifactEnricher.enrich(info, null);
205
206     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
207         ArgumentCaptor.forClass(ServiceArtifact.class);
208     Mockito.verify(enrichedServiceModelDaoMock, never())
209         .storeExternalArtifact(expectedServiceArtifact.capture());
210   }
211
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);
217
218     String componentName = vspId + "enrichMib_server";
219     String compositionData = "{\n" +
220         "  \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
221         "  \"displayName\": \"" + componentName + "\"\n" +
222         "}";
223     componentEntity.setCompositionData(compositionData);
224     return componentEntity;
225   }
226
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);
231   }
232 }