f2f4e4b88b43c961269de1de94cbdb881358dd3c
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / apigateway / impl / src / main / test / SimpleTests.java
1 import java.io.File;
2 import java.nio.file.Path;
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import org.json.JSONArray;
6 import org.json.JSONObject;
7
8 public class SimpleTests {
9
10
11         private static void testURI()
12         {
13                 String uri="/ms/1/api/";
14                 String query="task=getconfig";
15
16                 long id=0;
17                 String remoteUrl="";
18                 uri = uri.substring("/ms/".length());
19
20                 try {
21                         id = Long.parseLong(uri.substring(0, uri.indexOf("/")));
22                         uri=uri.substring(uri.indexOf("/"));
23                 } catch (Exception err) {
24                         System.out.println(err.getMessage());
25                 }
26
27                 if(id>0)
28                 {
29                 remoteUrl=baseUrl(id)+uri;
30                 if (query != null && query.length() > 0)
31                         remoteUrl += "?" + query;
32                 }
33                 System.out.println("url="+remoteUrl);
34         }
35         private static void testMSDBDataParser()
36         {
37                 String resp="{\"took\":6,\"timed_out\":false,\"_shards\":{\"total\":5,\"successful\":5,\"failed\":0},\"hits\":{\"total\":2,\"max_score\":1.0,\"hits\":[{\"_index\":\"mwtn_v1\",\"_type\":\"mediator-server\",\"_id\":\"2\",\"_score\":1.0,\"_source\":{\"id\":2,\"name\":\"Server 2\",\"url\":\"http://mediatorsnmp.fritz.box:7070\"}},{\"_index\":\"mwtn_v1\",\"_type\":\"mediator-server\",\"_id\":\"1\",\"_score\":1.0,\"_source\":{\"id\":1,\"name\":\"Server 1\",\"url\":\"http://192.168.11.44:7070\"}}]}}";
38                 JSONObject ro=new JSONObject(resp);
39                 JSONArray a=ro.getJSONObject("hits").getJSONArray("hits");
40                 for(int i=0;i<a.length();i++)
41                 {
42                         JSONObject x=a.getJSONObject(i).getJSONObject("_source");
43                         System.out.println("id="+x.getLong("id")+" name=\""+x.getString("name")+"\" url=\""+x.getString("url")+"\"");
44                 }
45
46         }
47
48          public static void walk(ArrayList<File> results, String path ) {
49
50                 File root = new File( path );
51                 File[] list = root.listFiles();
52
53                 if (list == null) return;
54
55                 for ( File f : list ) {
56                     if ( f.isDirectory() ) {
57                         walk(results, f.getAbsolutePath() );
58                         //System.out.println( "Dir:" + f.getAbsoluteFile() );
59                     }
60                     else {
61                         //System.out.println( "File:" + f.getAbsoluteFile() );
62                         if(f.isFile() && f.getName().endsWith(".md") )
63                                 results.add(f);
64                     }
65                 }
66             }
67         private static void testFindMDs()
68         {
69                 final String BASEURI = "/help";
70                 final int MAX_DEPTH = 10;
71                 String uri="/help/";//req.getRequestURI();
72                 Path basePath=new File("/home/herbert/odl/distribution-karaf-0.5.3-Boron-SR3").toPath();
73                 uri=uri.substring(BASEURI.length());
74                 try
75                 {
76                         ArrayList<File> mdfiles=new ArrayList<>();
77                         walk(mdfiles,basePath.toAbsolutePath().toString());
78                         JSONArray a=new JSONArray();
79                         Iterator<File> it=mdfiles.iterator();
80                         while(it.hasNext())
81                                 a.put(it.next().toString().substring(basePath.toAbsolutePath().toString().length()));
82                         //resp.getOutputStream().println(a.toString());
83                         System.out.println(a.toString());
84
85                 }
86                 catch(Exception err)
87                 {
88                         err.printStackTrace();
89                 }
90         }
91         private static void folderTests()
92         {
93                 Path basePath=new File("/home/herbert/odl/distribution-karaf-0.5.3-Boron-SR3").toPath();
94                 Path pHelp=basePath.resolve("help/");
95                 String uri="0.1.0-SNAPSHOT/OpenDaylight";
96                 System.out.println("phelp="+pHelp.toString());
97                 Path pUri=pHelp.resolve(uri);
98                 System.out.println("puri="+pUri.toString());
99         }
100
101         public static void main(String[] args)
102         {
103                 //testURI();
104                 //testMSDBDataParser();
105                 //testFindMDs();
106         }
107
108         private static String baseUrl(long id) {
109                 return "http://test.tld:7021";
110         }
111 }