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.
17 package org.onap.ccsdk.cds.sdclistener.service;
19 import org.apache.commons.io.FileUtils;
20 import org.junit.Before;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.TemporaryFolder;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.junit.MockitoJUnit;
28 import org.mockito.junit.MockitoRule;
29 import org.onap.ccsdk.cds.sdclistener.SdcListenerConfiguration;
30 import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor;
31 import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto;
32 import org.onap.ccsdk.cds.sdclistener.handler.BlueprintProcesssorHandler;
33 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus;
34 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
35 import org.onap.sdc.impl.mock.DistributionClientResultStubImpl;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.context.properties.EnableConfigurationProperties;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.test.mock.mockito.MockBean;
40 import org.springframework.test.context.junit4.SpringRunner;
43 import java.io.IOException;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
48 import static junit.framework.TestCase.assertTrue;
49 import static org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus.NotificationType.SDC_LISTENER_COMPONENT;
50 import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_OK;
52 @RunWith(SpringRunner.class)
53 @EnableConfigurationProperties({SdcListenerAuthClientInterceptor.class, BlueprintProcesssorHandler.class,
54 SdcListenerDto.class, ListenerServiceImpl.class, SdcListenerStatus.class, SdcListenerConfiguration.class})
55 @SpringBootTest(classes = {ListenerServiceImplTest.class})
56 public class ListenerServiceImplTest {
58 private static final String CSAR_SAMPLE = "src/test/resources/service-ServicePnfTest-csar.csar";
59 private static final String WRONG_CSAR_SAMPLE = "src/test/resources/wrong_csar_pattern.csar";
60 private static final String CBA_ZIP_PATH =
61 "Artifacts/[a-zA-Z0-9-_.]+/Deployment/CONTROLLER_BLUEPRINT_ARCHIVE/[a-zA-Z0-9-_.()]+[.]zip";
62 private static final String ZIP_FILE = ".zip";
63 private static final String CSAR_FILE = ".csar";
64 private static final String DISTRIBUTION_ID = "1";
65 private static final String URL = "/sdc/v1/artifact";
67 private String csarArchivePath;
68 private Path tempDirectoryPath;
71 public TemporaryFolder folder = new TemporaryFolder();
74 public MockitoRule mockitoRule = MockitoJUnit.rule();
77 private ListenerServiceImpl listenerService;
80 SdcListenerStatus status;
83 SdcListenerDto listenerDto;
87 MockitoAnnotations.initMocks(this);
88 csarArchivePath = folder.getRoot().toString();
89 tempDirectoryPath = Paths.get(csarArchivePath, "cds-sdc-listener-test");
93 public void extractBlueprintSuccessfully() throws IOException {
95 listenerService.extractBlueprint(CSAR_SAMPLE, tempDirectoryPath.toString());
98 String result = checkFileExists(tempDirectoryPath);
99 assertTrue(result.contains(ZIP_FILE));
103 public void extractBlueprintFailure() {
105 Mockito.when(listenerDto.getDistributionId()).thenReturn(DISTRIBUTION_ID);
106 Mockito.when(listenerDto.getArtifactUrl()).thenReturn(URL);
107 Mockito.doCallRealMethod().when(status).sendResponseBackToSdc(DISTRIBUTION_ID, COMPONENT_DONE_OK, null, URL,
108 SDC_LISTENER_COMPONENT);
111 listenerService.extractBlueprint(WRONG_CSAR_SAMPLE, tempDirectoryPath.toString());
114 Mockito.verify(status).sendResponseBackToSdc(DISTRIBUTION_ID, COMPONENT_DONE_OK, null, URL,
115 SDC_LISTENER_COMPONENT);
119 public void storeCsarArtifactToFileSuccessfully() throws IOException {
121 DistributionClientDownloadResultStubImpl resultStub = new DistributionClientDownloadResultStubImpl();
124 listenerService.extractCsarAndStore(resultStub, tempDirectoryPath);
127 String result = checkFileExists(tempDirectoryPath);
128 assertTrue(result.contains(CSAR_FILE));
131 private String checkFileExists(Path path) throws IOException {
132 return Files.walk(path).filter(Files::isRegularFile).map(Path::toFile).findAny().get().getName();
135 public byte[] convertFileToByteArray(File file) {
137 return FileUtils.readFileToByteArray(file);
138 } catch (IOException e) {
144 public class DistributionClientDownloadResultStubImpl extends DistributionClientResultStubImpl
145 implements IDistributionClientDownloadResult {
147 public DistributionClientDownloadResultStubImpl() {}
149 public byte[] getArtifactPayload() {
150 File file = Paths.get(CSAR_SAMPLE).toFile();
151 return convertFileToByteArray(file);
154 public String getArtifactName() {
155 return "MackArtifactName";
158 public String getArtifactFilename() {
159 return "MackArtifactName.csar";