b7689ed4962e1ebdbdc2f6d665c322af2957dc63
[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.database.elasticsearch.impl.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.impl.DataTreeProviderImpl;
42 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DatabaseDataProvider;
43 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.DataTreeObject;
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;
46
47 public class TestTree {
48
49     private static String resourceDirectoryPath = "/" + TestTree.class.getSimpleName() + "/";
50     private static DatabaseDataProvider dbProvider;
51     private static HtDatabaseClient dbRawProvider;
52
53     @BeforeClass
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);
59     }
60
61     public static void clearTestData(HtDatabaseClient dbRawProvider) throws IOException {
62         DeleteByQueryRequest query = new DeleteByQueryRequest(Entity.Inventoryequipment.getName(), true);
63         query.setQuery(QueryBuilders.matchAllQuery().toJSON());
64         dbRawProvider.deleteByQuery(query);
65     }
66
67     public static void fillTestData(HtDatabaseClient dbRawProvider, String filename) throws IOException {
68         SearchHit[] entries = loadEntries(filename);
69         for (SearchHit entry : entries) {
70             dbRawProvider.doWriteRaw(Entity.Inventoryequipment.getName(), entry.getId(), entry.getSourceAsString());
71         }
72
73     }
74
75     public static SearchHit[] loadEntries(String filename) throws IOException {
76         String content = getFileContent(filename);
77         JSONArray a = new JSONArray(content);
78         SearchHit[] results = new SearchHit[a.length()];
79         for (int i = 0; i < a.length(); i++) {
80             results[i] = new SearchHit(a.getJSONObject(i));
81         }
82         return results;
83     }
84
85     @Test
86     public void testInventoryTree1() throws IOException {
87         clearTestData(dbRawProvider);
88         fillTestData(dbRawProvider, "test1.json");
89         DataTreeProviderImpl provider = new DataTreeProviderImpl(dbRawProvider);
90
91
92         DataTreeObject tree = provider.readInventoryTree(null, null);
93         JSONObject o = new JSONObject(tree.toJSON());
94         System.out.println("Tree1.1: "+o);
95         JSONAssert.assertContainsExactKeys(o, new String[]{"sim1","sim2"});
96         JSONObject children = o.getJSONObject("sim1").getJSONObject("children");
97         this.assertSim1(children);
98
99         tree = provider.readInventoryTree(Arrays.asList("sim1"), "*");
100         this.assertSim1(new JSONObject(tree.toJSON()));
101         System.out.println("Tree1.2: "+tree.toJSON());
102     }
103
104
105     private void assertSim1(JSONObject sim1Children) {
106         JSONAssert.assertContainsExactKeys(sim1Children,
107                 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"),
114                 new String[] {"sim1/a2.module-1.55.1.2", "sim1/CARD-1.55.1.4"});
115         JSONAssert.assertContainsExactKeys(c3.getJSONObject("children"),
116                 new String[] {"sim1/a2.module-1.65.1.2", "sim1/CARD-1.65.1.4"});
117         JSONAssert.assertContainsExactKeys(c4.getJSONObject("children"),
118                 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",
119                         "sim1/CARD-1.1.9.0", "sim1/CARD-1.1.8.0"});
120         JSONObject c41 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.1.0");
121         JSONObject c42 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.5.0");
122         JSONObject c43 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.7.0");
123         JSONObject c44 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.6.0");
124         JSONObject c45 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.9.0");
125         JSONObject c46 = c4.getJSONObject("children").getJSONObject("sim1/CARD-1.1.8.0");
126         JSONAssert.assertContainsExactKeys(c41.getJSONObject("children"),
127                 new String[] {"sim1/a2.module-1.1.1.7", "sim1/a2.module-1.1.1.5", "sim1/a2.module-1.1.1.8"});
128         JSONAssert.assertContainsExactKeys(c42.getJSONObject("children"),
129                 new String[] {"sim1/a2.module-1.1.5.6", "sim1/a2.module-1.1.5.5"});
130         JSONAssert.assertContainsNoKeys(c43.getJSONObject("children"));
131         JSONAssert.assertContainsExactKeys(c44.getJSONObject("children"), new String[] {"sim1/a2.module-1.1.6.5"});
132         JSONAssert.assertContainsNoKeys(c45.getJSONObject("children"));
133         JSONAssert.assertContainsNoKeys(c46.getJSONObject("children"));
134     }
135
136     @Test
137     public void testUriConversion() {
138         EntityWithTree e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/sim1/sim1%2FODU");
139         System.out.println(e);
140         e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/");
141         System.out.println(e);
142     }
143
144     @Test
145     public void testUriConversion1() {
146         EntityWithTree e = DataTreeHttpServlet.getEntity("/tree/read-inventoryequipment-tree/sim1");
147         System.out.println(e);
148     }
149
150     private static String getFileContent(String filename) throws IOException {
151         return String.join("\n",
152                 IoUtils.readAllLines(TestTree.class.getResourceAsStream(resourceDirectoryPath + filename)));
153     }
154 }