[SDC] Onboarding 1710 rebase.
[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.vendorsoftwareproduct.dao.ComponentArtifactDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40 import org.testng.Assert;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
43
44 import java.io.File;
45 import java.io.InputStream;
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 /**
57  * @author shiria
58  * @since November 06, 2016.
59  */
60
61 public class MonitoringMibEnricherTest {
62   @Mock
63   private ComponentArtifactDao componentArtifactDaoMock;
64   @Mock
65   private EnrichedServiceModelDao enrichedServiceModelDaoMock;
66   @Mock
67   private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
68   @Mock
69   private ComponentDao componentDaoMock;
70
71   @InjectMocks
72   private MonitoringMibEnricher monitoringMibEnricher;
73
74
75   @BeforeMethod(alwaysRun = true)
76   public void injectDoubles() {
77     MockitoAnnotations.initMocks(this);
78   }
79
80   @Test
81   public void testEnrichComponent() throws Exception {
82     String vspId = "123";
83     String componentId = "1111111111";
84     Version version = new Version();
85     version.setMajor(1);
86     version.setMinor(0);
87
88     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
89     setMockToEnrichComponent(vspId, componentId, version);
90     monitoringMibEnricher.enrichComponent(componentEntity, vspId, version);
91
92     String componentName = componentEntity.getComponentCompositionData().getName();
93
94     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
95         ArgumentCaptor.forClass(ServiceArtifact.class);
96     Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
97         .storeExternalArtifact(expectedServiceArtifact.capture());
98     Assert
99         .assertEquals(expectedServiceArtifact.getValue().getName().startsWith(componentName), true);
100     Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
101         componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
102             File.separator + MonitoringUploadType.VES_EVENTS + File.separator + "mib1.yml");
103
104   }
105
106   @Test
107   public void testEnrich() throws Exception {
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 = "1111111111";
116     String componentId2 = "2222222222";
117
118
119     Collection<ComponentEntity> returnedComponents = new ArrayList<>();
120     returnedComponents.add(getComponentEntity(vspId, version, componentId1));
121     returnedComponents.add(getComponentEntity(vspId, version, componentId2));
122
123     Mockito.when(componentDaoMock.list(anyObject()))
124         .thenReturn(returnedComponents);
125     setMockToEnrichComponent(vspId, componentId1, version);
126
127     monitoringMibEnricher.enrich(enrichmentInfo);
128     Mockito.verify(enrichedServiceModelDaoMock, times(12)).storeExternalArtifact(anyObject());
129
130   }
131
132   private void setMockToEnrichComponent(String vspId, String componentId, Version version) {
133     ComponentMonitoringUploadEntity returnedArtifact = new ComponentMonitoringUploadEntity();
134     returnedArtifact.setVspId(vspId);
135     returnedArtifact.setVersion(version);
136     returnedArtifact.setComponentId(componentId);
137     returnedArtifact.setType(MonitoringUploadType.SNMP_POLL);
138     returnedArtifact.setArtifactName("mib.zip");
139     returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip"));
140
141     Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
142         .thenReturn(Optional.of(returnedArtifact));
143     Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
144   }
145
146   private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) {
147     ComponentEntity componentEntity = new ComponentEntity();
148     componentEntity.setId(componentId);
149     componentEntity.setVspId(vspId);
150     componentEntity.setVersion(version);
151
152     String componentName = vspId + "enrichMib_server";
153     String compositionData = "{\n" +
154         "  \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
155         "  \"displayName\": \"" + componentName + "\"\n" +
156         "}";
157     componentEntity.setCompositionData(compositionData);
158     return componentEntity;
159   }
160
161   private ByteBuffer getMibByteBuffer(String fileName) {
162     InputStream mibFile = FileUtils.getFileInputStream(this.getClass().getResource(fileName));
163     byte[] mibBytes = FileUtils.toByteArray(mibFile);
164     return ByteBuffer.wrap(mibBytes);
165   }
166
167 }