53c72115d23c6fa34b571261e80344e132d30ca6
[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.JSONObject;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
32 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
33 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
34 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest;
35 import org.onap.ccsdk.features.sdnr.wt.common.test.JSONAssert;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.data.ElasticSearchDataProvider;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet.EntityWithTree;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeHttpServlet.FilterMode;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.DataTreeObject;
41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.DataTreeProviderImpl;
42 import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
44
45 public class TestTree {
46
47     private static String resourceDirectoryPath="/"+TestTree.class.getSimpleName()+"/";
48     private static ElasticSearchDataProvider dbProvider;
49     private static HtDatabaseClient dbRawProvider;
50
51     @BeforeClass
52     public static void init() throws Exception {
53         HostInfo[] hosts = HostInfoForTest.get();
54         dbProvider = new ElasticSearchDataProvider(hosts);
55         dbProvider.waitForYellowDatabaseStatus(30, TimeUnit.SECONDS);
56         dbRawProvider = HtDatabaseClient.getClient(hosts);
57         DeleteByQueryRequest query = new DeleteByQueryRequest(Entity.Inventoryequipment.getName(), true);
58         query.setQuery(QueryBuilders.matchAllQuery().toJSON());
59         dbRawProvider.deleteByQuery(query);
60         fillTestData();
61     }
62
63     private static void fillTestData() throws IOException {
64         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.1.5.5", getFileContent("1.json"));
65         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/CARD-1.1.7.0", getFileContent("2.json"));
66         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.55.1.2", getFileContent("3.json"));
67         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.65.1.2", getFileContent("4.json"));
68         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/SHELF-1.1.0.0", getFileContent("5.json"));
69         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.1.1.5", getFileContent("6.json"));
70         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.1.1.8", getFileContent("7.json"));
71         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.1.6.5", getFileContent("8.json"));
72         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/ODU-1.56.0.0", getFileContent("9.json"));
73         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.56.1.2", getFileContent("10.json"));
74         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/IDU-1.65.0.0", getFileContent("11.json"));
75         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/CARD-1.65.1.4", getFileContent("12.json"));
76         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/CARD-1.1.6.0", getFileContent("13.json"));
77         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/CARD-1.1.8.0", getFileContent("14.json"));
78         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/CARD-1.1.9.0", getFileContent("15.json"));
79         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/CARD-1.55.1.4", getFileContent("16.json"));
80         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.1.1.7", getFileContent("17.json"));
81         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/IDU-1.55.0.0", getFileContent("18.json"));
82         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/CARD-1.1.1.0", getFileContent("19.json"));
83         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/CARD-1.1.5.0", getFileContent("20.json"));
84         dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), "sim1/a2.module-1.1.5.6", getFileContent("21.json"));
85
86     }
87
88     @Test
89     public void testInventoryTree() throws IOException {
90         DataTreeProviderImpl provider = new DataTreeProviderImpl();
91         provider.setDatabaseClient(dbRawProvider);
92
93
94         DataTreeObject tree = provider.readInventoryTree(null, null, FilterMode.Lazy);
95         System.out.println(tree.toJSON());
96         JSONObject o = new JSONObject(tree.toJSON());
97         JSONAssert.assertContainsOnlyKey(o, "sim1");
98         JSONObject children = o.getJSONObject("sim1").getJSONObject("children");
99         this.assertSim1(children);
100
101         tree = provider.readInventoryTree(Arrays.asList("sim1"), "*", FilterMode.Lazy);
102         this.assertSim1(new JSONObject(tree.toJSON()));
103         System.out.println(tree.toJSON());
104
105     }
106
107     private void assertSim1(JSONObject sim1Children) {
108         JSONAssert.assertContainsExactKeys(sim1Children,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"});
109         JSONObject c1 = sim1Children.getJSONObject("sim1/ODU-1.56.0.0");
110         JSONObject c2 = sim1Children.getJSONObject("sim1/IDU-1.55.0.0");
111         JSONObject c3 = sim1Children.getJSONObject("sim1/IDU-1.65.0.0");
112         JSONObject c4 = sim1Children.getJSONObject("sim1/SHELF-1.1.0.0");
113         JSONAssert.assertContainsExactKeys(c1.getJSONObject("children"),new String[] {"sim1/a2.module-1.56.1.2"});
114         JSONAssert.assertContainsExactKeys(c2.getJSONObject("children"),new String[] {"sim1/a2.module-1.55.1.2","sim1/CARD-1.55.1.4"});
115         JSONAssert.assertContainsExactKeys(c3.getJSONObject("children"),new String[] {"sim1/a2.module-1.65.1.2","sim1/CARD-1.65.1.4"});
116         JSONAssert.assertContainsExactKeys(c4.getJSONObject("children"),new String[] {"sim1/CARD-1.1.1.0",
117                 "sim1/CARD-1.1.5.0", "sim1/CARD-1.1.7.0","sim1/CARD-1.1.6.0", "sim1/CARD-1.1.9.0","sim1/CARD-1.1.8.0"});
118         JSONObject c41 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.1.0");
119         JSONObject c42 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.5.0");
120         JSONObject c43 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.7.0");
121         JSONObject c44 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.6.0");
122         JSONObject c45 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.9.0");
123         JSONObject c46 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.8.0");
124         JSONAssert.assertContainsExactKeys(c41.getJSONObject("children"),new String[] {"sim1/a2.module-1.1.1.7","sim1/a2.module-1.1.1.5","sim1/a2.module-1.1.1.8"});
125         JSONAssert.assertContainsExactKeys(c42.getJSONObject("children"),new String[] {"sim1/a2.module-1.1.5.6","sim1/a2.module-1.1.5.5"});
126         JSONAssert.assertContainsNoKeys(c43.getJSONObject("children"));
127         JSONAssert.assertContainsExactKeys(c44.getJSONObject("children"),new String[] {"sim1/a2.module-1.1.6.5"});
128         JSONAssert.assertContainsNoKeys(c45.getJSONObject("children"));
129         JSONAssert.assertContainsNoKeys(c46.getJSONObject("children"));
130     }
131     @Test
132     public void testUriConversion() {
133         EntityWithTree e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/sim1/sim1%2FODU");
134         System.out.println(e);
135         e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/");
136         System.out.println(e);
137     }
138
139     private static String getFileContent(String filename) throws IOException {
140         return String.join("\n",IoUtils.readAllLines(TestTree.class.getResourceAsStream(resourceDirectoryPath+filename)));
141     }
142 }