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.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;
47 import java.nio.ByteBuffer;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.Optional;
52 import static org.mockito.Matchers.anyObject;
53 import static org.mockito.Mockito.atLeastOnce;
54 import static org.mockito.Mockito.times;
57 public class MonitoringMibEnricherTest {
59 private ComponentArtifactDao componentArtifactDaoMock;
61 private EnrichedServiceModelDao enrichedServiceModelDaoMock;
63 private VendorSoftwareProductDao vendorSoftwareProductDaoMock;
65 private ComponentDao componentDaoMock;
68 private MonitoringMibEnricher monitoringMibEnricher;
71 @BeforeMethod(alwaysRun = true)
72 public void injectDoubles() {
73 MockitoAnnotations.initMocks(this);
77 public void testEnrichComponent() throws Exception {
79 String componentId = "1111111111";
80 Version version = new Version();
84 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
85 setMockToEnrichComponent(vspId, componentId, version);
86 monitoringMibEnricher.enrichComponent(componentEntity, vspId, version);
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");
104 public void testEnrich() throws Exception {
105 EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
106 Version version = new Version();
109 String vspId = "123";
110 enrichmentInfo.setKey(vspId);
111 enrichmentInfo.setVersion(version);
112 String componentId1 = "1111111111";
113 String componentId2 = "2222222222";
116 Collection<ComponentEntity> returnedComponents = new ArrayList<>();
117 returnedComponents.add(getComponentEntity(vspId, version, componentId1));
118 returnedComponents.add(getComponentEntity(vspId, version, componentId2));
120 Mockito.when(componentDaoMock.list(anyObject()))
121 .thenReturn(returnedComponents);
122 setMockToEnrichComponent(vspId, componentId1, version);
124 monitoringMibEnricher.enrich(enrichmentInfo);
125 Mockito.verify(enrichedServiceModelDaoMock, times(12)).storeExternalArtifact(anyObject());
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"));
138 Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
139 .thenReturn(Optional.of(returnedArtifact));
140 Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
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);
149 String componentName = vspId + "enrichMib_server";
150 String compositionData = "{\n" +
151 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
152 " \"displayName\": \"" + componentName + "\"\n" +
154 componentEntity.setCompositionData(compositionData);
155 return componentEntity;
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);