From 88df491a6c0f0493f6a508a98ac27c5aaffce49a Mon Sep 17 00:00:00 2001
From: Rustam_Lotsmanenko <Rustam_Lotsmanenko@epam.com>
Date: Fri, 23 Apr 2021 17:34:17 +0400
Subject: [PATCH] migrate int tests to v3 schema, added cases for hints in
 schema

---
 .../opengroup/osdu/common/RecordSteps.java    | 19 +++++++---
 .../osdu/common/SchemaServiceRecordSteps.java | 17 ++++++---
 .../org/opengroup/osdu/models/TestIndex.java  |  5 ++-
 .../org/opengroup/osdu/util/ElasticUtils.java | 18 +++++++++
 .../osdu/util/IndexerClientUtil.java          | 38 +++++++++++++++++++
 .../indexRecord-schema-service.feature        |  7 ++--
 .../r3-index_record_arrayofobjects.json       |  2 +-
 ...r3-index_record_arrayofobjects.schema.json |  2 +-
 .../step_definitions/index/record/Steps.java  | 18 ++++++---
 9 files changed, 104 insertions(+), 22 deletions(-)
 create mode 100644 testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/IndexerClientUtil.java

diff --git a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/common/RecordSteps.java b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/common/RecordSteps.java
index 322faff8c..da05637c1 100644
--- a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/common/RecordSteps.java
+++ b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/common/RecordSteps.java
@@ -151,20 +151,29 @@ public class RecordSteps extends TestsBase {
     public void i_should_get_the_documents_for_the_in_the_Elastic_Search_by_geoQuery (
             int expectedNumber, String index, Double topLatitude, Double topLongitude, Double bottomLatitude, Double bottomLongitude, String field) throws Throwable {
         index = generateActualName(index, timeStamp);
-        long numOfIndexedDocuments = createIndex(index);
-        long actualNumberOfRecords = elasticUtils.fetchRecordsByBoundingBoxQuery(index, field, topLatitude, topLongitude, bottomLatitude, bottomLongitude);
+        String actualName = generateActualName(index, timeStamp);
+        long numOfIndexedDocuments = createIndex(actualName);
+        long actualNumberOfRecords = elasticUtils.fetchRecordsByBoundingBoxQuery(actualName, field, topLatitude, topLongitude, bottomLatitude, bottomLongitude);
         assertEquals(expectedNumber, actualNumberOfRecords);
     }
 
     public void i_should_get_the_documents_for_the_in_the_Elastic_Search_by_nestedQuery(
         int expectedNumber, String index, String path, String firstNestedField, String firstNestedValue, String secondNestedField, String secondNestedValue)
         throws Exception {
-
-        long numOfIndexedDocuments = createIndex(index);
-        long actualNumberOfRecords = elasticUtils.fetchRecordsByNestedQuery(index, path, firstNestedField, firstNestedValue, secondNestedField, secondNestedValue);
+        String actualName = generateActualName(index, timeStamp);
+        long numOfIndexedDocuments = createIndex(actualName);
+        long actualNumberOfRecords = elasticUtils.fetchRecordsByNestedQuery(actualName, path, firstNestedField, firstNestedValue, secondNestedField, secondNestedValue);
         assertEquals(expectedNumber, actualNumberOfRecords);
     }
 
+    public void i_should_be_able_search_documents_for_the_by_flattened_inner_properties(int expectedCount, String index, String flattenedField,
+        String flattenedFieldValue) throws IOException, InterruptedException {
+        String actualName = generateActualName(index, timeStamp);
+        long numOfIndexedDocuments = createIndex(actualName);
+        long actualNumberOfRecords = elasticUtils.fetchRecordsWithFlattenedFieldsQuery(actualName, flattenedField, flattenedFieldValue);
+        assertEquals(expectedCount, actualNumberOfRecords);
+    }
+
     private long createIndex(String index) throws InterruptedException, IOException {
         long numOfIndexedDocuments = 0;
         int iterator;
diff --git a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/common/SchemaServiceRecordSteps.java b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/common/SchemaServiceRecordSteps.java
index 993c26b25..05acd914a 100644
--- a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/common/SchemaServiceRecordSteps.java
+++ b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/common/SchemaServiceRecordSteps.java
@@ -1,24 +1,29 @@
 package org.opengroup.osdu.common;
 
 import cucumber.api.DataTable;
+import java.util.List;
+import java.util.Map;
+import lombok.extern.java.Log;
 import org.opengroup.osdu.models.Setup;
 import org.opengroup.osdu.models.schema.PersistentSchemaTestIndex;
 import org.opengroup.osdu.util.ElasticUtils;
 import org.opengroup.osdu.util.HTTPClient;
+import org.opengroup.osdu.util.IndexerClientUtil;
 
-import java.util.List;
-import java.util.Map;
-
+@Log
 public class SchemaServiceRecordSteps extends RecordSteps {
 
+    private IndexerClientUtil indexerClient;
+
     public SchemaServiceRecordSteps(HTTPClient httpClient, ElasticUtils elasticUtils) {
         super(httpClient, elasticUtils);
+        indexerClient = new IndexerClientUtil(this.httpClient);
     }
 
     public void the_schema_is_created_with_the_following_kind(DataTable dataTable) {
         List<Setup> inputList = dataTable.asList(Setup.class);
         inputList.forEach(this::createSchema);
-        inputList.forEach(s -> deleteIndex(generateActualNameWithoutTs(s.getIndex())));
+        inputList.forEach(s -> deleteIndex(generateActualNameWithoutTs(s.getKind())));
         super.addShutDownHook();
     }
 
@@ -33,8 +38,8 @@ public class SchemaServiceRecordSteps extends RecordSteps {
         super.getInputIndexMap().put(testIndex.getKind(), testIndex);
     }
 
-    private void deleteIndex(String index) {
-        this.elasticUtils.deleteIndex(index);
+    private void deleteIndex(String kind) {
+        indexerClient.deleteIndex(kind);
     }
 
     @Override
diff --git a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/models/TestIndex.java b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/models/TestIndex.java
index d10712707..0a5725afa 100644
--- a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/models/TestIndex.java
+++ b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/models/TestIndex.java
@@ -19,6 +19,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.logging.Logger;
+import org.opengroup.osdu.util.IndexerClientUtil;
 
 import static org.junit.Assert.assertEquals;
 import static org.opengroup.osdu.util.Config.*;
@@ -38,6 +39,7 @@ public class TestIndex {
     private HTTPClient httpClient;
     private Map<String, String> headers;
     private ElasticUtils elasticUtils;
+    protected IndexerClientUtil indexerClient;
     private Gson gson = new Gson();
 
     public TestIndex(ElasticUtils elasticUtils){
@@ -47,6 +49,7 @@ public class TestIndex {
     public void setHttpClient(HTTPClient httpClient) {
         this.httpClient = httpClient;
         headers = httpClient.getCommonHeader();
+        indexerClient = new IndexerClientUtil(this.httpClient);
     }
 
     public void setupIndex() {
@@ -73,7 +76,7 @@ public class TestIndex {
     }
 
     public void cleanupIndex() {
-        this.elasticUtils.deleteIndex(index);
+        this.indexerClient.deleteIndex(kind);
     }
 
     private String getRecordFile() {
diff --git a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/ElasticUtils.java b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/ElasticUtils.java
index d42a4fd0f..668d1888c 100644
--- a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/ElasticUtils.java
+++ b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/ElasticUtils.java
@@ -292,6 +292,23 @@ public class ElasticUtils {
         }
     }
 
+
+    public long fetchRecordsWithFlattenedFieldsQuery(String index, String flattenedField, String flattenedFieldValue) throws IOException {
+        try (RestHighLevelClient client = this.createClient(username, password, host)) {
+            SearchRequest searchRequest = new SearchRequest(index);
+
+            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
+            searchSourceBuilder.query(boolQuery().must(matchQuery(flattenedField,flattenedFieldValue)));
+            searchRequest.source(searchSourceBuilder);
+
+            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
+            return searchResponse.getHits().getTotalHits().value;
+        } catch (ElasticsearchStatusException e) {
+            log.log(Level.INFO, String.format("Elastic search threw exception: %s", e.getMessage()));
+            return -1;
+        }
+    }
+
     public Map<String, MappingMetadata> getMapping(String index) throws IOException {
         try (RestHighLevelClient client = this.createClient(username, password, host)) {
             GetMappingsRequest request = new GetMappingsRequest();
@@ -433,4 +450,5 @@ public class ElasticUtils {
         }
         return false;
     }
+
 }
\ No newline at end of file
diff --git a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/IndexerClientUtil.java b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/IndexerClientUtil.java
new file mode 100644
index 000000000..33771a92b
--- /dev/null
+++ b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/IndexerClientUtil.java
@@ -0,0 +1,38 @@
+package org.opengroup.osdu.util;
+
+import static org.opengroup.osdu.util.Config.getDataPartitionIdTenant1;
+import static org.opengroup.osdu.util.Config.getIndexerBaseURL;
+
+import com.google.gson.Gson;
+import com.sun.jersey.api.client.ClientResponse;
+import java.util.Map;
+import javax.ws.rs.HttpMethod;
+import lombok.extern.java.Log;
+import org.opengroup.osdu.core.common.model.search.RecordChangedMessages;
+
+@Log
+public class IndexerClientUtil {
+
+    private final String purgeMessage = "{\"data\":\"[{\\\"kind\\\":\\\"%s\\\",\\\"op\\\":\\\"purge_schema\\\"}]\",\"attributes\":{\"account-id\":\"%s\"}}";
+
+    private final HTTPClient httpClient;
+    private Map<String, String> headers;
+
+    public IndexerClientUtil(HTTPClient httpClient) {
+        this.httpClient = httpClient;
+        headers = httpClient.getCommonHeader();
+    }
+
+    public void deleteIndex(String kind) {
+        String url = getIndexerBaseURL() + "index-cleanup";
+        log.info("URL: " + url);
+        ClientResponse response = httpClient.send(HttpMethod.POST, url, convertMessageIntoJson(kind), headers, httpClient.getAccessToken());
+        log.info(response.toString());
+    }
+
+    private String convertMessageIntoJson(String kind) {
+        RecordChangedMessages
+            recordChangedMessages = (new Gson()).fromJson(String.format(purgeMessage, kind, getDataPartitionIdTenant1()), RecordChangedMessages.class);
+        return new Gson().toJson(recordChangedMessages);
+    }
+}
diff --git a/testing/indexer-test-core/src/main/resources/features/indexrecord/indexRecord-schema-service.feature b/testing/indexer-test-core/src/main/resources/features/indexrecord/indexRecord-schema-service.feature
index 625303140..2991c9691 100644
--- a/testing/indexer-test-core/src/main/resources/features/indexrecord/indexRecord-schema-service.feature
+++ b/testing/indexer-test-core/src/main/resources/features/indexrecord/indexRecord-schema-service.feature
@@ -8,7 +8,7 @@ Feature: Indexing of the documents
       | tenant1:indexer:test-data--Integration:2.0.0    | tenant1-indexer-test-data--integration-2.0.0    | index_records_2                |
       | tenant1:indexer:test-data--Integration:3.0.0    | tenant1-indexer-test-data--integration-3.0.0    | index_records_3                |
       | tenant1:wks:master-data--Wellbore:2.0.3         | tenant1-wks-master-data--wellbore-2.0.3         | r3-index_record_wks_master     |
-      | tenant1:wks:ArraysOfObjectsTestCollection:1.0.0 | tenant1-wks-arraysofobjectstestcollection-1.0.0 | r3-index_record_arrayofobjects |
+      | tenant1:wks:ArraysOfObjectsTestCollection:4.0.0 | tenant1-wks-arraysofobjectstestcollection-4.0.0 | r3-index_record_arrayofobjects |
 
   Scenario Outline: Ingest the record and Index in the Elastic Search
     When I ingest records with the <recordFile> with <acl> for a given <kind>
@@ -47,7 +47,8 @@ Feature: Indexing of the documents
   Scenario Outline: Ingest the r3-record with arrays of objects and hints in schema and Index in the Elastic Search
     When I ingest records with the <recordFile> with <acl> for a given <kind>
     Then I should be able search <number> documents for the <index> by nested <path> and properties (<first_nested_field>, <first_nested_value>) and  (<second_nested_field>, <second_nested_value>)
+    Then I should be able search <number> documents for the <index> by flattened inner properties (<flattened_inner_field>, <flattened_inner_value>)
 
     Examples:
-      | kind                                              | recordFile                       | number | index                                             | acl                            | path              | first_nested_field           | first_nested_value | second_nested_field          | second_nested_value |
-      | "tenant1:wks:ArraysOfObjectsTestCollection:1.0.0" | "r3-index_record_arrayofobjects" | 1      | "tenant1-wks-arraysofobjectstestcollection-1.0.0" | "data.default.viewers@tenant1" | "data.NestedTest" | "data.NestedTest.NumberTest" | 12345              | "data.NestedTest.StringTest" | "test string"       |
\ No newline at end of file
+      | kind                                              | recordFile                       | number | index                                             | acl                            | path              | first_nested_field           | first_nested_value | second_nested_field          | second_nested_value | flattened_inner_field           | flattened_inner_value |
+      | "tenant1:wks:ArraysOfObjectsTestCollection:4.0.0" | "r3-index_record_arrayofobjects" | 1      | "tenant1-wks-arraysofobjectstestcollection-4.0.0" | "data.default.viewers@tenant1" | "data.NestedTest" | "data.NestedTest.NumberTest" | 12345              | "data.NestedTest.StringTest" | "test string"       | "data.FlattenedTest.StringTest" | "test string"         |
\ No newline at end of file
diff --git a/testing/indexer-test-core/src/main/resources/testData/r3-index_record_arrayofobjects.json b/testing/indexer-test-core/src/main/resources/testData/r3-index_record_arrayofobjects.json
index 4a8201337..f37563c94 100644
--- a/testing/indexer-test-core/src/main/resources/testData/r3-index_record_arrayofobjects.json
+++ b/testing/indexer-test-core/src/main/resources/testData/r3-index_record_arrayofobjects.json
@@ -1,6 +1,6 @@
 [
     {
-        "id": "tenant1:<kindSubType>:testIngest70<timestamp>",
+        "id": "tenant1:<kindSubType>:testIngest70",
         "data": {
             "NestedTest": [
                 {
diff --git a/testing/indexer-test-core/src/main/resources/testData/r3-index_record_arrayofobjects.schema.json b/testing/indexer-test-core/src/main/resources/testData/r3-index_record_arrayofobjects.schema.json
index 780a82cd9..c68efaceb 100644
--- a/testing/indexer-test-core/src/main/resources/testData/r3-index_record_arrayofobjects.schema.json
+++ b/testing/indexer-test-core/src/main/resources/testData/r3-index_record_arrayofobjects.schema.json
@@ -4,7 +4,7 @@
             "authority": "tenant1",
             "source": "wks",
             "entityType": "ArraysOfObjectsTestCollection",
-            "schemaVersionMajor": 1,
+            "schemaVersionMajor": 4,
             "schemaVersionMinor": 0,
             "schemaVersionPatch": 0
         },
diff --git a/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/record/Steps.java b/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/record/Steps.java
index 1d435fd80..186a9fc4e 100644
--- a/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/record/Steps.java
+++ b/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/record/Steps.java
@@ -6,6 +6,7 @@ import cucumber.api.java.Before;
 import cucumber.api.java.en.Given;
 import cucumber.api.java.en.Then;
 import cucumber.api.java.en.When;
+import java.io.IOException;
 import lombok.extern.java.Log;
 import org.opengroup.osdu.common.SchemaServiceRecordSteps;
 import org.opengroup.osdu.util.ElasticUtils;
@@ -48,12 +49,13 @@ public class Steps extends SchemaServiceRecordSteps {
     public void iShouldGetTheNumberDocumentsForTheIndexInTheElasticSearchWithOutSkippedAttribute(int expectedCount, String index, String skippedAttributes) throws Throwable {
         super.iShouldGetTheNumberDocumentsForTheIndexInTheElasticSearchWithOutSkippedAttribute(expectedCount, index, skippedAttributes);
     }
+//TODO fix tags step
 
-    @Then("^I should be able to search (\\d+) record with index \"([^\"]*)\" by tag \"([^\"]*)\" and value \"([^\"]*)\"$")
-    public void iShouldBeAbleToSearchRecordByTagKeyAndTagValue(int expectedNumber, String index, String tagKey, String tagValue) throws Throwable {
-        super.iShouldBeAbleToSearchRecordByTagKeyAndTagValue(index, tagKey, tagValue, expectedNumber);
-    }
-
+//    @Then("^I should be able to search (\\d+) record with index \"([^\"]*)\" by tag \"([^\"]*)\" and value \"([^\"]*)\"$")
+//    public void iShouldBeAbleToSearchRecordByTagKeyAndTagValue(int expectedNumber, String index, String tagKey, String tagValue) throws Throwable {
+//        super.iShouldBeAbleToSearchRecordByTagKeyAndTagValue(index, tagKey, tagValue, expectedNumber);
+//    }
+//
     @Then("^I should be able search (\\d+) documents for the \"([^\"]*)\" by bounding box query with points \\((-?\\d+), (-?\\d+)\\) and  \\((-?\\d+), (-?\\d+)\\) on field \"([^\"]*)\"$")
     public void i_should_get_the_documents_for_the_in_the_Elastic_Search_by_geoQuery(
         int expectedCount, String index, Double topLatitude, Double topLongitude, Double bottomLatitude, Double bottomLongitude, String field) throws Throwable {
@@ -68,4 +70,10 @@ public class Steps extends SchemaServiceRecordSteps {
             secondNestedProperty, secondNestedValue);
     }
 
+    @Then("^I should be able search (\\d+) documents for the \"([^\"]*)\" by flattened inner properties \\(\"([^\"]*)\", \"([^\"]*)\"\\)$")
+    public void i_should_be_able_search_documents_for_the_by_flattened_inner_properties(int expectedCount, String index, String flattenedField, String flattenedFieldValue)
+        throws IOException, InterruptedException {
+        super.i_should_be_able_search_documents_for_the_by_flattened_inner_properties(expectedCount,index,flattenedField,flattenedFieldValue);
+
+    }
 }
\ No newline at end of file
-- 
GitLab