2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.enrichment.impl.external.artifact;
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;
45 import java.nio.ByteBuffer;
46 import java.util.ArrayList;
47 import java.util.Collection;
48 import java.util.Optional;
50 import static org.mockito.Matchers.anyObject;
51 import static org.mockito.Mockito.atLeastOnce;
52 import static org.mockito.Mockito.times;
57 * @since November 06, 2016.
60 public class MonitoringMibEnricherTest {
62 private ComponentArtifactDao componentArtifactDaoMock;
64 private EnrichedServiceModelDao enrichedServiceModelDaoMock;
66 private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
68 private ComponentDao componentDaoMock;
71 private MonitoringMibEnricher monitoringMibEnricher;
74 @BeforeMethod(alwaysRun = true)
75 public void injectDoubles() {
76 MockitoAnnotations.initMocks(this);
80 public void testEnrichComponent() throws Exception {
82 String componentId = "1111111111";
83 Version version = new Version();
87 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
88 setMockToEnrichComponent(vspId, componentId, version);
89 monitoringMibEnricher.enrichComponent(componentEntity, vspId, version);
91 String componentName = componentEntity.getComponentCompositionData().getName();
93 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
94 ArgumentCaptor.forClass(ServiceArtifact.class);
95 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
96 .storeExternalArtifact(expectedServiceArtifact.capture());
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");
106 public void testEnrich() throws Exception {
107 EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
108 Version version = new Version();
111 String vspId = "123";
112 enrichmentInfo.setKey(vspId);
113 enrichmentInfo.setVersion(version);
114 String componentId1 = "1111111111";
115 String componentId2 = "2222222222";
118 Collection<ComponentEntity> returnedComponents = new ArrayList<>();
119 returnedComponents.add(getComponentEntity(vspId, version, componentId1));
120 returnedComponents.add(getComponentEntity(vspId, version, componentId2));
122 Mockito.when(componentDaoMock.list(anyObject()))
123 .thenReturn(returnedComponents);
124 setMockToEnrichComponent(vspId, componentId1, version);
126 monitoringMibEnricher.enrich(enrichmentInfo);
127 Mockito.verify(enrichedServiceModelDaoMock, times(12)).storeExternalArtifact(anyObject());
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"));
140 Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
141 .thenReturn(Optional.of(returnedArtifact));
142 Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
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);
151 String componentName = vspId + "enrichMib_server";
152 String compositionData = "{\n" +
153 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
154 " \"displayName\": \"" + componentName + "\"\n" +
156 componentEntity.setCompositionData(compositionData);
157 return componentEntity;
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);