dcf942f37705f58a4a1377242c2bb29383fbff5f
[ccsdk/cds.git] /
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
17 package org.onap.ccsdk.cds.sdclistener.service;
18
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.utils.DistributionActionResultEnum;
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;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
47
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;
51
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 {
57
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";
65
66     private String csarArchivePath;
67     private Path tempDirectoryPath;
68
69     @Rule
70     public TemporaryFolder folder = new TemporaryFolder();
71
72     @Rule
73     public MockitoRule mockitoRule = MockitoJUnit.rule();
74
75     @Autowired
76     private ListenerServiceImpl listenerService;
77
78     @MockBean
79     SdcListenerStatus status;
80
81     @MockBean
82     SdcListenerDto listenerDto;
83
84     @Before
85     public void setup() {
86         MockitoAnnotations.initMocks(this);
87         csarArchivePath = folder.getRoot().toString();
88         tempDirectoryPath = Paths.get(csarArchivePath, "cds-sdc-listener-test");
89     }
90
91     @Test
92     public void extractBluePrintSuccessfully() throws IOException {
93         // Act
94         listenerService.extractBluePrint(CSAR_SAMPLE, tempDirectoryPath.toString());
95
96         // Verify.
97         String result = checkFileExists(tempDirectoryPath);
98         assertTrue(result.contains(ZIP_FILE));
99     }
100
101     @Test
102     public void extractBluePrintFailure() {
103         // Arrange
104         Mockito.when(listenerDto.getDistributionId()).thenReturn(DISTRIBUTION_ID);
105         Mockito.when(listenerDto.getArtifactUrl()).thenReturn(URL);
106         Mockito.doCallRealMethod().when(status).sendResponseBackToSdc(DISTRIBUTION_ID, COMPONENT_DONE_OK, null, URL,
107                 SDC_LISTENER_COMPONENT);
108
109         // Act
110         listenerService.extractBluePrint(WRONG_CSAR_SAMPLE, tempDirectoryPath.toString());
111
112         // Verify
113         Mockito.verify(status).sendResponseBackToSdc(DISTRIBUTION_ID, COMPONENT_DONE_OK, null, URL,
114                 SDC_LISTENER_COMPONENT);
115     }
116
117     @Test
118     public void storeCsarArtifactToFileSuccessfully() throws IOException {
119         // Arrange
120         DistributionClientDownloadResultStubImpl resultStub = new DistributionClientDownloadResultStubImpl();
121
122         // Act
123         listenerService.extractCsarAndStore(resultStub, tempDirectoryPath);
124
125         // Verify
126         String result = checkFileExists(tempDirectoryPath);
127         assertTrue(result.contains(CSAR_FILE));
128     }
129
130     private String checkFileExists(Path path) throws IOException {
131         return Files.walk(path).filter(Files::isRegularFile).map(Path::toFile).findAny().get().getName();
132     }
133
134     public byte[] convertFileToByteArray(File file) {
135         try {
136             return FileUtils.readFileToByteArray(file);
137         } catch (IOException e) {
138             e.printStackTrace();
139         }
140         return null;
141     }
142
143     public class DistributionClientDownloadResultStubImpl implements IDistributionClientDownloadResult {
144
145         @Override
146         public DistributionActionResultEnum getDistributionActionResult() {
147             return DistributionActionResultEnum.SUCCESS;
148         }
149
150         @Override
151         public String getDistributionMessageResult() {
152             return "Stub Result, method not implemented!";
153         }
154
155         public DistributionClientDownloadResultStubImpl() {
156         }
157
158         public byte[] getArtifactPayload() {
159             File file = Paths.get(CSAR_SAMPLE).toFile();
160             return convertFileToByteArray(file);
161         }
162
163         public String getArtifactName() {
164             return "MackArtifactName";
165         }
166
167         public String getArtifactFilename() {
168             return "MackArtifactName.csar";
169         }
170
171     }
172
173 }