2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.test;
24 import static org.junit.Assert.*;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
29 import org.onap.ccsdk.features.sdnr.wt.common.database.IsEsObject;
30 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit;
31 import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
32 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
33 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
34 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
36 public class TestDbClient {
38 private static HtDatabaseClient dbClient;
39 private static HostInfo[] hosts = new HostInfo[] { new HostInfo("localhost", Integer
40 .valueOf(System.getProperty("databaseport") != null ? System.getProperty("databaseport") : "49200")) };
43 public static void init() {
45 dbClient = new HtDatabaseClient(hosts);
46 dbClient.waitForYellowStatus(20000);
50 public void testCRUD() {
51 final String IDX = "test23-knmoinsd";
52 final String ID = "abcddd";
53 final String JSON = "{\"data\":{\"inner\":\"more\"}}";
54 final String JSON2 = "{\"data\":{\"inner\":\"more2\"}}";
56 String esId=dbClient.doWriteRaw(IDX, ID, JSON);
57 assertEquals("inserted id is wrong",ID,esId);
59 SearchResult<SearchHit> result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
60 assertEquals("amount of results is wrong",1,result.getTotal());
61 assertEquals("data not valid", JSON,result.getHits().get(0).getSourceAsString());
63 esId= dbClient.doUpdateOrCreate(IDX, ID, JSON2);
64 assertEquals("update response not successfull",ID,esId);
65 //check that update with null fails
66 assertNull("update with id null should not be possible",dbClient.doUpdateOrCreate(IDX,null,JSON2));
68 result = dbClient.doReadByQueryJsonData( IDX, QueryBuilders.matchQuery("_id", ID));
69 assertEquals("amount of results is wrong",1,result.getTotal());
70 assertEquals("data not valid", JSON2,result.getHits().get(0).getSourceAsString());
72 String resStr = dbClient.doReadJsonData(IDX, new IsEsObject() {
75 public void setEsId(String id) {
80 public String getEsId() {
85 result = dbClient.doReadAllJsonData(IDX);
86 assertNotNull("all read not working",result);
88 assertEquals("read works not as expected", JSON2,resStr);
90 boolean del=dbClient.doRemove(IDX, new IsEsObject() {
93 public void setEsId(String id) {
97 public String getEsId() {
101 assertTrue("item not deleted",del);
103 result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
104 assertEquals("amount of results is wrong",0,result.getTotal());
110 public void testCRUD2() {
111 final String IDX = "test23-knmoinsd";
112 final String ID = "abcddd";
113 final String JSON = "{\"data\":{\"inner\":\"more\"}}";
114 final String JSON2 = "{\"data\":{\"inner\":\"more2\"}}";
116 String esId=dbClient.doWriteRaw(IDX, ID, JSON);
117 assertEquals("inserted id is wrong",ID,esId);
119 SearchResult<SearchHit> result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
120 assertEquals("amount of results is wrong",1,result.getTotal());
121 assertEquals("data not valid", JSON,result.getHits().get(0).getSourceAsString());
122 QueryBuilder matchQuery = QueryBuilders.matchQuery("_id", ID);
124 assertTrue("update response not successfull",dbClient.doUpdate(IDX, JSON2,matchQuery ));
125 //check that update with null fails
126 assertNull("update with id null should not be possible",dbClient.doUpdateOrCreate(IDX,null,JSON2));
128 result = dbClient.doReadByQueryJsonData( IDX, QueryBuilders.matchQuery("_id", ID));
129 assertEquals("amount of results is wrong",1,result.getTotal());
130 assertEquals("data not valid", JSON2,result.getHits().get(0).getSourceAsString());
132 String resStr = dbClient.doReadJsonData(IDX, new IsEsObject() {
135 public void setEsId(String id) {
140 public String getEsId() {
145 result = dbClient.doReadAllJsonData(IDX);
146 assertNotNull("all read not working",result);
148 assertEquals("read works not as expected", JSON2,resStr);
150 int del=dbClient.doRemove(IDX, matchQuery);
151 assertTrue("item not deleted",del>0);
153 result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
154 assertEquals("amount of results is wrong",0,result.getTotal());