Commit 4671be74 authored by Subham Agarwal's avatar Subham Agarwal
Browse files

updated code, test folder segratation, readme updated, code coverage increased

parent fc8c0dd5
Pipeline #3835 failed with stage
in 8 seconds
package org.opengroup.osdu.schema.constants;
public class HttpConnection {
public static final boolean FOLLOW_REDIRECTS = true;
public static final int CONNECTION_TIMEOUT_IN_MILLISECONDS = 80000;
public static final String HTTP_SOCKET_TIMEOUT = "http.socket.timeout";
public static final String HTTP_CONNECTION_TIMEOUT = "http.connection.timeout";
public static final String CONTENT_TYPE = "Content-Type";
public static final String APPLICATION_JSON = "application/json";
}
package org.opengroup.osdu.schema.constants;
public class TestConstants {
public static final String FORWARD_SLASH = "/";
public static final String PUT_ENDPOINT = "/api/schema-service/v1/schema";
public static final String POST_ENDPOINT = "/api/schema-service/v1/schema";
public static final String GET_LIST_ENDPOINT = "/api/schema-service/v1/schema";
public static final String GET_ENDPOINT = "/api/schema-service/v1/schema/";
public static final String GET_FLATTENED_ENDPOINT = "/api/schema-service/v1/schema/{id}/IndexerSchemaV1";
public static final String HOST = "https://open.opendes.cloud.slb-ds.com";
//public static final String HOST = "http://localhost:8080";
public static final String INTERNAL_SERVER_ERROR = "internal server error";
public static final String INTERNAL = "INTERNAL";
public static final String SCHEMA_KIND = "schema";
public static final String AUTHORITY = "authority";
public static final String LATEST_VERSION = "latestVersion";
public static final String GET_SUCCESSRESPONSECODE = "200";
public static final String SOURCE = "source";
public static final String ENTITY_TYPE = "entityType";
public static final String MAJOR_VERSION = "majorVersion";
public static final String MINOR_VERSION = "minorVersion";
public static final String PATCH_VERSION = "patchVersion";
public static final String CREATED_BY = "createdBy";
public static final String SUPERSEDED_BY = "supersededBy";
public static final String DATE_CREATED = "dateCreated";
public static final String SCHEMA = "schema";
public static final String OBSOLETE = "OBSOLETE";
public static final String PUBLISHED = "PUBLISHED";
public static final String SCOPE = "scope";
public static final String STATUS = "status";
public static final String ALREADY_EXISTS = "ALREADY_EXISTS";
public static final String DATAECOSYSTEM = "dataecosystem";
public static final String AUTHORIZATION = "authorization";
public static final String DATA_PARTITION_ID = "data-partition-id";
public static final String BEARER = "Bearer ";
public static final String CONTENT_TYPE = "Content-Type";
public static final String TENANT = "opendes";
public static final String JSON_CONTENT = "application/json";
public static final String schemaIdOfInputPayload = "schemaInfo.schemaIdentity.id";
public static final String SCHEMA_IDENTITY = "schemaIdentity";
public static final String ENTITY = "entityType";
public static final String SCHEMA_MAJOR_VERSION = "schemaVersionMajor";
public static final String SCHEMA_MINOR_VERSION = "schemaVersionMinor";
public static final String SCHEMA_PATCH_VERSION = "schemaVersionPatch";
public static final String ID = "id";
public static final String DOT = ".";
public static final String ERROR = "error";
public static final String MESSAGE = "message";
public static final String STORAGE_SCHEMA = "StorageSchema";
public static final String USER = "user";
public static final String STORAGE_SCHEMA_USER_EMAIL = "testUserEmail@test.com";
public static final String KIND = "kind";
}
package org.opengroup.osdu.schema.runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features", glue = { "classpath:org.opengroup.osdu.schema.stepdefs" }, tags = {
"@Startup" }, plugin = { "pretty", "junit:target/cucumber-reports/schema-service-test-report.xml" })
public class PreIntegrationTestsRunner {
}
\ No newline at end of file
package org.opengroup.osdu.schema.runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features", glue = { "classpath:org.opengroup.osdu.schema.stepdefs" }, tags = {
"@SchemaService" }, plugin = { "pretty", "junit:target/cucumber-reports/schema-service-test-report.xml" })
public class SchemaServiceTestsRunner {
}
package org.opengroup.osdu.schema.runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features", glue = { "classpath:org.opengroup.osdu.schema.stepdefs" }, tags = {
"@TearDown" }, plugin = { "pretty", "junit:target/cucumber-reports/schema-service-test-report.xml" })
public class TearDownTestsRunner {
}
package org.opengroup.osdu.schema.stepdefs;
import io.cucumber.java8.En;
import lombok.extern.java.Log;
@Log
public class LocalSetupStepDef implements En {
public LocalSetupStepDef() {
}
}
\ No newline at end of file
package org.opengroup.osdu.schema.stepdefs;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Assert;
import org.opengroup.osdu.schema.constants.TestConstants;
import org.opengroup.osdu.schema.stepdefs.model.HttpRequest;
import org.opengroup.osdu.schema.stepdefs.model.HttpResponse;
import org.opengroup.osdu.schema.stepdefs.model.SchemaServiceScope;
import org.opengroup.osdu.schema.util.AuthUtil;
import org.opengroup.osdu.schema.util.HttpClientFactory;
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;
public class SchemaServiceStepDef_GET implements En {
@Inject
private SchemaServiceScope context;
static String[] GetListBaseFilterArray;
static String[] GetListVersionFilterArray;
String queryParameter;
private static TreeSet<String> LIST_OF_AVAILABLE_SCHEMAS = new TreeSet<String>();
private final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
List<HashMap<String, String>> list_schemaParameterMap = new ArrayList<HashMap<String, String>>();
public SchemaServiceStepDef_GET() {
Given("I generate user token and set request headers", () -> {
if (this.context.getToken() == null) {
String token = new AuthUtil().getToken();
this.context.setToken(token);
}
if (this.context.getAuthHeaders() == null) {
Map<String, String> authHeaders = new HashMap<String, String>();
authHeaders.put(TestConstants.AUTHORIZATION, this.context.getToken());
authHeaders.put(TestConstants.DATA_PARTITION_ID, TestConstants.TENANT);
authHeaders.put(TestConstants.CONTENT_TYPE, TestConstants.JSON_CONTENT);
this.context.setAuthHeaders(authHeaders);
}
});
Given("I hit schema service GET List API with {string} , {string} , {string}",
(String parameter, String parameterVal, String latestVersion) -> {
Map<String, String> queryParams = new HashMap<String, String>();
queryParams.put(parameter, parameterVal);
queryParams.put("latestVersion", latestVersion);
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_LIST_ENDPOINT).queryParams(queryParams)
.httpMethod(HttpRequest.GET).requestHeaders(this.context.getAuthHeaders()).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
assertEquals("200", String.valueOf(response.getCode()));
LOGGER.log(Level.INFO, "resp - " + response.toString());
verifyGetListResponse(parameter, parameterVal);
});
Given("I hit schema service GET List API with filters of {string}, {string}, {string}, {string} and getLatest flag is {string}",
(String authority, String majorVersion, String minorVersion, String patchVersion,
String latestVersion) -> {
ArrayList<String> allValues = new ArrayList<>();
ArrayList<String> allKeys = new ArrayList<>();
Map<String, String> queryParams = new HashMap<String, String>();
if (majorVersion.equalsIgnoreCase(TestConstants.LATEST_VERSION))
majorVersion = this.context.getSchemaVersionMajor();
if (minorVersion.equalsIgnoreCase(TestConstants.LATEST_VERSION))
minorVersion = this.context.getSchemaVersionMinor();
if (patchVersion.equalsIgnoreCase(TestConstants.LATEST_VERSION))
patchVersion = this.context.getSchemaVersionPatch();
allValues.add(majorVersion);
allValues.add(minorVersion);
allValues.add(patchVersion);
allKeys.add(TestConstants.SCHEMA_MAJOR_VERSION);
allKeys.add(TestConstants.SCHEMA_MINOR_VERSION);
allKeys.add(TestConstants.SCHEMA_PATCH_VERSION);
for (int i = 0; i < allKeys.size(); i++) {
if (!allValues.get(i).equalsIgnoreCase("NA")) {
queryParams.put(allKeys.get(i), allValues.get(i));
}
}
queryParams.put(TestConstants.AUTHORITY, authority);
queryParams.put(TestConstants.LATEST_VERSION, latestVersion);
this.context.setQueryParams(queryParams);
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_LIST_ENDPOINT).queryParams(queryParams)
.httpMethod(HttpRequest.GET).requestHeaders(this.context.getAuthHeaders()).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
});
Then("service should respond back with schemaInfo list from internal as well as shared scope matching {string} and {string}",
(String parameter, String parameterVal) -> {
verifyGetListResponse(parameter, parameterVal);
});
Then("service should respond back with schemaInfo list matching {string} and {string}",
(String parameter, String parameterVal) -> {
verifySchemaIdentityElementValues(parameter, parameterVal);
});
Given("I hit schema service GET API with {string}", (String schemaId) -> {
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_ENDPOINT + schemaId).httpMethod(HttpRequest.GET)
.requestHeaders(this.context.getAuthHeaders()).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
});
Then("service should respond back with success {string} and response {string}",
(String ReponseStatusCode, String SchemaToBeVerified) -> {
HttpResponse response = this.context.getHttpResponse();
assertEquals(ReponseStatusCode, String.valueOf(response.getCode()));
String body = this.context.getFileUtils().read(SchemaToBeVerified);
Gson gsn = new Gson();
JsonObject expectedData = gsn.fromJson(body, JsonObject.class);
JsonObject responseMsg = gsn.fromJson(response.getBody().toString(), JsonObject.class);
assertEquals(expectedData.get("schema").toString(), responseMsg.get("schema").toString());
});
Then("I GET updated schema", () -> {
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_ENDPOINT + this.context.getSchemaIdFromInputPayload())
.httpMethod(HttpRequest.GET).requestHeaders(this.context.getAuthHeaders()).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
assertEquals(TestConstants.GET_SUCCESSRESPONSECODE, String.valueOf(response.getCode()));
});
Then("I get response {string} when I try to get schema from {string} other than from where it was ingested",
(String responseCode, String otherTenant) -> {
Map<String, String> authHeaders = this.context.getAuthHeaders();
authHeaders.put(TestConstants.DATA_PARTITION_ID, otherTenant);
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_ENDPOINT
+ this.context.getSchemaIdFromInputPayload())
.httpMethod(HttpRequest.GET).requestHeaders(authHeaders).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
assertEquals(responseCode, String.valueOf(response.getCode()));
});
Then("I GET udapted Schema with {string}", (String ReponseStatusCode) -> {
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_ENDPOINT + this.context.getSchemaIdFromInputPayload())
.httpMethod(HttpRequest.GET).requestHeaders(this.context.getAuthHeaders()).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
assertEquals(ReponseStatusCode, String.valueOf(response.getCode()));
});
Then("schema service should respond back with {string} and {string}",
(String ReponseStatusCode, String ResponseToBeVerified) -> {
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_ENDPOINT
+ this.context.getSchemaIdFromInputPayload())
.httpMethod(HttpRequest.GET).requestHeaders(this.context.getAuthHeaders()).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
assertEquals(ReponseStatusCode, String.valueOf(response.getCode()));
String body = this.context.getFileUtils().read(ResponseToBeVerified);
Gson gsn = new Gson();
JsonObject expectedData = gsn.fromJson(body, JsonObject.class);
JsonObject responseMsg = gsn.fromJson(response.getBody().toString(), JsonObject.class);
assertEquals(expectedData.toString(), responseMsg.toString());
});
Given("I hit schema service GET List API with {string} and {string}",
(String parameter, String parameterVal) -> {
Map<String, String> queryParams = new HashMap<String, String>();
queryParams.put(parameter, parameterVal);
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_LIST_ENDPOINT).queryParams(queryParams)
.httpMethod(HttpRequest.GET).requestHeaders(this.context.getAuthHeaders()).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
});
Given("I hit schema service GET API with {string} having blank {string}", (String schemaId, String header) -> {
Map<String, String> authHeaders = this.context.getAuthHeaders();
if (header.equalsIgnoreCase("authorization")) {
authHeaders.put(TestConstants.AUTHORIZATION, "");
} else {
authHeaders.put(TestConstants.DATA_PARTITION_ID, "");
}
Map<String, String> queryParams = new HashMap<String, String>();
HttpRequest httpRequest = HttpRequest.builder().url(TestConstants.HOST + TestConstants.GET_LIST_ENDPOINT)
.queryParams(queryParams).httpMethod(HttpRequest.GET).requestHeaders(authHeaders).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
});
Given("I hit schema service GET List API with query parameters having values {string}, {string}, {string}, {string}, {string}, {string}, {string}, {string}, {string}",
(String val1, String val2, String val3, String val4, String val5, String val6, String val7, String val8,
String count) -> {
ArrayList<String> allValues = new ArrayList<>();
ArrayList<String> allKeys = new ArrayList<>();
Map<String, String> queryParams = new HashMap<String, String>();
allValues.add(val1);
allValues.add(val2);
allValues.add(val3);
allValues.add(val4);
allValues.add(val5);
allValues.add(val6);
allValues.add(val7);
allValues.add(val8);
allKeys.add("authority");
allKeys.add("source");
allKeys.add("entityType");
allKeys.add("status");
allKeys.add("scope");
allKeys.add("schemaVersionMajor");
allKeys.add("schemaVersionMinor");
allKeys.add("schemaVersionPatch");
for (int i = 0; i < allKeys.size(); i++) {
if (!allValues.get(i).equalsIgnoreCase("NA")) {
queryParams.put(allKeys.get(i), allValues.get(i));
}
}
this.context.setQueryParams(queryParams);
HttpRequest httpRequest = HttpRequest.builder()
.url(TestConstants.HOST + TestConstants.GET_LIST_ENDPOINT).queryParams(queryParams)
.httpMethod(HttpRequest.GET).requestHeaders(this.context.getAuthHeaders()).build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
});
Then("service should respond back with {string} and schemaInfo list values matching to input",
(String responseCode) -> {
verifyGetResponseForMultipleValues(responseCode);
});
Then("service should respond back with {string} and {string} schema with correct major, minor and patch version.",
(String ReponseStatusCode, String ResponseMessage) -> {
String body = this.context.getFileUtils().read(ResponseMessage);
JsonObject jsonBody = new Gson().fromJson(body, JsonObject.class);
HttpResponse response = this.context.getHttpResponse();
if (response != null) {
assertEquals(ReponseStatusCode, String.valueOf(response.getCode()));
verifySchemaIdentityElementValues(TestConstants.SCHEMA_MAJOR_VERSION,
this.context.getSchemaVersionMajor());
verifySchemaIdentityElementValues(TestConstants.SCHEMA_MINOR_VERSION,
this.context.getSchemaVersionMinor());
verifySchemaIdentityElementValues(TestConstants.SCHEMA_PATCH_VERSION,
this.context.getSchemaVersionPatch());
}
});
}
private void verifyGetListResponse(String parameter, String parameterVal) throws IOException {
prepareSchemaParameterMapList();
verifySchemaInfoResponse(parameter, parameterVal);
}
public void prepareSchemaParameterMapList() throws IOException {
String response = this.context.getHttpResponse().getBody();
Gson gsn = new Gson();
JsonObject schemaInfosList = gsn.fromJson(response, JsonObject.class);
JsonArray root = (JsonArray) schemaInfosList.get("schemaInfos");
for (JsonElement eachSchemaInfo : root) {
Map<String, String> schemaIdentityMap = new HashMap<String, String>();
JsonObject schemaIdentity_ForEachSchemaStatus = (JsonObject) (eachSchemaInfo.getAsJsonObject());
this.context.setStatus(schemaIdentity_ForEachSchemaStatus.get("status").getAsString());
JsonObject schemaIdentity_ForEachSchemaInfo = (JsonObject) eachSchemaInfo.getAsJsonObject()
.get("schemaIdentity");
schemaIdentityMap.put("authority", schemaIdentity_ForEachSchemaInfo.get("authority").getAsString());
schemaIdentityMap.put("source", schemaIdentity_ForEachSchemaInfo.get("source").getAsString());
schemaIdentityMap.put("entityType", schemaIdentity_ForEachSchemaInfo.get("entityType").getAsString());
schemaIdentityMap.put("schemaVersionMajor",
schemaIdentity_ForEachSchemaInfo.get("schemaVersionMajor").getAsString());
this.context
.setSchemaVersionMajor(schemaIdentity_ForEachSchemaInfo.get("schemaVersionMajor").getAsString());
schemaIdentityMap.put("schemaVersionMinor",
schemaIdentity_ForEachSchemaInfo.get("schemaVersionMinor").getAsString());
this.context
.setSchemaVersionMinor(schemaIdentity_ForEachSchemaInfo.get("schemaVersionMinor").getAsString());
schemaIdentityMap.put("schemaVersionPatch",
schemaIdentity_ForEachSchemaInfo.get("schemaVersionPatch").getAsString());
this.context
.setSchemaVersionPatch(schemaIdentity_ForEachSchemaInfo.get("schemaVersionPatch").getAsString());
schemaIdentityMap.put("scope", eachSchemaInfo.getAsJsonObject().get("scope").getAsString());
schemaIdentityMap.put("status", eachSchemaInfo.getAsJsonObject().get("status").getAsString());
this.list_schemaParameterMap.add((HashMap<String, String>) schemaIdentityMap);
}
LOGGER.log(Level.INFO, "SchemaParameterMapList - " + this.list_schemaParameterMap.toString());
}
private void verifySchemaInfoResponse(String parameterName, String parameterVal) {
for (HashMap<String, String> schemaInfoMap : this.list_schemaParameterMap) {
assertEquals(
"Response schemaInfoList contains schemaInfo not matching parameter criteria - " + parameterName,
parameterVal.toString(), schemaInfoMap.get(parameterName).toString());
}
}
public void verifySchemaIdentityElementValues(String parameter, String value) throws IOException {
String response = this.context.getHttpResponse().getBody();
Gson gsn = new Gson();
JsonObject schemaInfosList = gsn.fromJson(response, JsonObject.class);
JsonArray root = (JsonArray) schemaInfosList.get("schemaInfos");
verifyResponseJsonElement(parameter, value, root);
}
private void verifyResponseJsonElement(String parameter, String value, JsonArray root) {
for (JsonElement eachSchemaInfo : root) {
if (parameter.equalsIgnoreCase("status") || parameter.equalsIgnoreCase("scope")) {
String ActualVal = eachSchemaInfo.getAsJsonObject().get(parameter).getAsString();
assertEquals(ActualVal, value);
} else {
JsonObject schemaIdentity_ForEachSchemaInfo = (JsonObject) eachSchemaInfo.getAsJsonObject()
.get("schemaIdentity");
String ActualVal = schemaIdentity_ForEachSchemaInfo.get(parameter).getAsString();
assertEquals(ActualVal, value);
}
}
}
public void verifyGetResponseForMultipleValues(String responseCode) throws IOException {
int respCode = this.context.getHttpResponse().getCode();
assertEquals(responseCode, String.valueOf(respCode));
Map<String, String> queryParams = this.context.getQueryParams();
String response = this.context.getHttpResponse().getBody();
Gson gsn = new Gson();
JsonObject schemaInfosList = gsn.fromJson(response, JsonObject.class);
JsonArray root = (JsonArray) schemaInfosList.get("schemaInfos");
queryParams.entrySet().stream().forEach(entry -> {
String key = entry.getKey();
String value = entry.getValue();
verifyResponseJsonElement(key, value, root);
});
}
}
\ No newline at end of file
package org.opengroup.osdu.schema.stepdefs;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.junit.Assert;
import org.opengroup.osdu.schema.constants.TestConstants;
import org.opengroup.osdu.schema.stepdefs.model.HttpRequest;
import org.opengroup.osdu.schema.stepdefs.model.HttpResponse;
import org.opengroup.osdu.schema.stepdefs.model.SchemaServiceScope;
import org.opengroup.osdu.schema.util.HttpClientFactory;
import org.opengroup.osdu.schema.util.JsonUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import io.cucumber.java8.En;
public class SchemaServiceStepDef_POST implements En {
@Inject
private SchemaServiceScope context;
static String[] GetListBaseFilterArray;
static String[] GetListVersionFilterArray;
String queryParameter;
private final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
List<HashMap<String, String>> list_schemaParameterMap = new ArrayList<HashMap<String, String>>();
public SchemaServiceStepDef_POST() {
Given("I hit schema service POST API with {string} and data-partition-id as {string} only if status is not development",
(String inputPayload, String tenant) -> {
if (!"DEVELOPMENT".equals(context.getStatus())) {
String body = this.context.getFileUtils().read(inputPayload);