2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.test;
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;
47 public class TestTree {
49 private static String resourceDirectoryPath = "/" + TestTree.class.getSimpleName() + "/";
50 private static DatabaseDataProvider dbProvider;
51 private static HtDatabaseClient dbRawProvider;
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);
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);
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());
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));
88 public void testInventoryTree() throws IOException {
94 private void test1() throws IOException {
95 clearTestData(dbRawProvider);
96 fillTestData(dbRawProvider, "test1.json");
97 DataTreeProviderImpl provider = new DataTreeProviderImpl();
98 provider.setDatabaseClient(dbRawProvider);
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);
108 tree = provider.readInventoryTree(Arrays.asList("sim1"), "*");
109 this.assertSim1(new JSONObject(tree.toJSON()));
110 System.out.println(tree.toJSON());
113 private void test2() throws IOException {
114 clearTestData(dbRawProvider);
115 fillTestData(dbRawProvider, "test2.json");
116 DataTreeProviderImpl provider = new DataTreeProviderImpl();
117 provider.setDatabaseClient(dbRawProvider);
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);
128 tree = provider.readInventoryTree(Arrays.asList("sim1"), "*");
129 this.assertSim1(new JSONObject(tree.toJSON()));
130 System.out.println(tree.toJSON());
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"));
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);
173 public void testUriConversion1() {
174 EntityWithTree e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/sim1");
175 System.out.println(e);
178 private static String getFileContent(String filename) throws IOException {
179 return String.join("\n",
180 IoUtils.readAllLines(TestTree.class.getResourceAsStream(resourceDirectoryPath + filename)));