From 82bd924396b495d983c8d42bee2767327275ffa3 Mon Sep 17 00:00:00 2001
From: Sherman Yang <SYang@slb.com>
Date: Fri, 1 Nov 2019 15:22:16 -0500
Subject: [PATCH] move test data to core lib and update test logic accordingly

---
 .../opengroup/osdu/common/RecordSteps.java    |  4 +--
 .../org/opengroup/osdu/models/TestIndex.java  | 15 +++-------
 .../org/opengroup/osdu/util/FileHandler.java  | 28 ++++++++++---------
 .../resources/features/delete/Delete.feature  |  0
 .../features/indexrecord/IndexRecord.feature  |  0
 .../features/kindschema/KindSchema.feature    |  0
 .../features/query/crosscluster/Query.feature |  0
 .../query/singlecluster/Query.feature         |  0
 .../crosscluster/QueryByCursor.feature        |  0
 .../singlecluster/QueryByCursor.feature       |  0
 .../features/smart/parser/SmartParser.feature |  0
 .../features/smart/search/Smart.feature       |  0
 .../updateindex/UpdateIndexMapping.feature    |  0
 .../resources/testData/index_records_1.json   |  0
 .../resources/testData/index_records_1.schema |  0
 .../resources/testData/index_records_2.json   |  0
 .../resources/testData/index_records_2.schema |  0
 .../main}/resources/testData/records_1.json   |  0
 .../resources/testData/records_1.mapping      |  0
 .../main}/resources/testData/records_2.json   |  0
 .../resources/testData/records_2.mapping      |  0
 .../main}/resources/testData/records_3.json   |  0
 .../resources/testData/records_3.mapping      |  0
 .../index/deleteschema/RunTest.java           |  2 +-
 .../index/getschema/RunTest.java              |  2 +-
 .../index/record/RunTest.java                 |  2 +-
 26 files changed, 23 insertions(+), 30 deletions(-)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/delete/Delete.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/indexrecord/IndexRecord.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/kindschema/KindSchema.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/query/crosscluster/Query.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/query/singlecluster/Query.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/querybycursor/crosscluster/QueryByCursor.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/querybycursor/singlecluster/QueryByCursor.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/smart/parser/SmartParser.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/smart/search/Smart.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/features/updateindex/UpdateIndexMapping.feature (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/index_records_1.json (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/index_records_1.schema (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/index_records_2.json (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/index_records_2.schema (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/records_1.json (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/records_1.mapping (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/records_2.json (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/records_2.mapping (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/records_3.json (100%)
 rename testing/{indexer-test-gcp/src/test => indexer-test-core/src/main}/resources/testData/records_3.mapping (100%)

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 8ac4bc2a3..9dbfc6060 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
@@ -89,10 +89,8 @@ public class RecordSteps extends TestsBase {
     public void i_ingest_records_with_the_for_a_given(String record, String dataGroup, String kind) {
 
         String actualKind = generateActualName(kind, timeStamp);
-        File resourcesDirectory = new File("src/test/resources");
         try {
-            String fileContent = FileHandler.readFile(
-                    String.format("%s/testData/%s", resourcesDirectory.getAbsolutePath(), String.format("%s.%s", record, "json")));
+            String fileContent = FileHandler.readFile(String.format("%s.%s", record, "json"));
             records = new Gson().fromJson(fileContent, new TypeToken<List<Map<String, Object>>>() {}.getType());
 
             for (Map<String, Object> testRecord : records) {
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 7eb53519b..00ac7b1c9 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
@@ -13,7 +13,6 @@ import org.opengroup.osdu.util.ElasticUtils;
 import org.opengroup.osdu.util.FileHandler;
 import org.opengroup.osdu.util.HTTPClient;
 
-import java.io.File;
 import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
@@ -84,10 +83,8 @@ public class TestIndex {
     }
 
     private List<Map<String, Object>> getRecordsFromTestFile() {
-        File resourcesDirectory = new File("src/test/resources");
-        try {
-            String fileContent = FileHandler.readFile(
-                    String.format("%s/testData/%s", resourcesDirectory.getAbsolutePath(), getRecordFile()));
+         try {
+            String fileContent = FileHandler.readFile(getRecordFile());
             List<Map<String, Object>> records = new Gson().fromJson(
                     fileContent, new TypeToken<List<Map<String,Object>>>() {}.getType());
 
@@ -105,10 +102,8 @@ public class TestIndex {
     }
 
     private String getIndexMappingFromJson() {
-        File resourcesDirectory = new File("src/test/resources");
         try {
-            String fileContent = FileHandler.readFile(
-                    String.format("%s/testData/%s", resourcesDirectory.getAbsolutePath(), getMappingFile()));
+            String fileContent = FileHandler.readFile(getMappingFile());
             JsonElement json = gson.fromJson(fileContent, JsonElement.class);
             return gson.toJson(json);
         } catch (Exception e) {
@@ -117,10 +112,8 @@ public class TestIndex {
     }
 
     private String getStorageSchemaFromJson() {
-        File resourcesDirectory = new File("src/test/resources");
         try {
-            String fileContent = FileHandler.readFile(
-                    String.format("%s/testData/%s", resourcesDirectory.getAbsolutePath(), getSchemaFile()));
+            String fileContent = FileHandler.readFile(getSchemaFile());
             fileContent = fileContent.replaceAll("KIND_VAL", this.kind);
             JsonElement json = gson.fromJson(fileContent, JsonElement.class);
             return gson.toJson(json);
diff --git a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/FileHandler.java b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/FileHandler.java
index a9aa4fe89..678cb9120 100644
--- a/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/FileHandler.java
+++ b/testing/indexer-test-core/src/main/java/org/opengroup/osdu/util/FileHandler.java
@@ -1,22 +1,24 @@
 package org.opengroup.osdu.util;
 
-import java.io.File;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.util.Scanner;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 
 public class FileHandler {
 
-    public static String readFile(String pathname) throws IOException {
-
-        File file = new File(pathname);
-        StringBuilder fileContents = new StringBuilder((int) file.length());
-        String lineSeparator = System.getProperty("line.separator");
-
-        try (Scanner scanner = new Scanner(file)) {
-            while (scanner.hasNextLine()) {
-                fileContents.append(scanner.nextLine()).append(lineSeparator);
-            }
-            return fileContents.toString();
+    public static String readFile(String fileName) throws IOException {
+        InputStream inputStream = FileHandler.class.getClass().getResourceAsStream(String.format("/testData/%s",fileName));
+        if(inputStream == null) {
+            throw new IOException();
+        }
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
+        byte[] buffer = new byte[1024];
+        int length;
+        while ((length = inputStream.read(buffer)) != -1) {
+            outputStream.write(buffer, 0, length);
         }
+        return outputStream.toString(StandardCharsets.UTF_8.toString());
     }
+    
 }
diff --git a/testing/indexer-test-gcp/src/test/resources/features/delete/Delete.feature b/testing/indexer-test-core/src/main/resources/features/delete/Delete.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/delete/Delete.feature
rename to testing/indexer-test-core/src/main/resources/features/delete/Delete.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/indexrecord/IndexRecord.feature b/testing/indexer-test-core/src/main/resources/features/indexrecord/IndexRecord.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/indexrecord/IndexRecord.feature
rename to testing/indexer-test-core/src/main/resources/features/indexrecord/IndexRecord.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/kindschema/KindSchema.feature b/testing/indexer-test-core/src/main/resources/features/kindschema/KindSchema.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/kindschema/KindSchema.feature
rename to testing/indexer-test-core/src/main/resources/features/kindschema/KindSchema.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/query/crosscluster/Query.feature b/testing/indexer-test-core/src/main/resources/features/query/crosscluster/Query.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/query/crosscluster/Query.feature
rename to testing/indexer-test-core/src/main/resources/features/query/crosscluster/Query.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/query/singlecluster/Query.feature b/testing/indexer-test-core/src/main/resources/features/query/singlecluster/Query.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/query/singlecluster/Query.feature
rename to testing/indexer-test-core/src/main/resources/features/query/singlecluster/Query.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/querybycursor/crosscluster/QueryByCursor.feature b/testing/indexer-test-core/src/main/resources/features/querybycursor/crosscluster/QueryByCursor.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/querybycursor/crosscluster/QueryByCursor.feature
rename to testing/indexer-test-core/src/main/resources/features/querybycursor/crosscluster/QueryByCursor.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/querybycursor/singlecluster/QueryByCursor.feature b/testing/indexer-test-core/src/main/resources/features/querybycursor/singlecluster/QueryByCursor.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/querybycursor/singlecluster/QueryByCursor.feature
rename to testing/indexer-test-core/src/main/resources/features/querybycursor/singlecluster/QueryByCursor.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/smart/parser/SmartParser.feature b/testing/indexer-test-core/src/main/resources/features/smart/parser/SmartParser.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/smart/parser/SmartParser.feature
rename to testing/indexer-test-core/src/main/resources/features/smart/parser/SmartParser.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/smart/search/Smart.feature b/testing/indexer-test-core/src/main/resources/features/smart/search/Smart.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/smart/search/Smart.feature
rename to testing/indexer-test-core/src/main/resources/features/smart/search/Smart.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/features/updateindex/UpdateIndexMapping.feature b/testing/indexer-test-core/src/main/resources/features/updateindex/UpdateIndexMapping.feature
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/features/updateindex/UpdateIndexMapping.feature
rename to testing/indexer-test-core/src/main/resources/features/updateindex/UpdateIndexMapping.feature
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/index_records_1.json b/testing/indexer-test-core/src/main/resources/testData/index_records_1.json
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/index_records_1.json
rename to testing/indexer-test-core/src/main/resources/testData/index_records_1.json
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/index_records_1.schema b/testing/indexer-test-core/src/main/resources/testData/index_records_1.schema
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/index_records_1.schema
rename to testing/indexer-test-core/src/main/resources/testData/index_records_1.schema
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/index_records_2.json b/testing/indexer-test-core/src/main/resources/testData/index_records_2.json
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/index_records_2.json
rename to testing/indexer-test-core/src/main/resources/testData/index_records_2.json
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/index_records_2.schema b/testing/indexer-test-core/src/main/resources/testData/index_records_2.schema
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/index_records_2.schema
rename to testing/indexer-test-core/src/main/resources/testData/index_records_2.schema
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/records_1.json b/testing/indexer-test-core/src/main/resources/testData/records_1.json
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/records_1.json
rename to testing/indexer-test-core/src/main/resources/testData/records_1.json
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/records_1.mapping b/testing/indexer-test-core/src/main/resources/testData/records_1.mapping
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/records_1.mapping
rename to testing/indexer-test-core/src/main/resources/testData/records_1.mapping
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/records_2.json b/testing/indexer-test-core/src/main/resources/testData/records_2.json
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/records_2.json
rename to testing/indexer-test-core/src/main/resources/testData/records_2.json
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/records_2.mapping b/testing/indexer-test-core/src/main/resources/testData/records_2.mapping
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/records_2.mapping
rename to testing/indexer-test-core/src/main/resources/testData/records_2.mapping
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/records_3.json b/testing/indexer-test-core/src/main/resources/testData/records_3.json
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/records_3.json
rename to testing/indexer-test-core/src/main/resources/testData/records_3.json
diff --git a/testing/indexer-test-gcp/src/test/resources/testData/records_3.mapping b/testing/indexer-test-core/src/main/resources/testData/records_3.mapping
similarity index 100%
rename from testing/indexer-test-gcp/src/test/resources/testData/records_3.mapping
rename to testing/indexer-test-core/src/main/resources/testData/records_3.mapping
diff --git a/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/deleteschema/RunTest.java b/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/deleteschema/RunTest.java
index 55dbb6bf0..b04eb1b30 100644
--- a/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/deleteschema/RunTest.java
+++ b/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/deleteschema/RunTest.java
@@ -6,7 +6,7 @@ import org.junit.runner.RunWith;
 
 @RunWith(Cucumber.class)
 @CucumberOptions(
-        features = "src/test/resources/features/delete/Delete.feature",
+        features = "classpath:features/delete/Delete.feature",
         glue={"classpath:org.opengroup.osdu.step_definitions/index/deleteschema"},
         format = {"pretty", "junit:target/cucumber-reports/TEST-deleteschema.xml"})
 public class RunTest {
diff --git a/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/getschema/RunTest.java b/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/getschema/RunTest.java
index b6c70e297..b60b4a1c5 100644
--- a/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/getschema/RunTest.java
+++ b/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/getschema/RunTest.java
@@ -7,7 +7,7 @@ import org.junit.runner.RunWith;
 
 @RunWith(Cucumber.class)
 @CucumberOptions(
-        features = "src/test/resources/features/kindschema/KindSchema.feature",
+        features = "classpath:features/kindschema/KindSchema.feature",
         glue = {"classpath:org.opengroup.osdu.step_definitions/index/getschema"},
         format = {"pretty", "junit:target/cucumber-reports/TEST-getschema.xml"})
 public class RunTest {
diff --git a/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/record/RunTest.java b/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/record/RunTest.java
index dc9fbcd3a..4978ddfcc 100644
--- a/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/record/RunTest.java
+++ b/testing/indexer-test-gcp/src/test/java/org/opengroup/osdu/step_definitions/index/record/RunTest.java
@@ -6,7 +6,7 @@ import org.junit.runner.RunWith;
 
 @RunWith(Cucumber.class)
 @CucumberOptions(
-        features = "src/test/resources/features/indexrecord/IndexRecord.feature",
+        features = "classpath:features/indexrecord/IndexRecord.feature",
         glue = {"classpath:org.opengroup.osdu.step_definitions/index/record"},
         plugin = {"pretty", "junit:target/cucumber-reports/TEST-indexrecord.xml"})
 public class RunTest {
-- 
GitLab