junit tests devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / test / TestAai.java
1 /*******************************************************************************
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk feature sdnr wt
4  *  ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  ******************************************************************************/
21 package org.onap.ccsdk.features.sdnr.wt.devicemanager.test;
22
23 import static org.junit.Assert.fail;
24 import com.google.common.io.Files;
25 import com.sun.net.httpserver.HttpExchange;
26 import com.sun.net.httpserver.HttpHandler;
27 import com.sun.net.httpserver.HttpServer;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.net.InetSocketAddress;
32 import java.nio.charset.StandardCharsets;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.concurrent.ExecutorService;
36 import java.util.concurrent.Executors;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InventoryInformation;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.AaiConfig;
44
45 @SuppressWarnings("restriction")
46 public class TestAai {
47
48     private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
49     private static final File ENABLEDAAI_TESTCONFIG_FILE = new File(ENABLEDAAI_TESTCONFIG_FILENAME);
50     private static final int AAI_SERVER_PORT=45454;
51     private static final String TESTCONFIG_CONTENT="[dcae]\n" +
52             "dcaeUserCredentials=admin:admin\n" +
53             "dcaeUrl=off\n" +
54             "dcaeHeartbeatPeriodSeconds=120\n" +
55             "dcaeTestCollector=no\n" +
56             "\n" +
57             "[aots]\n" +
58             "userPassword=passwd\n" +
59             "soapurladd=off\n" +
60             "soapaddtimeout=10\n" +
61             "soapinqtimeout=20\n" +
62             "userName=user\n" +
63             "inqtemplate=inqreq.tmpl.xml\n" +
64             "assignedto=userid\n" +
65             "addtemplate=addreq.tmpl.xml\n" +
66             "severitypassthrough=critical,major,minor,warning\n" +
67             "systemuser=user\n" +
68             "prt-offset=1200\n" +
69             "soapurlinq=off\n" +
70             "#smtpHost=\n" +
71             "#smtpPort=\n" +
72             "#smtpUsername=\n" +
73             "#smtpPassword=\n" +
74             "#smtpSender=\n" +
75             "#smtpReceivers=\n" +
76             "\n" +
77             "[es]\n" +
78             "esCluster=sendateodl5\n" +
79             "\n" +
80             "[aai]\n" +
81             "#keep comment\n" +
82             "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
83             "aaiUrl=http://localhost:"+AAI_SERVER_PORT+"\n" +
84             "aaiUserCredentials=AAI:AAI\n" +
85             "aaiDeleteOnMountpointRemove=true\n" +
86             "aaiTrustAllCerts=false\n" +
87             "aaiApiVersion=aai/v13\n" +
88             "aaiPropertiesFile=aaiclient.properties\n" +
89             "aaiApplicationId=SDNR\n" +
90             "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
91             "aaiPcks12ClientCertPassphrase=adminadmin\n" +
92             "aaiClientConnectionTimeout=30000\n" +
93             "\n" +
94             "[pm]\n" +
95             "pmCluster=sendateodl5\n" +
96             "pmEnabled=true\n" +
97             "\n" +
98             "";
99     private HttpServer server;
100     private ExecutorService httpThreadPool;
101     private HtDevicemanagerConfiguration globalCfg;
102
103     @Test
104     public void test() {
105         HtDevicemanagerConfiguration cfg=HtDevicemanagerConfiguration.getTestConfiguration();
106
107         AaiProviderClient provider = new AaiProviderClient(cfg, null);
108
109         String mountPointName = "testDevice 01";
110         String type="Unit";
111         String model="Horizon Compact+";
112         String vendor="DragonWave-X";
113         String ipv4="127.0.0.1";
114         String ipv6="::1";
115         List<String> ifInfos = new ArrayList<>();
116         ifInfos.add("LP-MWPS-RADIO");
117         InventoryInformation ii=new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
118         System.out.println("registering device");
119         provider.onDeviceRegistered(mountPointName,ii);
120         try {
121             Thread.sleep(5000);
122         } catch (InterruptedException e) {
123             e.printStackTrace();
124         }
125         System.out.println("unregistering device");
126         provider.onDeviceUnregistered(mountPointName);
127         System.out.println("finished");
128         try {
129             provider.close();
130         } catch (Exception e) {
131             e.printStackTrace();
132         }
133     }
134     @Test
135     public void test2() {
136         try {
137             Thread.sleep(3000);
138         } catch (InterruptedException e1) {
139             e1.printStackTrace();
140         }
141         AaiProviderClient provider = new AaiProviderClient(globalCfg, null);
142
143         String mountPointName = "testDevice 01";
144         String type="Unit";
145         String model="Horizon Compact+";
146         String vendor="DragonWave-X";
147         String ipv4="127.0.0.1";
148         String ipv6="::1";
149         List<String> ifInfos = new ArrayList<>();
150         ifInfos.add("LP-MWPS-RADIO");
151         InventoryInformation ii=new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
152         System.out.println("registering device");
153         provider.onDeviceRegistered(mountPointName);
154         provider.onDeviceRegistered(mountPointName,ii);
155         try {
156             Thread.sleep(5000);
157         } catch (InterruptedException e) {
158             e.printStackTrace();
159         }
160         System.out.println("unregistering device");
161         provider.onDeviceUnregistered(mountPointName);
162         System.out.println("finished");
163         try {
164             provider.close();
165         } catch (Exception e) {
166             e.printStackTrace();
167         }
168     }
169     @Before
170     public void initAaiTestWebserver() throws IOException {
171         try {
172             Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
173         } catch (IOException e1) {
174             fail(e1.getMessage());
175         }
176         globalCfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
177         AaiConfig.reload();
178         this.server = HttpServer.create(new InetSocketAddress(AAI_SERVER_PORT), 0);
179         this.httpThreadPool = Executors.newFixedThreadPool(5);
180         this.server.setExecutor(this.httpThreadPool);
181         this.server.createContext(globalCfg.getAai().getBaseUri(), new MyHandler());
182         //server.createContext("/", new MyRootHandler());
183         this.server.setExecutor(null); // creates a default executor
184         this.server.start();
185         System.out.println("http server started");
186     }
187
188     @After
189     public void stopTestWebserver() {
190         if (this.server != null) {
191             this.server.stop(0);
192             this.httpThreadPool.shutdownNow();
193             System.out.println("http server stopped" );
194         }
195         if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
196             ENABLEDAAI_TESTCONFIG_FILE.delete();
197         }
198
199     }
200     static class MyHandler implements HttpHandler {
201         @Override
202         public void handle(HttpExchange t) throws IOException {
203             String method = t.getRequestMethod();
204             System.out.println("req method: " + method);
205             OutputStream os = null;
206             try {
207                 String res="";
208                 if (method.equals("GET")) {
209                     t.sendResponseHeaders(404,res.length() );
210                     os = t.getResponseBody();
211                     os.write(res.getBytes());
212                 } else if (method.equals("DELETE")) {
213                     t.sendResponseHeaders(200, res.length());
214                     os = t.getResponseBody();
215                     os.write(res.getBytes());
216                 } else if (method.equals("PUT")) {
217                     t.sendResponseHeaders(200, res.length());
218                     os = t.getResponseBody();
219                     os.write(res.getBytes());
220                 } else {
221                     t.sendResponseHeaders(404, 0);
222                 }
223                 System.out.println("req handled successful");
224
225             } catch (Exception e) {
226                 System.out.println(e.getMessage());
227             }
228             finally {
229                 if (os != null)
230                 {
231                     os.close();
232                 }
233             }
234         }
235     }
236 }