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