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.type.ComponentEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41 import org.testng.Assert;
42 import org.testng.annotations.BeforeMethod;
43 import org.testng.annotations.Test;
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;
56 public class MonitoringMibEnricherTest {
58 private ComponentArtifactDao componentArtifactDaoMock;
60 private EnrichedServiceModelDao enrichedServiceModelDaoMock;
62 private ComponentDao componentDaoMock;
65 private MonitoringMibEnricher monitoringMibEnricher;
68 @BeforeMethod(alwaysRun = true)
69 public void injectDoubles() {
70 MockitoAnnotations.initMocks(this);
74 public void testEnrichComponent() throws Exception {
76 String componentId = "1111111111";
77 Version version = new Version();
81 ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId);
82 setMockToEnrichComponent(vspId, componentId, version);
83 monitoringMibEnricher.enrichComponent(componentEntity, vspId, version);
85 String componentName = componentEntity.getComponentCompositionData().getName();
86 String unifiedComponentName =
87 ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
88 ArgumentCaptor<ServiceArtifact> expectedServiceArtifact =
89 ArgumentCaptor.forClass(ServiceArtifact.class);
90 Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce())
91 .storeExternalArtifact(expectedServiceArtifact.capture());
92 Assert.assertEquals(expectedServiceArtifact.getValue().getName()
93 .startsWith(unifiedComponentName), true);
94 Assert.assertEquals(expectedServiceArtifact.getValue().getName(),
95 unifiedComponentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() +
96 File.separator + MonitoringUploadType.VES_EVENTS + File.separator + "mib1.yml");
101 public void testEnrich() throws Exception {
102 EnrichmentInfo enrichmentInfo = new EnrichmentInfo();
103 Version version = new Version();
106 String vspId = "123";
107 enrichmentInfo.setKey(vspId);
108 enrichmentInfo.setVersion(version);
109 String componentId1 = "1111111111";
110 String componentId2 = "2222222222";
113 Collection<ComponentEntity> returnedComponents = new ArrayList<>();
114 returnedComponents.add(getComponentEntity(vspId, version, componentId1));
115 returnedComponents.add(getComponentEntity(vspId, version, componentId2));
117 Mockito.when(componentDaoMock.list(anyObject()))
118 .thenReturn(returnedComponents);
119 setMockToEnrichComponent(vspId, componentId1, version);
121 monitoringMibEnricher.enrich(enrichmentInfo);
122 Mockito.verify(enrichedServiceModelDaoMock, times(12)).storeExternalArtifact(anyObject());
126 private void setMockToEnrichComponent(String vspId, String componentId, Version version) {
127 ComponentMonitoringUploadEntity returnedArtifact = new ComponentMonitoringUploadEntity();
128 returnedArtifact.setVspId(vspId);
129 returnedArtifact.setVersion(version);
130 returnedArtifact.setComponentId(componentId);
131 returnedArtifact.setType(MonitoringUploadType.SNMP_POLL);
132 returnedArtifact.setArtifactName("mib.zip");
133 returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip"));
135 Mockito.when(componentArtifactDaoMock.getByType(anyObject()))
136 .thenReturn(Optional.of(returnedArtifact));
137 Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject());
140 private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) {
141 ComponentEntity componentEntity = new ComponentEntity();
142 componentEntity.setId(componentId);
143 componentEntity.setVspId(vspId);
144 componentEntity.setVersion(version);
146 String componentName = vspId + "enrichMib_server";
147 String compositionData = "{\n" +
148 " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" +
149 " \"displayName\": \"" + componentName + "\"\n" +
151 componentEntity.setCompositionData(compositionData);
152 return componentEntity;
155 private ByteBuffer getMibByteBuffer(String fileName) {
156 byte[] mibBytes = FileUtils.readViaInputStream(this.getClass().getResource(fileName),
157 stream -> FileUtils.toByteArray(stream));
158 return ByteBuffer.wrap(mibBytes);