3730a15465cd39290fffeb640ed34c506105481a
[sdc.git] /
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.VendorSoftwareProductDao;
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.nio.ByteBuffer;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.Optional;
51
52 import static org.mockito.Matchers.anyObject;
53 import static org.mockito.Mockito.atLeastOnce;
54 import static org.mockito.Mockito.times;
55
56
57 public class MonitoringMibEnricherTest {
58   @Mock
59   private ComponentArtifactDao componentArtifactDaoMock;
60   @Mock
61   private EnrichedServiceModelDao enrichedServiceModelDaoMock;
62   @Mock
63   private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
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 = "1111111111";
80     Version version = new Version();
81     version.setMajor(1);
82     version.setMinor(0);
83
84     ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
85     setMockToEnrichComponent(vspId, componentId, version);
86     monitoringMibEnricher.enrichComponent(componentEntity, vspId, version);
87
88     String componentName = componentEntity.getComponentCompositionData().getName();
89     String unifiedComponentName =
90         ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
91     ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
92         ArgumentCaptor.forClass(ServiceArtifact.class);
93     Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
94         .storeExternalArtifact(expectedServiceArtifact.capture());
95     Assert.assertEquals(expectedServiceArtifact.getValue().getName()
96         .startsWith(unifiedComponentName), true);
97     Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
98         unifiedComponentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
99             File.separator + MonitoringUploadType.VES_EVENTS + File.separator + "mib1.yml");
100
101   }
102
103   @Test
104   public void testEnrich() throws Exception {
105     EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
106     Version version = new Version();
107     version.setMajor(1);
108     version.setMinor(0);
109     String vspId = "123";
110     enrichmentInfo.setKey(vspId);
111     enrichmentInfo.setVersion(version);
112     String componentId1 = "1111111111";
113     String componentId2 = "2222222222";
114
115
116     Collection<ComponentEntity> returnedComponents = new ArrayList<>();
117     returnedComponents.add(getComponentEntity(vspId, version, componentId1));
118     returnedComponents.add(getComponentEntity(vspId, version, componentId2));
119
120     Mockito.when(componentDaoMock.list(anyObject()))
121         .thenReturn(returnedComponents);
122     setMockToEnrichComponent(vspId, componentId1, version);
123
124     monitoringMibEnricher.enrich(enrichmentInfo);
125     Mockito.verify(enrichedServiceModelDaoMock, times(12)).storeExternalArtifact(anyObject());
126
127   }
128
129   private void setMockToEnrichComponent(String vspId, String componentId, Version version) {
130     ComponentMonitoringUploadEntity returnedArtifact = new ComponentMonitoringUploadEntity();
131     returnedArtifact.setVspId(vspId);
132     returnedArtifact.setVersion(version);
133     returnedArtifact.setComponentId(componentId);
134     returnedArtifact.setType(MonitoringUploadType.SNMP_POLL);
135     returnedArtifact.setArtifactName("mib.zip");
136     returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip"));
137
138     Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
139         .thenReturn(Optional.of(returnedArtifact));
140     Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
141   }
142
143   private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) {
144     ComponentEntity componentEntity = new ComponentEntity();
145     componentEntity.setId(componentId);
146     componentEntity.setVspId(vspId);
147     componentEntity.setVersion(version);
148
149     String componentName = vspId + "enrichMib_server";
150     String compositionData = "{\n" +
151         "  \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
152         "  \"displayName\": \"" + componentName + "\"\n" +
153         "}";
154     componentEntity.setCompositionData(compositionData);
155     return componentEntity;
156   }
157
158   private ByteBuffer getMibByteBuffer(String fileName) {
159     byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
160             stream -> FileUtils.toByteArray(stream));
161     return ByteBuffer.wrap(mibBytes);
162   }
163
164 }