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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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;
31 import com.att.aft.dme2.internal.apache.commons.io.IOUtils;
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;
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;
52 public class CsarHandlerTest {
54 private static final String sdcFolder = "/tmp/csar-handler-tests";
55 private static final String csarArtifactName = "testArtifact.csar";
58 public static void removeAllFiles() throws IOException {
60 Path path = Paths.get(sdcFolder + "/test-controller/" + csarArtifactName);
61 Files.deleteIfExists(path);
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());
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");
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());