Skip to content

Commit 68281fa

Browse files
author
eugenp
committed
minor cleanup
1 parent 966e53a commit 68281fa

5 files changed

Lines changed: 24 additions & 83 deletions

File tree

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-client-1/src/main/java/org/baeldung/persistence/model/Book.java‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,7 @@ public boolean equals(Object obj) {
104104
@Override
105105
public String toString() {
106106
final StringBuilder builder = new StringBuilder();
107-
builder.append("Book [id=")
108-
.append(id)
109-
.append(", title=")
110-
.append(title)
111-
.append(", author=")
112-
.append(author)
113-
.append("]");
107+
builder.append("Book [id=").append(id).append(", title=").append(title).append(", author=").append(author).append("]");
114108
return builder.toString();
115109
}
116110

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-client-1/src/test/java/org/baeldung/RestApiLiveTest.java‎

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public void whenGetCreatedBookById_thenOK() {
4444

4545
final Response response = RestAssured.get(location);
4646
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
47-
assertEquals(book.getTitle(), response.jsonPath()
48-
.get("title"));
47+
assertEquals(book.getTitle(), response.jsonPath().get("title"));
4948
}
5049

5150
@Test
@@ -55,8 +54,7 @@ public void whenGetCreatedBookByName_thenOK() {
5554

5655
final Response response = RestAssured.get(API_URI + "/search/findByTitle?title=" + book.getTitle());
5756
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
58-
assertTrue(response.jsonPath()
59-
.getLong("page.totalElements") > 0);
57+
assertTrue(response.jsonPath().getLong("page.totalElements") > 0);
6058
}
6159

6260
@Test
@@ -69,19 +67,15 @@ public void whenGetNotExistBookById_thenNotFound() {
6967
public void whenGetNotExistBookByName_thenNotFound() {
7068
final Response response = RestAssured.get(API_URI + "/search/findByTitle?title=" + randomAlphabetic(20));
7169
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
72-
assertTrue(response.jsonPath()
73-
.getLong("page.totalElements") == 0);
70+
assertTrue(response.jsonPath().getLong("page.totalElements") == 0);
7471
}
7572

7673
// POST
7774
@Test
7875
public void whenCreateNewBook_thenCreated() {
7976
final Book book = createRandomBook();
8077

81-
final Response response = RestAssured.given()
82-
.contentType(MediaType.APPLICATION_JSON_VALUE)
83-
.body(book)
84-
.post(API_URI);
78+
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).post(API_URI);
8579
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode());
8680
}
8781

@@ -91,10 +85,7 @@ public void whenCreateDuplicateBook_thenError() {
9185
createBookAsUri(book);
9286

9387
// duplicate
94-
final Response response = RestAssured.given()
95-
.contentType(MediaType.APPLICATION_JSON_VALUE)
96-
.body(book)
97-
.post(API_URI);
88+
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).post(API_URI);
9889
assertEquals(HttpStatus.CONFLICT.value(), response.getStatusCode());
9990
}
10091

@@ -103,10 +94,7 @@ public void whenInvalidBook_thenError() {
10394
final Book book = createRandomBook();
10495
book.setAuthor(null);
10596

106-
final Response response = RestAssured.given()
107-
.contentType(MediaType.APPLICATION_JSON_VALUE)
108-
.body(book)
109-
.post(API_URI);
97+
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).post(API_URI);
11098
assertEquals(HttpStatus.CONFLICT.value(), response.getStatusCode());
11199
}
112100

@@ -118,17 +106,13 @@ public void whenUpdateCreatedBook_thenUpdated() {
118106

119107
// update
120108
book.setAuthor("newAuthor");
121-
Response response = RestAssured.given()
122-
.contentType(MediaType.APPLICATION_JSON_VALUE)
123-
.body(book)
124-
.put(location);
109+
Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).put(location);
125110
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
126111

127112
// check if changes saved
128113
response = RestAssured.get(location);
129114
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
130-
assertEquals("newAuthor", response.jsonPath()
131-
.get("author"));
115+
assertEquals("newAuthor", response.jsonPath().get("author"));
132116

133117
}
134118

@@ -163,12 +147,8 @@ private Book createRandomBook() {
163147
}
164148

165149
private String createBookAsUri(Book book) {
166-
final Response response = RestAssured.given()
167-
.contentType(MediaType.APPLICATION_JSON_VALUE)
168-
.body(book)
169-
.post(API_URI);
170-
return response.jsonPath()
171-
.get("_links.self.href");
150+
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(book).post(API_URI);
151+
return response.jsonPath().get("_links.self.href");
172152
}
173153

