fix db version conflict issue 76/105476/1
authorMichael Dürre <michael.duerre@highstreet-technologies.com>
Wed, 8 Apr 2020 06:17:02 +0000 (08:17 +0200)
committerMichael Dürre <michael.duerre@highstreet-technologies.com>
Wed, 8 Apr 2020 06:17:16 +0000 (08:17 +0200)
merged write and refresh request together

Issue-ID: SDNC-1146
Signed-off-by: Michael Dürre <michael.duerre@highstreet-technologies.com>
Change-Id: I6d65f372ee27a84c9b55371cefd1d7ecb9fac9fb

12 files changed:
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/BaseRequest.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/CreateIndexRequest.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteByQueryRequest.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteRequest.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/IndexRequest.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateByQueryRequest.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateRequest.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/BaseResponse.java
sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteResponse.java
sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java
sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java

index e7b15bd..9a369ea 100644 (file)
@@ -130,7 +130,7 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
     public @Nullable String doWriteRaw(String indexName,String dataTypeName, @Nullable String esId, String json) {
                   
         IndexResponse response = null;
-        IndexRequest indexRequest = new IndexRequest(indexName,dataTypeName,esId);
+        IndexRequest indexRequest = new IndexRequest(indexName,dataTypeName,esId,this.doRefreshAfterWrite);
         indexRequest.source(json);
         try {
             response = this.index(indexRequest );
@@ -142,9 +142,9 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
             LOG.warn("Response null during write: {} {}", esId, json);
             return null;
         }
-               if(this.doRefreshAfterWrite) {
-                       this.doRefresh(dataTypeName);
-               }
+//             if(this.doRefreshAfterWrite) {
+//                     this.doRefresh(dataTypeName);
+//             }
                return response.getId();
     }
 
@@ -167,16 +167,16 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
 
     @Override
     public boolean doRemove(String dataTypeName, String esId) {
-        DeleteRequest deleteRequest = new DeleteRequest(dataTypeName,dataTypeName,esId);
+        DeleteRequest deleteRequest = new DeleteRequest(dataTypeName,dataTypeName,esId,this.doRefreshAfterWrite);
                DeleteResponse response = null;
                try {
                        response = this.delete(deleteRequest);
                } catch (IOException e) {
                        LOG.warn("Problem deleting from db: {}",e.getMessage());
                }
-               if(this.doRefreshAfterWrite) {
-                       this.doRefresh(dataTypeName);
-               }
+//             if(this.doRefreshAfterWrite) {
+//                     this.doRefresh(dataTypeName);
+//             }
         return response!=null?response.isDeleted():false;
     }
 
@@ -257,7 +257,7 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
                        return null;
                }
                boolean success = false;
-               UpdateRequest request = new UpdateRequest(dataTypeName, dataTypeName, esId);
+               UpdateRequest request = new UpdateRequest(dataTypeName, dataTypeName, esId,this.doRefreshAfterWrite);
                request.source(new JSONObject(json),onlyForInsert);
                try {
                        UpdateResponse response = this.update(request);
@@ -265,15 +265,15 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
                } catch (IOException e) {
                        LOG.warn("Problem updating {} with id {} and data {}: {}", dataTypeName, esId, json, e);
                }
-               if(this.doRefreshAfterWrite) {
-                       this.doRefresh(dataTypeName);
-               }
+//             if(this.doRefreshAfterWrite) {
+//                     this.doRefresh(dataTypeName);
+//             }
                return success ? esId : null;
        }
        @Override
        public boolean doUpdate(String dataTypeName, String json, QueryBuilder query) {
                boolean success = false;
-               UpdateByQueryRequest request = new UpdateByQueryRequest(dataTypeName, dataTypeName );
+               UpdateByQueryRequest request = new UpdateByQueryRequest(dataTypeName, dataTypeName ,this.doRefreshAfterWrite);
                request.source(new JSONObject(json),query);
                try {
                        UpdateByQueryResponse response = this.update(request);
@@ -281,9 +281,9 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
                } catch (IOException e) {
                        LOG.warn("Problem updating items in {} with query {} and data {}: {}", dataTypeName, query, json, e);
                }
-               if(this.doRefreshAfterWrite) {
-                       this.doRefresh(dataTypeName);
-               }
+//             if(this.doRefreshAfterWrite) {
+//                     this.doRefresh(dataTypeName);
+//             }
                return success;
        }
 
@@ -292,7 +292,7 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
        @Override
        public int doRemove(String dataTypeName, QueryBuilder query) {
                int del=0;
-               DeleteByQueryRequest request = new DeleteByQueryRequest(dataTypeName);
+               DeleteByQueryRequest request = new DeleteByQueryRequest(dataTypeName,this.doRefreshAfterWrite);
                request.source(query);
                try {
                        DeleteByQueryResponse response = this.deleteByQuery(request);
@@ -300,9 +300,9 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
                } catch (IOException e) {
                        LOG.warn("Problem delete in {} with query {}:{} ", dataTypeName, query.toJSON(), e);
                }
-               if(this.doRefreshAfterWrite) {
-                       this.doRefresh(dataTypeName);
-               }
+//             if(this.doRefreshAfterWrite) {
+//                     this.doRefresh(dataTypeName);
+//             }
                return del;
        }
        
