2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.sdc.controller.installer;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.List;
39 import org.apache.commons.io.IOUtils;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
43 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
44 import org.onap.clamp.clds.util.ResourceFileUtil;
45 import org.onap.sdc.api.notification.IArtifactInfo;
46 import org.onap.sdc.api.notification.INotificationData;
47 import org.onap.sdc.api.notification.IResourceInstance;
48 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
49 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
51 public class CsarHandlerTest {
53 private static final String SDC_FOLDER = "/tmp/csar-handler-tests";
54 private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
55 private static final String SERVICE_UUID = "serviceUUID";
56 private static final String RESOURCE1_UUID = "resource1UUID";
57 private static final String RESOURCE1_INSTANCE_NAME = "sim-1802 0";
58 private static final String RESOURCE1_INSTANCE_NAME_IN_CSAR = "sim18020";
59 private static final String BLUEPRINT1_NAME = "FOI.Simfoimap223S0112.event_proc_bp.yaml";
60 private static final String BLUEPRINT2_NAME = "FOI.Simfoimap223S0112.event_proc_bp2.yaml";
63 public void testConstructor() throws CsarHandlerException {
64 IArtifactInfo serviceArtifact = Mockito.mock(IArtifactInfo.class);
65 Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
66 Mockito.when(serviceArtifact.getArtifactName()).thenReturn(CSAR_ARTIFACT_NAME);
67 List<IArtifactInfo> servicesList = new ArrayList<>();
68 servicesList.add(serviceArtifact);
69 INotificationData notifData = Mockito.mock(INotificationData.class);
70 Mockito.when(notifData.getServiceArtifacts()).thenReturn(servicesList);
71 CsarHandler csar = new CsarHandler(notifData, "test-controller", SDC_FOLDER);
72 assertEquals(SDC_FOLDER + "/test-controller" + "/" + CSAR_ARTIFACT_NAME, csar.getFilePath());
75 @Test(expected = CsarHandlerException.class)
76 public void testFailingConstructor() throws CsarHandlerException {
77 INotificationData notifData = Mockito.mock(INotificationData.class);
78 Mockito.when(notifData.getServiceArtifacts()).thenReturn(new ArrayList<>());
79 new CsarHandler(notifData, "test-controller", "/tmp/csar-handler-tests");
80 fail("Exception should have been raised");
83 private INotificationData buildFakeSdcNotification() {
84 // BUild what is needed for CSAR
85 IArtifactInfo serviceArtifact = Mockito.mock(IArtifactInfo.class);
86 Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
87 Mockito.when(serviceArtifact.getArtifactName()).thenReturn(CSAR_ARTIFACT_NAME);
88 List<IArtifactInfo> servicesList = new ArrayList<>();
89 servicesList.add(serviceArtifact);
90 INotificationData notifData = Mockito.mock(INotificationData.class);
91 Mockito.when(notifData.getServiceArtifacts()).thenReturn(servicesList);
92 // Build what is needed for UUID
93 Mockito.when(notifData.getServiceInvariantUUID()).thenReturn(SERVICE_UUID);
94 // Build fake resource with one artifact BLUEPRINT
95 IResourceInstance resource1 = Mockito.mock(IResourceInstance.class);
96 Mockito.when(resource1.getResourceType()).thenReturn("VF");
97 Mockito.when(resource1.getResourceInvariantUUID()).thenReturn(RESOURCE1_UUID);
98 Mockito.when(resource1.getResourceInstanceName()).thenReturn(RESOURCE1_INSTANCE_NAME);
99 // Create a fake artifact for resource
100 IArtifactInfo blueprintArtifact = Mockito.mock(IArtifactInfo.class);
101 Mockito.when(blueprintArtifact.getArtifactType()).thenReturn(CsarHandler.BLUEPRINT_TYPE);
102 List<IArtifactInfo> artifactsListForResource = new ArrayList<>();
103 artifactsListForResource.add(blueprintArtifact);
104 Mockito.when(resource1.getArtifacts()).thenReturn(artifactsListForResource);
105 List<IResourceInstance> resourcesList = new ArrayList<>();
106 resourcesList.add(resource1);
107 Mockito.when(notifData.getResources()).thenReturn(resourcesList);
111 private IDistributionClientDownloadResult buildFakeSdcResut() throws IOException {
112 IDistributionClientDownloadResult resultArtifact = Mockito.mock(IDistributionClientDownloadResult.class);
113 Mockito.when(resultArtifact.getArtifactPayload()).thenReturn(
114 IOUtils.toByteArray(ResourceFileUtil.getResourceAsStream("example/sdc/service-Simsfoimap0112.csar")));
115 return resultArtifact;
119 public void testSave()
120 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
121 CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests");
123 csar.save(buildFakeSdcResut());
124 assertTrue((new File(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME)).exists());
125 assertEquals(CSAR_ARTIFACT_NAME, csar.getArtifactElement().getArtifactName());
126 assertNotNull(csar.getSdcCsarHelper());
127 // Test dcaeBlueprint
128 String blueprint = csar.getMapOfBlueprints().get(BLUEPRINT1_NAME).getDcaeBlueprint();
129 assertNotNull(blueprint);
130 assertTrue(!blueprint.isEmpty());
131 assertTrue(blueprint.contains("DCAE-VES-PM-EVENT-v1"));
132 // Test additional properties from Sdc notif
133 assertEquals(BLUEPRINT1_NAME, csar.getMapOfBlueprints().get(BLUEPRINT1_NAME).getBlueprintArtifactName());
134 assertEquals(RESOURCE1_UUID,
135 csar.getMapOfBlueprints().get(BLUEPRINT1_NAME).getResourceAttached().getResourceInvariantUUID());
136 assertEquals(SERVICE_UUID, csar.getMapOfBlueprints().get(BLUEPRINT1_NAME).getBlueprintInvariantServiceUuid());
138 // Just check the second one is there as well
139 assertEquals(BLUEPRINT2_NAME, csar.getMapOfBlueprints().get(BLUEPRINT2_NAME).getBlueprintArtifactName());
140 blueprint = csar.getMapOfBlueprints().get(BLUEPRINT2_NAME).getDcaeBlueprint();
141 assertNotNull(blueprint);
142 assertTrue(!blueprint.isEmpty());
143 assertTrue(blueprint.contains("DCAE-VES-PM-EVENT-v1"));
145 Path path = Paths.get(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME);
146 Files.deleteIfExists(path);
151 public void testDoubleSave()
152 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
153 CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests");
155 csar.save(buildFakeSdcResut());
156 assertTrue((new File(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME)).exists());
157 assertEquals(CSAR_ARTIFACT_NAME, csar.getArtifactElement().getArtifactName());
158 assertNotNull(csar.getSdcCsarHelper());
159 // Test dcaeBlueprint
160 String blueprint = csar.getMapOfBlueprints().get(BLUEPRINT1_NAME).getDcaeBlueprint();
161 assertNotNull(blueprint);
162 assertTrue(!blueprint.isEmpty());
163 assertTrue(blueprint.contains("DCAE-VES-PM-EVENT-v1"));
164 // Test additional properties from Sdc notif
165 assertEquals(BLUEPRINT1_NAME, csar.getMapOfBlueprints().get(BLUEPRINT1_NAME).getBlueprintArtifactName());
166 assertEquals(RESOURCE1_UUID,
167 csar.getMapOfBlueprints().get(BLUEPRINT1_NAME).getResourceAttached().getResourceInvariantUUID());
168 assertEquals(SERVICE_UUID, csar.getMapOfBlueprints().get(BLUEPRINT1_NAME).getBlueprintInvariantServiceUuid());
169 Path path = Paths.get(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME);
170 // A double save should simply overwrite the existing
171 csar.save(buildFakeSdcResut());
173 Files.deleteIfExists(path);