Remove ECOMP in headers
[clamp.git] / src / test / java / org / onap / clamp / clds / sdc / controller / installer / CsarHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  * 
22  */
23
24 package org.onap.clamp.clds.sdc.controller.installer;
25
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;
30
31 import com.att.aft.dme2.internal.apache.commons.io.IOUtils;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
44 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
45 import org.onap.clamp.clds.util.ResourceFileUtil;
46 import org.onap.sdc.api.notification.IArtifactInfo;
47 import org.onap.sdc.api.notification.INotificationData;
48 import org.onap.sdc.api.notification.IResourceInstance;
49 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
50 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
51
52 public class CsarHandlerTest {
53
54     private static final String SDC_FOLDER = "/tmp/csar-handler-tests";
55     private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
56     private static final String SERVICE_UUID = "serviceUUID";
57     private static final String RESOURCE1_UUID = "resource1UUID";
58     private static final String RESOURCE1_INSTANCE_NAME = "sim-1802 0";
59     private static final String RESOURCE1_INSTANCE_NAME_IN_CSAR = "sim18020";
60     private static final String BLUEPRINT1_NAME = "FOI.Simfoimap223S0112.event_proc_bp.yaml";
61
62     @Test
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());
73     }
74
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");
81     }
82
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);
108         return notifData;
109     }
110
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;
116     }
117
118     @Test
119     public void testSave()
120             throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
121         CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests");
122         // Test the save
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(RESOURCE1_INSTANCE_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,
134                 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getBlueprintArtifactName());
135         assertEquals(RESOURCE1_UUID, csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getResourceAttached()
136                 .getResourceInvariantUUID());
137         assertEquals(SERVICE_UUID,
138                 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getBlueprintInvariantServiceUuid());
139         // Do some cleanup
140         Path path = Paths.get(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME);
141         Files.deleteIfExists(path);
142     }
143
144     @Test
145     public void testDoubleSave()
146             throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
147         CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests");
148         // Test the save
149         csar.save(buildFakeSdcResut());
150         assertTrue((new File(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME)).exists());
151         assertEquals(CSAR_ARTIFACT_NAME, csar.getArtifactElement().getArtifactName());
152         assertNotNull(csar.getSdcCsarHelper());
153         // Test dcaeBlueprint
154         String blueprint = csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getDcaeBlueprint();
155         assertNotNull(blueprint);
156         assertTrue(!blueprint.isEmpty());
157         assertTrue(blueprint.contains("DCAE-VES-PM-EVENT-v1"));
158         // Test additional properties from Sdc notif
159         assertEquals(BLUEPRINT1_NAME,
160                 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getBlueprintArtifactName());
161         assertEquals(RESOURCE1_UUID, csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getResourceAttached()
162                 .getResourceInvariantUUID());
163         assertEquals(SERVICE_UUID,
164                 csar.getMapOfBlueprints().get(RESOURCE1_INSTANCE_NAME).getBlueprintInvariantServiceUuid());
165         Path path = Paths.get(SDC_FOLDER + "/test-controller/" + CSAR_ARTIFACT_NAME);
166         // A double save should simply overwrite the existing
167         csar.save(buildFakeSdcResut());
168         // Do some cleanup
169         Files.deleteIfExists(path);
170     }
171 }