014a96f03fa3083e4966af678c31840425aa8393
[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.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl.MountpointRegistrarImpl;
36 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.mock.odlapi.ClusterSingletonServiceProviderMock;
37 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.mock.odlapi.DataBrokerNetconfMock;
38 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.mock.odlapi.MountPointServiceMock;
39 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.mock.odlapi.NotificationPublishServiceMock;
40 import org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.test.mock.odlapi.RpcProviderRegistryMock;
41 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
42 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
43 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
44 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 @SuppressWarnings("deprecation")
49 public class TestMountpointRegistrarImpl {
50
51     private static Path KARAF_ETC = Paths.get("etc");
52     private static MountpointRegistrarImpl mountpointRegistrar;
53     private static DataBrokerNetconfMock dataBrokerNetconf;
54
55     private static final Logger LOG = LoggerFactory.getLogger(TestMountpointRegistrarImpl.class);
56
57
58
59     @Before
60     public void before() throws InterruptedException, IOException {
61
62         System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
63         // Call System property to get the classpath value
64         Path etc = KARAF_ETC;
65         delete(etc);
66
67         System.out.println("Create empty:" + etc.toString());
68         Files.createDirectories(etc);
69
70         // Create mocks
71         dataBrokerNetconf = new DataBrokerNetconfMock();
72         ClusterSingletonServiceProvider clusterSingletonService = new ClusterSingletonServiceProviderMock();
73                 MountPointService mountPointService = new MountPointServiceMock(null);
74         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
75         RpcProviderRegistry rpcProviderRegistry = new RpcProviderRegistryMock();
76
77         // start using blueprint interface
78         try {
79                 mountpointRegistrar = new MountpointRegistrarImpl();
80
81                 mountpointRegistrar.setDataBroker(dataBrokerNetconf);
82                 mountpointRegistrar.setMountPointService(mountPointService);
83                 mountpointRegistrar.setNotificationPublishService(notificationPublishService);
84                 mountpointRegistrar.setRpcProviderRegistry(rpcProviderRegistry);
85                 mountpointRegistrar.setClusterSingletonService(clusterSingletonService);
86                 mountpointRegistrar.init();
87         } catch (Exception e) {
88             StringWriter sw = new StringWriter();
89             e.printStackTrace(new PrintWriter(sw));
90             fail("Not initialized" +sw.toString());
91         }
92         System.out.println("Initialization status: " + mountpointRegistrar.isInitializationOk());
93         System.out.println("Initialization done");
94     }
95
96     @After
97     public void after() throws InterruptedException, IOException {
98
99         System.out.println("Start shutdown");
100         // close using blueprint interface
101         try {
102                 mountpointRegistrar.close();
103         } catch (Exception e) {
104             System.out.println(e);
105         }
106         delete(KARAF_ETC);
107
108     }
109
110     @Test
111     public void test1() {
112         System.out.println("Test1: slave mountpoint");
113         assertNotNull(mountpointRegistrar);
114         System.out.println("Initialization status: " + mountpointRegistrar.isInitializationOk());
115         System.out.println("Test2: Done");
116     }
117
118     // ********************* Private
119
120     @SuppressWarnings("unused")
121         private static void sleep(int millis) {
122         try {
123             Thread.sleep(millis);
124         } catch (InterruptedException e) {
125             LOG.warn(e.getMessage());
126             Thread.interrupted();
127         }
128     }
129
130     private static void delete(Path etc) throws IOException {
131         if (Files.exists(etc)) {
132             System.out.println("Found and remove:" + etc.toString());
133             delete(etc.toFile());
134         }
135     }
136
137     private static void delete(File f) throws IOException {
138         if (f.isDirectory()) {
139             for (File c : f.listFiles()) {
140                 delete(c);
141             }
142         }
143         if (!f.delete()) {
144             throw new FileNotFoundException("Failed to delete file: " + f);
145         }
146     }
147 }