2 * Copyright © 2019 Bell Canada
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.cds.sdclistener.service;
18 import static junit.framework.TestCase.assertTrue;
20 import java.io.IOException;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import org.apache.commons.io.FileUtils;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.junit.runner.RunWith;
30 import org.onap.ccsdk.cds.sdclistener.SdcListenerConfiguration;
31 import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor;
32 import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto;
33 import org.onap.ccsdk.cds.sdclistener.handler.BluePrintProcesssorHandler;
34 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus;
35 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
36 import org.onap.sdc.impl.mock.DistributionClientResultStubImpl;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.boot.context.properties.EnableConfigurationProperties;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.test.context.junit4.SpringRunner;
42 @RunWith(SpringRunner.class)
43 @EnableConfigurationProperties({SdcListenerAuthClientInterceptor.class,
44 BluePrintProcesssorHandler.class, SdcListenerDto.class, ListenerServiceImpl.class, SdcListenerStatus.class,
45 SdcListenerConfiguration.class})
46 @SpringBootTest(classes = {ListenerServiceImplTest.class})
47 public class ListenerServiceImplTest {
49 private static final String CSAR_SAMPLE = "src/test/resources/service-Testsvc140.csar";
50 private static final String ZIP_FILE = ".zip";
51 private static final String CSAR_FILE = ".csar";
52 private String csarArchivePath;
53 private Path tempDirectoryPath;
56 public TemporaryFolder folder = new TemporaryFolder();
59 private ListenerServiceImpl listenerService;
63 csarArchivePath = folder.getRoot().toString();
64 tempDirectoryPath = Paths.get(csarArchivePath, "cds-sdc-listener-test");
67 public void extractBluePrintSuccessfully() throws IOException {
69 listenerService.extractBluePrint(CSAR_SAMPLE, tempDirectoryPath.toString());
72 String result = checkFileExists(tempDirectoryPath);
73 assertTrue(result.contains(ZIP_FILE));
77 public void storeCsarArtifactToFileSuccessfully() throws IOException {
79 DistributionClientDownloadResultStubImpl resultStub = new DistributionClientDownloadResultStubImpl();
82 listenerService.extractCsarAndStore(resultStub, tempDirectoryPath.toString());
85 String result = checkFileExists(tempDirectoryPath);
86 assertTrue(result.contains(CSAR_FILE));
89 private String checkFileExists(Path path) throws IOException {
90 return Files.walk(path)
91 .filter(Files::isRegularFile)
98 public byte[] convertFileToByteArray(File file) {
100 return FileUtils.readFileToByteArray(file);
101 } catch (IOException e) {
107 public class DistributionClientDownloadResultStubImpl extends DistributionClientResultStubImpl implements
108 IDistributionClientDownloadResult {
110 public DistributionClientDownloadResultStubImpl() {
113 public byte[] getArtifactPayload() {
114 File file = Paths.get(CSAR_SAMPLE).toFile();
115 return convertFileToByteArray(file);
118 public String getArtifactName() {
119 return "MackArtifactName";
122 public String getArtifactFilename() {
123 return "MackArtifactName.csar";