Advertisement
regzarr

SVV - Cucumber lab

Nov 26th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. package ro.upt.test;
  2.  
  3. import io.cucumber.cienvironment.internal.com.eclipsesource.json.JsonObject;
  4. import io.cucumber.java.en.Given;
  5. import io.cucumber.java.en.Then;
  6. import io.cucumber.java.en.When;
  7. import io.restassured.RestAssured;
  8. import io.restassured.response.Response;
  9. import io.restassured.specification.RequestSpecification;
  10. import org.junit.jupiter.api.Assertions;
  11.  
  12. import java.util.concurrent.ThreadLocalRandom;
  13.  
  14. public class StepDefinitions {
  15.     private static final String BASE_URL = "https://fakerestapi.azurewebsites.net";
  16.     private static final RequestSpecification requestSpecification;
  17.     private static Response response;
  18.  
  19.     private static int randomId;
  20.     private static int idToCheck_Scenario3;
  21.  
  22.     static {
  23.         RestAssured.baseURI = BASE_URL;
  24.         // ToDo: Uncomment if you run the tests from the lab environment
  25.         RestAssured.proxy("proxy.intranet.cs.upt.ro", 3128);
  26.         requestSpecification = RestAssured.given();
  27.     }
  28.  
  29.     @Given("A list of activities is available")
  30.     public void aListOfActivitiesIsAvailabie() {
  31.         response = requestSpecification.get("/api/v1/Activities");
  32.         Assertions.assertEquals(200, response.getStatusCode());
  33.     }
  34.  
  35.     @When("The user requests for an activity with a specific id")
  36.     public void theUserRequestsForAnActivityWithASpecificId() {
  37.         response = requestSpecification
  38.                 .get("/api/v1/Activities/1");
  39.     }
  40.  
  41.     @Then("The requested activity should be returned")
  42.     public void theRequestedActivityShouldBeReturned() {
  43.         Assertions.assertEquals(200, response.getStatusCode());
  44.         Assertions.assertEquals(1, response.jsonPath().getInt("id"));
  45.         Assertions.assertEquals("Activity 1", response.jsonPath().getString("title"));
  46.         Assertions.assertEquals(false, response.jsonPath().getBoolean("completed"));
  47.     }
  48.  
  49.     @Given("An activity id that does not exist")
  50.     public void getActivityIdThatDoesNotExist(){
  51.         RestAssured.baseURI = BASE_URL;
  52.         //requestSpecification = RestAssured.given();
  53.         randomId = ThreadLocalRandom.current().nextInt(100, 10000 + 1); // get random id between 100 and 10k
  54.         response = requestSpecification.get("/api/v1/Activities/" + randomId);
  55.  
  56.         Assertions.assertEquals(404, response.getStatusCode());
  57.     }
  58.  
  59.     @When("The user creates a new activity with that id")
  60.     public void userCreatesNewActivityWithId(){
  61.  
  62.         JsonObject requestParams = new JsonObject();
  63.         requestParams.add("id", randomId);
  64.         requestParams.add("title", "SomeActivityTitle");
  65.         requestParams.add("dueDate", "2024-11-29T13:06:46.518Z");
  66.         requestParams.add("completed", false);
  67.  
  68.         response = requestSpecification
  69.                 .contentType("application/json")
  70.                 .body(requestParams.toString())
  71.                 .post("/api/v1/Activities");
  72.     }
  73.  
  74.     @Then("The activity should be created successfully")
  75.     public void activityShouldBeCreatedSuccessfully(){
  76.         Assertions.assertEquals(200, response.getStatusCode());
  77.     }
  78.  
  79.     @Given("An author id that already exists")
  80.     public void authorIdThatAlreadyExists(){
  81.         idToCheck_Scenario3 = 1;
  82.         response = requestSpecification.get("/api/v1/Authors/" + idToCheck_Scenario3);
  83.  
  84.         Assertions.assertEquals(200, response.getStatusCode());
  85.     }
  86.  
  87.     @When("The user deletes the author with the specific id")
  88.     public void userDeletesTheAuthorWithTheSpecificId(){
  89.         response = requestSpecification
  90.                 .delete("/api/v1/Authors/" + idToCheck_Scenario3);
  91.     }
  92.  
  93.     @Then("The author should be successfully deleted")
  94.     public void authorShouldBeSuccessfullyDeleted(){
  95.         Assertions.assertEquals(200, response.getStatusCode());
  96.     }
  97.  
  98.     @Given("A list of books is available")
  99.     public void listOfBooksIsAvailable(){
  100.  
  101.     }
  102.  
  103.     @When("The user requests for all books")
  104.     public void userRequestsForAllBooks(){
  105.         response = requestSpecification.get("/api/v1/Books");
  106.     }
  107.  
  108.     @Then("All books should be returned")
  109.     public void allBooksShouldBeReturned(){
  110.         Assertions.assertEquals(200, response.getStatusCode());
  111.         // Take the list returned by the GET request, and check its size (in our case, the API returns a list with 200 elements)
  112.         Assertions.assertEquals(200, response.jsonPath().getList("").size());
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement