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