Merge "Resource dictionary- updated license text"
[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 static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_ERROR;
20 import java.io.File;
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import mockit.Mock;
26 import org.apache.commons.io.FileUtils;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.TemporaryFolder;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.internal.junit.JUnitRule;
35 import org.mockito.junit.MockitoJUnit;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.mockito.junit.MockitoRule;
38 import org.onap.ccsdk.cds.sdclistener.SdcListenerConfiguration;
39 import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor;
40 import org.onap.ccsdk.cds.sdclistener.dto.SdcListenerDto;
41 import org.onap.ccsdk.cds.sdclistener.handler.BluePrintProcesssorHandler;
42 import org.onap.ccsdk.cds.sdclistener.status.SdcListenerStatus;
43 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
44 import org.onap.sdc.impl.mock.DistributionClientResultStubImpl;
45 import org.onap.sdc.utils.DistributionStatusEnum;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.context.properties.EnableConfigurationProperties;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.mock.mockito.MockBean;
50 import org.springframework.test.context.junit4.SpringRunner;
51
52 @RunWith(SpringRunner.class)
53 @EnableConfigurationProperties({SdcListenerAuthClientInterceptor.class,
54     BluePrintProcesssorHandler.class, SdcListenerDto.class, ListenerServiceImpl.class, SdcListenerStatus.class,
55     SdcListenerConfiguration.class})
56 @SpringBootTest(classes = {ListenerServiceImplTest.class})
57 public class ListenerServiceImplTest {
58
59     private static final String CSAR_SAMPLE = "src/test/resources/service-ServicePnfTest-csar.csar";
60     private static final String WRONG_CSAR_SAMPLE = "src/test/resources/wrong_csar_pattern.csar";
61     private static final String CBA_ZIP_PATH = "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
66
67     private String csarArchivePath;
68     private Path tempDirectoryPath;
69
70     @Rule
71     public TemporaryFolder folder = new TemporaryFolder();
72
73     @Rule
74     public MockitoRule mockitoRule = MockitoJUnit.rule();
75
76     @Autowired
77     private ListenerServiceImpl listenerService;
78
79     @MockBean
80     SdcListenerStatus status;
81
82     @MockBean
83     SdcListenerDto listenerDto;
84
85     @Before
86     public void setup() {
87         MockitoAnnotations.initMocks(this);
88         csarArchivePath = folder.getRoot().toString();
89         tempDirectoryPath = Paths.get(csarArchivePath, "cds-sdc-listener-test");
90     }
91
92     @Test
93     public void extractBluePrintSuccessfully() throws IOException {
94         // Act
95         listenerService.extractBluePrint(CSAR_SAMPLE, tempDirectoryPath.toString());
96
97         // Verify
98         String result = checkFileExists(tempDirectoryPath);
99         assertTrue(result.contains(ZIP_FILE));
100     }
101
102     @Test
103     public void extractBluePrintFailure() {
104         // Arrange
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.doCallRealMethod().when(status)
109             .sendResponseStatusBackToSDC(DISTRIBUTION_ID, COMPONENT_DONE_ERROR, errorMessage);
110
111         // Act
112         listenerService.extractBluePrint(WRONG_CSAR_SAMPLE, tempDirectoryPath.toString());
113
114         // Verify
115         Mockito.verify(status).sendResponseStatusBackToSDC(DISTRIBUTION_ID, COMPONENT_DONE_ERROR, errorMessage);
116     }
117
118     @Test
119     public void storeCsarArtifactToFileSuccessfully() throws  IOException {
120         // Arrange
121         DistributionClientDownloadResultStubImpl resultStub = new DistributionClientDownloadResultStubImpl();
122
123         // Act
124         listenerService.extractCsarAndStore(resultStub, tempDirectoryPath);
125
126         // Verify
127         String result = checkFileExists(tempDirectoryPath);
128         assertTrue(result.contains(CSAR_FILE));
129     }
130
131     private String checkFileExists(Path path) throws IOException {
132         return Files.walk(path)
133             .filter(Files::isRegularFile)
134             .map(Path::toFile)
135             .findAny()
136             .get()
137             .getName();
138     }
139
140     public byte[] convertFileToByteArray(File file) {
141         try {
142             return FileUtils.readFileToByteArray(file);
143         } catch (IOException e) {
144             e.printStackTrace();
145         }
146         return null;
147     }
148
149     public class DistributionClientDownloadResultStubImpl extends DistributionClientResultStubImpl implements
150         IDistributionClientDownloadResult {
151
152         public DistributionClientDownloadResultStubImpl() {
153         }
154
155         public byte[] getArtifactPayload() {
156             File file = Paths.get(CSAR_SAMPLE).toFile();
157             return convertFileToByteArray(file);
158         }
159
160         public String getArtifactName() {
161             return "MackArtifactName";
162         }
163
164         public String getArtifactFilename() {
165             return "MackArtifactName.csar";
166         }
167     }
168 }