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.ArtifactType;
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.MibDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.MibEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
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.io.InputStream;
46 import java.nio.ByteBuffer;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Optional;
51 import static org.mockito.Matchers.anyObject;
52 import static org.mockito.Mockito.atLeastOnce;
53 import static org.mockito.Mockito.times;
58 * @since November 06, 2016.
61 public class MonitoringMibEnricherTest {
63 private MibDao mibDaoMock;
65 private EnrichedServiceModelDao enrichedServiceModelDaoMock;
67 private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
69 private ComponentDao componentDaoMock;
72 private MonitoringMibEnricher monitoringMibEnricher;
75 @BeforeMethod(alwaysRun = true)
76 public void injectDoubles() {
77 MockitoAnnotations.initMocks(this);
81 public void testEnrichComponent() throws Exception {
83 String componentId = "1111111111";
84 Version version = new Version();
88 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
89 setMockToEnrichComponent(vspId, componentId, version);
90 monitoringMibEnricher.enrichComponent(componentEntity, vspId, version);
92 String componentName = componentEntity.getComponentCompositionData().getName();
94 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
95 ArgumentCaptor.forClass(ServiceArtifact.class);
96 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
97 .storeExternalArtifact(expectedServiceArtifact.capture());
99 .assertEquals(expectedServiceArtifact.getValue().getName().startsWith(componentName), true);
100 Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
101 componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
102 File.separator + ArtifactType.SNMP_POLL + File.separator + "mib1.yml");
107 public void testEnrich() throws Exception {
108 EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
109 Version version = new Version();
112 String vspId = "123";
113 enrichmentInfo.setKey(vspId);
114 enrichmentInfo.setVersion(version);
115 String componentId1 = "1111111111";
116 String componentId2 = "2222222222";
119 Collection<ComponentEntity> returnedComponents = new ArrayList<>();
120 returnedComponents.add(getComponentEntity(vspId, version, componentId1));
121 returnedComponents.add(getComponentEntity(vspId, version, componentId2));
123 Mockito.when(componentDaoMock.list(anyObject()))
124 .thenReturn(returnedComponents);
125 setMockToEnrichComponent(vspId, componentId1, version);
127 monitoringMibEnricher.enrich(enrichmentInfo);
128 Mockito.verify(enrichedServiceModelDaoMock, times(8)).storeExternalArtifact(anyObject());
132 private void setMockToEnrichComponent(String vspId, String componentId, Version version) {
133 MibEntity returnedArtifact = new MibEntity();
134 returnedArtifact.setVspId(vspId);
135 returnedArtifact.setVersion(version);
136 returnedArtifact.setComponentId(componentId);
137 returnedArtifact.setType(ArtifactType.SNMP_POLL);
138 returnedArtifact.setArtifactName("mib.zip");
139 returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip"));
141 Mockito.when(mibDaoMock.getByType(anyObject()))
142 .thenReturn(Optional.of(returnedArtifact));
143 Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
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);
152 String componentName = vspId + "enrichMib_server";
153 String compositionData = "{\n" +
154 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
155 " \"displayName\": \"" + componentName + "\"\n" +
157 componentEntity.setCompositionData(compositionData);
158 return componentEntity;
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);