Remove proxy docker buildArg
[ccsdk/cds.git] / ms / sdclistener / application / src / test / java / org / onap / ccsdk / cds / sdclistener / service / ListenerServiceImplTest.java
1 /*
2  * Copyright © 2019 Bell Canada
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.ccsdk.cds.sdclistener.service;
17
18 import static junit.framework.TestCase.assertTrue;
19 import java.io.File;
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;
41
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 {
48
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;
54
55     @Rule
56     public TemporaryFolder folder = new TemporaryFolder();
57
58     @Autowired
59     private ListenerServiceImpl listenerService;
60
61     @Before
62     public void setup() {
63         csarArchivePath = folder.getRoot().toString();
64         tempDirectoryPath = Paths.get(csarArchivePath, "cds-sdc-listener-test");
65     }
66     @Test
67     public void extractBluePrintSuccessfully() throws IOException {
68         // Act
69         listenerService.extractBluePrint(CSAR_SAMPLE, tempDirectoryPath.toString());
70
71         // Verify
72         String result = checkFileExists(tempDirectoryPath);
73         assertTrue(result.contains(ZIP_FILE));
74     }
75
76     @Test
77     public void storeCsarArtifactToFileSuccessfully() throws  IOException {
78         // Arrange
79         DistributionClientDownloadResultStubImpl resultStub = new DistributionClientDownloadResultStubImpl();
80
81         // Act
82         listenerService.extractCsarAndStore(resultStub, tempDirectoryPath.toString());
83
84         // Verify
85         String result = checkFileExists(tempDirectoryPath);
86         assertTrue(result.contains(CSAR_FILE));
87     }
88
89     private String checkFileExists(Path path) throws IOException {
90         return Files.walk(path)
91             .filter(Files::isRegularFile)
92             .map(Path::toFile)
93             .findAny()
94             .get()
95             .getName();
96     }
97
98     public byte[] convertFileToByteArray(File file) {
99         try {
100             return FileUtils.readFileToByteArray(file);
101         } catch (IOException e) {
102             e.printStackTrace();
103         }
104         return null;
105     }
106
107     public class DistributionClientDownloadResultStubImpl extends DistributionClientResultStubImpl implements
108         IDistributionClientDownloadResult {
109
110         public DistributionClientDownloadResultStubImpl() {
111         }
112
113         public byte[] getArtifactPayload() {
114             File file = Paths.get(CSAR_SAMPLE).toFile();
115             return convertFileToByteArray(file);
116         }
117
118         public String getArtifactName() {
119             return "MackArtifactName";
120         }
121
122         public String getArtifactFilename() {
123             return "MackArtifactName.csar";
124         }
125     }
126 }