Add collaboration feature
[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.mockito.ArgumentCaptor;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.MockitoAnnotations;
28 import org.openecomp.core.enrichment.types.ArtifactCategory;
29 import org.openecomp.core.enrichment.types.MonitoringUploadType;
30 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
31 import org.openecomp.core.model.types.ServiceArtifact;
32 import org.openecomp.core.utilities.file.FileUtils;
33 import org.openecomp.sdc.enrichment.EnrichmentInfo;
34 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
35 import org.openecomp.sdc.tosca.services.DataModelUtil;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41 import org.testng.Assert;
42 import org.testng.annotations.BeforeMethod;
43 import org.testng.annotations.Test;
44
45 import java.io.File;
46 import java.nio.ByteBuffer;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Optional;
50
51 import static org.mockito.Matchers.anyObject;
52 import static org.mockito.Mockito.atLeastOnce;
53 import static org.mockito.Mockito.times;
54
55
56 public class MonitoringMibEnricherTest {
57   @Mock
58   private ComponentArtifactDao componentArtifactDaoMock;
59   @Mock
60   private EnrichedServiceModelDao enrichedServiceModelDaoMock;
61   @Mock
62   private ComponentDao componentDaoMock;
63
64   @InjectMocks
65   private MonitoringMibEnricher monitoringMibEnricher;
66
67
68   @BeforeMethod(alwaysRun = true)
69   public void injectDoubles() {
70     MockitoAnnotations.initMocks(this);
71   }
72
73   @Test
74   public void testEnrichComponent() throws Exception {
75     String vspId = "123";
76     String componentId = "1111111111";
77     Version version = new Version();
78     version.setMajor(1);
79     version.setMinor(0);
80
81     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
82     setMockToEnrichComponent(vspId, componentId, version);
83     monitoringMibEnricher.enrichComponent(componentEntity, vspId, version);
84
85     String componentName = componentEntity.getComponentCompositionData().getName();
86     String unifiedComponentName =
87         ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
88     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
89         ArgumentCaptor.forClass(ServiceArtifact.class);
90     Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
91         .storeExternalArtifact(expectedServiceArtifact.capture());
92     Assert.assertEquals(expectedServiceArtifact.getValue().getName()
93         .startsWith(unifiedComponentName), true);
94     Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
95         unifiedComponentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
96             File.separator + MonitoringUploadType.VES_EVENTS + File.separator + "mib1.yml");
97
98   }
99
100   @Test
101   public void testEnrich() throws Exception {
102     EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
103     Version version = new Version();
104     version.setMajor(1);
105     version.setMinor(0);
106     String vspId = "123";
107     enrichmentInfo.setKey(vspId);
108     enrichmentInfo.setVersion(version);
109     String componentId1 = "1111111111";
110     String componentId2 = "2222222222";
111
112
113     Collection<ComponentEntity> returnedComponents = new ArrayList<>();
114     returnedComponents.add(getComponentEntity(vspId, version, componentId1));
115     returnedComponents.add(getComponentEntity(vspId, version, componentId2));
116
117     Mockito.when(componentDaoMock.list(anyObject()))
118         .thenReturn(returnedComponents);
119     setMockToEnrichComponent(vspId, componentId1, version);
120
121     monitoringMibEnricher.enrich(enrichmentInfo);
122     Mockito.verify(enrichedServiceModelDaoMock, times(12)).storeExternalArtifact(anyObject());
123
124   }
125
126   private void setMockToEnrichComponent(String vspId, String componentId, Version version) {
127     ComponentMonitoringUploadEntity returnedArtifact = new ComponentMonitoringUploadEntity();
128     returnedArtifact.setVspId(vspId);
129     returnedArtifact.setVersion(version);
130     returnedArtifact.setComponentId(componentId);
131     returnedArtifact.setType(MonitoringUploadType.SNMP_POLL);
132     returnedArtifact.setArtifactName("mib.zip");
133     returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip"));
134
135     Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
136         .thenReturn(Optional.of(returnedArtifact));
137     Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
138   }
139
140   private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) {
141     ComponentEntity componentEntity = new ComponentEntity();
142     componentEntity.setId(componentId);
143     componentEntity.setVspId(vspId);
144     componentEntity.setVersion(version);
145
146     String componentName = vspId + "enrichMib_server";
147     String compositionData = "{\n" +
148         "  \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
149         "  \"displayName\": \"" + componentName + "\"\n" +
150         "}";
151     componentEntity.setCompositionData(compositionData);
152     return componentEntity;
153   }
154
155   private ByteBuffer getMibByteBuffer(String fileName) {
156     byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
157         stream -> FileUtils.toByteArray(stream));
158     return ByteBuffer.wrap(mibBytes);
159   }
160
161 }