174154
}

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-client-1/src/test/java/org/baeldung/SessionLiveTest.java‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ public void givenUnauthorizeUser_whenAccessResources_then_unAuthorized() {
4545
@Test
4646
public void givenAuthorizedUser_whenDeleteSession_thenUnauthorized() {
4747
// authorize User
48-
Response response = RestAssured.given()
49-
.auth()
50-
.preemptive()
51-
.basic("user", "userPass")
52-
.get(API_URI);
48+
Response response = RestAssured.given().auth().preemptive().basic("user", "userPass").get(API_URI);
5349
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
5450
final String sessionCookie = response.getCookie("SESSION");
5551

@@ -58,18 +54,14 @@ public void givenAuthorizedUser_whenDeleteSession_thenUnauthorized() {
5854
assertTrue(redisResult.size() > 0);
5955

6056
// login with cookie
61-
response = RestAssured.given()
62-
.cookie("SESSION", sessionCookie)
63-
.get(API_URI);
57+
response = RestAssured.given().cookie("SESSION", sessionCookie).get(API_URI);
6458
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
6559

6660
// empty redis
6761
jedis.flushAll();
6862

6963
// login with cookie again
70-
response = RestAssured.given()
71-
.cookie("SESSION", sessionCookie)
72-
.get(API_URI);
64+
response = RestAssured.given().cookie("SESSION", sessionCookie).get(API_URI);
7365
assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatusCode());
7466
}
7567
}

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/main/java/org/baeldung/persistence/model/BookReview.java‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,7 @@ public boolean equals(Object obj) {
113113
@Override
114114
public String toString() {
115115
final StringBuilder builder = new StringBuilder();
116-
builder.append("BookReview [id=")
117-
.append(id)
118-
.append(", content=")
119-
.append(content)
120-
.append(", rating=")
121-
.append(rating)
122-
.append(", bookId=")
123-
.append(bookId)
124-
.append("]");
116+
builder.append("BookReview [id=").append(id).append(", content=").append(content).append(", rating=").append(rating).append(", bookId=").append(bookId).append("]");
125117
return builder.toString();
126118
}
127119

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/java/org/baeldung/RestApiLiveTest.java‎

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public void whenGetCreatedReviewById_thenOK() {
4444

4545
final Response response = RestAssured.get(location);
4646
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
47-
assertEquals(review.getContent(), response.jsonPath()
48-
.get("content"));
47+
assertEquals(review.getContent(), response.jsonPath().get("content"));
4948
}
5049

5150
@Test
@@ -55,8 +54,7 @@ public void whenGetCreatedReviewByBookId_thenOK() {
5554

5655
final Response response = RestAssured.get(API_URI + "/search/findByBookId?bookId=" + review.getBookId());
5756
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
58-
assertTrue(response.jsonPath()
59-
.getLong("page.totalElements") > 0);
57+
assertTrue(response.jsonPath().getLong("page.totalElements") > 0);
6058
}
6159

6260
@Test
@@ -69,19 +67,15 @@ public void whenGetNotExistReviewById_thenNotFound() {
6967
public void whenGetNotExistReviewByBookId_thenNotFound() {
7068
final Response response = RestAssured.get(API_URI + "/search/findByBookId?bookId=" + randomNumeric(4));
7169
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
72-
assertTrue(response.jsonPath()
73-
.getLong("page.totalElements") == 0);
70+
assertTrue(response.jsonPath().getLong("page.totalElements") == 0);
7471
}
7572

7673
// POST
7774
@Test
7875
public void whenCreateNewReview_thenCreated() {
7976
final BookReview review = createRandomReview();
8077

81-
final Response response = RestAssured.given()
82-
.contentType(MediaType.APPLICATION_JSON_VALUE)
83-
.body(review)
84-
.post(API_URI);
78+
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(review).post(API_URI);
8579
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode());
8680
}
8781

@@ -90,10 +84,7 @@ public void whenCreateInvalidReview_thenError() {
9084
final BookReview review = createRandomReview();
9185
review.setBookId(null);
9286

93-
final Response response = RestAssured.given()
94-
.contentType(MediaType.APPLICATION_JSON_VALUE)
95-
.body(review)
96-
.post(API_URI);
87+
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(review).post(API_URI);
9788
assertEquals(HttpStatus.CONFLICT.value(), response.getStatusCode());
9889
}
9990

@@ -105,17 +96,13 @@ public void whenUpdateCreatedReview_thenUpdated() {
10596

10697
// update
10798
review.setRating(4);
108-
Response response = RestAssured.given()
109-
.contentType(MediaType.APPLICATION_JSON_VALUE)
110-
.body(review)
111-
.put(location);
99+
Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(review).put(location);
112100
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
113101

114102
// check if changes saved
115103
response = RestAssured.get(location);
116104
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
117-
assertEquals(4, response.jsonPath()
118-
.getInt("rating"));
105+
assertEquals(4, response.jsonPath().getInt("rating"));
119106

120107
}
121108

@@ -151,12 +138,8 @@ private BookReview createRandomReview() {
151138
}
152139

153140
private String createReviewAsUri(BookReview review) {
154-
final Response response = RestAssured.given()
155-
.contentType(MediaType.APPLICATION_JSON_VALUE)
156-
.body(review)
157-
.post(API_URI);
158-
return response.jsonPath()
159-
.get("_links.self.href");
141+
final Response response = RestAssured.given().contentType(MediaType.APPLICATION_JSON_VALUE).body(review).post(API_URI);
142+
return response.jsonPath().get("_links.self.href");
160143
}
161144

162145
}

0 commit comments

Comments
 (0)