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;
19 import static org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus.NotificationType.SDC_LISTENER_COMPONENT;
20 import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_ERROR;
21 import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_OK;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import org.apache.commons.io.FileUtils;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.TemporaryFolder;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.mockito.junit.MockitoJUnit;
36 import org.mockito.junit.MockitoRule;
37 import org.onap.ccsdk.cds.sdclistener.SdcListenerConfiguration;
38 import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor;
39 import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto;
40 import org.onap.ccsdk.cds.sdclistener.handler.BluePrintProcesssorHandler;
41 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus;
42 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus.NotificationType;
43 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
44 import org.onap.sdc.impl.mock.DistributionClientResultStubImpl;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.context.properties.EnableConfigurationProperties;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.mock.mockito.MockBean;
49 import org.springframework.test.context.junit4.SpringRunner;
51 @RunWith(SpringRunner.class)
52 @EnableConfigurationProperties({SdcListenerAuthClientInterceptor.class,
53 BluePrintProcesssorHandler.class, SdcListenerDto.class, ListenerServiceImpl.class, SdcListenerStatus.class,
54 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 = "Artifacts/[a-zA-Z0-9-_.]+/Deployment/CONTROLLER_BLUEPRINT_ARCHIVE/[a-zA-Z0-9-_.()]+[.]zip";
61 private static final String ZIP_FILE = ".zip";
62 private static final String CSAR_FILE = ".csar";
63 private static final String DISTRIBUTION_ID = "1";
64 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 final String errorMessage = String
106 .format("The CBA Archive doesn't exist as per this given regex %s", CBA_ZIP_PATH);
107 Mockito.when(listenerDto.getDistributionId()).thenReturn(DISTRIBUTION_ID);
108 Mockito.when(listenerDto.getArtifactUrl()).thenReturn(URL);
109 Mockito.doCallRealMethod().when(status)
110 .sendResponseBackToSdc(DISTRIBUTION_ID, COMPONENT_DONE_OK, null, URL, SDC_LISTENER_COMPONENT);
113 listenerService.extractBluePrint(WRONG_CSAR_SAMPLE, tempDirectoryPath.toString());
116 Mockito.verify(status)
117 .sendResponseBackToSdc(DISTRIBUTION_ID, COMPONENT_DONE_OK, null, URL, SDC_LISTENER_COMPONENT);
121 public void storeCsarArtifactToFileSuccessfully() throws IOException {
123 DistributionClientDownloadResultStubImpl resultStub = new DistributionClientDownloadResultStubImpl();
126 listenerService.extractCsarAndStore(resultStub, tempDirectoryPath);
129 String result = checkFileExists(tempDirectoryPath);
130 assertTrue(result.contains(CSAR_FILE));
133 private String checkFileExists(Path path) throws IOException {
134 return Files.walk(path)
135 .filter(Files::isRegularFile)
142 public byte[] convertFileToByteArray(File file) {
144 return FileUtils.readFileToByteArray(file);
145 } catch (IOException e) {
151 public class DistributionClientDownloadResultStubImpl extends DistributionClientResultStubImpl implements
152 IDistributionClientDownloadResult {
154 public DistributionClientDownloadResultStubImpl() {
157 public byte[] getArtifactPayload() {
158 File file = Paths.get(CSAR_SAMPLE).toFile();
159 return convertFileToByteArray(file);
162 public String getArtifactName() {
163 return "MackArtifactName";
166 public String getArtifactFilename() {
167 return "MackArtifactName.csar";