e33fbcdcc151cf6fa7ae7dda933c58f150646305
[ccsdk/cds.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada. All rights reserved.
3  *
4  * NOTICE:  All the intellectual and technical concepts contained herein are
5  * proprietary to Bell Canada and are protected by trade secret or copyright law.
6  * Unauthorized copying of this file, via any medium is strictly prohibited.
7  */
8
9 package org.onap.ccsdk.cds.cdssdclistener.service;
10
11 import static junit.framework.TestCase.assertTrue;
12 import java.io.IOException;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.Paths;
16 import org.junit.Before;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.TemporaryFolder;
20 import org.junit.runner.RunWith;
21 import org.onap.ccsdk.cds.cdssdclistener.client.CdsSdcListenerAuthClientInterceptor;
22 import org.onap.ccsdk.cds.cdssdclistener.handler.BluePrintProcesssorHandler;
23 import org.onap.sdc.impl.mock.DistributionClientDownloadResultStubImpl;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.boot.context.properties.EnableConfigurationProperties;
26 import org.springframework.boot.test.context.SpringBootTest;
27 import org.springframework.test.context.junit4.SpringRunner;
28
29 @RunWith(SpringRunner.class)
30 @EnableConfigurationProperties({ListenerServiceImpl.class, CdsSdcListenerAuthClientInterceptor.class,
31     BluePrintProcesssorHandler.class})
32 @SpringBootTest(classes = {ListenerServiceImplTest.class})
33 public class ListenerServiceImplTest {
34
35     private static final String CSAR_SAMPLE = "src/test/resources/service-Testsvc140.csar";
36     private static final String ZIP_FILE = ".zip";
37     private static final String CSAR_FILE = ".csar";
38     private String csarArchivePath;
39     private Path tempDirectoryPath;
40
41     @Rule
42     public TemporaryFolder folder = new TemporaryFolder();
43
44     @Autowired
45     private ListenerServiceImpl listenerService;
46
47     @Before
48     public void setup() {
49         csarArchivePath = folder.getRoot().toString();
50         tempDirectoryPath = Paths.get(csarArchivePath, "cds-sdc-listener-test");
51     }
52     @Test
53     public void extractBluePrintSuccessfully() throws IOException {
54         // Act
55         listenerService.extractBluePrint(CSAR_SAMPLE, tempDirectoryPath.toString());
56
57         // Verify
58         String result = checkFileExists(tempDirectoryPath);
59         assertTrue(result.contains(ZIP_FILE));
60     }
61
62     @Test
63     public void storeCsarArtifactToFileSuccessfully() throws  IOException {
64         // Arrange
65         DistributionClientDownloadResultStubImpl resultStub = new DistributionClientDownloadResultStubImpl();
66
67         // Act
68         listenerService.extractCsarAndStore(resultStub, tempDirectoryPath.toString());
69
70         // Verify
71         String result = checkFileExists(tempDirectoryPath);
72         assertTrue(result.contains(CSAR_FILE));
73     }
74
75     private String checkFileExists(Path path) throws IOException {
76         return Files.walk(path)
77             .filter(Files::isRegularFile)
78             .map(Path::toFile)
79             .findAny()
80             .get()
81             .getName();
82     }
83 }