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 static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.atLeastOnce;
25 import static org.mockito.Mockito.times;
28 import java.io.IOException;
29 import java.nio.ByteBuffer;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
35 import java.util.Optional;
37 import java.util.regex.Pattern;
38 import java.util.stream.Collectors;
39 import java.util.stream.Stream;
40 import org.apache.commons.collections4.CollectionUtils;
41 import org.junit.Assert;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.ArgumentCaptor;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.mockito.Mockito;
48 import org.mockito.MockitoAnnotations;
49 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
50 import org.onap.sdc.tosca.services.YamlUtil;
51 import org.openecomp.core.enrichment.types.ArtifactCategory;
52 import org.openecomp.core.enrichment.types.MonitoringUploadType;
53 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
54 import org.openecomp.core.model.types.ServiceArtifact;
55 import org.openecomp.core.utilities.file.FileUtils;
56 import org.openecomp.sdc.enrichment.EnrichmentInfo;
57 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
58 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
59 import org.openecomp.sdc.tosca.services.DataModelUtil;
60 import org.openecomp.sdc.tosca.services.ToscaUtil;
61 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
62 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
63 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
64 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
65 import org.openecomp.sdc.versioning.dao.types.Version;
68 public class MonitoringMibEnricherTest {
70 private ComponentArtifactDao componentArtifactDaoMock;
72 private EnrichedServiceModelDao enrichedServiceModelDaoMock;
74 private ComponentDao componentDaoMock;
77 private MonitoringMibEnricher monitoringMibEnricher;
81 public void injectDoubles() {
82 MockitoAnnotations.openMocks(this);
86 public void testEnrichComponent() throws Exception {
88 String componentId = "1";
89 Version version = new Version();
93 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId, vspId +
95 setMockToEnrichComponent(vspId, version, componentId);
96 String componentName = componentEntity.getComponentCompositionData().getName();
97 String unifiedComponentName =
98 ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
100 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
101 ArgumentCaptor.forClass(ServiceArtifact.class);
102 monitoringMibEnricher.enrichComponent(vspId, version, componentEntity,
103 Stream.of(unifiedComponentName).collect(Collectors.toSet()));
105 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
106 .storeExternalArtifact(expectedServiceArtifact.capture());
107 Assert.assertEquals(expectedServiceArtifact.getValue().getName()
108 .startsWith(unifiedComponentName), true);
109 Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
110 unifiedComponentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
111 File.separator + MonitoringUploadType.VES_EVENTS + File.separator + "mib1.yml");
116 public void testEnrichmentOfTwoComponentsFromSameType() throws IOException {
117 EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
118 Version version = new Version();
121 String vspId = "123";
122 enrichmentInfo.setKey(vspId);
123 enrichmentInfo.setVersion(version);
124 String componentId1 = "1";
125 String componentId2 = "2";
126 String abstType = "org.openecomp.resource.abstract.nodes.pd_server";
127 String abstType1 = "org.openecomp.resource.abstract.nodes.pd_server_1";
129 List<ComponentEntity> returnedComponents = new ArrayList<>();
130 returnedComponents.add(getComponentEntity(vspId, version, componentId1,
132 returnedComponents.add(getComponentEntity(vspId, version, componentId2,
134 Mockito.when(componentDaoMock.list(any()))
135 .thenReturn(returnedComponents);
136 setMockToEnrichComponent(vspId, version, componentId1);
138 ToscaServiceModel mockServiceModel =
139 getMockServiceModel("/mock/enrichMib/toscaModel/twoAbstractNodesFromSameType");
141 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
142 ArgumentCaptor.forClass(ServiceArtifact.class);
143 monitoringMibEnricher.enrich(enrichmentInfo, mockServiceModel);
145 Mockito.verify(enrichedServiceModelDaoMock, times(24))
146 .storeExternalArtifact(expectedServiceArtifact.capture());
148 Set<String> prefixes = getAllMibDirectoryPrefixes(expectedServiceArtifact.getAllValues());
150 validateExpectedAbstractTypes(Stream.of(abstType, abstType1).collect(Collectors.toSet()), prefixes);
153 private void validateExpectedAbstractTypes(Set<String> expectedAbstractTypes,
154 Set<String> prefixes) {
155 for(String abstType : expectedAbstractTypes){
156 Assert.assertTrue(prefixes.contains(abstType));
160 private void setMockToEnrichComponent(String vspId, Version version, String componentId) {
161 ComponentMonitoringUploadEntity returnedArtifact = new ComponentMonitoringUploadEntity();
162 returnedArtifact.setVspId(vspId);
163 returnedArtifact.setVersion(version);
164 returnedArtifact.setComponentId(componentId);
165 returnedArtifact.setType(MonitoringUploadType.SNMP_POLL);
166 returnedArtifact.setArtifactName("mib.zip");
167 returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip"));
169 Mockito.when(componentArtifactDaoMock.getByType(any()))
170 .thenReturn(Optional.of(returnedArtifact));
171 Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(any());
174 private ComponentEntity getComponentEntity(String vspId,
177 String componentNameSuffix) {
178 ComponentEntity componentEntity = new ComponentEntity();
179 componentEntity.setId(componentId);
180 componentEntity.setVspId(vspId);
181 componentEntity.setVersion(version);
183 String compositionData = "{\n" +
184 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentNameSuffix + "\",\n" +
185 " \"displayName\": \"" + componentNameSuffix + "\"\n" +
187 componentEntity.setCompositionData(compositionData);
188 return componentEntity;
191 private ByteBuffer getMibByteBuffer(String fileName) {
192 byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
193 FileUtils::toByteArray);
194 return ByteBuffer.wrap(mibBytes);
197 private ToscaServiceModel getMockServiceModel(String serviceTemplatesDirectory)
199 File directory = new File(this.getClass().getResource(serviceTemplatesDirectory).getFile());
200 ToscaServiceModel serviceModel = new ToscaServiceModel();
201 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
203 for (final File serviceTemplateFile : directory.listFiles()) {
204 byte[] content = FileUtils
205 .readViaInputStream(this.getClass().getResource(serviceTemplatesDirectory + File
206 .separator + serviceTemplateFile.getName()),
207 FileUtils::toByteArray);
208 ServiceTemplate serviceTemplate =
209 new YamlUtil().yamlToObject(new String(content), ServiceTemplate.class);
210 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate);
213 serviceModel.setServiceTemplates(serviceTemplates);
217 private Set<String> getAllMibDirectoryPrefixes(List<ServiceArtifact> serviceArtifacts) {
218 if(CollectionUtils.isEmpty(serviceArtifacts)){
219 return new HashSet<>();
222 Set<String> prefixes = new HashSet<>();
223 for(ServiceArtifact serviceArtifact : serviceArtifacts){
224 String absolutePath = serviceArtifact.getName();
225 prefixes.add(absolutePath.split(Pattern.quote(File.separator))[0]);