2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.enrichment.impl.external.artifact;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.openecomp.core.enrichment.types.ArtifactCategory;
30 import org.openecomp.core.enrichment.types.MonitoringUploadType;
31 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
32 import org.openecomp.core.model.types.ServiceArtifact;
33 import org.openecomp.core.utilities.file.FileUtils;
34 import org.openecomp.sdc.enrichment.EnrichmentInfo;
35 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
36 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
37 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
38 import org.openecomp.sdc.tosca.services.DataModelUtil;
39 import org.openecomp.sdc.tosca.services.ToscaUtil;
40 import org.onap.sdc.tosca.services.YamlUtil;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
45 import org.openecomp.sdc.versioning.dao.types.Version;
46 import org.testng.Assert;
47 import org.testng.annotations.BeforeMethod;
48 import org.testng.annotations.Test;
51 import java.io.IOException;
52 import java.nio.ByteBuffer;
53 import java.util.ArrayList;
54 import java.util.HashMap;
55 import java.util.HashSet;
56 import java.util.List;
58 import java.util.Optional;
60 import java.util.regex.Pattern;
61 import java.util.stream.Collectors;
62 import java.util.stream.Stream;
64 import static org.mockito.Matchers.anyObject;
65 import static org.mockito.Mockito.atLeastOnce;
66 import static org.mockito.Mockito.times;
69 public class MonitoringMibEnricherTest {
71 private ComponentArtifactDao componentArtifactDaoMock;
73 private EnrichedServiceModelDao enrichedServiceModelDaoMock;
75 private ComponentDao componentDaoMock;
78 private MonitoringMibEnricher monitoringMibEnricher;
81 @BeforeMethod(alwaysRun = true)
82 public void injectDoubles() {
83 MockitoAnnotations.initMocks(this);
87 public void testEnrichComponent() throws Exception {
89 String componentId = "1";
90 Version version = new Version();
94 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId, vspId +
96 setMockToEnrichComponent(vspId, version, componentId);
97 String componentName = componentEntity.getComponentCompositionData().getName();
98 String unifiedComponentName =
99 ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
101 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
102 ArgumentCaptor.forClass(ServiceArtifact.class);
103 monitoringMibEnricher.enrichComponent(vspId, version, componentEntity,
104 Stream.of(unifiedComponentName).collect(Collectors.toSet()));
106 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
107 .storeExternalArtifact(expectedServiceArtifact.capture());
108 Assert.assertEquals(expectedServiceArtifact.getValue().getName()
109 .startsWith(unifiedComponentName), true);
110 Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
111 unifiedComponentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
112 File.separator + MonitoringUploadType.VES_EVENTS + File.separator + "mib1.yml");
117 public void testEnrichmentOfTwoComponentsFromSameType() throws IOException {
118 EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
119 Version version = new Version();
122 String vspId = "123";
123 enrichmentInfo.setKey(vspId);
124 enrichmentInfo.setVersion(version);
125 String componentId1 = "1";
126 String componentId2 = "2";
127 String abstType = "org.openecomp.resource.abstract.nodes.pd_server";
128 String abstType1 = "org.openecomp.resource.abstract.nodes.pd_server_1";
130 List<ComponentEntity> returnedComponents = new ArrayList<>();
131 returnedComponents.add(getComponentEntity(vspId, version, componentId1,
133 returnedComponents.add(getComponentEntity(vspId, version, componentId2,
135 Mockito.when(componentDaoMock.list(anyObject()))
136 .thenReturn(returnedComponents);
137 setMockToEnrichComponent(vspId, version, componentId1);
139 ToscaServiceModel mockServiceModel =
140 getMockServiceModel("/mock/enrichMib/toscaModel/twoAbstractNodesFromSameType");
142 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
143 ArgumentCaptor.forClass(ServiceArtifact.class);
144 monitoringMibEnricher.enrich(enrichmentInfo, mockServiceModel);
146 Mockito.verify(enrichedServiceModelDaoMock, times(24))
147 .storeExternalArtifact(expectedServiceArtifact.capture());
149 Set<String> prefixes = getAllMibDirectoryPrefixes(expectedServiceArtifact.getAllValues());
151 validateExpectedAbstractTypes(Stream.of(abstType, abstType1).collect(Collectors.toSet()), prefixes);
154 private void validateExpectedAbstractTypes(Set<String> expectedAbstractTypes,
155 Set<String> prefixes) {
156 for(String abstType : expectedAbstractTypes){
157 Assert.assertTrue(prefixes.contains(abstType));
161 private void setMockToEnrichComponent(String vspId, Version version, String componentId) {
162 ComponentMonitoringUploadEntity returnedArtifact = new ComponentMonitoringUploadEntity();
163 returnedArtifact.setVspId(vspId);
164 returnedArtifact.setVersion(version);
165 returnedArtifact.setComponentId(componentId);
166 returnedArtifact.setType(MonitoringUploadType.SNMP_POLL);
167 returnedArtifact.setArtifactName("mib.zip");
168 returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip"));
170 Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
171 .thenReturn(Optional.of(returnedArtifact));
172 Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
175 private ComponentEntity getComponentEntity(String vspId,
178 String componentNameSuffix) {
179 ComponentEntity componentEntity = new ComponentEntity();
180 componentEntity.setId(componentId);
181 componentEntity.setVspId(vspId);
182 componentEntity.setVersion(version);
184 String compositionData = "{\n" +
185 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentNameSuffix + "\",\n" +
186 " \"displayName\": \"" + componentNameSuffix + "\"\n" +
188 componentEntity.setCompositionData(compositionData);
189 return componentEntity;
192 private ByteBuffer getMibByteBuffer(String fileName) {
193 byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
194 FileUtils::toByteArray);
195 return ByteBuffer.wrap(mibBytes);
198 private ToscaServiceModel getMockServiceModel(String serviceTemplatesDirectory)
200 File directory = new File(this.getClass().getResource(serviceTemplatesDirectory).getFile());
201 ToscaServiceModel serviceModel = new ToscaServiceModel();
202 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
204 for (final File serviceTemplateFile : directory.listFiles()) {
205 byte[] content = FileUtils
206 .readViaInputStream(this.getClass().getResource(serviceTemplatesDirectory + File
207 .separator + serviceTemplateFile.getName()),
208 FileUtils::toByteArray);
209 ServiceTemplate serviceTemplate =
210 new YamlUtil().yamlToObject(new String(content), ServiceTemplate.class);
211 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate);
214 serviceModel.setServiceTemplates(serviceTemplates);
218 private Set<String> getAllMibDirectoryPrefixes(List<ServiceArtifact> serviceArtifacts) {
219 if(CollectionUtils.isEmpty(serviceArtifacts)){
220 return new HashSet<>();
223 Set<String> prefixes = new HashSet<>();
224 for(ServiceArtifact serviceArtifact : serviceArtifacts){
225 String absolutePath = serviceArtifact.getName();
226 prefixes.add(absolutePath.split(Pattern.quote(File.separator))[0]);