ce4044780f76839da41a0f733b7fc37e87aa5048
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 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 static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import java.io.IOException;
27 import org.apache.sshd.common.util.io.IoUtils;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
31 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
32 import org.onap.ccsdk.features.sdnr.wt.common.test.JSONAssert;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.HtUserdataManagerImpl;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest;
35
36 public class TestUserdata {
37
38     private static final String USERNAME = "admin132";
39     private static HtDatabaseClient dbRawProvider;
40     private static HtUserdataManagerImpl userDbProvider;
41
42     @BeforeClass
43     public static void init() throws Exception {
44
45         HostInfo[] hosts = HostInfoForTest.get();
46         dbRawProvider = HtDatabaseClient.getClient(hosts);
47         userDbProvider = new HtUserdataManagerImpl(dbRawProvider);
48     }
49
50     public static void trySleep(long ms) {
51         try {
52             Thread.sleep(ms);
53         } catch (Exception e) {
54             Thread.currentThread().interrupt();
55         }
56     }
57
58     @Test
59     public void test1() {
60         String fullContent = "";
61         boolean success = false;
62         try {
63             fullContent = getFileContent("/userdata/full.json");
64             success = userDbProvider.setUserdata(USERNAME, fullContent);
65         } catch (IOException e) {
66             e.printStackTrace();
67             fail(e.getMessage());
68         }
69         assertTrue("problem writing data into db",success);
70
71         trySleep(2000);
72
73         String userdata = userDbProvider.getUserdata(USERNAME);
74         JSONAssert.assertEquals(fullContent, userdata, false);
75         String networkMapContent = "";
76         String mergedContent = "";
77         try {
78             networkMapContent = getFileContent("/userdata/networkmap.json");
79             mergedContent = getFileContent("/userdata/merged.json");
80             userDbProvider.setUserdata(USERNAME, "networkMap", networkMapContent);
81         } catch (IOException e) {
82             e.printStackTrace();
83             fail(e.getMessage());
84         }
85
86         trySleep(2000);
87
88         userdata = userDbProvider.getUserdata(USERNAME);
89         JSONAssert.assertEquals(mergedContent, userdata, false);
90     }
91
92     private static String getFileContent(String filename) throws IOException {
93         return String.join("\n", IoUtils.readAllLines(TestUserdata.class.getResourceAsStream(filename)));
94     }
95 }