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