├── .gitignore ├── README.md ├── pom.xml └── src ├── main └── java │ └── Entities │ ├── Booking.java │ ├── BookingDates.java │ └── User.java └── test ├── java └── BookingTests.java └── resources ├── createBookingRequestSchema.json └── createBookingResponseSchema.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/java 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=java 3 | 4 | .idea/* 5 | .allure/* 6 | 7 | ### Java ### 8 | # Compiled class file 9 | *.class 10 | 11 | # Log file 12 | *.log 13 | 14 | # BlueJ files 15 | *.ctxt 16 | 17 | # Mobile Tools for Java (J2ME) 18 | .mtj.tmp/ 19 | 20 | # Package Files # 21 | *.jar 22 | *.war 23 | *.nar 24 | *.ear 25 | *.zip 26 | *.tar.gz 27 | *.rar 28 | 29 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 30 | hs_err_pid* 31 | replay_pid* 32 | 33 | ### Maven ### 34 | target/ 35 | pom.xml.tag 36 | pom.xml.releaseBackup 37 | pom.xml.versionsBackup 38 | pom.xml.next 39 | release.properties 40 | dependency-reduced-pom.xml 41 | buildNumber.properties 42 | .mvn/timing.properties 43 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 44 | .mvn/wrapper/maven-wrapper.jar 45 | 46 | # Eclipse m2e generated files 47 | # Eclipse Core 48 | .project 49 | # JDT-specific (Eclipse Java Development Tools) 50 | .classpath 51 | 52 | # End of https://www.topta 53 | /*.iml 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/digitalinnovationone/api-automation-tests-challenge-rest-assured/8b42ec57ef613bb3574dda11e33c9830e9544379/README.md -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | api-automation-tests-challenge-rest-assured 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 8 13 | 8 14 | UTF-8 15 | UTF-8 16 | 1.8 17 | 5.5.1 18 | 5.9.0 19 | 1.9.0 20 | 1.9.9.1 21 | 2.19.0 22 | 23 | https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline 24 | 25 | 26 | 27 | 28 | 29 | io.rest-assured 30 | rest-assured 31 | 4.5.1 32 | test 33 | 34 | 35 | io.rest-assured 36 | json-path 37 | 4.5.1 38 | test 39 | 40 | 41 | io.rest-assured 42 | json-schema-validator 43 | 4.5.1 44 | test 45 | 46 | 47 | org.junit.jupiter 48 | junit-jupiter-engine 49 | 5.9.0 50 | test 51 | 52 | 53 | org.junit.platform 54 | junit-platform-runner 55 | 1.9.0 56 | test 57 | 58 | 59 | com.github.javafaker 60 | javafaker 61 | 1.0.2 62 | 63 | 64 | org.slf4j 65 | slf4j-simple 66 | 2.0.1 67 | 68 | 69 | io.qameta.allure 70 | allure-junit5 71 | 2.19.0 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-surefire-plugin 80 | 2.22.2 81 | 82 | false 83 | 84 | -Dfile.encoding=UTF-8 85 | -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar" 86 | 87 | 88 | ${project.build.directory}/allure-results 89 | true 90 | true 91 | dynamic 92 | 93 | 94 | 95 | 96 | org.aspectj 97 | aspectjweaver 98 | ${aspectj.version} 99 | 100 | 101 | 102 | 103 | 104 | io.qameta.allure 105 | allure-maven 106 | 2.10.0 107 | 108 | ${allure.version} 109 | ${project.build.directory}/allure-results 110 | 111 | 112 | 113 | 114 | org.apache.maven.plugins 115 | maven-compiler-plugin 116 | 3.8.1 117 | 118 | 1.8 119 | 1.8 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /src/main/java/Entities/Booking.java: -------------------------------------------------------------------------------- 1 | package Entities; 2 | 3 | 4 | public class Booking { 5 | private String lastname; 6 | private String firstname; 7 | private float totalprice; 8 | private boolean depositpaid; 9 | private BookingDates bookingdates; 10 | private String additionalneeds; 11 | 12 | public Booking(String firstName, String lastName, float totalPrice, boolean depositPaid, BookingDates bookingDates, String additionalNeeds) { 13 | this.firstname = firstName; 14 | this.lastname = lastName; 15 | this.totalprice = totalPrice; 16 | this.depositpaid = depositPaid; 17 | this.bookingdates = bookingDates; 18 | this.additionalneeds = additionalNeeds; 19 | } 20 | 21 | public float getTotalprice() { 22 | return totalprice; 23 | } 24 | 25 | public void setTotalprice(float totalprice) { 26 | this.totalprice = totalprice; 27 | } 28 | 29 | public boolean getDepositpaid() { 30 | return depositpaid; 31 | } 32 | 33 | public void setDepositpaid(boolean depositpaid) { 34 | this.depositpaid = depositpaid; 35 | } 36 | 37 | public String getAdditionalneeds() { 38 | return additionalneeds; 39 | } 40 | 41 | public void setAdditionalneeds(String additionalneeds) { 42 | this.additionalneeds = additionalneeds; 43 | } 44 | 45 | public BookingDates getBookingdates() { 46 | return bookingdates; 47 | } 48 | 49 | public void setBookingdates(BookingDates bookingdates) { 50 | this.bookingdates = bookingdates; 51 | } 52 | 53 | public String getLastname() { 54 | return lastname; 55 | } 56 | 57 | public void setLastname(String lastname) { 58 | this.lastname = lastname; 59 | } 60 | 61 | public String getFirstname() { 62 | return firstname; 63 | } 64 | 65 | public void setFirstname(String firstname) { 66 | this.firstname = firstname; 67 | } 68 | 69 | 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/Entities/BookingDates.java: -------------------------------------------------------------------------------- 1 | package Entities; 2 | 3 | public class BookingDates { 4 | 5 | private String checkin; 6 | private String checkout; 7 | public BookingDates(String checkin, String checkout) { 8 | this.checkin = checkin; 9 | this.checkout = checkout; 10 | } 11 | 12 | public String getCheckin() { 13 | return checkin; 14 | } 15 | 16 | public void setCheckin(String checkin) { 17 | this.checkin = checkin; 18 | } 19 | 20 | public String getCheckout() { 21 | return checkout; 22 | } 23 | 24 | public void setCheckout(String checkout) { 25 | this.checkout = checkout; 26 | } 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/Entities/User.java: -------------------------------------------------------------------------------- 1 | package Entities; 2 | 3 | public class User { 4 | private String username; 5 | private String firstName; 6 | private String lastName; 7 | private String email; 8 | private String password; 9 | private String phone; 10 | 11 | public User(String username, String firstName, String lastName, String email, String password, String phone) { 12 | this.username = username; 13 | this.firstName = firstName; 14 | this.lastName = lastName; 15 | this.email = email; 16 | this.password = password; 17 | this.phone = phone; 18 | } 19 | 20 | public String getUsername() { 21 | return username; 22 | } 23 | 24 | public void setUsername(String username) { 25 | this.username = username; 26 | } 27 | 28 | public String getFirstName() { 29 | return firstName; 30 | } 31 | 32 | public void setFirstName(String firstName) { 33 | this.firstName = firstName; 34 | } 35 | 36 | public String getLastName() { 37 | return lastName; 38 | } 39 | 40 | public void setLastName(String lastName) { 41 | this.lastName = lastName; 42 | } 43 | 44 | public String getEmail() { 45 | return email; 46 | } 47 | 48 | public void setEmail(String email) { 49 | this.email = email; 50 | } 51 | 52 | public String getPassword() { 53 | return password; 54 | } 55 | 56 | public void setPassword(String password) { 57 | this.password = password; 58 | } 59 | 60 | public String getPhone() { 61 | return phone; 62 | } 63 | 64 | public void setPhone(String phone) { 65 | this.phone = phone; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/BookingTests.java: -------------------------------------------------------------------------------- 1 | import Entities.Booking; 2 | import Entities.BookingDates; 3 | import Entities.User; 4 | import com.github.javafaker.Faker; 5 | import io.restassured.RestAssured; 6 | import io.restassured.filter.log.ErrorLoggingFilter; 7 | import io.restassured.filter.log.RequestLoggingFilter; 8 | import io.restassured.filter.log.ResponseLoggingFilter; 9 | import io.restassured.http.ContentType; 10 | import io.restassured.response.Response; 11 | import io.restassured.specification.RequestSpecification; 12 | import org.junit.jupiter.api.*; 13 | 14 | import static io.restassured.RestAssured.given; 15 | import static io.restassured.config.LogConfig.logConfig; 16 | import static io.restassured.module.jsv.JsonSchemaValidator.*; 17 | import static org.hamcrest.Matchers.*; 18 | 19 | public class BookingTests { 20 | public static Faker faker; 21 | private static RequestSpecification request; 22 | private static Booking booking; 23 | private static BookingDates bookingDates; 24 | private static User user; 25 | 26 | @BeforeAll 27 | public static void Setup(){ 28 | RestAssured.baseURI = "https://restful-booker.herokuapp.com"; 29 | faker = new Faker(); 30 | user = new User(faker.name().username(), 31 | faker.name().firstName(), 32 | faker.name().lastName(), 33 | faker.internet().safeEmailAddress(), 34 | faker.internet().password(8,10), 35 | faker.phoneNumber().toString()); 36 | 37 | bookingDates = new BookingDates("2018-01-02", "2018-01-03"); 38 | booking = new Booking(user.getFirstName(), user.getLastName(), 39 | (float)faker.number().randomDouble(2, 50, 100000), 40 | true,bookingDates, 41 | ""); 42 | RestAssured.filters(new RequestLoggingFilter(),new ResponseLoggingFilter(), new ErrorLoggingFilter()); 43 | } 44 | 45 | @BeforeEach 46 | void setRequest(){ 47 | request = given().config(RestAssured.config().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails())) 48 | .contentType(ContentType.JSON) 49 | .auth().basic("admin", "password123"); 50 | } 51 | 52 | @Test 53 | public void getAllBookingsById_returnOk(){ 54 | Response response = request 55 | .when() 56 | .get("/booking") 57 | .then() 58 | .extract() 59 | .response(); 60 | 61 | 62 | Assertions.assertNotNull(response); 63 | Assertions.assertEquals(200, response.statusCode()); 64 | } 65 | 66 | @Test 67 | public void getAllBookingsByUserFirstName_BookingExists_returnOk(){ 68 | request 69 | .when() 70 | .queryParam("firstName", "Carol") 71 | .get("/booking") 72 | .then() 73 | .assertThat() 74 | .statusCode(200) 75 | .contentType(ContentType.JSON) 76 | .and() 77 | .body("results", hasSize(greaterThan(0))); 78 | 79 | } 80 | 81 | @Test 82 | public void CreateBooking_WithValidData_returnOk(){ 83 | 84 | Booking test = booking; 85 | given().config(RestAssured.config().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails())) 86 | .contentType(ContentType.JSON) 87 | .when() 88 | .body(booking) 89 | .post("/booking") 90 | .then() 91 | .body(matchesJsonSchemaInClasspath("createBookingRequestSchema.json")) 92 | .and() 93 | .assertThat() 94 | .statusCode(200) 95 | .contentType(ContentType.JSON).and().time(lessThan(2000L)); 96 | 97 | 98 | 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/test/resources/createBookingRequestSchema.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "object", 3 | "properties": { 4 | "bookingid": { 5 | "type": "integer" 6 | }, 7 | "booking": { 8 | "type": "object", 9 | "properties": { 10 | "firstname": { 11 | "type": "string" 12 | }, 13 | "lastname": { 14 | "type": "string" 15 | }, 16 | "totalprice": { 17 | "type": "number" 18 | }, 19 | "depositpaid": { 20 | "type": "boolean" 21 | }, 22 | "bookingdates": { 23 | "type": "object", 24 | "properties": { 25 | "checkin": { 26 | "type": "string" 27 | }, 28 | "checkout": { 29 | "type": "string" 30 | } 31 | }, 32 | "required": [ 33 | "checkin", 34 | "checkout" 35 | ] 36 | }, 37 | "additionalneeds": { 38 | "type": "string" 39 | } 40 | }, 41 | "required": [ 42 | "firstname", 43 | "lastname", 44 | "totalprice", 45 | "depositpaid", 46 | "bookingdates" 47 | ] 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /src/test/resources/createBookingResponseSchema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-04/schema#", 3 | "type": "object", 4 | "properties": { 5 | "bookingid": { 6 | "type": "integer" 7 | }, 8 | "booking": { 9 | "type": "object", 10 | "properties": { 11 | "firstname": { 12 | "type": "string" 13 | }, 14 | "lastname": { 15 | "type": "string" 16 | }, 17 | "totalprice": { 18 | "type": "integer" 19 | }, 20 | "depositpaid": { 21 | "type": "boolean" 22 | }, 23 | "bookingdates": { 24 | "type": "object", 25 | "properties": { 26 | "checkin": { 27 | "type": "string" 28 | }, 29 | "checkout": { 30 | "type": "string" 31 | } 32 | }, 33 | "required": [ 34 | "checkin", 35 | "checkout" 36 | ] 37 | }, 38 | "additionalneeds": { 39 | "type": "string" 40 | } 41 | }, 42 | "required": [ 43 | "firstname", 44 | "lastname", 45 | "totalprice", 46 | "depositpaid", 47 | "bookingdates", 48 | "additionalneeds" 49 | ] 50 | } 51 | }, 52 | "required": [ 53 | "bookingid", 54 | "booking" 55 | ] 56 | } --------------------------------------------------------------------------------