diff --git a/devops/Jenkinsfile b/devops/Jenkinsfile index 01b440c649f27ae6fdcaa1110e95fff942c18454..84a24dd3c826eb52acad13826d342fd1d493c3b9 100644 --- a/devops/Jenkinsfile +++ b/devops/Jenkinsfile @@ -81,4 +81,5 @@ spec: } } + } \ No newline at end of file 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 0000000000000000000000000000000000000000..4be8e4eb317aac8c9c6af2d7f847fc0896faa62b --- /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 4978ddfccad197628432d4ccb8b5985b4af5017e..897f02c15372583ef985dfc77e2884c4035640fd 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 518b2b6f970c5c07b918d7e0ed31aabe819dcdae..3c885b11ca68f15dc3cad6ba15fae1ae7395b514 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