minor cleanup

This commit is contained in:
eugenp 2017-01-29 16:08:05 +02:00
parent 966e53a85b
commit 68281fa705
5 changed files with 24 additions and 83 deletions

View File

@ -104,13 +104,7 @@ public class Book {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Book [id=")
.append(id)
.append(", title=")
.append(title)
.append(", author=")
.append(author)
.append("]");
builder.append("Book [id=").append(id).append(", title=").append(title).append(", author=").append(author).append("]");
return builder.toString();
}

View File

@ -44,8 +44,7 @@ public class RestApiLiveTest {
final Response response = RestAssured.get(location);
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
assertEquals(book.getTitle(), response.jsonPath()
.get("title"));
assertEquals(book.getTitle(), response.jsonPath().get("title"));
}
@Test
@ -55,8 +54,7 @@ public class RestApiLiveTest {
final Response response = RestAssured.get(API_URI + "/search/findByTitle?title=" + book.getTitle());
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
assertTrue(response.jsonPath()
.getLong("page.totalElements") > 0);
assertTrue(response.jsonPath().getLong("page.totalElements") > 0);
}
@Test
@ -69,8 +67,7 @@ public class RestApiLiveTest {
public void whenGetNotExistBookByName_thenNotFound() {
final Response response = RestAssured.get(API_URI + "/search/findByTitle?title=" + randomAlphabetic(20));
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
assertTrue(response.jsonPath()
.getLong("page.totalElements") == 0);
assertTrue(response.jsonPath().getLong("page.totalElements") == 0);
}
// POST
@ -78,10 +75,7 @@ public class RestApiLiveTest {
public void whenCreateNewBook_thenCreated() {
final Book book = createRandomBook();
final Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(book)
.post(API_URI);
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).post(API_URI);
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode());
}
@ -91,10 +85,7 @@ public class RestApiLiveTest {
createBookAsUri(book);
// duplicate
final Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(book)
.post(API_URI);
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).post(API_URI);
assertEquals(HttpStatus.CONFLICT.value(), response.getStatusCode());
}
@ -103,10 +94,7 @@ public class RestApiLiveTest {
final Book book = createRandomBook();
book.setAuthor(null);
final Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(book)
.post(API_URI);
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).post(API_URI);
assertEquals(HttpStatus.CONFLICT.value(), response.getStatusCode());
}
@ -118,17 +106,13 @@ public class RestApiLiveTest {
// update
book.setAuthor("newAuthor");
Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(book)
.put(location);
Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).put(location);
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
// check if changes saved
response = RestAssured.get(location);
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
assertEquals("newAuthor", response.jsonPath()
.get("author"));
assertEquals("newAuthor", response.jsonPath().get("author"));
}
@ -163,12 +147,8 @@ public class RestApiLiveTest {
}
private String createBookAsUri(Book book) {
final Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(book)
.post(API_URI);
return response.jsonPath()
.get("_links.self.href");
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).post(API_URI);
return response.jsonPath().get("_links.self.href");
}
}

View File

@ -45,11 +45,7 @@ public class SessionLiveTest {
@Test
public void givenAuthorizedUser_whenDeleteSession_thenUnauthorized() {
// authorize User
Response response = RestAssured.given()
.auth()
.preemptive()
.basic("user", "userPass")
.get(API_URI);
Response response = RestAssured.given().auth().preemptive().basic("user", "userPass").get(API_URI);
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
final String sessionCookie = response.getCookie("SESSION");
@ -58,18 +54,14 @@ public class SessionLiveTest {
assertTrue(redisResult.size() > 0);
// login with cookie
response = RestAssured.given()
.cookie("SESSION", sessionCookie)
.get(API_URI);
response = RestAssured.given().cookie("SESSION", sessionCookie).get(API_URI);
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
// empty redis
jedis.flushAll();
// login with cookie again
response = RestAssured.given()
.cookie("SESSION", sessionCookie)
.get(API_URI);
response = RestAssured.given().cookie("SESSION", sessionCookie).get(API_URI);
assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatusCode());
}
}

View File

@ -113,15 +113,7 @@ public class BookReview {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("BookReview [id=")
.append(id)
.append(", content=")
.append(content)
.append(", rating=")
.append(rating)
.append(", bookId=")
.append(bookId)
.append("]");
builder.append("BookReview [id=").append(id).append(", content=").append(content).append(", rating=").append(rating).append(", bookId=").append(bookId).append("]");
return builder.toString();
}

View File

@ -44,8 +44,7 @@ public class RestApiLiveTest {
final Response response = RestAssured.get(location);
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
assertEquals(review.getContent(), response.jsonPath()
.get("content"));
assertEquals(review.getContent(), response.jsonPath().get("content"));
}
@Test
@ -55,8 +54,7 @@ public class RestApiLiveTest {
final Response response = RestAssured.get(API_URI + "/search/findByBookId?bookId=" + review.getBookId());
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
assertTrue(response.jsonPath()
.getLong("page.totalElements") > 0);
assertTrue(response.jsonPath().getLong("page.totalElements") > 0);
}
@Test
@ -69,8 +67,7 @@ public class RestApiLiveTest {
public void whenGetNotExistReviewByBookId_thenNotFound() {
final Response response = RestAssured.get(API_URI + "/search/findByBookId?bookId=" + randomNumeric(4));
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
assertTrue(response.jsonPath()
.getLong("page.totalElements") == 0);
assertTrue(response.jsonPath().getLong("page.totalElements") == 0);
}
// POST
@ -78,10 +75,7 @@ public class RestApiLiveTest {
public void whenCreateNewReview_thenCreated() {
final BookReview review = createRandomReview();
final Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(review)
.post(API_URI);
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(review).post(API_URI);
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode());
}
@ -90,10 +84,7 @@ public class RestApiLiveTest {
final BookReview review = createRandomReview();
review.setBookId(null);
final Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(review)
.post(API_URI);
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(review).post(API_URI);
assertEquals(HttpStatus.CONFLICT.value(), response.getStatusCode());
}
@ -105,17 +96,13 @@ public class RestApiLiveTest {
// update
review.setRating(4);
Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(review)
.put(location);
Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(review).put(location);
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
// check if changes saved
response = RestAssured.get(location);
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
assertEquals(4, response.jsonPath()
.getInt("rating"));
assertEquals(4, response.jsonPath().getInt("rating"));
}
@ -151,12 +138,8 @@ public class RestApiLiveTest {
}
private String createReviewAsUri(BookReview review) {
final Response response = RestAssured.given()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(review)
.post(API_URI);
return response.jsonPath()
.get("_links.self.href");
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(review).post(API_URI);
return response.jsonPath().get("_links.self.href");
}
}