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