Java tutorial
/* * Copyright 2014 Guillaume Bailleul * * 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 net.gbmb.collector; import com.jayway.restassured.response.ValidatableResponse; import cucumber.api.PendingException; import cucumber.api.java.en.And; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; import org.apache.http.entity.ContentType; import org.hamcrest.Matcher; import org.springframework.http.MediaType; import java.util.Random; import java.util.UUID; import java.util.regex.Pattern; import static com.jayway.restassured.RestAssured.given; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; public class CollectionStepdefs { private String currentCid; @Given("^a clean proof service$") public void init_client() throws Throwable { // ensure test implementations are used // TODO ensure environment is clean } @When("^I cancel a collection named \"(.*?)\"$") public void cancel_collection_named(String cid) throws Throwable { response = given().when().header("ContentLength", "0").put(String.format("/records/%s/cancel", cid)).then(); } @When("^I cancel it$") public void cancel_collection_not_named() throws Throwable { cancel_collection_named(currentCid); } @Then("^I find it in final storage$") public void I_find_it_in_final_storage() throws Throwable { // check existence in final storage byte[] is = given().when().get(String.format("/test/final/%s", currentCid)).then().statusCode(200).extract() .asByteArray(); assertNotNull(is); } @Then("^I do not find it in final storage$") public void I_do_not_find_it_in_final_storage() throws Throwable { // check existence in final storage given().log().all().when().get(String.format("/test/final/%s", currentCid)).then().statusCode(406); } @Then("^I add in it a simple record with (.+)$") public void i_add_in_it_a_simple_record_with(String json) throws Throwable { response = given().log().all().when().contentType(ContentType.APPLICATION_JSON.getMimeType()).body(json) .post(String.format("/records/%s/add", currentCid)).then(); } @Then("^I add in it an attachment of (\\d+) bytes with (.+)$") public void i_add_in_it_an_attachment_of_bytes_with(int arg1, String json) throws Throwable { byte[] buffer = new byte[arg1]; new Random().nextBytes(buffer); response = given().log().all().when().contentType(ContentType.MULTIPART_FORM_DATA.getMimeType()) .multiPart("attached", new String(buffer), MediaType.APPLICATION_OCTET_STREAM_VALUE) .multiPart("record", json.getBytes(), MediaType.APPLICATION_JSON_VALUE) .post(String.format("/records/%s/attach", currentCid)).then(); } @Then("^ensure creating \"(.*?)\" ends in \"(.*?)\"$") public void ensure_creating_ends_in(String cid, String status) throws Throwable { boolean shouldSuccess = status.equalsIgnoreCase("accepted"); given().when().header("ContentLength", "0").put(String.format("/records/%s/create", cid)).then() .statusCode(shouldSuccess ? 200 : 400).extract(); if (shouldSuccess) { // clean the collection (cancel it) given().when().header("ContentLength", "0").put(String.format("/records/%s/cancel", cid)).then() .statusCode(204).extract(); } } private ValidatableResponse response; @When("^I create a collection named \"([^\"]+)\" in application named \"([^\"]+)\"$") public void i_create_a_collection_named_in_application_named(String cid, String app) throws Throwable { response = given().when().header("ContentLength", "0") .put(String.format("/records/%s/create?app=%s", cid, app)).then(); currentCid = cid; } @When("^I create a collection named \"([^\"]+)\"$") public void create_collection_named(String cid) throws Throwable { response = given().when().header("ContentLength", "0").put(String.format("/records/%s/create", cid)).then(); currentCid = cid; } @When("^I create a not named collection in application named \"([^\"]+)\"$") public void i_create_a_not_named_collection_in_application_named(String app) throws Throwable { response = given().when().header("ContentLength", "0").post(String.format("/records/create?app=%s", app)) .then(); } @Then("^I am warned a parameter is invalid$") public void i_am_warned_a_parameter_is_invalid() throws Throwable { response.statusCode(400); } @Then("^I am advised the collection is created$") public void i_am_advised_the_collection_is_created() throws Throwable { Collection col = response.statusCode(200).extract().as(Collection.class); // TODO more check ? } @When("^I create a not named collection$") public void I_create_a_not_named_collection() throws Throwable { response = given().when().header("ContentLength", "0").post("/records/create").then(); } @And("^I receive the identifier as UUID$") public void I_receive_the_identifier_as_UUID() throws Throwable { Collection col = response.statusCode(200).extract().as(Collection.class); UUID.fromString(col.getId()); currentCid = col.getId(); } @When("^I create a not named collection in test mode$") public void I_create_a_not_named_collection_in_test_mode() throws Throwable { response = given().when().header("ContentLength", "0") .post(String.format("/records/create?mode=%s", "test")).then(); } @When("^I create a collection named \"([^\"]*)\" in test mode$") public void I_create_a_collection_named_in_test_mode(String cid) throws Throwable { response = given().when().header("ContentLength", "0") .put(String.format("/records/%s/create?mode=test", cid)).then(); currentCid = cid; } @Then("^I am warned a conflict occured$") public void I_am_warned_a_conflict_occured() throws Throwable { response.statusCode(409); } @When("^I end a collection named \"([^\"]*)\"$") public void end_collection_named(String cid) throws Throwable { response = given().when().header("ContentLength", "0").put(String.format("/records/%s/end", cid)).then(); } @When("^I end it$") public void end_collection_not_named() throws Throwable { end_collection_named(currentCid); } @Then("^I am advised the operation succeeded$") public void I_am_advised_the_operation_succeeded() throws Throwable { response.statusCode(200); } @Then("^I am advised the operation succeeded returning nothing$") public void I_am_advised_the_operation_succeeded_no_content() throws Throwable { response.statusCode(204); } @Then("^I find a collection named \"([^\"]*)\" in \"(\\w+)\" state$") public void named_collection_in_state(String cid, String stateString) { CollectionState state = CollectionState.valueOf(stateString); CollectionDetail details = given().when().header("ContentLength", "0") .get(String.format("/records/%s", cid)).then().statusCode(200).extract().as(CollectionDetail.class); assertEquals(state, details.getCollection().getState()); } @Then("^I find it in \"(\\w+)\" state$") public void I_find_it_in_a_state(String stateString) throws Throwable { named_collection_in_state(currentCid, stateString); } }