433972435a5aea330c452430f68d1d01c9c17c15
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START======================================================= ONAP : ccsdk
3  * feature sdnr wt ================================================================================
4  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
5  * ================================================================================ Licensed under
6  * the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
7  * with the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License. ============LICENSE_END=========================================================
15  */
16 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test;
17
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.fail;
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MountpointRegistrarImpl;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class TestMountpointRegistrarImpl {
38
39     private static Path KARAF_ETC = Paths.get("etc");
40     private static MountpointRegistrarImpl mountpointRegistrar;
41
42     private static final Logger LOG = LoggerFactory.getLogger(TestMountpointRegistrarImpl.class);
43
44
45
46     @Before
47     public void before() throws InterruptedException, IOException {
48
49         System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
50         // Call System property to get the classpath value
51         Path etc = KARAF_ETC;
52         delete(etc);
53
54         System.out.println("Create empty:" + etc.toString());
55         Files.createDirectories(etc);
56
57         // Create mocks
58
59         // start using blueprint interface
60         try {
61             mountpointRegistrar = new MountpointRegistrarImpl();
62             mountpointRegistrar.init();
63         } catch (Exception e) {
64             StringWriter sw = new StringWriter();
65             e.printStackTrace(new PrintWriter(sw));
66             fail("Not initialized" + sw.toString());
67         }
68         System.out.println("Initialization status: " + mountpointRegistrar.isInitializationOk());
69         System.out.println("Initialization done");
70     }
71
72     @After
73     public void after() throws InterruptedException, IOException {
74
75         System.out.println("Start shutdown");
76         // close using blueprint interface
77         try {
78             mountpointRegistrar.close();
79         } catch (Exception e) {
80             System.out.println(e);
81         }
82         delete(KARAF_ETC);
83
84     }
85
86     @Test
87     public void test1() {
88         System.out.println("Test1: slave mountpoint");
89         assertNotNull(mountpointRegistrar);
90         System.out.println("Initialization status: " + mountpointRegistrar.isInitializationOk());
91         System.out.println("Test2: Done");
92     }
93
94     // ********************* Private
95
96     @SuppressWarnings("unused")
97     private static void sleep(int millis) {
98         try {
99             Thread.sleep(millis);
100         } catch (InterruptedException e) {
101             LOG.warn(e.getMessage());
102             Thread.currentThread().interrupt();
103         }
104     }
105
106     private static void delete(Path etc) throws IOException {
107         if (Files.exists(etc)) {
108             System.out.println("Found and remove:" + etc.toString());
109             delete(etc.toFile());
110         }
111     }
112
113     private static void delete(File f) throws IOException {
114         if (f.isDirectory()) {
115             for (File c : f.listFiles()) {
116                 delete(c);
117             }
118         }
119         if (!f.delete()) {
120             throw new FileNotFoundException("Failed to delete file: " + f);
121         }
122     }
123 }