2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.enrichment.impl.external.artifact;
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;
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;
48 public class ProcessArtifactEnricherTest {
50 ProcessDao processDaoMock;
52 EnrichedServiceModelDao enrichedServiceModelDaoMock;
54 ComponentDao componentDaoMock;
56 ProcessArtifactEnricher processArtifactEnricher;
59 public void injectDoubles() {
60 MockitoAnnotations.initMocks(this);
64 public void testEnrichComponent() throws Exception {
66 String componentId = "1111111111";
67 Version version = new Version();
71 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
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"));
82 Collection<ComponentEntity> componentList = new ArrayList<>();
83 componentList.add(componentEntity);
84 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
86 Collection<ProcessEntity> list = new ArrayList<>();
87 list.add(processEntity);
88 when(processDaoMock.list(entity)).thenReturn(list);
90 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
92 EnrichmentInfo info = new EnrichmentInfo();
93 info.setVersion(version);
95 processArtifactEnricher.enrich(info, null);
97 String componentName = componentEntity.getComponentCompositionData().getName();
99 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
100 ArgumentCaptor.forClass(ServiceArtifact.class);
101 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
102 .storeExternalArtifact(expectedServiceArtifact.capture());
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");
112 public void testEnrichComponentArtifactNameIsNull() throws Exception {
113 String vspId = "123";
114 String componentId = "1111111111";
115 Version version = new Version();
119 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
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"));
130 Collection<ComponentEntity> componentList = new ArrayList<>();
131 componentList.add(componentEntity);
132 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
134 Collection<ProcessEntity> list = new ArrayList<>();
135 list.add(processEntity);
136 when(processDaoMock.list(entity)).thenReturn(list);
138 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
140 EnrichmentInfo info = new EnrichmentInfo();
141 info.setVersion(version);
143 processArtifactEnricher.enrich(info, null);
145 String componentName = componentEntity.getComponentCompositionData().getName();
147 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
148 ArgumentCaptor.forClass(ServiceArtifact.class);
149 Mockito.verify(enrichedServiceModelDaoMock, never())
150 .storeExternalArtifact(expectedServiceArtifact.capture());
154 public void testEnrichComponentProcessEntityIsNull() throws Exception {
155 String vspId = "123";
156 String componentId = "1111111111";
157 Version version = new Version();
161 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
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"));
172 Collection<ComponentEntity> componentList = new ArrayList<>();
173 componentList.add(componentEntity);
174 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
176 Collection<ProcessEntity> list = new ArrayList<>();
177 list.add(processEntity);
178 when(processDaoMock.list(entity)).thenReturn(list);
180 when(processDaoMock.getArtifact(anyObject())).thenReturn(null);
182 EnrichmentInfo info = new EnrichmentInfo();
183 info.setVersion(version);
185 processArtifactEnricher.enrich(info, null);
187 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
188 ArgumentCaptor.forClass(ServiceArtifact.class);
189 Mockito.verify(enrichedServiceModelDaoMock, never())
190 .storeExternalArtifact(expectedServiceArtifact.capture());
194 public void testEnrichComponentNotALifecycleOperations() throws Exception {
195 String vspId = "123";
196 String componentId = "1111111111";
197 Version version = new Version();
201 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
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"));
212 Collection<ComponentEntity> componentList = new ArrayList<>();
213 componentList.add(componentEntity);
214 when(componentDaoMock.list(anyObject())).thenReturn(componentList);
216 Collection<ProcessEntity> list = new ArrayList<>();
217 list.add(processEntity);
218 when(processDaoMock.list(entity)).thenReturn(list);
220 when(processDaoMock.getArtifact(anyObject())).thenReturn(processEntity);
222 EnrichmentInfo info = new EnrichmentInfo();
223 info.setVersion(version);
225 processArtifactEnricher.enrich(info, null);
227 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
228 ArgumentCaptor.forClass(ServiceArtifact.class);
229 Mockito.verify(enrichedServiceModelDaoMock, never())
230 .storeExternalArtifact(expectedServiceArtifact.capture());
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);
239 String componentName = vspId + "enrichMib_server";
240 String compositionData = "{\n" +
241 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
242 " \"displayName\": \"" + componentName + "\"\n" +
244 componentEntity.setCompositionData(compositionData);
245 return componentEntity;
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);