Increase Test coverage
[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
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.net.InetSocketAddress;
29 import java.nio.charset.StandardCharsets;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InventoryInformation;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
41 import com.sun.net.httpserver.HttpExchange;
42 import com.sun.net.httpserver.HttpHandler;
43 import com.sun.net.httpserver.HttpServer;
44 import com.google.common.io.Files;
45
46 public class TestAai {
47
48     private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
49     private static final int AAI_SERVER_PORT=45454;
50     private static final String TESTCONFIG_CONTENT="[dcae]\n" +
51             "dcaeUserCredentials=admin:admin\n" +
52             "dcaeUrl=off\n" +
53             "dcaeHeartbeatPeriodSeconds=120\n" +
54             "dcaeTestCollector=no\n" +
55             "\n" +
56             "[aots]\n" +
57             "userPassword=passwd\n" +
58             "soapurladd=off\n" +
59             "soapaddtimeout=10\n" +
60             "soapinqtimeout=20\n" +
61             "userName=user\n" +
62             "inqtemplate=inqreq.tmpl.xml\n" +
63             "assignedto=userid\n" +
64             "addtemplate=addreq.tmpl.xml\n" +
65             "severitypassthrough=critical,major,minor,warning\n" +
66             "systemuser=user\n" +
67             "prt-offset=1200\n" +
68             "soapurlinq=off\n" +
69             "#smtpHost=\n" +
70             "#smtpPort=\n" +
71             "#smtpUsername=\n" +
72             "#smtpPassword=\n" +
73             "#smtpSender=\n" +
74             "#smtpReceivers=\n" +
75             "\n" +
76             "[es]\n" +
77             "esCluster=sendateodl5\n" +
78             "\n" +
79             "[aai]\n" +
80             "#keep comment\n" +
81             "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
82             "aaiUrl=http://localhost:"+AAI_SERVER_PORT+"\n" +
83             "aaiUserCredentials=AAI:AAI\n" +
84             "aaiDeleteOnMountpointRemove=true\n" +
85             "aaiTrustAllCerts=false\n" +
86             "aaiApiVersion=aai/v13\n" +
87             "aaiPropertiesFile=aaiclient.properties\n" +
88             "aaiApplicationId=SDNR\n" +
89             "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
90             "aaiPcks12ClientCertPassphrase=adminadmin\n" +
91             "aaiClientConnectionTimeout=30000\n" +
92             "\n" +
93             "[pm]\n" +
94             "pmCluster=sendateodl5\n" +
95             "pmEnabled=true\n" +
96             "\n" +
97             "";
98     private HttpServer server;
99     private ExecutorService httpThreadPool;
100     private HtDevicemanagerConfiguration cfg;
101
102     @Test
103     public void test() {
104         HtDevicemanagerConfiguration cfg=HtDevicemanagerConfiguration.getTestConfiguration();
105
106         AaiProviderClient provider = new AaiProviderClient(cfg, null);
107
108         String mountPointName = "testDevice 01";
109         String type="Unit";
110         String model="Horizon Compact+";
111         String vendor="DragonWave-X";
112         String ipv4="127.0.0.1";
113         String ipv6="::1";
114         List<String> ifInfos = new ArrayList<>();
115         ifInfos.add("LP-MWPS-RADIO");
116         InventoryInformation ii=new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
117         System.out.println("registering device");
118         provider.onDeviceRegistered(mountPointName,ii);
119         try {
120             Thread.sleep(5000);
121         } catch (InterruptedException e) {
122             e.printStackTrace();
123         }
124         System.out.println("unregistering device");
125         provider.onDeviceUnregistered(mountPointName);
126         System.out.println("finished");
127         try {
128             provider.close();
129         } catch (Exception e) {
130             e.printStackTrace();
131         }
132     }
133     @Test
134     public void test2() {
135         try {
136             Thread.sleep(3000);
137         } catch (InterruptedException e1) {
138             // TODO Auto-generated catch block
139             e1.printStackTrace();
140         }
141         AaiProviderClient provider = new AaiProviderClient(cfg, 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(new File(ENABLEDAAI_TESTCONFIG_FILENAME), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
173         } catch (IOException e1) {
174             fail(e1.getMessage());
175         }
176         cfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
177         cfg.getAai().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(cfg.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     }
196     static class MyHandler implements HttpHandler {
197         @Override
198         public void handle(HttpExchange t) throws IOException {
199             String method = t.getRequestMethod();
200             System.out.println("req method: " + method);
201             OutputStream os = null;
202             try {
203                 String res="";
204                 if (method.equals("GET")) {
205                     t.sendResponseHeaders(404,res.length() );
206                     os = t.getResponseBody();
207                     os.write(res.getBytes());
208                 } else if (method.equals("DELETE")) {
209                     t.sendResponseHeaders(200, res.length());
210                     os = t.getResponseBody();
211                     os.write(res.getBytes());
212                 } else if (method.equals("PUT")) {
213                     t.sendResponseHeaders(200, res.length());
214                     os = t.getResponseBody();
215                     os.write(res.getBytes());
216                 } else {
217                     t.sendResponseHeaders(404, 0);
218                 }
219                 System.out.println("req handled successful");
220
221             } catch (Exception e) {
222                 System.out.println(e.getMessage());
223             }
224             finally {
225                 if (os != null)
226                 {
227                     os.close();
228                 }
229             }
230         }
231     }
232 }