diff --git a/src/test/java/api/test/rest/batch/BatchStepDefinitions.java b/src/test/java/api/test/rest/batch/BatchStepDefinitions.java new file mode 100644 index 0000000000000000000000000000000000000000..48c1b9ebe60e42d0e3f3a7cd0316a60a81efa0fa --- /dev/null +++ b/src/test/java/api/test/rest/batch/BatchStepDefinitions.java @@ -0,0 +1,157 @@ +/* +Copyright (c) 2018 Vereign AG [https://www.vereign.com] + +This is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package api.test.rest.batch; + +import api.test.core.BaseStepDefinitions; +import api.test.rest.RestSessionContainer; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.jayway.jsonpath.JsonPath; +import core.DataContainer; +import core.Request; +import core.Response; +import core.RestClient; +import cucumber.api.java.en.Given; +import cucumber.api.java.en.Then; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import utils.RAFRandomize; + +import java.util.Base64; + +import static org.junit.Assert.assertTrue; + +public class BatchStepDefinitions extends BaseStepDefinitions { + private static final Logger logger = LogManager.getLogger(BatchStepDefinitions.class.getSimpleName()); + RestSessionContainer restSessionContainer; + Request currentRequest; + + public BatchStepDefinitions(RestSessionContainer restSessionContainer, Request currentRequest, DataContainer dataContainer) { + super(dataContainer); + this.restSessionContainer = restSessionContainer; + this.currentRequest = currentRequest; + } + + @Given("^I send a new batch request via API$") + public void I_send_a_new_batch_request_via_API() { + currentRequest.setPath("/sendBatch"); + + Response response = RestClient.post(currentRequest); + addRequest(currentRequest); + addResponse(response); + } + + @Given("^I call getStatuses request via API$") + public void I_call_getStatuses_request_via_API() { + currentRequest.setPath("/getStatuses"); + + Response response = RestClient.post(currentRequest); + addRequest(currentRequest); + addResponse(response); + } + + @Then("^I assert that getStatus status is the same as the sendBatch value via API$") + public void I_assert_that_getStatus_status_is_the_same_as_the_sendBatch_value_via_API() { + String responseBody = getLastResponse().getBody(); + if (restSessionContainer.getBatchValue() != null) { + String actualValue = JsonPath.read(responseBody, "$..Status").toString(); + assertTrue("'Batch Value' doesn't match expected claim: ", actualValue.contains(restSessionContainer.getBatchValue())); + } + } + + + @Given("^I call getStatuses with the current (sealKey|sealKey2) via API$") + public void I_call_getStatuses_with_the_current_sealKey_via_API(String sealKey) { + Gson gson = new Gson(); + JsonElement initialBody = gson.fromJson(currentRequest.getBody(), JsonElement.class); + // only try to load body as JsonObject if it is not null. + JsonObject newBody = new JsonObject(); + if (initialBody != null) newBody = initialBody.getAsJsonObject(); + + switch (sealKey) { + case "sealKey": + newBody.addProperty("key", restSessionContainer.getBatchKey()); + break; + case "sealKey2": + newBody.addProperty("key", restSessionContainer.getBatchKey2()); + break; + } + + currentRequest.setBody(gson.toJson(newBody)); + + I_call_getStatuses_request_via_API(); + } + + @Given("^I send a new random batch request via API$") + public void I_send_a_new_random_batch_request_via_API() throws Throwable { + String originalInput = "batchValue+" + RAFRandomize.getAlphabetic(5, 5); + String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); + restSessionContainer.setBatchValue(encodedString); + + String batchKey = "seal_id_" + RAFRandomize.getAlphabetic(8, 8); + restSessionContainer.setBatchKey(batchKey); + + Gson gson = new Gson(); + JsonElement initialBody = gson.fromJson(currentRequest.getBody(), JsonElement.class); + // only try to load body as JsonObject if it is not null. + JsonObject newBody = new JsonObject(); + JsonObject batch = new JsonObject(); + if (initialBody != null) newBody = initialBody.getAsJsonObject(); + batch.addProperty(batchKey, encodedString); + newBody.add("batch", batch); + currentRequest.setBody(gson.toJson(newBody)); + + I_send_a_new_batch_request_via_API(); + } + + @Given("^I send a new random batch request with 2 objects via API$") + public void I_send_a_new_random_batch_request_with_2_objects_via_API() throws Throwable { + String originalInput = "batchValue+" + RAFRandomize.getAlphabetic(5, 5); + String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); + restSessionContainer.setBatchValue(encodedString); + + String batchKey = "seal_id_" + RAFRandomize.getAlphabetic(8, 8); + String batchKey2 = "seal_id_" + RAFRandomize.getAlphabetic(8, 8); + restSessionContainer.setBatchKey(batchKey); + restSessionContainer.setBatchKey2(batchKey2); + + Gson gson = new Gson(); + JsonElement initialBody = gson.fromJson(currentRequest.getBody(), JsonElement.class); + // only try to load body as JsonObject if it is not null. + JsonObject newBody = new JsonObject(); + JsonObject batch = new JsonObject(); + if (initialBody != null) newBody = initialBody.getAsJsonObject(); + batch.addProperty(batchKey, encodedString); + batch.addProperty(batchKey2, encodedString); + newBody.add("batch", batch); + currentRequest.setBody(gson.toJson(newBody)); + + I_send_a_new_batch_request_via_API(); + } + + @Given("^I send a total count request via API$") + public void I_send_a_total_count_request_via_API() { + currentRequest.setPath("/getTotalCount"); + + Response response = RestClient.post(currentRequest); + addRequest(currentRequest); + addResponse(response); + } + +} \ No newline at end of file diff --git a/src/test/resources/features/batch/api/getStatuses/POST.feature b/src/test/resources/features/batch/api/getStatuses/POST.feature new file mode 100644 index 0000000000000000000000000000000000000000..beff4258066ed6153cabd7c0870ea0aaae4b9090 --- /dev/null +++ b/src/test/resources/features/batch/api/getStatuses/POST.feature @@ -0,0 +1,91 @@ +#Copyright (c) 2018 Vereign AG [https://www.vereign.com] +# +#This is free software: you can redistribute it and/or modify +#it under the terms of the GNU Affero General Public License as +#published by the Free Software Foundation, either version 3 of the +#License, or (at your option) any later version. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU Affero General Public License for more details. +# +#You should have received a copy of the GNU Affero General Public License +#along with this program. If not, see <http://www.gnu.org/licenses/>. + +#http://localhost/api/getStatuses +#Author: Rosen Georgiev rosen.georgiev@vereign.com + +@rest @batch @all +Feature: API - getStatuses POST + Get the previously added Batches + + Background: + Given we are testing the VIAM Api + + @getStatuses @test + Scenario: Send a batch request and then fetch it with getStatuses - Positive +#Send Batch + Given I load the REST request {Batch.json} with profile {successful_batch} + Given I send a new random batch request via API + And the status code should be {200} +#Get Statuses + Then I wait for {60000} mseconds + Given I clear the request body + Then I call getStatuses with the current sealKey via API + And the status code should be {200} + And the field {$.status} has the value {OK} + And the field {$.code} has the value {200} + And the response is valid according to the {Batch_GetStatuses_schema.json} REST schema + And I assert that getStatus status is the same as the sendBatch value via API + + @getStatuses @negative + Scenario Outline: Try to call getStatuses with invalid request - <profile> - Negative +#Get Statuses + Given I clear the request body + Given I load the REST request {Batch.json} with profile {<profile>} + Then I call getStatuses request via API + And the status code should be {400} + And the field {$.message} has the value {<msg>} + Examples: + | profile | msg | + | getStatuses_missing_key | sealID (key) is missing | + | getStatuses_missing_value | sealID (key) is missing | + | getStatuses_empty | sealID (key) is missing | + + @sendbatch + Scenario: Send a batch requests with 2 objects and then get them - Positive +#Send Batch + Given I load the REST request {Batch.json} with profile {successful_batch} + Then I send a new random batch request with 2 objects via API + And the status code should be {200} + And the field {message} has the value {batch is queued} +#Get Statuses of seal key 1 + Then I wait for {60000} mseconds + Given I clear the request body + Then I call getStatuses with the current sealKey via API + And the status code should be {200} + And the response is valid according to the {Batch_GetStatuses_schema.json} REST schema + And I assert that getStatus status is the same as the sendBatch value via API +#Get Statuses of seal key 2 + Given I clear the request body + Then I call getStatuses with the current sealKey2 via API + And the status code should be {200} + And the response is valid according to the {Batch_GetStatuses_schema.json} REST schema + And I assert that getStatus status is the same as the sendBatch value via API + + @sendbatch + Scenario: Send 2 separate batch requests for the same key and then get them - Positive +#Send Batch + Given I load the REST request {Batch.json} with profile {successful_batch} + Given I send a new random batch request via API + And the status code should be {200} + Then I send a new batch request via API + And the status code should be {200} + And the field {message} has the value {batch is queued} +#Get Statuses + Then I wait for {120000} mseconds + Given I clear the request body + Then I call getStatuses with the current sealKey via API + And the status code should be {200} + And the field {$..Status} contains {2} elements \ No newline at end of file diff --git a/src/test/resources/features/batch/api/getTotalCount/POST.feature b/src/test/resources/features/batch/api/getTotalCount/POST.feature new file mode 100644 index 0000000000000000000000000000000000000000..adcef82bab203cf994fa7f1110c52aa6bfe622a7 --- /dev/null +++ b/src/test/resources/features/batch/api/getTotalCount/POST.feature @@ -0,0 +1,41 @@ +#Copyright (c) 2018 Vereign AG [https://www.vereign.com] +# +#This is free software: you can redistribute it and/or modify +#it under the terms of the GNU Affero General Public License as +#published by the Free Software Foundation, either version 3 of the +#License, or (at your option) any later version. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU Affero General Public License for more details. +# +#You should have received a copy of the GNU Affero General Public License +#along with this program. If not, see <http://www.gnu.org/licenses/>. + +#http://localhost/api/sendbatch +#Author: Georgi Michev georgi.michev@vereign.como + +@rest @batch @all +Feature: API - getTotalCount POST + Retrieve counters for processed seal statuses and batches + + Background: + Given we are testing the VIAM Api + + @getTotalCount + Scenario: Send request with empty body - Positive + #Send Request + Given I send a total count request via API + And the status code should be {200} + And the response is valid according to the {Batch_GetTotalCount_schema.json} REST schema + And the field {data} is present and not empty + And the field {data} is not equal to the value {0} + And the field {receivedStatus} is present and not empty + And the field {receivedStatus} is not equal to the value {0} + And the field {storedStatus} is present and not empty + And the field {storedStatus} is not equal to the value {0} + And the field {anchoredStatus} is present and not empty + And the field {anchoredStatus} is not equal to the value {0} + And the field {anchoredBatches} is present and not empty + And the field {anchoredBatches} is not equal to the value {0} diff --git a/src/test/resources/features/batch/api/sendBatch/POST.feature b/src/test/resources/features/batch/api/sendBatch/POST.feature new file mode 100644 index 0000000000000000000000000000000000000000..005ee34866dd37d73ad61bfb7e784fbad880e48d --- /dev/null +++ b/src/test/resources/features/batch/api/sendBatch/POST.feature @@ -0,0 +1,61 @@ +#Copyright (c) 2018 Vereign AG [https://www.vereign.com] +# +#This is free software: you can redistribute it and/or modify +#it under the terms of the GNU Affero General Public License as +#published by the Free Software Foundation, either version 3 of the +#License, or (at your option) any later version. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU Affero General Public License for more details. +# +#You should have received a copy of the GNU Affero General Public License +#along with this program. If not, see <http://www.gnu.org/licenses/>. + +#http://localhost/api/sendbatch +#Author: Rosen Georgiev rosen.georgiev@vereign.com + +@rest @batch @all +Feature: API - sendBatch POST + This all sends batch request + + Background: + Given we are testing the VIAM Api + + @sendbatch + Scenario: Send a new random batch - Positive +#Send Batch + Given I load the REST request {Batch.json} with profile {successful_batch} + Given I send a new random batch request via API + And the status code should be {200} + And the field {message} has the value {batch is queued} + + @sendbatch + Scenario: Send 2 batch requests for the same key - Positive +#Send Batch + Given I load the REST request {Batch.json} with profile {successful_batch} + Given I send a new random batch request via API + And the status code should be {200} + Then I send a new batch request via API + And the status code should be {200} + And the field {message} has the value {batch is queued} + + @sendbatch @negative + Scenario Outline: Try to send invalid batch requests for sendBatch - <profile> - Negative +#Send Batch + Given I load the REST request {Batch.json} with profile {<profile>} + Given I send a new batch request via API + And the status code should be {<code>} + And the field {message} has the value {<message>} + And the field {kind} has the value {<kind>} + And the field {id} is present and not empty + Examples: + | profile | message | code | kind | + | missing_batch | no status entries to process | 400 | 1 | + | empty_seal_key | invalid batch | 400 | 1 | + | empty_seal_value | invalid batch | 400 | 1 | + | missing_seal | no status entries to process | 400 | 1 | + | not_base64_value | invalid batch | 400 | 1 | + +