Rename packages from openecomp to onap.
[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.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;
49
50 import java.io.File;
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;
57 import java.util.Map;
58 import java.util.Optional;
59 import java.util.Set;
60 import java.util.regex.Pattern;
61 import java.util.stream.Collectors;
62 import java.util.stream.Stream;
63
64 import static org.mockito.Matchers.anyObject;
65 import static org.mockito.Mockito.atLeastOnce;
66 import static org.mockito.Mockito.times;
67
68
69 public class MonitoringMibEnricherTest {
70   @Mock
71   private ComponentArtifactDao componentArtifactDaoMock;
72   @Mock
73   private EnrichedServiceModelDao enrichedServiceModelDaoMock;
74   @Mock
75   private ComponentDao componentDaoMock;
76
77   @InjectMocks
78   private MonitoringMibEnricher monitoringMibEnricher;
79
80
81   @BeforeMethod(alwaysRun = true)
82   public void injectDoubles() {
83     MockitoAnnotations.initMocks(this);
84   }
85
86   @Test
87   public void testEnrichComponent() throws Exception {
88     String vspId = "123";
89     String componentId = "1";
90     Version version = new Version();
91     version.setMajor(1);
92     version.setMinor(0);
93
94     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId, vspId +
95         "enrichMib_server");
96     setMockToEnrichComponent(vspId, version, componentId);
97     String componentName = componentEntity.getComponentCompositionData().getName();
98     String unifiedComponentName =
99         ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
100
101     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
102         ArgumentCaptor.forClass(ServiceArtifact.class);
103     monitoringMibEnricher.enrichComponent(vspId, version, componentEntity,
104         Stream.of(unifiedComponentName).collect(Collectors.toSet()));
105
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");
113
114   }
115
116   @Test
117   public void testEnrichmentOfTwoComponentsFromSameType() throws IOException {
118     EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
119     Version version = new Version();
120     version.setMajor(1);
121     version.setMinor(0);
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";
129
130     List<ComponentEntity> returnedComponents = new ArrayList<>();
131     returnedComponents.add(getComponentEntity(vspId, version, componentId1,
132         "pd_server"));
133     returnedComponents.add(getComponentEntity(vspId, version, componentId2,
134         "pd_server"));
135     Mockito.when(componentDaoMock.list(anyObject()))
136         .thenReturn(returnedComponents);
137     setMockToEnrichComponent(vspId, version, componentId1);
138
139     ToscaServiceModel mockServiceModel =
140         getMockServiceModel("/mock/enrichMib/toscaModel/twoAbstractNodesFromSameType");
141
142     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
143         ArgumentCaptor.forClass(ServiceArtifact.class);
144     monitoringMibEnricher.enrich(enrichmentInfo, mockServiceModel);
145
146     Mockito.verify(enrichedServiceModelDaoMock, times(24))
147         .storeExternalArtifact(expectedServiceArtifact.capture());
148
149     Set<String> prefixes = getAllMibDirectoryPrefixes(expectedServiceArtifact.getAllValues());
150
151     validateExpectedAbstractTypes(Stream.of(abstType, abstType1).collect(Collectors.toSet()), prefixes);
152   }
153
154   private void validateExpectedAbstractTypes(Set<String> expectedAbstractTypes,
155                                              Set<String> prefixes) {
156     for(String abstType : expectedAbstractTypes){
157       Assert.assertTrue(prefixes.contains(abstType));
158     }
159   }
160
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"));
169
170     Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
171         .thenReturn(Optional.of(returnedArtifact));
172     Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
173   }
174
175   private ComponentEntity getComponentEntity(String vspId,
176                                              Version version,
177                                              String componentId,
178                                              String componentNameSuffix) {
179     ComponentEntity componentEntity = new ComponentEntity();
180     componentEntity.setId(componentId);
181     componentEntity.setVspId(vspId);
182     componentEntity.setVersion(version);
183
184     String compositionData = "{\n" +
185         "  \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentNameSuffix + "\",\n" +
186         "  \"displayName\": \"" + componentNameSuffix + "\"\n" +
187         "}";
188     componentEntity.setCompositionData(compositionData);
189     return componentEntity;
190   }
191
192   private ByteBuffer getMibByteBuffer(String fileName) {
193     byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
194         FileUtils::toByteArray);
195     return ByteBuffer.wrap(mibBytes);
196   }
197
198   private ToscaServiceModel getMockServiceModel(String serviceTemplatesDirectory)
199       throws IOException {
200     File directory = new File(this.getClass().getResource(serviceTemplatesDirectory).getFile());
201     ToscaServiceModel serviceModel = new ToscaServiceModel();
202     Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
203
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);
212     }
213
214     serviceModel.setServiceTemplates(serviceTemplates);
215     return serviceModel;
216   }
217
218   private Set<String> getAllMibDirectoryPrefixes(List<ServiceArtifact> serviceArtifacts) {
219     if(CollectionUtils.isEmpty(serviceArtifacts)){
220       return new HashSet<>();
221     }
222
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]);
227     }
228
229     return prefixes;
230   }
231
232 }