Merge "Add swagger generation for jaxrs api"
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.AfterClass;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
45 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
46 import org.onap.clamp.clds.util.ResourceFileUtil;
47 import org.openecomp.sdc.api.notification.IArtifactInfo;
48 import org.openecomp.sdc.api.notification.INotificationData;
49 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
50 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
51
52 public class CsarHandlerTest {
53
54     private static final String sdcFolder = "/tmp/csar-handler-tests";
55     private static final String csarArtifactName = "testArtifact.csar";
56
57     @AfterClass
58     public static void removeAllFiles() throws IOException {
59         // Do some cleanup
60         Path path = Paths.get(sdcFolder + "/test-controller/" + csarArtifactName);
61         Files.deleteIfExists(path);
62     }
63
64     @Test
65     public void testConstructor() throws CsarHandlerException {
66         IArtifactInfo serviceArtifact = Mockito.mock(IArtifactInfo.class);
67         Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
68         Mockito.when(serviceArtifact.getArtifactName()).thenReturn(csarArtifactName);
69         List<IArtifactInfo> servicesList = new ArrayList<>();
70         servicesList.add(serviceArtifact);
71         INotificationData iNotifData = Mockito.mock(INotificationData.class);
72         Mockito.when(iNotifData.getServiceArtifacts()).thenReturn(servicesList);
73         CsarHandler csar = new CsarHandler(iNotifData, "test-controller", sdcFolder);
74         assertEquals(sdcFolder + "/test-controller" + "/" + csarArtifactName, csar.getFilePath());
75     }
76
77     @Test(expected = CsarHandlerException.class)
78     public void testFailingConstructor() throws CsarHandlerException {
79         INotificationData iNotifData = Mockito.mock(INotificationData.class);
80         Mockito.when(iNotifData.getServiceArtifacts()).thenReturn(new ArrayList<>());
81         new CsarHandler(iNotifData, "test-controller", "/tmp/csar-handler-tests");
82         fail("Exception should have been raised");
83     }
84
85     @Test
86     public void testSave()
87             throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
88         IArtifactInfo serviceArtifact = Mockito.mock(IArtifactInfo.class);
89         Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
90         Mockito.when(serviceArtifact.getArtifactName()).thenReturn(csarArtifactName);
91         List<IArtifactInfo> servicesList = new ArrayList<>();
92         servicesList.add(serviceArtifact);
93         INotificationData iNotifData = Mockito.mock(INotificationData.class);
94         Mockito.when(iNotifData.getServiceArtifacts()).thenReturn(servicesList);
95         CsarHandler csar = new CsarHandler(iNotifData, "test-controller", "/tmp/csar-handler-tests");
96         IDistributionClientDownloadResult resultArtifact = Mockito.mock(IDistributionClientDownloadResult.class);
97         Mockito.when(resultArtifact.getArtifactPayload()).thenReturn(
98                 IOUtils.toByteArray(ResourceFileUtil.getResourceAsStream("example/sdc/service-Simsfoimap0112.csar")));
99         csar.save(resultArtifact);
100         assertTrue((new File(sdcFolder + "/test-controller/" + csarArtifactName)).exists());
101         assertEquals(csarArtifactName, csar.getArtifactElement().getArtifactName());
102         assertNotNull(csar.getSdcCsarHelper());
103     }
104 }