Skip to content

Commit 60f5dd0

Browse files
committed
add live test
1 parent d939853 commit 60f5dd0

5 files changed

Lines changed: 191 additions & 27 deletions

File tree

‎spring-cloud/spring-cloud-rest/spring-cloud-rest-client/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2424
<java.version>1.8</java.version>
25+
<rest-assured.version>3.0.1</rest-assured.version>
2526
</properties>
2627

2728
<dependencies>
@@ -51,6 +52,12 @@
5152
<artifactId>spring-boot-starter-test</artifactId>
5253
<scope>test</scope>
5354
</dependency>
55+
<dependency>
56+
<groupId>io.rest-assured</groupId>
57+
<artifactId>rest-assured</artifactId>
58+
<version>${rest-assured.version}</version>
59+
</dependency>
60+
5461
</dependencies>
5562

5663
<dependencyManagement>
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package org.baeldung.persistence.dao;
22

33
import org.baeldung.persistence.model.Book;
4-
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.repository.CrudRepository;
7+
import org.springframework.data.repository.query.Param;
58
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
69

710
@RepositoryRestResource(collectionResourceRel = "books", path = "books")
8-
public interface BookRepository extends JpaRepository<Book, Long> {
9-
11+
public interface BookRepository extends CrudRepository<Book, Long> {
12+
Page<Book> findByTitle(@Param("title") String title, Pageable pageable);
1013
}

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ public class Book {
1212
@GeneratedValue(strategy = GenerationType.AUTO)
1313
private long id;
1414

15-
@Column(nullable = false)
15+
@Column(nullable = false, unique = true)
1616
private String title;
1717

18-
@Column(nullable = false)
19-
private String isbn;
20-
2118
@Column(nullable = false)
2219
private String author;
2320

@@ -27,10 +24,9 @@ public Book() {
2724
super();
2825
}
2926

30-
public Book(String title, String isbn, String author) {
27+
public Book(String title, String author) {
3128
super();
3229
this.title = title;
33-
this.isbn = isbn;
3430
this.author = author;
3531
}
3632

@@ -52,14 +48,6 @@ public void setTitle(String title) {
5248
this.title = title;
5349
}
5450

55-
public String getIsbn() {
56-
return isbn;
57-
}
58-
59-
public void setIsbn(String isbn) {
60-
this.isbn = isbn;
61-
}
62-
6351
public String getAuthor() {
6452
return author;
6553
}
@@ -76,7 +64,6 @@ public int hashCode() {
7664
int result = 1;
7765
result = (prime * result) + ((author == null) ? 0 : author.hashCode());
7866
result = (prime * result) + (int) (id ^ (id >>> 32));
79-
result = (prime * result) + ((isbn == null) ? 0 : isbn.hashCode());
8067
result = (prime * result) + ((title == null) ? 0 : title.hashCode());
8168
return result;
8269
}
@@ -103,13 +90,7 @@ public boolean equals(Object obj) {
10390
if (id != other.id) {
10491
return false;
10592
}
106-
if (isbn == null) {
107-
if (other.isbn != null) {
108-
return false;
109-
}
110-
} else if (!isbn.equals(other.isbn)) {
111-
return false;
112-
}
93+
11394
if (title == null) {
11495
if (other.title != null) {
11596
return false;
@@ -127,8 +108,6 @@ public String toString() {
127108
.append(id)
128109
.append(", title=")
129110
.append(title)
130-
.append(", isbn=")
131-
.append(isbn)
132111
.append(", author=")
133112
.append(author)
134113
.append("]");
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package org.baeldung;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertTrue;
7+
import io.restassured.RestAssured;
8+
import io.restassured.response.Response;
9+
10+
import org.baeldung.persistence.model.Book;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
15+
import org.springframework.http.HttpStatus;
16+
import org.springframework.http.MediaType;
17+
import org.springframework.test.context.junit4.SpringRunner;
18+
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest(classes = { SpringCloudRestClientApplication.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
21+
public class RestApiLiveTest {
22+
23+
private static final String API_URI = "http://localhost:8084/books";
24+
25+
// GET
26+
27+
@Test
28+
public void whenGetAllBooks_thenOK() {
29+
final Response response = RestAssured.get(API_URI);
30+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
31+
}
32+
33+
@Test
34+
public void whenGetCreatedBookById_thenOK() {
35+
final Book book = createRandomBook();
36+
final String location = createBookAsUri(book);
37+
38+
final Response response = RestAssured.get(location);
39+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
40+
assertEquals(book.getTitle(), response.jsonPath()
41+
.get("title"));
42+
}
43+
44+
@Test
45+
public void whenGetCreatedBookByName_thenOK() {
46+
final Book book = createRandomBook();
47+
createBookAsUri(book);
48+
49+
final Response response = RestAssured.get(API_URI + "/search/findByTitle?title=" + book.getTitle());
50+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
51+
assertTrue(response.jsonPath()
52+
.getLong("page.totalElements") > 0);
53+
}
54+
55+
@Test
56+
public void whenGetNotExistBookById_thenNotFound() {
57+
final Response response = RestAssured.get(API_URI + "/" + randomNumeric(4));
58+
assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode());
59+
}
60+
61+
@Test
62+
public void whenGetNotExistBookByName_thenNotFound() {
63+
final Response response = RestAssured.get(API_URI + "/search/findByTitle?title=" + randomAlphabetic(20));
64+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
65+
assertTrue(response.jsonPath()
66+
.getLong("page.totalElements") == 0);
67+
}
68+
69+
// POST
70+
@Test
71+
public void whenCreateNewBook_thenCreated() {
72+
final Book book = createRandomBook();
73+
74+
final Response response = RestAssured.given()
75+
.contentType(MediaType.APPLICATION_JSON_VALUE)
76+
.body(book)
77+
.post(API_URI);
78+
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode());
79+
}
80+
81+
@Test
82+
public void whenCreateDuplicateBook_thenError() {
83+
final Book book = createRandomBook();
84+
createBookAsUri(book);
85+
86+
// duplicate
87+
final Response response = RestAssured.given()
88+
.contentType(MediaType.APPLICATION_JSON_VALUE)
89+
.body(book)
90+
.post(API_URI);
91+
assertEquals(HttpStatus.CONFLICT.value(), response.getStatusCode());
92+
}
93+
94+
@Test
95+
public void whenInvalidBook_thenError() {
96+
final Book book = createRandomBook();
97+
book.setAuthor(null);
98+
99+
final Response response = RestAssured.given()
100+
.contentType(MediaType.APPLICATION_JSON_VALUE)
101+
.body(book)
102+
.post(API_URI);
103+
assertEquals(HttpStatus.CONFLICT.value(), response.getStatusCode());
104+
}
105+
106+
@Test
107+
public void whenUpdateCreatedBook_thenUpdated() {
108+
// create
109+
final Book book = createRandomBook();
110+
final String location = createBookAsUri(book);
111+
112+
// update
113+
book.setAuthor("newAuthor");
114+
Response response = RestAssured.given()
115+
.contentType(MediaType.APPLICATION_JSON_VALUE)
116+
.body(book)
117+
.put(location);
118+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
119+
120+
// check if changes saved
121+
response = RestAssured.get(location);
122+
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
123+
assertEquals("newAuthor", response.jsonPath()
124+
.get("author"));
125+
126+
}
127+
128+
@Test
129+
public void whenDeleteCreatedBook_thenOk() {
130+
// create
131+
final Book book = createRandomBook();
132+
final String location = createBookAsUri(book);
133+
134+
// delete
135+
Response response = RestAssured.delete(location);
136+
assertEquals(HttpStatus.NO_CONTENT.value(), response.getStatusCode());
137+
138+
// confirm it was deleted
139+
response = RestAssured.get(location);
140+
assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode());
141+
}
142+
143+
@Test
144+
public void whenDeleteNotExistBook_thenError() {
145+
final Response response = RestAssured.delete(API_URI + "/" + randomNumeric(4));
146+
assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode());
147+
}
148+
149+
// =============================== Util
150+
151+
public Book createRandomBook() {
152+
final Book book = new Book();
153+
book.setTitle(randomAlphabetic(10));
154+
book.setAuthor(randomAlphabetic(15));
155+
return book;
156+
}
157+
158+
public String createBookAsUri(Book book) {
159+
final Response response = RestAssured.given()
160+
.contentType(MediaType.APPLICATION_JSON_VALUE)
161+
.body(book)
162+
.post(API_URI);
163+
return response.jsonPath()
164+
.get("_links.self.href");
165+
}
166+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.application.name=spring-cloud-eureka-client
2+
server.port=8084
3+
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
4+
eureka.instance.preferIpAddress=true
5+
6+
spring.datasource.driver-class-name=org.h2.Driver
7+
spring.datasource.url=jdbc:h2:mem:cloud_rest;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
8+
spring.datasource.username=sa
9+
spring.datasource.password=

0 commit comments

Comments
 (0)