29f35560e48b9b9560fe1b347eea3a42d44e495b
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 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  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.test;
23
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.concurrent.TimeUnit;
27 import org.apache.sshd.common.util.io.IoUtils;
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
33 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit;
34 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
35 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
36 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest;
37 import org.onap.ccsdk.features.sdnr.wt.common.test.JSONAssert;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet;
41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet.EntityWithTree;
42 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeObject;
43 import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.DataTreeProviderImpl;
44 import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
46
47 public class TestTree {
48
49     private static String resourceDirectoryPath = "/" + TestTree.class.getSimpleName() + "/";
50     private static DatabaseDataProvider dbProvider;
51     private static HtDatabaseClient dbRawProvider;
52
53     @BeforeClass
54     public static void init() throws Exception {
55         HostInfo[] hosts = HostInfoForTest.get();
56         dbProvider = new ElasticSearchDataProvider(hosts);
57         dbProvider.waitForYellowDatabaseStatus(30, TimeUnit.SECONDS);
58         dbRawProvider = HtDatabaseClient.getClient(hosts);
59
60
61     }
62
63     public static void clearTestData(HtDatabaseClient dbRawProvider) throws IOException {
64         DeleteByQueryRequest query = new DeleteByQueryRequest(Entity.Inventoryequipment.getName(), true);
65         query.setQuery(QueryBuilders.matchAllQuery().toJSON());
66         dbRawProvider.deleteByQuery(query);
67     }
68
69     public static void fillTestData(HtDatabaseClient dbRawProvider, String filename) throws IOException {
70         SearchHit[] entries = loadEntries(filename);
71         for (SearchHit entry : entries) {
72             dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), entry.getId(), entry.getSourceAsString());
73         }
74
75     }
76
77     public static SearchHit[] loadEntries(String filename) throws IOException {
78         String content = getFileContent(filename);
79         JSONArray a = new JSONArray(content);
80         SearchHit[] results = new SearchHit[a.length()];
81         for (int i = 0; i < a.length(); i++) {
82             results[i] = new SearchHit(a.getJSONObject(i));
83         }
84         return results;
85     }
86
87     @Test
88     public void testInventoryTree() throws IOException {
89         test1();
90         //test2();
91
92     }
93
94     private void test1() throws IOException {
95         clearTestData(dbRawProvider);
96         fillTestData(dbRawProvider, "test1.json");
97         DataTreeProviderImpl provider = new DataTreeProviderImpl();
98         provider.setDatabaseClient(dbRawProvider);
99
100
101         DataTreeObject tree = provider.readInventoryTree(null, null);
102         System.out.println(tree.toJSON());
103         JSONObject o = new JSONObject(tree.toJSON());
104         JSONAssert.assertContainsExactKeys(o, new String[]{"sim1","sim2"});
105         JSONObject children = o.getJSONObject("sim1").getJSONObject("children");
106         this.assertSim1(children);
107
108         tree = provider.readInventoryTree(Arrays.asList("sim1"), "*");
109         this.assertSim1(new JSONObject(tree.toJSON()));
110         System.out.println(tree.toJSON());
111     }
112
113     private void test2() throws IOException {
114         clearTestData(dbRawProvider);
115         fillTestData(dbRawProvider, "test2.json");
116         DataTreeProviderImpl provider = new DataTreeProviderImpl();
117         provider.setDatabaseClient(dbRawProvider);
118
119
120         DataTreeObject tree =
121                 provider.readInventoryTree(Arrays.asList("netconf_server_simulator"), "*");
122         System.out.println(tree.toJSON());
123         JSONObject o = new JSONObject(tree.toJSON());
124         JSONAssert.assertContainsOnlyKey(o, "sim1");
125         JSONObject children = o.getJSONObject("sim1").getJSONObject("children");
126         this.assertSim1(children);
127
128         tree = provider.readInventoryTree(Arrays.asList("sim1"), "*");
129         this.assertSim1(new JSONObject(tree.toJSON()));
130         System.out.println(tree.toJSON());
131     }
132
133     private void assertSim1(JSONObject sim1Children) {
134         JSONAssert.assertContainsExactKeys(sim1Children,
135                 new String[] {"sim1/ODU-1.56.0.0", "sim1/IDU-1.55.0.0", "sim1/IDU-1.65.0.0", "sim1/SHELF-1.1.0.0"});
136         JSONObject c1 = sim1Children.getJSONObject("sim1/ODU-1.56.0.0");
137         JSONObject c2 = sim1Children.getJSONObject("sim1/IDU-1.55.0.0");
138         JSONObject c3 = sim1Children.getJSONObject("sim1/IDU-1.65.0.0");
139         JSONObject c4 = sim1Children.getJSONObject("sim1/SHELF-1.1.0.0");
140         JSONAssert.assertContainsExactKeys(c1.getJSONObject("children"), new String[] {"sim1/a2.module-1.56.1.2"});
141         JSONAssert.assertContainsExactKeys(c2.getJSONObject("children"),
142                 new String[] {"sim1/a2.module-1.55.1.2", "sim1/CARD-1.55.1.4"});
143         JSONAssert.assertContainsExactKeys(c3.getJSONObject("children"),
144                 new String[] {"sim1/a2.module-1.65.1.2", "sim1/CARD-1.65.1.4"});
145         JSONAssert.assertContainsExactKeys(c4.getJSONObject("children"),
146                 new String[] {"sim1/CARD-1.1.1.0", "sim1/CARD-1.1.5.0", "sim1/CARD-1.1.7.0", "sim1/CARD-1.1.6.0",
147                         "sim1/CARD-1.1.9.0", "sim1/CARD-1.1.8.0"});
148         JSONObject c41 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.1.0");
149         JSONObject c42 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.5.0");
150         JSONObject c43 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.7.0");
151         JSONObject c44 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.6.0");
152         JSONObject c45 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.9.0");
153         JSONObject c46 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.8.0");
154         JSONAssert.assertContainsExactKeys(c41.getJSONObject("children"),
155                 new String[] {"sim1/a2.module-1.1.1.7", "sim1/a2.module-1.1.1.5", "sim1/a2.module-1.1.1.8"});
156         JSONAssert.assertContainsExactKeys(c42.getJSONObject("children"),
157                 new String[] {"sim1/a2.module-1.1.5.6", "sim1/a2.module-1.1.5.5"});
158         JSONAssert.assertContainsNoKeys(c43.getJSONObject("children"));
159         JSONAssert.assertContainsExactKeys(c44.getJSONObject("children"), new String[] {"sim1/a2.module-1.1.6.5"});
160         JSONAssert.assertContainsNoKeys(c45.getJSONObject("children"));
161         JSONAssert.assertContainsNoKeys(c46.getJSONObject("children"));
162     }
163
164     @Test
165     public void testUriConversion() {
166         EntityWithTree e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/sim1/sim1%2FODU");
167         System.out.println(e);
168         e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/");
169         System.out.println(e);
170     }
171
172     @Test
173     public void testUriConversion1() {
174         EntityWithTree e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/sim1");
175         System.out.println(e);
176     }
177
178     private static String getFileContent(String filename) throws IOException {
179         return String.join("\n",
180                 IoUtils.readAllLines(TestTree.class.getResourceAsStream(resourceDirectoryPath + filename)));
181     }
182 }