diff --git a/README.md b/README.md index 0b392ed..201c64f 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,11 @@ Please write here your group number and members ## License -This project is licensed under the MIT License \ No newline at end of file +This project is licensed under the MIT License + + +Group 4 +Team members: +Musabir Musabayli +Hans Henrik Viinalass +Vladislav Stafinjak \ No newline at end of file diff --git a/src/test/java/requests/Availability.java b/src/test/java/requests/Availability.java index 6865493..3ce6ed4 100644 --- a/src/test/java/requests/Availability.java +++ b/src/test/java/requests/Availability.java @@ -32,6 +32,13 @@ public Availability tomorrowFromNow1H(){ return this; } + public Availability yesterdayFromNow1H(){ + final Instant tomorrow = Instant.now().truncatedTo(ChronoUnit.HOURS).minus(1, ChronoUnit.DAYS); + this.availability.put("startDateTime", tomorrow.toString()); + this.availability.put("endDateTime", tomorrow.plus(1, ChronoUnit.HOURS).toString()); + return this; + } + public Response add(final Tutor tutor){ this.availability.put("discount", 0); this.availability.put("privateLocationIds",new JSONArray()); diff --git a/src/test/java/tests/BookingCancellationChecks.java b/src/test/java/tests/BookingCancellationChecks.java index f4983dd..e4417a9 100644 --- a/src/test/java/tests/BookingCancellationChecks.java +++ b/src/test/java/tests/BookingCancellationChecks.java @@ -1,11 +1,102 @@ package tests; +import io.restassured.response.Response; +import org.apache.http.HttpStatus; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import requests.Availability; +import requests.BookingRequest; +import utils.builders.BookingRequestBuilder; +import utils.data_generation.Any; +import utils.data_generation.DefaultBookingData; +import utils.data_generation.StudentCreator; +import utils.data_generation.TutorCreator; +import utils.domain.Student; +import utils.domain.Tutor; import utils.junit_extensions.PropertiesExtension; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; + @ExtendWith(PropertiesExtension.class) public class BookingCancellationChecks { + @Test + public void Tutor_RequestBookingAnd_Decline_Success() { + final Tutor tutor = new TutorCreator(Any.tutorUsername()).create(); + final Student student = new StudentCreator(Any.studentUsername()).create(); + + final Response response = new BookingRequest(new DefaultBookingData().tutorScheduled(tutor, student)) + .asTutor(tutor, student); + + response.then() + .statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + + System.out.println(response.getBody().asString()); + + int bookingId = new JSONObject(response.getBody().asString()).getJSONArray("data").getJSONObject(0).getInt("id"); + Response res = new BookingRequest(new DefaultBookingData().tutorScheduled(tutor, student)).decline(tutor.getCookieFilter(), tutor.getNickname(), bookingId); + String bookingEventStatus = new JSONObject(res.getBody().asString()) + .getJSONObject("data").getJSONArray("instances").getJSONObject(0) + .getString("bookingEventStatus"); + System.out.println(res.getBody().asString()); + System.out.println(bookingEventStatus); + + assertEquals(bookingEventStatus, "cancelledByTutorPending"); + } + + @Test + public void Tutor_RequestBookingAnd_ConfirmAndDecline_Fail() { + final Tutor tutor = new TutorCreator(Any.tutorUsername()).create(); + final Student student = new StudentCreator(Any.studentUsername()).create(); + BookingRequest bookingRequest = new BookingRequest(new DefaultBookingData().tutorScheduled(tutor, student)); + final Response response = bookingRequest.asTutor(tutor, student); + + response.then() + .statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + + //System.out.println(response.getBody().asString()); + + int bookingId = new JSONObject(response.getBody().asString()).getJSONArray("data").getJSONObject(0).getInt("id"); + Response confirm = bookingRequest.confirm(tutor.getCookieFilter(), tutor.getNickname(), bookingId); + Response confirmDecline = bookingRequest.confirmDecline(tutor.getCookieFilter(), tutor.getNickname(), bookingId); + String bookingEventStatus = new JSONObject(confirmDecline.getBody().asString()) + .getJSONObject("data").getJSONArray("instances").getJSONObject(0) + .getString("bookingEventStatus"); + System.out.println(confirmDecline.getBody().asString()); + System.out.println(bookingId); + // System.out.println(bookingEventStatus); + assertEquals(bookingEventStatus, "cancelledByTutorConfirmed"); + + } + + + @Test + public void Student_RequestBookingAnd_Decline_Fail() { + final Tutor tutor = new TutorCreator().create(); + final Student student = new StudentCreator(Any.studentUsername()).create(); + final Response r = new Availability().tomorrowFromNow1H().add(tutor); + r.then().body("description", equalTo("Availability created")); + + final BookingRequestBuilder builder = new DefaultBookingData().bookedByStudentViaTimeSlot(tutor, student); + final Response response = new BookingRequest(builder).asStudent(tutor, student); + response.then().statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + int bookingId = new JSONObject(response.getBody().asString()).getJSONArray("data").getJSONObject(0).getInt("id"); + Response res = new BookingRequest(builder).decline(student.getCookieFilter(), student.getNickname(), bookingId); + System.out.println(res.getBody().asString()); + + String bookingEventStatus = new JSONObject(res.getBody().asString()) + .getJSONObject("data").getJSONArray("instances").getJSONObject(0) + .getString("bookingEventStatus"); + System.out.println(bookingEventStatus); + assertEquals(bookingEventStatus, "cancelledByStudentPending"); + + } + } diff --git a/src/test/java/tests/BookingConfirmationChecks.java b/src/test/java/tests/BookingConfirmationChecks.java index 5dc0272..d586283 100644 --- a/src/test/java/tests/BookingConfirmationChecks.java +++ b/src/test/java/tests/BookingConfirmationChecks.java @@ -1,9 +1,147 @@ package tests; +import io.restassured.response.Response; +import org.apache.http.HttpStatus; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import requests.Availability; +import requests.BookingRequest; +import requests.Invite; +import utils.builders.BookingRequestBuilder; +import utils.data_generation.Any; +import utils.data_generation.DefaultBookingData; +import utils.data_generation.StudentCreator; +import utils.data_generation.TutorCreator; +import utils.domain.Student; +import utils.domain.Tutor; import utils.junit_extensions.PropertiesExtension; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; + @ExtendWith(PropertiesExtension.class) public class BookingConfirmationChecks { + + @Test + public void Tutor_RequestsBookingOnBehalfOfStudentAndConfirm_Success(){ + final Tutor tutor = new TutorCreator(Any.tutorUsername()).create(); + final Student student = new StudentCreator(Any.studentUsername()).create(); + + final Response response = new BookingRequest(new DefaultBookingData().tutorScheduled(tutor, student)) + .asTutor(tutor, student); + + response.then() + .statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + + System.out.println(response.getBody().asString()); + + int bookingId = new JSONObject(response.getBody().asString()).getJSONArray("data").getJSONObject(0).getInt("id"); + Response res = new BookingRequest(new DefaultBookingData().tutorScheduled(tutor, student)).confirm(tutor.getCookieFilter(),tutor.getNickname(), bookingId); + String bookingEventStatus = new JSONObject(res.getBody().asString()) + .getJSONObject("data").getJSONArray("instances").getJSONObject(0) + .getString("bookingEventStatus"); + System.out.println(res.getBody().asString()); + System.out.println(bookingEventStatus); + + assertEquals(bookingEventStatus,"confirmed"); + } + @Test + public void Student_RequestsBookingAndConfirmViaTutorAvailableTimeSlot_Success(){ + //First we create a logged in tutor and student + final Tutor tutor = new TutorCreator(Any.tutorUsername()).create(); + final Student student = new StudentCreator(Any.studentUsername()).create(); + //Tutor adds his/her availability for tomorrow + final Response r = new Availability().tomorrowFromNow1H().add(tutor); + r.then().body("description", equalTo("Availability created")); + // System.out.println(r.getBody().asString()); + //Tutor invites student to join his/her network + sendInviteAndAcceptByStudent(tutor, student); + + //Stetup booking request data + final BookingRequestBuilder builder = new DefaultBookingData().bookedByStudentViaTimeSlot(tutor, student); + // System.out.println(builder.build()); + //Send booking request + final Response response = new BookingRequest(builder).asStudent(tutor, student); + // System.out.println(response.getBody().asString()); + response.then().statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + int bookingId = new JSONObject(response.getBody().asString()).getJSONArray("data").getJSONObject(0).getInt("id"); + Response res = new BookingRequest(builder).confirm(tutor.getCookieFilter(),tutor.getNickname(), bookingId); + String bookingEventStatus = new JSONObject(res.getBody().asString()) + .getJSONObject("data").getJSONArray("instances").getJSONObject(0) + .getString("bookingEventStatus"); + System.out.println(res.getBody().asString()); + System.out.println(bookingEventStatus); + assertEquals(bookingEventStatus,"confirmed"); + + } + + @Test + public void Tutor_RequestBooking_ConfirmAlreadyCancelled_Success(){ + //First we create a logged in tutor and student + final Tutor tutor = new TutorCreator(Any.tutorUsername()).create(); + final Student student = new StudentCreator(Any.studentUsername()).create(); + //Tutor adds his/her availability for tomorrow + final Response r = new Availability().tomorrowFromNow1H().add(tutor); + r.then().body("description", equalTo("Availability created")); + // System.out.println(r.getBody().asString()); + //Tutor invites student to join his/her network + sendInviteAndAcceptByStudent(tutor, student); + + //Stetup booking request data + final BookingRequestBuilder builder = new DefaultBookingData().bookedByStudentViaTimeSlot(tutor, student); + // System.out.println(builder.build()); + //Send booking request + final Response response = new BookingRequest(builder).asStudent(tutor, student); + // System.out.println(response.getBody().asString()); + response.then().statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + int bookingId = new JSONObject(response.getBody().asString()).getJSONArray("data").getJSONObject(0).getInt("id"); + Response decline = new BookingRequest(builder).decline(tutor.getCookieFilter(),tutor.getNickname(), bookingId); + Response confirm = new BookingRequest(builder).confirm(tutor.getCookieFilter(),tutor.getNickname(), bookingId); + String bookingEventStatus = new JSONObject(confirm.getBody().asString()) + .getJSONObject("data").getJSONArray("instances").getJSONObject(0) + .getString("bookingEventStatus"); + System.out.println(confirm.getBody().asString()); + assertEquals(bookingEventStatus,"Action not allowed on the selected booking event"); + + } + + @Test + public void Tutor_ConfirmTwoBookingForSameTime_Fail(){ + //First we create a logged in tutor and student + final Tutor tutor = new TutorCreator(Any.tutorUsername()).create(); + final Student student = new StudentCreator(Any.studentUsername()).create(); + final Student student2 = new StudentCreator(Any.studentUsername()).create(); + final Response r = new Availability().tomorrowFromNow1H().add(tutor); + r.then().body("description", equalTo("Availability created")); + + final BookingRequestBuilder builder = new DefaultBookingData().bookedByStudentViaTimeSlot(tutor, student); + final BookingRequestBuilder builder2 = new DefaultBookingData().bookedByStudentViaTimeSlot(tutor, student2); + final Response response = new BookingRequest(builder).asStudent(tutor, student); + final Response response2 = new BookingRequest(builder).asStudent(tutor, student2); + response.then().statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + response2.then().statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + + } + + + + private void sendInviteAndAcceptByStudent(final Tutor tutor, final Student student) { + new Invite().send(tutor, student.getEmail()); + final Response invitationLinkResponse = new Invite().invitationLink(student); + final int invitationId = new JSONObject(invitationLinkResponse.getBody().asString()).getJSONObject("data").getInt("id"); + + final Response acceptResponse = new Invite().accept(invitationId, student); + acceptResponse.then() + .statusCode(HttpStatus.SC_OK) + .body("success", equalTo(true)); + } + + } diff --git a/src/test/java/tests/BookingRequestChecks.java b/src/test/java/tests/BookingRequestChecks.java index d627d58..6868409 100644 --- a/src/test/java/tests/BookingRequestChecks.java +++ b/src/test/java/tests/BookingRequestChecks.java @@ -60,6 +60,36 @@ public void Student_RequestsBookingViaTutorAvailableTimeSlot_Success(){ .body("description", equalTo("Booking created")); } + @Test + public void Student_RequestsBookingForYesterday_Fail(){ + //First we create a logged in tutor and student + final Tutor tutor = new TutorCreator(Any.tutorUsername()).create(); + final Student student = new StudentCreator(Any.studentUsername()).create(); + + final Response r = new Availability().yesterdayFromNow1H().add(tutor); + System.out.println(r.getBody().asString()); + + r.then().body("description", equalTo("Cannot book for yesterday")); + + } + + + @Test + public void Student_RequestsBookingSendingNoInvitationRequestViaTutorAvailableTimeSlot_Success(){ + Tutor tutor = new TutorCreator().create(); + final Student student = new StudentCreator().create(); + + final Response r = new Availability().tomorrowFromNow1H().add(tutor); + r.then().body("description", equalTo("Availability created")); + System.out.println(r.getBody().asString()); + + final BookingRequestBuilder builder = new DefaultBookingData().bookedByStudentViaTimeSlot(tutor, student); + final Response response = new BookingRequest(builder).asStudent(tutor, student); + System.out.println(response.getBody().asString()); + response.then().statusCode(HttpStatus.SC_OK) + .body("description", equalTo("Booking created")); + } + private void sendInviteAndAcceptByStudent(final Tutor tutor, final Student student) { new Invite().send(tutor, student.getEmail()); final Response invitationLinkResponse = new Invite().invitationLink(student); @@ -67,8 +97,8 @@ private void sendInviteAndAcceptByStudent(final Tutor tutor, final Student stude final Response acceptResponse = new Invite().accept(invitationId, student); acceptResponse.then() - .statusCode(HttpStatus.SC_OK) - .body("success", equalTo(true)); + .statusCode(HttpStatus.SC_OK) + .body("success", equalTo(true)); } -} +} \ No newline at end of file