Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ro.upt.test;
- import io.cucumber.cienvironment.internal.com.eclipsesource.json.JsonObject;
- import io.cucumber.java.en.Given;
- import io.cucumber.java.en.Then;
- import io.cucumber.java.en.When;
- import io.restassured.RestAssured;
- import io.restassured.response.Response;
- import io.restassured.specification.RequestSpecification;
- import org.junit.jupiter.api.Assertions;
- import java.util.concurrent.ThreadLocalRandom;
- public class StepDefinitions {
- private static final String BASE_URL = "https://fakerestapi.azurewebsites.net";
- private static final RequestSpecification requestSpecification;
- private static Response response;
- private static int randomId;
- private static int idToCheck_Scenario3;
- static {
- RestAssured.baseURI = BASE_URL;
- // ToDo: Uncomment if you run the tests from the lab environment
- RestAssured.proxy("proxy.intranet.cs.upt.ro", 3128);
- requestSpecification = RestAssured.given();
- }
- @Given("A list of activities is available")
- public void aListOfActivitiesIsAvailabie() {
- response = requestSpecification.get("/api/v1/Activities");
- Assertions.assertEquals(200, response.getStatusCode());
- }
- @When("The user requests for an activity with a specific id")
- public void theUserRequestsForAnActivityWithASpecificId() {
- response = requestSpecification
- .get("/api/v1/Activities/1");
- }
- @Then("The requested activity should be returned")
- public void theRequestedActivityShouldBeReturned() {
- Assertions.assertEquals(200, response.getStatusCode());
- Assertions.assertEquals(1, response.jsonPath().getInt("id"));
- Assertions.assertEquals("Activity 1", response.jsonPath().getString("title"));
- Assertions.assertEquals(false, response.jsonPath().getBoolean("completed"));
- }
- @Given("An activity id that does not exist")
- public void getActivityIdThatDoesNotExist(){
- RestAssured.baseURI = BASE_URL;
- //requestSpecification = RestAssured.given();
- randomId = ThreadLocalRandom.current().nextInt(100, 10000 + 1); // get random id between 100 and 10k
- response = requestSpecification.get("/api/v1/Activities/" + randomId);
- Assertions.assertEquals(404, response.getStatusCode());
- }
- @When("The user creates a new activity with that id")
- public void userCreatesNewActivityWithId(){
- JsonObject requestParams = new JsonObject();
- requestParams.add("id", randomId);
- requestParams.add("title", "SomeActivityTitle");
- requestParams.add("dueDate", "2024-11-29T13:06:46.518Z");
- requestParams.add("completed", false);
- response = requestSpecification
- .contentType("application/json")
- .body(requestParams.toString())
- .post("/api/v1/Activities");
- }
- @Then("The activity should be created successfully")
- public void activityShouldBeCreatedSuccessfully(){
- Assertions.assertEquals(200, response.getStatusCode());
- }
- @Given("An author id that already exists")
- public void authorIdThatAlreadyExists(){
- idToCheck_Scenario3 = 1;
- response = requestSpecification.get("/api/v1/Authors/" + idToCheck_Scenario3);
- Assertions.assertEquals(200, response.getStatusCode());
- }
- @When("The user deletes the author with the specific id")
- public void userDeletesTheAuthorWithTheSpecificId(){
- response = requestSpecification
- .delete("/api/v1/Authors/" + idToCheck_Scenario3);
- }
- @Then("The author should be successfully deleted")
- public void authorShouldBeSuccessfullyDeleted(){
- Assertions.assertEquals(200, response.getStatusCode());
- }
- @Given("A list of books is available")
- public void listOfBooksIsAvailable(){
- }
- @When("The user requests for all books")
- public void userRequestsForAllBooks(){
- response = requestSpecification.get("/api/v1/Books");
- }
- @Then("All books should be returned")
- public void allBooksShouldBeReturned(){
- Assertions.assertEquals(200, response.getStatusCode());
- // Take the list returned by the GET request, and check its size (in our case, the API returns a list with 200 elements)
- Assertions.assertEquals(200, response.jsonPath().getList("").size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement