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";
62 public void testConstructor() throws CsarHandlerException {
63 IArtifactInfo serviceArtifact = Mockito.mock(IArtifactInfo.class);
64 Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
65 Mockito.when(serviceArtifact.getArtifactName()).thenReturn(CSAR_ARTIFACT_NAME);
66 List<IArtifactInfo> servicesList = new ArrayList<>();
67 servicesList.add(serviceArtifact);
68 INotificationData notifData = Mockito.mock(INotificationData.class);
69 Mockito.when(notifData.getServiceArtifacts()).thenReturn(servicesList);
70 CsarHandler csar = new CsarHandler(notifData, "test-controller", SDC_FOLDER);
71 assertEquals(SDC_FOLDER + "/test-controller" + "/" + CSAR_ARTIFACT_NAME, csar.getFilePath());
74 @Test(expected = CsarHandlerException.class)
75 public void testFailingConstructor() throws CsarHandlerException {
76 INotificationData notifData = Mockito.mock(INotificationData.class);
77 Mockito.when(notifData.getServiceArtifacts()).thenReturn(new ArrayList<>());
78 new CsarHandler(notifData, "test-controller", "/tmp/csar-handler-tests");
79 fail("Exception should have been raised");
82 private INotificationData buildFakeSdcNotification() {
83 // BUild what is needed for CSAR
84 IArtifactInfo serviceArtifact = Mockito.mock(IArtifactInfo.class);
85 Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
86 Mockito.when(serviceArtifact.getArtifactName()).thenReturn(CSAR_ARTIFACT_NAME);
87 List<IArtifactInfo> servicesList = new ArrayList<>();
88 servicesList.add(serviceArtifact);
89 INotificationData notifData = Mockito.mock(INotificationData.class);
90 Mockito.when(notifData.getServiceArtifacts()).thenReturn(servicesList);
91 // Build what is needed for UUID
92 Mockito.when(notifData.getServiceInvariantUUID()).thenReturn(SERVICE_UUID);
93 // Build fake resource with one artifact BLUEPRINT
94 IResourceInstance resource1 = Mockito.mock(IResourceInstance.class);
95 Mockito.when(resource1.getResourceType()).thenReturn("VF");
96 Mockito.when(resource1.getResourceInvariantUUID()).thenReturn(RESOURCE1_UUID);
97 Mockito.when(resource1.getResourceInstanceName()).thenReturn(RESOURCE1_INSTANCE_NAME);
98 // Create a fake artifact for resource
99 IArtifactInfo blueprintArtifact = Mockito.mock(IArtifactInfo.class);
100 Mockito.when(blueprintArtifact.getArtifactType()).thenReturn(CsarHandler.BLUEPRINT_TYPE);
101 List<IArtifactInfo> artifactsListForResource = new ArrayList<>();
102 artifactsListForResource.add(blueprintArtifact);
103 Mockito.when(resource1.getArtifacts()).thenReturn(artifactsListForResource);
104 List<IResourceInstance> resourcesList = new ArrayList<>();
105 resourcesList.add(resource1);
106 Mockito.when(notifData.getResources()).thenReturn(resourcesList);
110 private IDistributionClientDownloadResult buildFakeSdcResut() throws IOException {
111 IDistributionClientDownloadResult resultArtifact = Mockito.mock(IDistributionClientDownloadResult.class);
112 Mockito.when(resultArtifact.getArtifactPayload()).thenReturn(
113 IOUtils.toByteArray(ResourceFileUtil.getResourceAsStream("example/sdc/service-Simsfoimap0112.csar")));
114 return resultArtifact;
118 public void testSave()
119 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
120 CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests");
122 csar.save(buildFakeSdcResut());
123 assertTrue((new File(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME)).exists());
124 assertEquals(CSAR_ARTIFACT_NAME, csar.getArtifactElement().getArtifactName());
125 assertNotNull(csar.getSdcCsarHelper());
126 // Test dcaeBlueprint
127 String blueprint = csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getDcaeBlueprint();
128 assertNotNull(blueprint);
129 assertTrue(!blueprint.isEmpty());
130 assertTrue(blueprint.contains("DCAE-VES-PM-EVENT-v1"));
131 // Test additional properties from Sdc notif
132 assertEquals(BLUEPRINT1_NAME,
133 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getBlueprintArtifactName());
134 assertEquals(RESOURCE1_UUID,
135 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getResourceAttached().getResourceInvariantUUID());
136 assertEquals(SERVICE_UUID,
137 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getBlueprintInvariantServiceUuid());
139 Path path = Paths.get(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME);
140 Files.deleteIfExists(path);
144 public void testDoubleSave()
145 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
146 CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests");
148 csar.save(buildFakeSdcResut());
149 assertTrue((new File(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME)).exists());
150 assertEquals(CSAR_ARTIFACT_NAME, csar.getArtifactElement().getArtifactName());
151 assertNotNull(csar.getSdcCsarHelper());
152 // Test dcaeBlueprint
153 String blueprint = csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getDcaeBlueprint();
154 assertNotNull(blueprint);
155 assertTrue(!blueprint.isEmpty());
156 assertTrue(blueprint.contains("DCAE-VES-PM-EVENT-v1"));
157 // Test additional properties from Sdc notif
158 assertEquals(BLUEPRINT1_NAME,
159 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getBlueprintArtifactName());
160 assertEquals(RESOURCE1_UUID,
161 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getResourceAttached().getResourceInvariantUUID());
162 assertEquals(SERVICE_UUID,
163 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getBlueprintInvariantServiceUuid());
164 Path path = Paths.get(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME);
165 // A double save should simply overwrite the existing
166 csar.save(buildFakeSdcResut());
168 Files.deleteIfExists(path);