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;
25 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
26 import org.onap.sdc.tosca.services.YamlUtil;
27 import org.openecomp.core.enrichment.types.ArtifactCategory;
28 import org.openecomp.core.enrichment.types.MonitoringUploadType;
29 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
30 import org.openecomp.core.model.types.ServiceArtifact;
31 import org.openecomp.core.utilities.file.FileUtils;
32 import org.openecomp.sdc.enrichment.EnrichmentInfo;
33 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
34 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
35 import org.openecomp.sdc.tosca.services.DataModelUtil;
36 import org.openecomp.sdc.tosca.services.ToscaUtil;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.testng.Assert;
43 import org.testng.annotations.BeforeMethod;
44 import org.testng.annotations.Test;
47 import java.io.IOException;
48 import java.nio.ByteBuffer;
50 import java.util.regex.Pattern;
51 import java.util.stream.Collectors;
52 import java.util.stream.Stream;
54 import static org.mockito.Matchers.anyObject;
55 import static org.mockito.Mockito.atLeastOnce;
56 import static org.mockito.Mockito.times;
59 public class MonitoringMibEnricherTest {
61 private ComponentArtifactDao componentArtifactDaoMock;
63 private EnrichedServiceModelDao enrichedServiceModelDaoMock;
65 private ComponentDao componentDaoMock;
68 private MonitoringMibEnricher monitoringMibEnricher;
71 @BeforeMethod(alwaysRun = true)
72 public void injectDoubles() {
73 MockitoAnnotations.initMocks(this);
77 public void testEnrichComponent() throws Exception {
79 String componentId = "1";
80 Version version = new Version();
84 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId, vspId +
86 setMockToEnrichComponent(vspId, version, componentId);
87 String componentName = componentEntity.getComponentCompositionData().getName();
88 String unifiedComponentName =
89 ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
91 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
92 ArgumentCaptor.forClass(ServiceArtifact.class);
93 monitoringMibEnricher.enrichComponent(vspId, version, componentEntity,
94 Stream.of(unifiedComponentName).collect(Collectors.toSet()));
96 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
97 .storeExternalArtifact(expectedServiceArtifact.capture());
98 Assert.assertEquals(expectedServiceArtifact.getValue().getName()
99 .startsWith(unifiedComponentName), true);
100 Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
101 unifiedComponentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
102 File.separator + MonitoringUploadType.VES_EVENTS + File.separator + "mib1.yml");
107 public void testEnrichmentOfTwoComponentsFromSameType() throws IOException {
108 EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
109 Version version = new Version();
112 String vspId = "123";
113 enrichmentInfo.setKey(vspId);
114 enrichmentInfo.setVersion(version);
115 String componentId1 = "1";
116 String componentId2 = "2";
117 String abstType = "org.openecomp.resource.abstract.nodes.pd_server";
118 String abstType1 = "org.openecomp.resource.abstract.nodes.pd_server_1";
120 List<ComponentEntity> returnedComponents = new ArrayList<>();
121 returnedComponents.add(getComponentEntity(vspId, version, componentId1,
123 returnedComponents.add(getComponentEntity(vspId, version, componentId2,
125 Mockito.when(componentDaoMock.list(anyObject()))
126 .thenReturn(returnedComponents);
127 setMockToEnrichComponent(vspId, version, componentId1);
129 ToscaServiceModel mockServiceModel =
130 getMockServiceModel("/mock/enrichMib/toscaModel/twoAbstractNodesFromSameType");
132 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
133 ArgumentCaptor.forClass(ServiceArtifact.class);
134 monitoringMibEnricher.enrich(enrichmentInfo, mockServiceModel);
136 Mockito.verify(enrichedServiceModelDaoMock, times(24))
137 .storeExternalArtifact(expectedServiceArtifact.capture());
139 Set<String> prefixes = getAllMibDirectoryPrefixes(expectedServiceArtifact.getAllValues());
141 validateExpectedAbstractTypes(Stream.of(abstType, abstType1).collect(Collectors.toSet()), prefixes);
144 private void validateExpectedAbstractTypes(Set<String> expectedAbstractTypes,
145 Set<String> prefixes) {
146 for(String abstType : expectedAbstractTypes){
147 Assert.assertTrue(prefixes.contains(abstType));
151 private void setMockToEnrichComponent(String vspId, Version version, String componentId) {
152 ComponentMonitoringUploadEntity returnedArtifact = new ComponentMonitoringUploadEntity();
153 returnedArtifact.setVspId(vspId);
154 returnedArtifact.setVersion(version);
155 returnedArtifact.setComponentId(componentId);
156 returnedArtifact.setType(MonitoringUploadType.SNMP_POLL);
157 returnedArtifact.setArtifactName("mib.zip");
158 returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip"));
160 Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
161 .thenReturn(Optional.of(returnedArtifact));
162 Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
165 private ComponentEntity getComponentEntity(String vspId,
168 String componentNameSuffix) {
169 ComponentEntity componentEntity = new ComponentEntity();
170 componentEntity.setId(componentId);
171 componentEntity.setVspId(vspId);
172 componentEntity.setVersion(version);
174 String compositionData = "{\n" +
175 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentNameSuffix + "\",\n" +
176 " \"displayName\": \"" + componentNameSuffix + "\"\n" +
178 componentEntity.setCompositionData(compositionData);
179 return componentEntity;
182 private ByteBuffer getMibByteBuffer(String fileName) {
183 byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
184 FileUtils::toByteArray);
185 return ByteBuffer.wrap(mibBytes);
188 private ToscaServiceModel getMockServiceModel(String serviceTemplatesDirectory)
190 File directory = new File(this.getClass().getResource(serviceTemplatesDirectory).getFile());
191 ToscaServiceModel serviceModel = new ToscaServiceModel();
192 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
194 for (final File serviceTemplateFile : directory.listFiles()) {
195 byte[] content = FileUtils
196 .readViaInputStream(this.getClass().getResource(serviceTemplatesDirectory + File
197 .separator + serviceTemplateFile.getName()),
198 FileUtils::toByteArray);
199 ServiceTemplate serviceTemplate =
200 new YamlUtil().yamlToObject(new String(content), ServiceTemplate.class);
201 serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate);
204 serviceModel.setServiceTemplates(serviceTemplates);
208 private Set<String> getAllMibDirectoryPrefixes(List<ServiceArtifact> serviceArtifacts) {
209 if(CollectionUtils.isEmpty(serviceArtifacts)){
210 return new HashSet<>();
213 Set<String> prefixes = new HashSet<>();
214 for(ServiceArtifact serviceArtifact : serviceArtifacts){
215 String absolutePath = serviceArtifact.getName();
216 prefixes.add(absolutePath.split(Pattern.quote(File.separator))[0]);