add common lib
[ccsdk/features.git] / sdnr / wt / common / src / test / java / org / onap / ccsdk / features / sdnr / wt / common / test / TestDbClient.java
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.test;
19
20 import static org.junit.Assert.*;
21
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
25 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit;
26 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
27 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
29
30 public class TestDbClient {
31         
32         private static HtDatabaseClient dbClient;
33         private static HostInfo[] hosts = new HostInfo[] { new HostInfo("localhost", Integer
34                         .valueOf(System.getProperty("databaseport") != null ? System.getProperty("databaseport") : "49200")) };
35
36         @BeforeClass
37         public static void init() {
38
39                 dbClient = new HtDatabaseClient(hosts);
40                 dbClient.waitForYellowStatus(20000);
41
42         }
43         @Test
44         public void testCRUD() {
45                 final String IDX = "test23-knmoinsd";
46                 final String ID = "abcddd";
47                 final String JSON = "{\"data\":{\"inner\":\"more\"}}";
48                 final String JSON2 = "{\"data\":{\"inner\":\"more2\"}}";
49                 //Create
50                 String esId=dbClient.doWriteRaw(IDX, ID, JSON);
51                 assertEquals("inserted id is wrong",ID,esId);
52                 //Read
53                 SearchResult<SearchHit> result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
54                 assertEquals("amount of results is wrong",1,result.getTotal());
55                 assertEquals("data not valid", JSON,result.getHits().get(0).getSourceAsString());
56                 //Update
57                 esId= dbClient.doUpdateOrCreate(IDX, ID, JSON2);
58                 assertEquals("update response not successfull",ID,esId);
59                 //Verify
60                 result = dbClient.doReadByQueryJsonData( IDX, QueryBuilders.matchQuery("_id", ID));
61                 assertEquals("amount of results is wrong",1,result.getTotal());
62                 assertEquals("data not valid", JSON2,result.getHits().get(0).getSourceAsString());
63                 //Delete
64                 boolean del=dbClient.doRemove(IDX, ID);
65                 assertTrue("item not deleted",del);
66                 //Verify
67                 result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
68                 assertEquals("amount of results is wrong",0,result.getTotal());
69         }
70
71 }