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