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