Commit c430eea6 authored by RPrakash3's avatar RPrakash3
Browse files

Test suite refactor

parent 1316e866
Pipeline #46577 failed with stages
in 26 minutes and 54 seconds
package org.opengroup.osdu.wks.stepdefs;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.hash.Hashing;
import org.apache.commons.lang3.RandomStringUtils;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.opengroup.osdu.de.automation.model.HttpRequest;
import org.opengroup.osdu.de.automation.model.HttpResponse;
import org.opengroup.osdu.de.automation.util.HttpClientFactory;
import org.opengroup.osdu.wks.model.AutomationConstants;
import org.opengroup.osdu.wks.model.WksIntegrationTestScope;
import org.opengroup.osdu.wks.util.AuthUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import io.cucumber.java8.En;
import io.restassured.path.json.JsonPath;
import org.opengroup.osdu.wks.util.KindsUtil;
import io.cucumber.datatable.DataTable;
public class HappyFlowStepDefs implements En {
public final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
CommonMethods commonMethods = new CommonMethods();
public WksIntegrationTestScope context = commonMethods.getContext();
public HappyFlowStepDefs() {
Given("I hit Storage service put end point with {string}", (String inputPayload) -> {
String payload = this.context.getFileUtils().read(inputPayload);
payload = commonMethods.updatePlaceholdersInInputPayload(payload);
String recordId = commonMethods.generateRandomId(payload);
this.context.setCustomRecordId(recordId);
LOGGER.log(Level.INFO, "Random record ID : " + this.context.getCustomRecordId());
this.context.setCustomValToVerifyUpdateOperation(RandomStringUtils.randomAlphabetic(10));
payload = payload.replace("<CreateUniqueID>", recordId).replace("<RandomText>",
this.context.getCustomValToVerifyUpdateOperation());
commonMethods.postToStorage(payload);
LOGGER.log(Level.INFO, "Storage Post Response : " + this.context.getHttpResponse().getBody());
});
Then("Transformed record should be created in storage with {string}", (String tagsblock) -> {
String responseCode = commonMethods
.pollStorageForWksOfCustomRecordInContext(AutomationConstants.OS_TARGET_SCHEMA_KIND);
boolean recordCreatedOrUpdated = responseCode.equals(AutomationConstants.RECORD_CREATED)
| responseCode.equals(AutomationConstants.REQUEST_SUCCESS);
assertTrue(recordCreatedOrUpdated);
String wksResponseBody = commonMethods
.pollStorageForWksOfCustomRecordBodyInContext(AutomationConstants.OS_TARGET_SCHEMA_KIND);
JsonElement responseGiven = new Gson().fromJson(wksResponseBody, JsonElement.class);
JsonObject tagsResponseArray = responseGiven.getAsJsonObject().getAsJsonObject("tags");
String expectedMetaBody = this.context.getFileUtils().read(tagsblock);
JsonElement expectedJsonBody = new Gson().fromJson(expectedMetaBody, JsonElement.class);
JsonObject tagsExpectedArray = expectedJsonBody.getAsJsonObject().getAsJsonObject("tags");
assertTrue(tagsResponseArray.toString().contentEquals(tagsExpectedArray.toString()));
});
Then("Transformed records should be created in storage", () -> {
List<String> targetSchemaKinds = Arrays.asList(
AutomationConstants.TARGET_FOR_TESTSOURCE_C_TESTENTITY_C.get(0),
AutomationConstants.TARGET_FOR_TESTSOURCE_C_TESTENTITY_C.get(1));
List<String> responseCodes = commonMethods
.pollStorageForMultipleWksOfCustomRecordInContext(targetSchemaKinds);
for (String responseCode : responseCodes) {
boolean recordCreatedOrUpdated = responseCode.equals(AutomationConstants.RECORD_CREATED)
| responseCode.equals(AutomationConstants.REQUEST_SUCCESS);
assertTrue(recordCreatedOrUpdated);
}
});
And("Wks records should be created in the intended kind", () -> {
List<HttpResponse> httpResponseList = this.context.getResponseList();
for (int i = 0; i < httpResponseList.size(); i++) {
String expectedKind = AutomationConstants.TARGET_FOR_TESTSOURCE_C_TESTENTITY_C.get(i);
String actualKind = new JsonPath(httpResponseList.get(i).getBody()).get("kind").toString();
assertEquals(expectedKind, actualKind);
}
});
When("I hit Storage service put end point to update raw record with {string}", (String updatePayload) -> {
String payload = this.context.getFileUtils().read(updatePayload);
payload = commonMethods.updatePlaceholdersInInputPayload(payload);
String existingRecordInStorage = this.context.getCustomRecordId();
this.context.setCustomValToVerifyUpdateOperation(RandomStringUtils.randomAlphabetic(10));
payload = payload.replace("<CreateUniqueID>", existingRecordInStorage).replace("<RandomText>",
this.context.getCustomValToVerifyUpdateOperation());
this.context.setInputPayload(payload);
commonMethods.postToStorage(payload);
LOGGER.log(Level.INFO, "Storage Post Response : " + this.context.getHttpResponse().getBody());
});
Then("Existing WKS records in storage should get updated", () -> {
String initialWksRecordData = this.context.getCustomValToVerifyUpdateOperation();
String postUpdateWksRecordData = null;
List<HttpResponse> httpResponseList = this.context.getResponseList();
for (int i = 0; i < httpResponseList.size(); i++) {
long initial = System.currentTimeMillis();
while ((System.currentTimeMillis() - initial) < (AutomationConstants.RECORD_SEARCH_MAX_TIMEOUT_SEC)
* 1000) {
String wksId = new JsonPath(httpResponseList.get(i).getBody()).get("id").toString();
String responseCode = commonMethods.storageGetResponse(wksId);
if (responseCode.equals(AutomationConstants.REQUEST_SUCCESS)) {
postUpdateWksRecordData = new JsonPath(this.context.getHttpResponse().getBody()).get("data.uwi")
.toString();
if (initialWksRecordData.equalsIgnoreCase(postUpdateWksRecordData))
break;
}
}
LOGGER.log(Level.INFO, "Storage Response : " + this.context.getHttpResponse().getBody());
assertTrue("Expected: " + initialWksRecordData + " , Actual: " + postUpdateWksRecordData,
initialWksRecordData.equalsIgnoreCase(postUpdateWksRecordData));
}
});
Then("Raw Record should be created in Storage", () -> {
String rawRecordWithVersionForAncestry = null;
long initial = System.currentTimeMillis();
while ((System.currentTimeMillis() - initial) < (AutomationConstants.RECORD_SEARCH_MAX_TIMEOUT_SEC)
* 1000) {
String responseCode = commonMethods.storageGetResponse(this.context.getCustomRecordId());
if (responseCode.equals(AutomationConstants.REQUEST_SUCCESS)) {
String version = new JsonPath(this.context.getHttpResponse().getBody()).get("version").toString();
rawRecordWithVersionForAncestry = this.context.getCustomRecordId()
+ AutomationConstants.COLON_SEPARATOR + version;
this.context.setRawRecordWithVersionForAncestry(rawRecordWithVersionForAncestry);
LOGGER.log(Level.INFO, "ancestry value - " + rawRecordWithVersionForAncestry);
break;
}
}
});
And("Data block in wks should be transformed as defined in {string} as per {string}",
(String expectedWksRecord, String mappingFile) -> {
String actualWksDataBlock = new JsonPath(this.context.getHttpResponse().getBody()).get("data")
.toString();
String expectedWksDataBlock = new JsonPath(this.context.getFileUtils().read(expectedWksRecord))
.get("data").toString();
assertEquals("Expected: " + expectedWksDataBlock + " , Actual: " + actualWksDataBlock,
expectedWksDataBlock, actualWksDataBlock);
});
And("Ancestry block should be updated correctly", () -> {
String actualWksAncestry = new JsonPath(this.context.getHttpResponse().getBody()).get("ancestry.parents[0]")
.toString();
String expectedWksAncestry = this.context.getRawRecordWithVersionForAncestry();
assertEquals("Expected: " + expectedWksAncestry + " , Actual: " + actualWksAncestry, expectedWksAncestry,
actualWksAncestry);
commonMethods.deleteFromStorage(this.context.getCustomRecordId());
assertTrue(commonMethods.recordDeleted());
commonMethods.deleteFromStorage(this.context.getWksRecordIdForCustomRawRecord());
assertTrue(commonMethods.recordDeleted());
});
And("Metablock references in the transformed record should be updated as defined in {string} as per {string}",
(String expectedWksRecord, String mappingFile) -> {
String actualWksMetaBlock = new JsonPath(this.context.getHttpResponse().getBody()).get("meta")
.toString();
String expectedWksMetaBlock = new JsonPath(this.context.getFileUtils().read(expectedWksRecord))
.get("meta").toString();
assertEquals("Expected: " + expectedWksMetaBlock + " , Actual: " + actualWksMetaBlock,
expectedWksMetaBlock, actualWksMetaBlock);
});
And("Wks record should be created in the intended kind", () -> {
String expectedKind = AutomationConstants.OS_TARGET_SCHEMA_KIND;
String actualKind = new JsonPath(this.context.getHttpResponse().getBody()).get("kind").toString();
assertEquals(expectedKind, actualKind);
});
Then("Existing WKS record in storage should get updated", () -> {
String initialWksRecordData = this.context.getCustomValToVerifyUpdateOperation();
String postUpdateWksRecordData = null;
long initial = System.currentTimeMillis();
while ((System.currentTimeMillis() - initial) < (AutomationConstants.RECORD_SEARCH_MAX_TIMEOUT_SEC)
* 1000) {
String responseCode = commonMethods.storageGetResponse(this.context.getWksRecordIdForCustomRawRecord());
if (responseCode.equals(AutomationConstants.REQUEST_SUCCESS)) {
postUpdateWksRecordData = new JsonPath(this.context.getHttpResponse().getBody()).get("data.uwi")
.toString();
if (initialWksRecordData.equalsIgnoreCase(postUpdateWksRecordData))
break;
}
}
LOGGER.log(Level.INFO, "Storage Response : " + this.context.getHttpResponse().getBody());
assertTrue("Expected: " + initialWksRecordData + " , Actual: " + postUpdateWksRecordData,
initialWksRecordData.equalsIgnoreCase(postUpdateWksRecordData));
commonMethods.deleteFromStorage(this.context.getCustomRecordId());
assertTrue(commonMethods.recordDeleted());
commonMethods.deleteFromStorage(this.context.getWksRecordIdForCustomRawRecord());
assertTrue(commonMethods.recordDeleted());
});
When("I hit Storage service put end point with {string} having multiple records where few records are already Wks records",
(String bulkRecord) -> {
String payload = this.context.getFileUtils().read(bulkRecord);
payload = commonMethods.updatePlaceholdersInInputPayload(payload);
JsonArray bulkRecords = new Gson().fromJson(payload, JsonArray.class);
List<String> recordList = new ArrayList<String>();
recordList.add(commonMethods.generateRandomId(bulkRecords.get(0).toString()));
recordList.add(commonMethods.generateRawRecordId(AutomationConstants.OS_TARGET_SCHEMA_KIND_TENANT));
recordList.add(commonMethods.generateRandomId(bulkRecords.get(2).toString()));
this.context.setBulkRawRecordIdList(recordList);
payload = payload.replace("<CreateUniqueID1>", recordList.get(0))
.replace("<CreateUniqueID2>", recordList.get(1))
.replace("<CreateUniqueID3>", recordList.get(2));
LOGGER.log(Level.INFO, "Payload to Post : " + payload);
commonMethods.postToStorage(payload);
LOGGER.log(Level.INFO, "Storage Post Response : " + this.context.getHttpResponse().getBody());
});
Then("Transformation of Wks records should be skipped", () -> {
String responseCode = commonMethods.fetchRecordFromStorage(commonMethods.createWKSRecordId(
this.context.getBulkRawRecordIdList().get(1), AutomationConstants.OS_TARGET_SCHEMA_KIND));
assertEquals(AutomationConstants.RECORD_NOT_FOUND, responseCode);
});
And("All other valid records should get transformed", () -> {
String responseCode = commonMethods.fetchRecordFromStorage(commonMethods.createWKSRecordId(
this.context.getBulkRawRecordIdList().get(0), AutomationConstants.OS_TARGET_SCHEMA_KIND));
assertEquals(AutomationConstants.REQUEST_SUCCESS, responseCode);
responseCode = commonMethods.fetchRecordFromStorage(commonMethods.createWKSRecordId(
this.context.getBulkRawRecordIdList().get(2), AutomationConstants.OS_TARGET_SCHEMA_KIND));
assertEquals(AutomationConstants.REQUEST_SUCCESS, responseCode);
this.context.getBulkRawRecordIdList().forEach(recordId -> {
commonMethods.deleteFromStorage(recordId);
assertTrue(commonMethods.recordDeleted());
});
commonMethods.deleteFromStorage(commonMethods.createWksRecordIdFromCustomRawRecord(
this.context.getBulkRawRecordIdList().get(0), AutomationConstants.OS_TARGET_SCHEMA_KIND));
assertTrue(commonMethods.recordDeleted());
commonMethods.deleteFromStorage(commonMethods.createWksRecordIdFromCustomRawRecord(
this.context.getBulkRawRecordIdList().get(2), AutomationConstants.OS_TARGET_SCHEMA_KIND));
assertTrue(commonMethods.recordDeleted());
});
Then("Transformed record should be created in storage", () -> {
String responseCode = commonMethods.pollStorageForWksOfCustomRecordInContext(AutomationConstants.OS_TARGET_SCHEMA_KIND);
boolean recordCreatedOrUpdated = responseCode.equals(AutomationConstants.RECORD_CREATED)
| responseCode.equals(AutomationConstants.REQUEST_SUCCESS);
assertTrue(recordCreatedOrUpdated);
});
}
}
\ No newline at end of file
package org.opengroup.osdu.wks.stepdefs;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.hash.Hashing;
import org.apache.commons.lang3.RandomStringUtils;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.opengroup.osdu.de.automation.model.HttpRequest;
import org.opengroup.osdu.de.automation.model.HttpResponse;
import org.opengroup.osdu.de.automation.util.HttpClientFactory;
import org.opengroup.osdu.wks.model.AutomationConstants;
import org.opengroup.osdu.wks.model.WksIntegrationTestScope;
import org.opengroup.osdu.wks.util.AuthUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import io.cucumber.java8.En;
import io.restassured.path.json.JsonPath;
import org.opengroup.osdu.wks.util.KindsUtil;
import io.cucumber.datatable.DataTable;
public class NegativeTestStepDefs implements En {
public final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
CommonMethods commonMethods = new CommonMethods();
public WksIntegrationTestScope context = commonMethods.getContext();
@Inject
public NegativeTestStepDefs() {
Given("I hit Storage service put end point with {string} which is already a WKS record",
(String inputPayload) -> {
String payload = this.context.getFileUtils().read(inputPayload);
payload = commonMethods.updatePlaceholdersInInputPayload(payload);
String recordId = commonMethods
.generateRawRecordId(AutomationConstants.OS_TARGET_SCHEMA_KIND_TENANT);
this.context.setCustomRecordId(recordId);
commonMethods.postToStorage(payload.replace("<CreateUniqueID>", recordId));
LOGGER.log(Level.INFO, "Storage Post Response : " + this.context.getHttpResponse().getBody());
assertEquals(String.valueOf(this.context.getHttpResponse().getCode()),
AutomationConstants.RECORD_CREATED);
});
Then("No corresponding transformed record should be created for this wks record in storage", () -> {
String responseCode = commonMethods
.pollStorageForAlreadyTransformedRecordType(AutomationConstants.OS_TARGET_SCHEMA_KIND);
assertEquals(AutomationConstants.RECORD_NOT_FOUND, responseCode);
commonMethods.deleteFromStorage(this.context.getCustomRecordId());
assertTrue(commonMethods.recordDeleted());
});
Given("I hit Storage service put end point with {string} which is already a WKE record",
(String inputPayload) -> {
String payload = this.context.getFileUtils().read(inputPayload);
payload = commonMethods.updatePlaceholdersInInputPayload(payload);
String kindVal = (AutomationConstants.OS_TARGET_SCHEMA_KIND_TENANT).replace("wks:", "wke:");
String recordId = commonMethods.generateRawRecordId(kindVal);
this.context.setCustomRecordId(recordId);
commonMethods.postToStorage(payload.replace("<CreateUniqueID>", recordId));
LOGGER.log(Level.INFO, "Storage Post Response : " + this.context.getHttpResponse().getBody());
assertEquals(String.valueOf(this.context.getHttpResponse().getCode()),
AutomationConstants.RECORD_CREATED);
});
Then("No corresponding transformed record should be created for this wke record in storage", () -> {
String responseCode = commonMethods.pollStorageForAlreadyTransformedRecordType(
(AutomationConstants.OS_TARGET_SCHEMA_KIND).replace("wks:", "wke:"));
assertEquals(AutomationConstants.RECORD_NOT_FOUND, responseCode);
commonMethods.deleteFromStorage(this.context.getCustomRecordId());
assertTrue(commonMethods.recordDeleted());
});
Given("I hit Storage service put end point with {string} which has invalid source so that mapping service does not return any mapping",
(String inputPayload) -> {
String payload = this.context.getFileUtils().read(inputPayload);
payload = commonMethods.updatePlaceholdersInInputPayload(payload);
String recordId = commonMethods.generateRandomId(payload);
this.context.setCustomRecordId(recordId);
LOGGER.log(Level.INFO, "Random record ID : " + this.context.getCustomRecordId());
payload = payload.replace("<CreateUniqueID>", recordId);
commonMethods.postToStorage(payload);
LOGGER.log(Level.INFO, "Storage Post Response : " + this.context.getHttpResponse().getBody());
assertEquals(String.valueOf(this.context.getHttpResponse().getCode()),
AutomationConstants.RECORD_CREATED);
});
Then("No corresponding transformed record should be created for this record in storage", () -> {
String responseCode = commonMethods.pollStorageForCurrentRecordInContext();
assertEquals(AutomationConstants.RECORD_NOT_FOUND, responseCode);
commonMethods.deleteFromStorage(this.context.getCustomRecordId());
assertTrue(commonMethods.recordDeleted());
});
}
}
package org.opengroup.osdu.wks.stepdefs;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.hash.Hashing;
import org.apache.commons.lang3.RandomStringUtils;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.opengroup.osdu.de.automation.model.HttpRequest;
import org.opengroup.osdu.de.automation.model.HttpResponse;
import org.opengroup.osdu.de.automation.util.HttpClientFactory;
import org.opengroup.osdu.wks.model.AutomationConstants;
import org.opengroup.osdu.wks.model.WksIntegrationTestScope;
import org.opengroup.osdu.wks.util.AuthUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import io.cucumber.java8.En;
import io.restassured.path.json.JsonPath;
import org.opengroup.osdu.wks.util.KindsUtil;
import io.cucumber.datatable.DataTable;
public class PreIntegrationStepDefs implements En {
public final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
CommonMethods commonMethods = new CommonMethods();
public WksIntegrationTestScope context = commonMethods.getContext();
@Inject
public PreIntegrationStepDefs() {
Given("I create below source schemas used in Mapping files used for IT test", (DataTable sourceSchemaPaths) -> {
List<String> sourceSchemaPathsAsList = sourceSchemaPaths.asList();
for (String sourceSchemaPath : sourceSchemaPathsAsList) {
commonMethods.createSchemas(sourceSchemaPath);
}
});
And("I create below target schemas used in Mapping files used for IT test", (DataTable targetSchemaPaths) -> {
List<String> targetSchemaPathsAsList = targetSchemaPaths.asList();
for (String targetSchemaPath : targetSchemaPathsAsList) {
commonMethods.createSchemas(targetSchemaPath);
}
});
}
}
package org.opengroup.osdu.wks.stepdefs;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.hash.Hashing;
import org.apache.commons.lang3.RandomStringUtils;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.opengroup.osdu.de.automation.model.HttpRequest;
import org.opengroup.osdu.de.automation.model.HttpResponse;
import org.opengroup.osdu.de.automation.util.HttpClientFactory;
import org.opengroup.osdu.wks.model.AutomationConstants;
import org.opengroup.osdu.wks.model.WksIntegrationTestScope;
import org.opengroup.osdu.wks.util.AuthUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import io.cucumber.java8.En;
import io.restassured.path.json.JsonPath;
import org.opengroup.osdu.wks.util.KindsUtil;
import io.cucumber.datatable.DataTable;
public class RelationshipBlockTestStepDefs implements En {
public final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
CommonMethods commonMethods = new CommonMethods();
public WksIntegrationTestScope context = commonMethods.getContext();
@Inject
public RelationshipBlockTestStepDefs() {
Given("I hit Storage service put end point with {string} to persist a record say raw1",
(String inputPayload) -> {
commonMethods.persistRecordToBeUsedInRelationshipBlock(inputPayload);
});
And("I verify that wks is created for above raw record", () -> {
String wksRecordId = commonMethods.getWksIdForRecordFromRequestPayloadArrayAtIndex(0,
AutomationConstants.OS_TARGET_SCHEMA_KIND);
String responseCode = commonMethods.pollStorageForWksOfRawInRelationshipBlock(wksRecordId);
boolean recordCreatedOrUpdated = responseCode.equals(AutomationConstants.RECORD_CREATED)
| responseCode.equals(AutomationConstants.REQUEST_SUCCESS);
assertTrue(recordCreatedOrUpdated);
commonMethods.staticWaitForIndexingToCommence();
});