From c940217b2935868b0a7eef9bc9286525c205dd1d Mon Sep 17 00:00:00 2001 From: Gokul Nagare <gonagar1@in.ibm.com> Date: Mon, 19 Oct 2020 06:19:30 -0400 Subject: [PATCH] Ibm api key test --- NOTICE | 17 ++-- devops/Jenkinsfile | 85 ++++++++++++++++++ provider/indexer-ibm/pom.xml | 2 +- .../indexer/ibm/security/SecurityConfig.java | 2 + .../ibm/util/GlobalExceptionMapper.java | 83 ++++++++++++++++++ .../ibm/util/IndexerQueueTaskBuilderIbm.java | 86 +++++++++++++++++++ .../indexer/ibm/util/RequestInfoImpl.java | 25 ++++-- testing/indexer-test-ibm/pom.xml | 2 +- .../src/main/resources/apikey.feature | 10 +++ .../index/record/RunTest.java | 2 +- .../step_definitions/index/record/Steps.java | 60 ++++++++++++- 11 files changed, 354 insertions(+), 20 deletions(-) create mode 100644 devops/Jenkinsfile create mode 100644 provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/GlobalExceptionMapper.java create mode 100644 provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/IndexerQueueTaskBuilderIbm.java create mode 100644 testing/indexer-test-ibm/src/main/resources/apikey.feature diff --git a/NOTICE b/NOTICE index e87d8d2c9..3483ecfed 100644 --- a/NOTICE +++ b/NOTICE @@ -11,7 +11,6 @@ The following software have components provided under the terms of this license: - Cobertura code coverage (from http://cobertura.sourceforge.net) - Plexus :: Default Container (from ) - Plexus Common Utilities (from http://plexus.codehaus.org/plexus-utils) -- StAX (from http://stax.codehaus.org/) - oro (from ) ======================================================================== @@ -244,6 +243,10 @@ The following software have components provided under the terms of this license: - Guava: Google Core Libraries for Java (from https://github.com/google/guava.git) - HPPC Collections (from http://labs.carrotsearch.com) - Hibernate Validator Engine (from ) +- IBM COS Java SDK for Amazon S3 (from https://github.com/ibm/ibm-cos-sdk-java) +- IBM COS Java SDK for COS KMS (from https://github.com/ibm/ibm-cos-sdk-java) +- IBM COS SDK For Java (from https://github.com/ibm/ibm-cos-sdk-java) +- IBM COS SDK for Java - Core (from https://github.com/ibm/ibm-cos-sdk-java) - Identity and Access Management (IAM) API v1-rev247-1.23.0 (from ) - J2ObjC Annotations (from https://github.com/google/j2objc/) - J2ObjC Annotations (from https://github.com/google/j2objc/) @@ -336,9 +339,9 @@ The following software have components provided under the terms of this license: - Microsoft Application Insights Log4j 2 Appender (from https://github.com/Microsoft/ApplicationInsights-Java) - Microsoft Azure Netty HTTP Client Library (from https://github.com/Azure/azure-sdk-for-java) - Mockito (from http://mockito.org) -- Mockito (from http://mockito.org) - Mockito (from http://www.mockito.org) - Mockito (from http://mockito.org) +- Mockito (from http://mockito.org) - Mojo's Maven plugin for Cobertura (from http://mojo.codehaus.org/cobertura-maven-plugin/) - Netty Reactive Streams Implementation (from ) - Netty/Buffer (from http://netty.io/) @@ -368,7 +371,6 @@ The following software have components provided under the terms of this license: - OkHttp URLConnection (from ) - OkHttp URLConnection (from ) - Okio (from ) -- Okio (from ) - OpenCensus (from https://github.com/census-instrumentation/opencensus-java) - OpenCensus (from https://github.com/census-instrumentation/opencensus-java) - OpenCensus (from https://github.com/census-instrumentation/opencensus-java) @@ -385,7 +387,6 @@ The following software have components provided under the terms of this license: - Reactive Streams Netty driver (from https://github.com/reactor/reactor-netty) - Retrofit (from ) - Servlet Specification 2.5 API (from ) -- Simple XML (from http://simple.sourceforge.net) - SnakeYAML (from http://www.snakeyaml.org) - Spatial4J (from http://www.locationtech.org/projects/locationtech.spatial4j) - Spring AOP (from https://github.com/spring-projects/spring-framework) @@ -425,8 +426,6 @@ The following software have components provided under the terms of this license: - Spring Transaction (from https://github.com/spring-projects/spring-framework) - Spring Web (from https://github.com/spring-projects/spring-framework) - Spring Web MVC (from https://github.com/spring-projects/spring-framework) -- StAX (from http://stax.codehaus.org/) -- StAX API (from http://stax.codehaus.org/) - T-Digest (from https://github.com/tdunning/t-digest) - Woodstox (from https://github.com/FasterXML/woodstox) - Xerces2-j (from https://xerces.apache.org/xerces2-j/) @@ -457,6 +456,7 @@ The following software have components provided under the terms of this license: - io.grpc:grpc-protobuf-lite (from https://github.com/grpc/grpc-java) - io.grpc:grpc-stub (from https://github.com/grpc/grpc-java) - ion-java (from https://github.com/amznlabs/ion-java/) +- ion-java (from https://github.com/amznlabs/ion-java/) - jackson-databind (from http://github.com/FasterXML/jackson) - java-cloudant (from https://cloudant.com) - java-cloudant (from https://cloudant.com) @@ -479,7 +479,6 @@ The following software have components provided under the terms of this license: - lettuce (from http://github.com/mp911de/lettuce/wiki) - micrometer-core (from https://github.com/micrometer-metrics/micrometer) - micrometer-registry-azure-monitor (from https://github.com/micrometer-metrics/micrometer) -- minio (from https://github.com/minio/minio-java) - org.xmlunit:xmlunit-core (from http://www.xmlunit.org/) - oro (from ) - parent-join (from https://github.com/elastic/elasticsearch) @@ -542,7 +541,6 @@ The following software have components provided under the terms of this license: - Lucene Common Analyzers (from ) - Plexus :: Default Container (from ) - Plexus Common Utilities (from http://plexus.codehaus.org/plexus-utils) -- StAX (from http://stax.codehaus.org/) - Stax2 API (from http://github.com/FasterXML/stax2-api) - jersey-ext-bean-validation (from ) - jersey-spring4 (from ) @@ -616,7 +614,6 @@ CC-BY-3.0 ======================================================================== The following software have components provided under the terms of this license: -- "Java Concurrency in Practice" book annotations (from http://jcip.net/) - FindBugs-jsr305 (from http://findbugs.sourceforge.net/) ======================================================================== @@ -813,7 +810,6 @@ The following software have components provided under the terms of this license: - Microsoft Application Insights Java SDK Spring Boot starter (from https://github.com/Microsoft/ApplicationInsights-Java) - Microsoft Application Insights Java SDK Web Module (from https://github.com/Microsoft/ApplicationInsights-Java) - Microsoft Application Insights Log4j 2 Appender (from https://github.com/Microsoft/ApplicationInsights-Java) -- SpotBugs Annotations (from https://spotbugs.github.io/) - java-getopt (from ) ======================================================================== @@ -825,7 +821,6 @@ The following software have components provided under the terms of this license: - Java Native Access Platform (from https://github.com/java-native-access/jna) - Javassist (from http://www.javassist.org/) - SnakeYAML (from http://www.snakeyaml.org) -- SpotBugs Annotations (from https://spotbugs.github.io/) ======================================================================== LGPL-3.0-only diff --git a/devops/Jenkinsfile b/devops/Jenkinsfile new file mode 100644 index 000000000..84a24dd3c --- /dev/null +++ b/devops/Jenkinsfile @@ -0,0 +1,85 @@ +pipeline { + agent { + kubernetes { + cloud 'openshift' + label 'maven-openjdk18' + yaml """ +spec: + containers: + - name: jnlp + image: quay.io/openshift/origin-jenkins-agent-maven:v4.0.0 + volumeMounts: + - mountPath: "/tmp" + name: "workspace-volume" + readOnly: false + workingDir: "/tmp" + securityContext: + privileged: false + tty: false + resources: + limits: + cpu: 200m + memory: 2Gi + requests: + cpu: 200m + memory: 2Gi + restartPolicy: "Never" +""" + } + } + + environment { + + //Cluster environment variable(CLS_ENV). Like QA, DEV, PERF, PROD etc. + CLS_ENV = "dev" + + //Service variable(CORE_SERVICE). Like indexer, search, delivery, storage, legal etc. + CORE_SERVICE = "indexer" + + //GitHub repo URL credential ID for Environment variable files which saved as Secure text in Jenkins Credential. + GIT_ENV_VAR_PATH_URL = credentials('GitRepo-URL-For-Environment-variables') + + //Personal token variable ID which saved as Secure text in Jenkins Credential. Like: GitHub-PRIVATE-TOKEN. + PRIVATE_TOKEN = credentials('GitHub-PRIVATE-TOKEN') + + def runShell = sh (returnStdout: true, script: "curl --header 'PRIVATE-TOKEN: $PRIVATE_TOKEN' ''$GIT_ENV_VAR_PATH_URL'%2F'$CORE_SERVICE'_'$CLS_ENV'_env.json/raw?ref=master' -s -o env.json") + + } + + stages { + stage('Integration_test') { + environment { + def readContent = readJSON file: 'env.json' + + AUTH_USER_ACCESS = "${readContent['AUTH_USER_ACCESS']}" + AUTH_USER_ACCESS_PASSWORD = "${readContent['AUTH_USER_ACCESS_PASSWORD']}" + DEFAULT_DATA_PARTITION_ID_TENANT1 = "${readContent['DEFAULT_DATA_PARTITION_ID_TENANT1']}" + DEFAULT_DATA_PARTITION_ID_TENANT2 = "${readContent['DEFAULT_DATA_PARTITION_ID_TENANT2']}" + ELASTIC_HOST = "${readContent['ELASTIC_HOST']}" + ELASTIC_PASSWORD = "${readContent['ELASTIC_PASSWORD']}" + ELASTIC_PORT = "${readContent['ELASTIC_PORT']}" + ELASTIC_USER_NAME = "${readContent['ELASTIC_USER_NAME']}" + ENTITLEMENTS_DOMAIN = "${readContent['ENTITLEMENTS_DOMAIN']}" + KEYCLOAK_CLIENT_ID = "${readContent['KEYCLOAK_CLIENT_ID']}" + KEYCLOAK_CLIENT_SECRET = "${readContent['KEYCLOAK_CLIENT_SECRET']}" + KEYCLOAK_REALM = "${readContent['KEYCLOAK_REALM']}" + KEYCLOAK_URL = "${readContent['KEYCLOAK_URL']}" + LEGAL_TAG = "${readContent['LEGAL_TAG']}" + OTHER_RELEVANT_DATA_COUNTRIES = "${readContent['OTHER_RELEVANT_DATA_COUNTRIES']}" + STORAGE_HOST = "${readContent['STORAGE_HOST']}" + SEARCH_HOST = "${readContent['SEARCH_HOST']}" + LEGAL_HOST = "${readContent['LEGAL_HOST']}" + INDEXER_HOST = "${readContent['INDEXER_HOST']}" + ENTITLEMENT_URL = "${readContent['ENTITLEMENT_URL']}" + + } + steps { + script { + sh 'mvn -f testing/indexer-test-ibm/pom.xml test' + } + } + } + } + + +} \ No newline at end of file diff --git a/provider/indexer-ibm/pom.xml b/provider/indexer-ibm/pom.xml index ae6917c09..07b269a72 100644 --- a/provider/indexer-ibm/pom.xml +++ b/provider/indexer-ibm/pom.xml @@ -33,7 +33,7 @@ <packaging>jar</packaging> <properties> - <os-core-lib-ibm.version>0.0.18</os-core-lib-ibm.version> + <os-core-lib-ibm.version>0.3.8-SNAPSHOT</os-core-lib-ibm.version> </properties> <profiles> diff --git a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/security/SecurityConfig.java b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/security/SecurityConfig.java index 2cf274bea..561edbc1e 100644 --- a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/security/SecurityConfig.java +++ b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/security/SecurityConfig.java @@ -29,6 +29,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .csrf().disable() .authorizeRequests() .antMatchers("/", "/index.html", + "/liveness_check", + "/readiness_check", "/index-worker", "/_dps/task-handlers", "/_dps/task-handlers/**", "/reindex", "/v2/api-docs", diff --git a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/GlobalExceptionMapper.java b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/GlobalExceptionMapper.java new file mode 100644 index 000000000..60e0be562 --- /dev/null +++ b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/GlobalExceptionMapper.java @@ -0,0 +1,83 @@ +// Copyright 2017-2019, Schlumberger +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.ibm.util; + +import javax.validation.ValidationException; + +import javassist.NotFoundException; +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; +import org.opengroup.osdu.core.common.model.http.AppException; + +@Order(Ordered.HIGHEST_PRECEDENCE) +@ControllerAdvice +public class GlobalExceptionMapper extends ResponseEntityExceptionHandler { + + @Autowired + private JaxRsDpsLog logger; + + @ExceptionHandler(AppException.class) + protected ResponseEntity<Object> handleAppException(AppException e) { + return this.getErrorResponse(e); + } + + @ExceptionHandler(ValidationException.class) + protected ResponseEntity<Object> handleValidationException(ValidationException e) { + return this.getErrorResponse( + new AppException(HttpStatus.BAD_REQUEST.value(), "Validation error.", e.getMessage(), e)); + } + + @ExceptionHandler(NotFoundException.class) + protected ResponseEntity<Object> handleNotFoundException(NotFoundException e) { + return this.getErrorResponse( + new AppException(HttpStatus.NOT_FOUND.value(), "Resource not found.", e.getMessage(), e)); + } + + @ExceptionHandler(AccessDeniedException.class) + protected ResponseEntity<Object> handleAccessDeniedException(AccessDeniedException e) { + return this.getErrorResponse( + new AppException(HttpStatus.FORBIDDEN.value(), "Access denied", e.getMessage(), e)); + } + + @ExceptionHandler(Exception.class) + protected ResponseEntity<Object> handleGeneralException(Exception e) { + return this.getErrorResponse( + new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Server error.", + "An unknown error has occurred.", e)); + } + + private ResponseEntity<Object> getErrorResponse(AppException e) { + + String exceptionMsg = e.getOriginalException() != null + ? e.getOriginalException().getMessage() + : e.getError().getMessage(); + + if (e.getError().getCode() > 499) { + this.logger.error(exceptionMsg, e); + } else { + this.logger.warning(exceptionMsg, e); + } + + return new ResponseEntity<Object>(e.getError(), HttpStatus.resolve(e.getError().getCode())); + } +} \ No newline at end of file diff --git a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/IndexerQueueTaskBuilderIbm.java b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/IndexerQueueTaskBuilderIbm.java new file mode 100644 index 000000000..a429d1455 --- /dev/null +++ b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/IndexerQueueTaskBuilderIbm.java @@ -0,0 +1,86 @@ +package org.opengroup.osdu.indexer.ibm.util; + +import java.util.Map; + +import javax.inject.Inject; + +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.search.RecordChangedMessages; +import org.opengroup.osdu.core.ibm.messagebus.IMessageFactory; +import org.opengroup.osdu.indexer.util.IndexerQueueTaskBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; + +@Primary +@Component +public class IndexerQueueTaskBuilderIbm extends IndexerQueueTaskBuilder { + private static final Logger logger = LoggerFactory.getLogger(IndexerQueueTaskBuilderIbm.class); + + @Inject + IMessageFactory mq; + + private Gson gson; + private final static String RETRY_STRING = "retry"; + private final static String ERROR_CODE = "errorCode"; + private final static String ERROR_MESSAGE = "errorMessage"; + + @Inject + public void init() { + gson = new Gson(); + } + + @Override + public void createWorkerTask(String payload, DpsHeaders headers) { + createTask(payload, headers); + } + + @Override + public void createReIndexTask(String payload, DpsHeaders headers) { + createTask(payload, headers); + } + + private void createTask(String payload, DpsHeaders headers) { + + try { + RecordChangedMessages receivedPayload = gson.fromJson(payload, RecordChangedMessages.class); + + Map<String, String> attributes = receivedPayload.getAttributes(); + int retryCount = 0; + if (attributes.containsKey(RETRY_STRING)) { + retryCount = Integer.parseInt(attributes.get(RETRY_STRING)); + retryCount++; + } else { + retryCount = 1; + } + attributes.put(RETRY_STRING, String.valueOf(retryCount)); + attributes.put(ERROR_CODE, "999"); //error code TBD + attributes.put(ERROR_MESSAGE, "Indexer could not process record"); + receivedPayload.setAttributes(attributes); + + // incase if we need to shift logic from indexer-queue-ibm/subscriber.java + /* + * if(Integer.parseInt(receivedPayload.getAttributes().get(RETRY_STRING))>3) { + * //add DLQ in IMessageFactory + * + * mq.sendMessage("DLQ", gson.toJson(receivedPayload)); } + */ + logger.info("Message send back to queue : " + receivedPayload); + mq.sendMessage(IMessageFactory.DEFAULT_QUEUE_NAME, gson.toJson(receivedPayload)); + } catch (JsonSyntaxException e) { + logger.error("JsonSyntaxException in IndexerQueueTaskBuilderIbm " + e.toString()); + e.printStackTrace(); + } catch (NumberFormatException e) { + logger.error("NumberFormatException in IndexerQueueTaskBuilderIbm " + e.toString()); + e.printStackTrace(); + } catch (Exception e) { + logger.error("Exception in IndexerQueueTaskBuilderIbm " + e.toString()); + e.printStackTrace(); + } + + } +} \ No newline at end of file diff --git a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/RequestInfoImpl.java b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/RequestInfoImpl.java index 12fd218f8..6ddf941be 100644 --- a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/RequestInfoImpl.java +++ b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/ibm/util/RequestInfoImpl.java @@ -53,10 +53,14 @@ public class RequestInfoImpl implements IRequestInfo { @Inject private TenantInfo tenantInfo; - + @Value("${DEPLOYMENT_ENVIRONMENT}") private String DEPLOYMENT_ENVIRONMENT; + private static final String INDEXER_API_KEY_HEADER="x-api-key"; + + @Value("${INDEXER_API_KEY}") + private String tokenFromProperty; @Override public DpsHeaders getHeaders() { @@ -65,9 +69,19 @@ public class RequestInfoImpl implements IRequestInfo { // throw to prevent null reference exception below throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid Headers", "Headers Map DpsHeaders is null"); } - DpsHeaders headers = this.getCoreServiceHeaders(headersMap.getHeaders()); - return headers; - } + DpsHeaders headers = this.getCoreServiceHeaders(headersMap.getHeaders()); + + if (headers.getHeaders().containsKey(INDEXER_API_KEY_HEADER)) { + String apiToken = headers.getHeaders().get(INDEXER_API_KEY_HEADER); + if (!apiToken.equals(tokenFromProperty)) { + logger.error("Indexer API Token in header is mismatched"); + throw new AppException(HttpStatus.SC_UNAUTHORIZED, "Indexer API Token in header mismatched.", "Indexer API Token in header mismatched."); + } + } else { + throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Missing Header", "The headers "+ INDEXER_API_KEY_HEADER + " is missing!"); + } + return headers; + } @Override public String getPartitionId() { @@ -95,7 +109,8 @@ public class RequestInfoImpl implements IRequestInfo { @Override public boolean isTaskQueueRequest() { - //if (!this.dpsHeaders.getHeaders().containsKey(INDEXER_QUEUE_KEY)) return false; + //if (!this.dpsHeaders.getHeaders().containsKey(INDEXER_API_KEY_HEADER)) return false; + // String queueId = this.headersInfo.getHeadersMap().get(AppEngineHeaders.TASK_QUEUE_NAME); // return queueId.endsWith(Constants.INDEXER_QUEUE_IDENTIFIER); diff --git a/testing/indexer-test-ibm/pom.xml b/testing/indexer-test-ibm/pom.xml index c9119b69b..62842b147 100644 --- a/testing/indexer-test-ibm/pom.xml +++ b/testing/indexer-test-ibm/pom.xml @@ -14,7 +14,7 @@ <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <cucumber.version>1.2.5</cucumber.version> - <os-core-lib-ibm.version>0.0.18</os-core-lib-ibm.version> + <os-core-lib-ibm.version>0.3.8-SNAPSHOT</os-core-lib-ibm.version> </properties> <dependencies> diff --git a/testing/indexer-test-ibm/src/main/resources/apikey.feature b/testing/indexer-test-ibm/src/main/resources/apikey.feature new file mode 100644 index 000000000..4be8e4eb3 --- /dev/null +++ b/testing/indexer-test-ibm/src/main/resources/apikey.feature @@ -0,0 +1,10 @@ +Feature: check the api key in header + This feature check the api key received from headers is matching with configured in environment + + Scenario Outline: compare the api key with configured environment key + When I pass api key + Then compare with key configured in properties file + + Examples: + | apiKey | + | "abcd" | diff --git a/testing/indexer-test-ibm/src/test/java/org/opengroup/osdu/step_definitions/index/record/RunTest.java b/testing/indexer-test-ibm/src/test/java/org/opengroup/osdu/step_definitions/index/record/RunTest.java index 4978ddfcc..897f02c15 100644 --- a/testing/indexer-test-ibm/src/test/java/org/opengroup/osdu/step_definitions/index/record/RunTest.java +++ b/testing/indexer-test-ibm/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 = "classpath:features/indexrecord/IndexRecord.feature", + features = {"classpath:features/indexrecord/IndexRecord.feature","classpath:apikey.feature"}, glue = {"classpath:org.opengroup.osdu.step_definitions/index/record"}, plugin = {"pretty", "junit:target/cucumber-reports/TEST-indexrecord.xml"}) public class RunTest { diff --git a/testing/indexer-test-ibm/src/test/java/org/opengroup/osdu/step_definitions/index/record/Steps.java b/testing/indexer-test-ibm/src/test/java/org/opengroup/osdu/step_definitions/index/record/Steps.java index 518b2b6f9..3c885b11c 100644 --- a/testing/indexer-test-ibm/src/test/java/org/opengroup/osdu/step_definitions/index/record/Steps.java +++ b/testing/indexer-test-ibm/src/test/java/org/opengroup/osdu/step_definitions/index/record/Steps.java @@ -1,8 +1,25 @@ package org.opengroup.osdu.step_definitions.index.record; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; import org.opengroup.osdu.common.RecordSteps; +import org.opengroup.osdu.core.common.Constants; +import org.opengroup.osdu.core.common.http.HttpClient; +import org.opengroup.osdu.core.common.http.HttpRequest; +import org.opengroup.osdu.core.common.http.HttpResponse; +import org.opengroup.osdu.core.common.model.http.AppError; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.search.RecordChangedMessages; +import org.opengroup.osdu.core.ibm.util.Config; import org.opengroup.osdu.util.IBMHTTPClient; +import com.google.gson.Gson; + import cucumber.api.DataTable; import cucumber.api.Scenario; import cucumber.api.java.Before; @@ -48,5 +65,46 @@ public class Steps extends RecordSteps { public void iShouldGetTheNumberDocumentsForTheIndexInTheElasticSearchWithOutSkippedAttribute(int expectedCount, String index, String skippedAttributes) throws Throwable { super.iShouldGetTheNumberDocumentsForTheIndexInTheElasticSearchWithOutSkippedAttribute(expectedCount, index, skippedAttributes); } - + + @When("^I pass api key$") + public void i_pass_the_api_key() { + } + + @Then("^compare with key configured in properties file$") + public void compare_with_key_configured_in_propertiesFile() throws Throwable { + + final String CORRELATION_ID = "1234"; + final String OPENDES = "opendes"; + final Gson gson = new Gson(); + + String INDEXER_API_KEY = Config.getEnvironmentVariable("INDEXER_API_KEY"); + String INDEXER_HOST_URL = Config.getEnvironmentVariable("INDEXER_HOST_URL"); + + RecordChangedMessages recordChangeMessage = new RecordChangedMessages(); + + String data = "[{\"id\":\"opendes:doc:1234\",\"kind\":\"opendes:test:test:1.0.0\",\"op\":\"create\"}]"; + + Map<String, String> attributes = new HashMap<>(); + attributes.put("correlation-id", CORRELATION_ID); + attributes.put("data-partition-id", OPENDES); + recordChangeMessage.setAttributes(attributes); + recordChangeMessage.setData(data); + + String url = StringUtils.join(INDEXER_HOST_URL, Constants.WORKER_RELATIVE_URL); + HttpClient httpClient = new HttpClient(); + DpsHeaders dpsHeaders = new DpsHeaders(); + dpsHeaders.put("x-api-key", INDEXER_API_KEY); + dpsHeaders.put("correlation-id", CORRELATION_ID); + dpsHeaders.put("data-partition-id", OPENDES); + + HttpRequest rq = HttpRequest.post(recordChangeMessage).url(url).headers(dpsHeaders.getHeaders()).build(); + HttpResponse result = httpClient.send(rq); + if(result.hasException().equals(false) && result.getResponseCode() == 500) { + assertTrue(result.getResponseCode() == 500); + } else { + AppError error = gson.fromJson(result.getBody(), AppError.class); + assertFalse("Token is mismatched", error.getCode() == 401); + } + + } } \ No newline at end of file -- GitLab