index 061941b..e7261f3 100644 (file)
@@ -35,12 +35,31 @@ public abstract class BaseRequest {
 
        private static final Logger LOG = LoggerFactory.getLogger(BaseRequest.class);
 
+       public static final int DEFAULT_RETRIES = 1;
+
        protected final Request request;
        private String query;
+       private final boolean refresh;
+
        public BaseRequest(String method, String endpoint) {
-               LOG.debug("create request {} {}", method, endpoint);
-               this.request = new Request(method, endpoint);
-               query=null;
+               LOG.debug("create request {} {}" ,method, endpoint);
+               this.refresh = false;
+               this.request = new Request(method,  endpoint);
+               query = null;
+       }
+
+       public BaseRequest(String method, String endpoint, boolean refresh) {
+               LOG.debug("create request {} {} with refresh={}", method, endpoint, refresh);
+               this.refresh = refresh;
+               this.request = new Request(method, String.format("%s?refresh=%s", endpoint, String.valueOf(refresh)));
+               query = null;
+       }
+
+       public BaseRequest(String method, String endpoint, boolean refresh, int retries) {
+               LOG.debug("create request {} {} with refresh={}", method, endpoint, refresh);
+               this.refresh = refresh;
+               this.request = new Request(method, String.format("%s?refresh=%s&retry_on_conflict=%d", endpoint, String.valueOf(refresh),retries));
+               query = null;
        }
 
        public Request getInner() {
@@ -58,9 +77,11 @@ public abstract class BaseRequest {
                }
                return value;
        }
+
        @Override
        public String toString() {
-               return this.request.getMethod() + " "+this.request.getEndpoint()+ " : "+(this.query!=null?this.query:"no query");
+               return this.request.getMethod() + " " + this.request.getEndpoint() + " : "
+                               + (this.query != null ? this.query : "no query");
        }
 
        protected void setQuery(QueryBuilder query) {
@@ -72,8 +93,16 @@ public abstract class BaseRequest {
        }
 
        public void setQuery(String content) {
-               this.query=content;
-               LOG.trace("query={}",content);
+               this.query = content;
+               LOG.trace("query={}", content);
                this.request.setJsonEntity(this.query);
        }
+
+       protected String getQuery() {
+               return this.query;
+       }
+
+       protected boolean doRefresh() {
+               return this.refresh;
+       }
 }
index 934c0f6..6f06019 100644 (file)
@@ -47,9 +47,10 @@ public class CreateIndexRequest extends BaseRequest{
         super.setQuery(o);
     }
     @SuppressWarnings("hiding")
-    public void mappings(JSONObject mappings) {
+    public CreateIndexRequest mappings(JSONObject mappings) {
         this.mappings=mappings;
         this.setRequest();
+        return this;
     }
 
     public void settings(JSONObject settings) {
index ded69dd..15c876e 100644 (file)
@@ -26,11 +26,16 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
 public class DeleteByQueryRequest  extends BaseRequest {
 
        public DeleteByQueryRequest(String alias) {
-               super("POST",String.format("/%s/_delete_by_query",alias));
+               this(alias, false);
        }
 
-       public void source(QueryBuilder query) {
+       public DeleteByQueryRequest(String alias, boolean refresh) {
+               super("POST",String.format("/%s/_delete_by_query",alias), refresh);
+       }
+
+       public DeleteByQueryRequest source(QueryBuilder query) {
                this.setQuery(query);
+               return this;
        }
 
        
index 325b529..63202b1 100644 (file)
@@ -23,8 +23,22 @@ package org.onap.ccsdk.features.sdnr.wt.common.database.requests;
 
 public class DeleteRequest extends BaseRequest {
 
+       private final String alias;
+       private final String esId;
        public DeleteRequest(String alias,String dataType,String esId) {
-               super("DELETE",String.format("/%s/%s/%s",alias,dataType,BaseRequest.urlEncodeValue(esId)));
+               this(alias, dataType, esId, false);
+       }
+       public DeleteRequest(String alias,String dataType,String esId, boolean refresh) {
+               super("DELETE",String.format("/%s/%s/%s",alias,dataType,BaseRequest.urlEncodeValue(esId)), refresh);
+               this.alias = alias;
+               this.esId = esId;
+       }
+       protected String getAlias() {
+               return this.alias;
+       }
+
+       protected String getEsId() {
+               return this.esId;
        }
 
 }
index cf4aabb..a3ee578 100644 (file)
@@ -23,20 +23,39 @@ package org.onap.ccsdk.features.sdnr.wt.common.database.requests;
 
 import javax.annotation.Nullable;
 
-public class IndexRequest extends BaseRequest{
+public class IndexRequest extends BaseRequest {
+
+       private final String alias;
+       private final String esId;
 
        public IndexRequest(String alias, String dataType) {
-               this(alias,dataType,null);
+               this(alias, dataType, null);
+       }
+
+       public IndexRequest(String alias, String dataType, @Nullable String esId) {
+               super("POST", esId != null ? String.format("/%s/%s/%s", alias, dataType, BaseRequest.urlEncodeValue(esId))
+                               : String.format("/%s/%s", alias, dataType));
+               this.alias = alias;
+               this.esId = esId;
        }
 
-       public IndexRequest(String alias,String dataType, @Nullable String esId) {
-               super("POST",esId!=null?String.format("/%s/%s/%s",alias,dataType,BaseRequest.urlEncodeValue(esId)):String.format("/%s/%s",alias,dataType));
+       public IndexRequest(String alias, String dataType, @Nullable String esId, boolean refresh) {
+               super("POST", esId != null ? String.format("/%s/%s/%s", alias, dataType, BaseRequest.urlEncodeValue(esId))
+                               : String.format("/%s/%s", alias, dataType),refresh);
+               this.alias = alias;
+               this.esId = esId;
        }
 
        public void source(String content) {
                super.setQuery(content);
        }
 
+       protected String getAlias() {
+               return this.alias;
+       }
 
+       protected String getEsId() {
+               return this.esId;
+       }
 
 }
index 1eb6037..8bca04f 100644 (file)
@@ -29,9 +29,15 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
 public class UpdateByQueryRequest extends BaseRequest {
 
        private JSONObject params;
-
+       private final String alias;
+       
        public UpdateByQueryRequest(String alias, String dataType) {
-               super("POST", String.format("/%s/%s/_update_by_query", alias, dataType));
+               this(alias, dataType, false);
+       }
+
+       public UpdateByQueryRequest(String alias, String dataType, boolean refresh) {
+               super("POST", String.format("/%s/%s/_update_by_query", alias, dataType), refresh);
+               this.alias = alias;
                this.params = null;
        }
 
@@ -103,4 +109,8 @@ public class UpdateByQueryRequest extends BaseRequest {
 
        }
 
+       protected String getAlias() {
+               return this.alias;
+       }
+
 }
index 7445e15..16923b2 100644 (file)
@@ -33,12 +33,36 @@ public class UpdateRequest extends BaseRequest {
 
        private static final Logger LOG = LoggerFactory.getLogger(UpdateRequest.class);
        private JSONObject params;
+       private String alias;
+       private String esId;
+       private int retries;
 
        public UpdateRequest(String alias, String dataType, String esId) {
-               super("POST", String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId)));
+               this(alias, dataType, esId, BaseRequest.DEFAULT_RETRIES);
+       }
+       public UpdateRequest(String alias, String dataType, String esId, boolean refresh) {
+               this(alias, dataType, esId, BaseRequest.DEFAULT_RETRIES, refresh);
+       }
+       public UpdateRequest(String alias, String dataType, String esId, int retries) {
+               this(alias, dataType, esId, retries, false);
+       }
+
+       public UpdateRequest(String alias, String dataType, String esId, int retries, boolean refresh) {
+               this(String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId)), refresh);
+               this.alias = alias;
+               this.esId = esId;
+               this.retries = retries;
+       }
+
+       public UpdateRequest(String uri, boolean refresh) {
+               super("POST", uri, refresh,  BaseRequest.DEFAULT_RETRIES);
                this.params = null;
+               this.retries = 1;
+
        }
 
+       
+
        private UpdateRequest withParam(String key, JSONObject p) {
                if (this.params == null) {
                        this.params = new JSONObject();
@@ -54,16 +78,18 @@ public class UpdateRequest extends BaseRequest {
                this.params.put(key, p);
                return this;
        }
+
        public void source(JSONObject map) {
-               this.source(map,null);
+               this.source(map, null);
        }
+
        public void source(JSONObject map, List<String> onlyForInsert) {
                JSONObject outer = new JSONObject();
                JSONObject script = new JSONObject();
                script.put("lang", "painless");
-               script.put("source", this.createInline(map,onlyForInsert));
-               if(this.params!=null) {
-                       script.put("params",this.params);
+               script.put("source", this.createInline(map, onlyForInsert));
+               if (this.params != null) {
+                       script.put("params", this.params);
                }
                outer.put("script", script);
                outer.put("upsert", map);
@@ -72,16 +98,16 @@ public class UpdateRequest extends BaseRequest {
        }
 
        private String createInline(JSONObject map, List<String> onlyForInsert) {
-               if(onlyForInsert==null) {
+               if (onlyForInsert == null) {
                        onlyForInsert = new ArrayList<String>();
                }
-               String s = "",k="";
+               String s = "", k = "";
                Object value;
                String pkey;
                int i = 0;
                for (Object key : map.keySet()) {
-                       k=String.valueOf(key);
-                       if(onlyForInsert.contains(k)) {
+                       k = String.valueOf(key);
+                       if (onlyForInsert.contains(k)) {
                                continue;
                        }
                        value = map.get(k);
@@ -93,7 +119,7 @@ public class UpdateRequest extends BaseRequest {
                                        this.withParam(pkey, (JSONArray) value);
                                }
 
-                               s += String.format("ctx._source['%s']=%s;", key, "params."+pkey);
+                               s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
                        } else {
                                s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
                        }
@@ -113,4 +139,15 @@ public class UpdateRequest extends BaseRequest {
 
        }
 
+       protected String getAlias() {
+               return this.alias;
+       }
+
+       protected String getEsId() {
+               return this.esId;
+       }
+
+       protected int getRetries() {
+               return this.retries;
+       }
 }
index 2fe81bb..a3a1b16 100644 (file)
@@ -49,6 +49,10 @@ public class BaseResponse {
        }
 
        JSONObject getJson(Response response) {
+               if(response==null) {
+                       LOG.warn("unable to parse response. response is null.");
+                       return null;
+               }
                try {
                        String sresponse = EntityUtils.toString(response.getEntity());
                        LOG.debug("parsing response={}", sresponse);
index c7bba0e..d4dee04 100644 (file)
@@ -30,13 +30,15 @@ public class DeleteResponse extends BaseResponse {
 
        public DeleteResponse(Response response) {
                super(response);
-               int code = response.getStatusLine().getStatusCode();
-               if (code < 210) {
+               if (this.isResponseSucceeded()) {
 
                        JSONObject o = this.getJson(response);
                        if (o != null) {
                                this.isDeleted = "deleted".equals(o.getString("result"));
                        }
+                       else {
+                               this.isDeleted=false;
+                       }
                }
                else {
                        this.isDeleted=false;
index d04aac6..04259b9 100644 (file)
@@ -23,6 +23,8 @@ package org.onap.ccsdk.features.sdnr.wt.common.test;
 
 import static org.junit.Assert.*;
 
+import java.io.IOException;
+
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
@@ -32,50 +34,63 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder;
 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
+import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateIndexRequest;
+import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest;
+import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetIndexRequest;
 
 public class TestDbClient {
-       
+
        private static HtDatabaseClient dbClient;
        private static HostInfo[] hosts = new HostInfo[] { new HostInfo("localhost", Integer
                        .valueOf(System.getProperty("databaseport") != null ? System.getProperty("databaseport") : "49200")) };
 
        @BeforeClass
-       public static void init() {
+       public static void init() throws Exception {
 
                dbClient = new HtDatabaseClient(hosts);
                dbClient.waitForYellowStatus(20000);
 
        }
+
        @Test
        public void testCRUD() {
                final String IDX = "test23-knmoinsd";
                final String ID = "abcddd";
                final String JSON = "{\"data\":{\"inner\":\"more\"}}";
                final String JSON2 = "{\"data\":{\"inner\":\"more2\"}}";
+
+               try {
+                       if (!dbClient.indicesExists(new GetIndexRequest(IDX))) {
+                               dbClient.createIndex(new CreateIndexRequest(IDX));
+                       }
+               } catch (IOException e) {
+                       fail("unable to create index");
+               }
+               clearIndexData(IDX);
                //Create
-               String esId=dbClient.doWriteRaw(IDX, ID, JSON);
-               assertEquals("inserted id is wrong",ID,esId);
+               String esId = dbClient.doWriteRaw(IDX, ID, JSON);
+               assertEquals("inserted id is wrong", ID, esId);
                //Read
                SearchResult<SearchHit> result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
-               assertEquals("amount of results is wrong",1,result.getTotal());
-               assertEquals("data not valid", JSON,result.getHits().get(0).getSourceAsString());
+               assertEquals("amount of results is wrong", 1, result.getTotal());
+               assertEquals("data not valid", JSON, result.getHits().get(0).getSourceAsString());
                //Update
-               esId= dbClient.doUpdateOrCreate(IDX, ID, JSON2);
-               assertEquals("update response not successfull",ID,esId);
+               esId = dbClient.doUpdateOrCreate(IDX, ID, JSON2);
+               assertEquals("update response not successfull", ID, esId);
                //check that update with null fails
-               assertNull("update with id null should not be possible",dbClient.doUpdateOrCreate(IDX,null,JSON2));
+               assertNull("update with id null should not be possible", dbClient.doUpdateOrCreate(IDX, null, JSON2));
                //Verify update
-               result = dbClient.doReadByQueryJsonData( IDX, QueryBuilders.matchQuery("_id", ID));
-               assertEquals("amount of results is wrong",1,result.getTotal());
-               assertEquals("data not valid", JSON2,result.getHits().get(0).getSourceAsString());
+               result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
+               assertEquals("amount of results is wrong", 1, result.getTotal());
+               assertEquals("data not valid", JSON2, result.getHits().get(0).getSourceAsString());
                //test second read
                String resStr = dbClient.doReadJsonData(IDX, new IsEsObject() {
-                       
+
                        @Override
                        public void setEsId(String id) {
-                               
+
                        }
-                       
+
                        @Override
                        public String getEsId() {
                                return ID;
@@ -83,59 +98,79 @@ public class TestDbClient {
                });
                //test all read
                result = dbClient.doReadAllJsonData(IDX);
-               assertNotNull("all read not working",result);
-               
-               assertEquals("read works not as expected", JSON2,resStr);
+               assertNotNull("all read not working", result);
+
+               assertEquals("read works not as expected", JSON2, resStr);
                //Delete
-               boolean del=dbClient.doRemove(IDX, new IsEsObject() {
-                       
+               boolean del = dbClient.doRemove(IDX, new IsEsObject() {
+
                        @Override
                        public void setEsId(String id) {
-                       
+
                        }
+
                        @Override
                        public String getEsId() {
                                return ID;
                        }
                });
-               assertTrue("item not deleted",del);
+               assertTrue("item not deleted", del);
                //Verify
                result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
-               assertEquals("amount of results is wrong",0,result.getTotal());
-               
-               
+               assertEquals("amount of results is wrong", 0, result.getTotal());
+
+       }
+
+       /**
+        * @param iDX
+        */
+       private void clearIndexData(String idx) {
+               try {
+                       dbClient.deleteByQuery(new DeleteByQueryRequest(idx, true).source(QueryBuilders.matchAllQuery()));
+               } catch (IOException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
                
        }
+
        @Test
        public void testCRUD2() {
-               final String IDX = "test23-knmoinsd";
+               final String IDX = "test23-knmoins3d";
                final String ID = "abcddd";
                final String JSON = "{\"data\":{\"inner\":\"more\"}}";
                final String JSON2 = "{\"data\":{\"inner\":\"more2\"}}";
+               try {
+                       if (!dbClient.indicesExists(new GetIndexRequest(IDX))) {
+                               dbClient.createIndex(new CreateIndexRequest(IDX));
+                       }
+               } catch (IOException e) {
+                       fail("unable to create index");
+               }
                //Create
-               String esId=dbClient.doWriteRaw(IDX, ID, JSON);
-               assertEquals("inserted id is wrong",ID,esId);
+               String esId = dbClient.doWriteRaw(IDX, ID, JSON);
+               assertEquals("inserted id is wrong", ID, esId);
                //Read
                SearchResult<SearchHit> result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
-               assertEquals("amount of results is wrong",1,result.getTotal());
-               assertEquals("data not valid", JSON,result.getHits().get(0).getSourceAsString());
+               assertEquals("amount of results is wrong", 1, result.getTotal());
+               assertEquals("data not valid", JSON, result.getHits().get(0).getSourceAsString());
                QueryBuilder matchQuery = QueryBuilders.matchQuery("_id", ID);
                //Update
-               assertTrue("update response not successfull",dbClient.doUpdate(IDX, JSON2,matchQuery ));
+               assertTrue("update response not successfull", dbClient.doUpdate(IDX, JSON2, matchQuery));
                //check that update with null fails
-               assertNull("update with id null should not be possible",dbClient.doUpdateOrCreate(IDX,null,JSON2));
+               assertNull("update with id null should not be possible", dbClient.doUpdateOrCreate(IDX, null, JSON2));
                //Verify update
-               result = dbClient.doReadByQueryJsonData( IDX, QueryBuilders.matchQuery("_id", ID));
-               assertEquals("amount of results is wrong",1,result.getTotal());
-               assertEquals("data not valid", JSON2,result.getHits().get(0).getSourceAsString());
+               result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
+               assertEquals("amount of results is wrong", 1, result.getTotal());
+               assertEquals("data not valid", JSON2, result.getHits().get(0).getSourceAsString());
                //test second read
                String resStr = dbClient.doReadJsonData(IDX, new IsEsObject() {
-                       
+
                        @Override
                        public void setEsId(String id) {
-                               
+
                        }
-                       
+
                        @Override
                        public String getEsId() {
                                return ID;
@@ -143,18 +178,16 @@ public class TestDbClient {
                });
                //test all read
                result = dbClient.doReadAllJsonData(IDX);
-               assertNotNull("all read not working",result);
-               
-               assertEquals("read works not as expected", JSON2,resStr);
+               assertNotNull("all read not working", result);
+
+               assertEquals("read works not as expected", JSON2, resStr);
                //Delete
-               int del=dbClient.doRemove(IDX, matchQuery);
-               assertTrue("item not deleted",del>0);
+               int del = dbClient.doRemove(IDX, matchQuery);
+               assertTrue("item not deleted", del > 0);
                //Verify
                result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
-               assertEquals("amount of results is wrong",0,result.getTotal());
-               
-               
-               
+               assertEquals("amount of results is wrong", 0, result.getTotal());
+
        }
 
 }
index d876f66..2226c5e 100644 (file)
@@ -35,6 +35,7 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteAliasReque
 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest;
 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteIndexRequest;
 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteRequest;
+import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetIndexRequest;
 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetRequest;
 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.IndexRequest;
 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.NodeStatsRequest;
@@ -50,7 +51,6 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteByQueryRe
 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteIndexResponse;
 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteResponse;
 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.GetResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.IndexResponse;
 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ListIndicesResponse;
 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.NodeStatsResponse;
 import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse;
@@ -78,17 +78,19 @@ public class TestDbRequests {
                        .valueOf(System.getProperty("databaseport") != null ? System.getProperty("databaseport") : "49200")) };
 
        @BeforeClass
-       public static void init() {
+       public static void init() throws Exception {
 
                dbClient = new HtDatabaseClient(hosts);
 
        }
+
        @AfterClass
        public static void deinit() {
-               if(dbClient!=null) {
+               if (dbClient != null) {
                        dbClient.close();
                }
        }
+
        @Test
        public void testHealth() {
 
@@ -108,10 +110,11 @@ public class TestDbRequests {
        public void testCount() {
 
        }
+
        @Test
        public void testIndexAndAliasList() {
-               final String ALIAS="asdoi32kmasd";
-               final String IDX=ALIAS+"-v1";
+               final String ALIAS = "asdoi32kmsasd";
+               final String IDX = ALIAS + "-v1";
                CreateIndexRequest request = new CreateIndexRequest(IDX);
                CreateIndexResponse response = null;
                try {
@@ -121,7 +124,7 @@ public class TestDbRequests {
                }
                assertNotNull(response);
 
-               CreateAliasRequest request3 = new CreateAliasRequest(IDX,ALIAS);
+               CreateAliasRequest request3 = new CreateAliasRequest(IDX, ALIAS);
                CreateAliasResponse response3 = null;
                try {
                        response3 = dbClient.createAlias(request3);
@@ -130,18 +133,18 @@ public class TestDbRequests {
                }
                assertNotNull(response3);
                assertTrue(response3.isResponseSucceeded());
-               
+
                assertTrue("index not existing", dbClient.isExistsIndex(IDX));
-               ListIndicesResponse response2=null;
+               ListIndicesResponse response2 = null;
                try {
-                        response2 = dbClient.getIndices();
+                       response2 = dbClient.getIndices();
                } catch (ParseException | IOException e) {
                        fail(e.getMessage());
                }
                assertNotNull(response2);
                assertNotNull(response2.getEntries());
-               assertTrue(response2.getEntries().size()>0);
-               
+               assertTrue(response2.getEntries().size() > 0);
+
                DeleteIndexRequest request11 = new DeleteIndexRequest(IDX);
 
                DeleteIndexResponse response11 = null;
@@ -185,9 +188,16 @@ public class TestDbRequests {
 
        @Test
        public void testInsertAndDelete() {
-               final String IDX = "test23-knmoinsd";
+               final String IDX = "tesnt23-knmoinsd";
                final String ID = "abcddd";
                final String JSON = "{\"data\":{\"inner\":\"more\"}}";
+               try {
+                       if (!dbClient.indicesExists(new GetIndexRequest(IDX))) {
+                               dbClient.createIndex(new CreateIndexRequest(IDX).mappings(defaultMappings(IDX, false)));
+                       }
+               } catch (IOException e) {
+                       fail("unable to create index");
+               }
                this.insert(IDX, ID, JSON);
                // delete data
                DeleteRequest request2 = new DeleteRequest(IDX, IDX, ID);
@@ -217,11 +227,28 @@ public class TestDbRequests {
                this.deleteIndex(IDX);
        }
 
+       /**
+        * @param b
+        * @return
+        */
+       private JSONObject defaultMappings(String idx, boolean useStrict) {
+               String mapping = "{}";
+               return new JSONObject(String.format("{\"%s\":{%s\"properties\":%s}}", idx,
+                               useStrict ? "\"dynamic\": false," : "\"dynamic\": true,", mapping));
+       }
+
        @Test
        public void testInsertAndDeleteByQuery() {
-               final String IDX = "test34-knmoinsd";
+               final String IDX = "test534-knmoinsd";
                final String ID = "abcdddseae";
                final String JSON = "{\"data\":{\"inner\":\"more\"}}";
+               try {
+                       if (!dbClient.indicesExists(new GetIndexRequest(IDX))) {
+                               dbClient.createIndex(new CreateIndexRequest(IDX));
+                       }
+               } catch (IOException e) {
+                       fail("unable to create index");
+               }
                this.insert(IDX, ID, JSON);
 
                // delete data
@@ -258,17 +285,13 @@ public class TestDbRequests {
                // create data
                IndexRequest request = new IndexRequest(IDX, IDX, ID);
                request.source(JSON);
-               IndexResponse response = null;
-               try {
-                       response = dbClient.index(request);
-               } catch (IOException e) {
-                       fail(e.getMessage());
-               }
-               assertNotNull(response);
+               String responseId = null;
+               responseId = dbClient.doWriteRaw(IDX, ID, JSON);
+               assertNotNull(responseId);
                if (ID != null) {
-                       assertEquals("id not correct", ID, response.getId());
+                       assertEquals("id not correct", ID, responseId);
                } else {
-                       ID = response.getId();
+                       ID = responseId;
                }
                // do db refresh
                try {
@@ -277,26 +300,28 @@ public class TestDbRequests {
                        fail(e.getMessage());
                }
                // verify data exists
-               GetRequest request3 = new GetRequest(IDX, IDX, ID);
-               GetResponse response3 = null;
-               try {
-                       response3 = dbClient.get(request3);
-               } catch (IOException e1) {
-                       fail(e1.getMessage());
-               }
+               String response3 = null;
+               response3 = dbClient.doReadJsonData(IDX, ID);
                assertNotNull(response3);
-               JSONAssert.assertEquals("could not verify update", JSON, response3.getSourceAsBytesRef(), true);
+               JSONAssert.assertEquals("could not verify update", JSON, response3, true);
        }
 
        @Test
        public void testSearch() {
-               final String IDX = "test44-moinsd";
+               final String IDX = "testb44-moinsd";
                final String ID = "abe";
                final String JSON = "{\"data\":{\"inner\":\"more\"}}";
                final String ID2 = "abe2";
                final String JSON2 = "{\"data\":{\"inner\":\"more2\"}}";
                final String ID3 = "abe3";
                final String JSON3 = "{\"data\":{\"inner\":\"more3\"}}";
+               try {
+                       if (!dbClient.indicesExists(new GetIndexRequest(IDX))) {
+                               dbClient.createIndex(new CreateIndexRequest(IDX));
+                       }
+               } catch (IOException e) {
+                       fail("unable to create index");
+               }
                this.insert(IDX, ID, JSON);
                this.insert(IDX, ID2, JSON2);
                this.insert(IDX, ID3, JSON3);
@@ -310,18 +335,24 @@ public class TestDbRequests {
                }
                assertNotNull(response);
                assertEquals("not all items found", 3, response.getHits().length);
-               assertEquals("incorrect index",IDX,response.getHits()[0].getIndex());
-               assertEquals("incorrect type",IDX,response.getHits()[0].getType());
+               assertEquals("incorrect index", IDX, response.getHits()[0].getIndex());
+               assertEquals("incorrect type", IDX, response.getHits()[0].getType());
                this.deleteIndex(IDX);
        }
 
        @Test
        public void testUpdate() {
-               final String IDX = "test4534-moinsd";
+               final String IDX = "test45134-moinsd";
                final String ID = "assbe";
                final String JSON = "{\"data\":{\"inner\":\"more\"}}";
                final String JSON2 = "{\"data\":{\"inner\":\"more2\"},\"data2\":\"value2\",\"data3\":true}";
-
+               try {
+                       if (!dbClient.indicesExists(new GetIndexRequest(IDX))) {
+                               dbClient.createIndex(new CreateIndexRequest(IDX));
+                       }
+               } catch (IOException e) {
+                       fail("unable to create index");
+               }
                this.insert(IDX, ID, JSON);
                UpdateRequest request = new UpdateRequest(IDX, IDX, ID);
                UpdateResponse response = null;
@@ -354,11 +385,17 @@ public class TestDbRequests {
 
        @Test
        public void testUpdateByQuery() {
-               final String IDX = "test224534-moinsd";
+               final String IDX = "test224534k-moinsd";
                final String ID = "asssabe";
                final String JSON = "{\"data\":{\"inner\":\"more\"}}";
                final String JSON2 = "{\"data\":{\"inner\":\"more2\"},\"data2\":\"value2\",\"data3\":true}";
-
+               try {
+                       if (!dbClient.indicesExists(new GetIndexRequest(IDX))) {
+                               dbClient.createIndex(new CreateIndexRequest(IDX));
+                       }
+               } catch (IOException e) {
+                       fail("unable to create index");
+               }
                this.insert(IDX, ID, JSON);
                UpdateByQueryRequest request = new UpdateByQueryRequest(IDX, IDX);
                UpdateByQueryResponse response = null;
@@ -397,18 +434,21 @@ public class TestDbRequests {
                final String JSON3 = "{ \"node-id\":\"sim3\",\"severity\":\"minor\"}";
                final String JSON4 = "{ \"node-id\":\"sim4\",\"severity\":\"warning\"}";
                final String JSON5 = "{ \"node-id\":\"sim5\",\"severity\":\"major\"}";
-               final String MAPPINGS = "{\""+IDX+"\":{\"properties\":{\"node-id\": {\"type\": \"keyword\"},\"severity\": {\"type\": \"keyword\"}}}}";
-               //create index with mapping keyword
-               CreateIndexRequest irequest = new CreateIndexRequest(IDX);
-               irequest.mappings(new JSONObject(MAPPINGS));
+               final String MAPPINGS = String.format("{\"" + IDX + "\":{\"properties\":%s}}",
+                               "{\"node-id\":{\"type\": \"keyword\"},\"severity\": {\"type\": \"keyword\"}}");
+               // create index with mapping keyword
                CreateIndexResponse iresponse = null;
                try {
-                       iresponse = dbClient.createIndex(irequest);
+                       if (!dbClient.isExistsIndex(IDX)) {
+                               iresponse = dbClient.createIndex(new CreateIndexRequest(IDX).mappings(new JSONObject(MAPPINGS)));
+                               assertNotNull(iresponse);
+                               assertTrue(iresponse.isAcknowledged());
+                       }
                } catch (IOException e1) {
-                       fail("unable to create index: "+e1.getMessage());
+                       this.deleteIndex(IDX);
+                       fail("unable to create index: " + e1.getMessage());
                }
-               assertNotNull(iresponse);
-               assertTrue(iresponse.isAcknowledged());
+
                // fill index
                this.insert(IDX, null, JSON);
                this.insert(IDX, null, JSON2);
@@ -436,21 +476,21 @@ public class TestDbRequests {
 
                List<String> items1 = Arrays.asList(response.getAggregations("severity").getKeysAsPagedStringList(2, 0));
                List<String> items2 = Arrays.asList(response.getAggregations("severity").getKeysAsPagedStringList(2, 2));
-               assertEquals("pagination does not work", 2,items1.size());
-               assertEquals("pagination does not work", 2,items2.size());
-               for(String s:items1) {
-                       assertFalse("pagination overlap is not allowed",items2.contains(s));
+               assertEquals("pagination does not work", 2, items1.size());
+               assertEquals("pagination does not work", 2, items2.size());
+               for (String s : items1) {
+                       assertFalse("pagination overlap is not allowed", items2.contains(s));
                }
-               for(String s:items2) {
-                       assertFalse("pagination overlap is not allowed",items1.contains(s));
+               for (String s : items2) {
+                       assertFalse("pagination overlap is not allowed", items1.contains(s));
                }
 
                this.deleteIndex(IDX);
        }
-       
+
        @Test
        public void testStatistics() {
-               NodeStatsResponse stats=null;
+               NodeStatsResponse stats = null;
                try {
                        stats = dbClient.stats(new NodeStatsRequest());
                } catch (IOException e) {
@@ -460,18 +500,18 @@ public class TestDbRequests {
                System.out.println(stats.getNodesInfo());
                System.out.println(stats.getNodeStatistics());
        }
-       
-       //@Test
+
+       // @Test
        public void testPreventAutoCreateIndex() {
-               final String IDX1="acidx1";
-               final String ID1="acid1";
-               final String IDX2="acidx2";
-               final String ID2="acid2";
-               final String OBJ="{\"test\":5}";
-               
-               ClusterSettingsResponse settingsResponse=null;
-               String esId=null;
-               //set setting to allow autocreate
+               final String IDX1 = "acidx1";
+               final String ID1 = "acid1";
+               final String IDX2 = "acidx2";
+               final String ID2 = "acid2";
+               final String OBJ = "{\"test\":5}";
+
+               ClusterSettingsResponse settingsResponse = null;
+               String esId = null;
+               // set setting to allow autocreate
                try {
                        settingsResponse = dbClient.setupClusterSettings(new ClusterSettingsRequest(true));
                } catch (IOException e) {
@@ -479,10 +519,10 @@ public class TestDbRequests {
                }
                assertNotNull(settingsResponse);
                assertTrue(settingsResponse.isAcknowledged());
-               //test if something new can be created
+               // test if something new can be created
                esId = dbClient.doWriteRaw(IDX1, IDX1, ID1, OBJ);
                assertEquals(ID1, esId);
-               //set setting to deny autocreate
+               // set setting to deny autocreate
                try {
                        settingsResponse = dbClient.setupClusterSettings(new ClusterSettingsRequest(false));
                } catch (IOException e) {
@@ -490,10 +530,10 @@ public class TestDbRequests {
                }
                assertNotNull(settingsResponse);
                assertTrue(settingsResponse.isAcknowledged());
-               //test if something new cannot be created
+               // test if something new cannot be created
                esId = dbClient.doWriteRaw(IDX2, IDX2, ID2, OBJ);
                assertNull(esId);
-               //set setting to allow autocreate
+               // set setting to allow autocreate
                try {
                        settingsResponse = dbClient.setupClusterSettings(new ClusterSettingsRequest(true));
                } catch (IOException e) {
@@ -501,22 +541,23 @@ public class TestDbRequests {
                }
                assertNotNull(settingsResponse);
                assertTrue(settingsResponse.isAcknowledged());
-               
+
        }
-       private void deleteAlias(String idx,String alias) {
+
+       private void deleteAlias(String idx, String alias) {
                try {
-                       dbClient.deleteAlias( new DeleteAliasRequest(idx,alias));
+                       dbClient.deleteAlias(new DeleteAliasRequest(idx, alias));
                } catch (IOException e) {
 
                }
        }
+
        private void deleteIndex(String idx) {
                try {
-                       dbClient.deleteIndex( new DeleteIndexRequest(idx));
+                       dbClient.deleteIndex(new DeleteIndexRequest(idx));
                } catch (IOException e) {
 
                }
        }
-       
 
 }