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 / MonitoringMibEnricherTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.enrichment.impl.external.artifact;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.mockito.*;
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;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.nio.ByteBuffer;
49 import java.util.*;
50 import java.util.regex.Pattern;
51 import java.util.stream.Collectors;
52 import java.util.stream.Stream;
53
54 import static org.mockito.Matchers.anyObject;
55 import static org.mockito.Mockito.atLeastOnce;
56 import static org.mockito.Mockito.times;
57
58
59 public class MonitoringMibEnricherTest {
60   @Mock
61   private ComponentArtifactDao componentArtifactDaoMock;
62   @Mock
63   private EnrichedServiceModelDao enrichedServiceModelDaoMock;
64   @Mock
65   private ComponentDao componentDaoMock;
66
67   @InjectMocks
68   private MonitoringMibEnricher monitoringMibEnricher;
69
70
71   @BeforeMethod(alwaysRun = true)
72   public void injectDoubles() {
73     MockitoAnnotations.initMocks(this);
74   }
75
76   @Test
77   public void testEnrichComponent() throws Exception {
78     String vspId = "123";
79     String componentId = "1";
80     Version version = new Version();
81     version.setMajor(1);
82     version.setMinor(0);
83
84     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId, vspId +
85         "enrichMib_server");
86     setMockToEnrichComponent(vspId, version, componentId);
87     String componentName = componentEntity.getComponentCompositionData().getName();
88     String unifiedComponentName =
89         ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
90
91     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
92         ArgumentCaptor.forClass(ServiceArtifact.class);
93     monitoringMibEnricher.enrichComponent(vspId, version, componentEntity,
94         Stream.of(unifiedComponentName).collect(Collectors.toSet()));
95
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");
103
104   }
105
106   @Test
107   public void testEnrichmentOfTwoComponentsFromSameType() throws IOException {
108     EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
109     Version version = new Version();
110     version.setMajor(1);
111     version.setMinor(0);
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";
119
120     List<ComponentEntity> returnedComponents = new ArrayList<>();
121     returnedComponents.add(getComponentEntity(vspId, version, componentId1,
122         "pd_server"));
123     returnedComponents.add(getComponentEntity(vspId, version, componentId2,
124         "pd_server"));
125     Mockito.when(componentDaoMock.list(anyObject()))
126         .thenReturn(returnedComponents);
127     setMockToEnrichComponent(vspId, version, componentId1);
128
129     ToscaServiceModel mockServiceModel =
130         getMockServiceModel("/mock/enrichMib/toscaModel/twoAbstractNodesFromSameType");
131
132     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
133         ArgumentCaptor.forClass(ServiceArtifact.class);
134     monitoringMibEnricher.enrich(enrichmentInfo, mockServiceModel);
135
136     Mockito.verify(enrichedServiceModelDaoMock, times(24))
137         .storeExternalArtifact(expectedServiceArtifact.capture());
138
139     Set<String> prefixes = getAllMibDirectoryPrefixes(expectedServiceArtifact.getAllValues());
140
141     validateExpectedAbstractTypes(Stream.of(abstType, abstType1).collect(Collectors.toSet()), prefixes);
142   }
143
144   private void validateExpectedAbstractTypes(Set<String> expectedAbstractTypes,
145                                              Set<String> prefixes) {
146     for(String abstType : expectedAbstractTypes){
147       Assert.assertTrue(prefixes.contains(abstType));
148     }
149   }
150
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"));
159
160     Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
161         .thenReturn(Optional.of(returnedArtifact));
162     Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
163   }
164
165   private ComponentEntity getComponentEntity(String vspId,
166                                              Version version,
167                                              String componentId,
168                                              String componentNameSuffix) {
169     ComponentEntity componentEntity = new ComponentEntity();
170     componentEntity.setId(componentId);
171     componentEntity.setVspId(vspId);
172     componentEntity.setVersion(version);
173
174     String compositionData = "{\n" +
175         "  \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentNameSuffix + "\",\n" +
176         "  \"displayName\": \"" + componentNameSuffix + "\"\n" +
177         "}";
178     componentEntity.setCompositionData(compositionData);
179     return componentEntity;
180   }
181
182   private ByteBuffer getMibByteBuffer(String fileName) {
183     byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
184         FileUtils::toByteArray);
185     return ByteBuffer.wrap(mibBytes);
186   }
187
188   private ToscaServiceModel getMockServiceModel(String serviceTemplatesDirectory)
189       throws IOException {
190     File directory = new File(this.getClass().getResource(serviceTemplatesDirectory).getFile());
191     ToscaServiceModel serviceModel = new ToscaServiceModel();
192     Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
193
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);
202     }
203
204     serviceModel.setServiceTemplates(serviceTemplates);
205     return serviceModel;
206   }
207
208   private Set<String> getAllMibDirectoryPrefixes(List<ServiceArtifact> serviceArtifacts) {
209     if(CollectionUtils.isEmpty(serviceArtifacts)){
210       return new HashSet<>();
211     }
212
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]);
217     }
218
219     return prefixes;
220   }
221
222 }