Skip to content

Commit 91134e1

Browse files
committed
Add missing documentation and enhance test coverage for getting-started-reactive-crud.
1 parent 36c6794 commit 91134e1

2 files changed

Lines changed: 221 additions & 6 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Getting started with Quarkus
2+
3+
This is a CRUD service exposing endpoints over REST. These endpoints are used for get, add, update
4+
and
5+
remove entries from database.
6+
7+
## Requirements
8+
9+
To compile and run this demo you will need:
10+
11+
- JDK 11+
12+
- GraalVM
13+
- httpie (or curl)
14+
15+
### Configuring GraalVM and JDK 11+
16+
17+
Make sure that both the `GRAALVM_HOME` and `JAVA_HOME` environment variables have been set, and that
18+
a JDK 11+ `java` command is on the path.
19+
20+
See the [Building a Native Executable guide](https://quarkus.io/guides/building-native-image-guide)
21+
for help setting up your environment.
22+
23+
## Building the application
24+
25+
Launch the Maven build on the checked out sources of this demo:
26+
27+
> ./mvnw package
28+
29+
### Running the demo
30+
31+
The Maven Quarkus plugin provides a development mode that supports live coding. To try this out:
32+
33+
> ./mvnw quarkus:dev
34+
35+
This command will leave Quarkus running in the foreground listening on port 8080.
36+
37+
Launch the second terminal which will be used to send a request.
38+
39+
To get list of all fruits visit [http://127.0.0.1:8080/fruits](http://127.0.0.1:8080/fruits) and all
40+
fruit in database should be returned or you can use terminal and get all fruits by one of these
41+
commands:
42+
> http :8080/fruits
43+
>
44+
> curl 127.0.0.1:8080/fruits
45+
46+
The output of these command should display same content as browser.
47+
48+
You can also get fruit by id. In this example used id is equal to 1. Get fruit by id is
49+
possible either by using browser or by terminal. To get fruit by browser
50+
visit [http://127.0.0.1:8080/fruits/1](http://127.0.0.1:8080/fruits/1) and info about kiwi fruit
51+
should be displayed. To get fruit by terminal:
52+
> http :8080/fruits/1
53+
>
54+
> curl 127.0.0.1:8080/fruits/1
55+
56+
The output of these command should display same content as browser.
57+
58+
Next we add lemon fruit. To add fruit the POST request needs to be sent. This request must contain
59+
key-value pair. In this case `name={fruitName}`. To add fruit send request from terminal:
60+
> http POST :8080/fruits name=Lemon
61+
>
62+
> curl -X POST http://localhost:8080/fruits -H 'Content-Type: application/json' -d '{"name":"Lemon"}'
63+
64+
This request create fruit with name `Lemon`. Now you can check all fruits and the newly added fruit
65+
should be visible.
66+
67+
To update name of fruit the PUT request needs to be sent. This request must contain two key-value
68+
pairs. In this case `id={fruitId}` and `name={newFruitName}`. In this example used id is equal to 1.
69+
To update fruit name send request from terminal:
70+
> http PUT :8080/fruits/1 id=1 name=Pitaya
71+
>
72+
> curl -X PUT http://localhost:8080/fruits/1 -H "Content-Type: application/json" -d '{"id":1,
73+
> "name":"Pitaya"}'
74+
75+
This request update name of fruit with index 1 to `Pitaya`. Now you can check all fruits and fruit
76+
with index 1 should be updated.
77+
78+
Now as you know how to show, add and update fruits last step will be to delete fruit. To delete
79+
fruit you need to know its id. In case of this example the fruit with id equal to 1 will be
80+
deleted. To delete fruit the DELETE request needs to be sent. To delete fruit send request from
81+
terminal:
82+
> http DELETE :8080/fruits/1
83+
>
84+
> curl -X DELETE http://localhost:8080/fruits/1
85+
86+
Check that fruit with id 1 is not existing anymore.
87+
88+
### Run Quarkus in JVM mode
89+
90+
When you're done iterating in developer mode, you can run the application as a conventional jar
91+
file.
92+
93+
First compile it:
94+
95+
> ./mvnw package
96+
97+
Then run it:
98+
99+
> java -jar ./target/quarkus-app/quarkus-run.jar
100+
101+
Have a look at how fast it boots, or measure the total native memory consumption.
102+
103+
### Run Quarkus as a native executable
104+
105+
You can also create a native executable from this application without making any source code
106+
changes. A native executable removes the dependency on the JVM: everything needed to run the
107+
application on the target platform is included in the executable, allowing the application to run
108+
with minimal resource overhead.
109+
110+
Compiling a native executable takes a bit longer, as GraalVM performs additional steps to remove
111+
unnecessary codepaths. Use the `native` profile to compile a native executable:
112+
113+
> ./mvnw package -Dnative
114+
115+
After getting a cup of coffee, you'll be able to run this executable directly:
116+
117+
> ./target/getting-started-reactive-rest-1.0.0-SNAPSHOT-runner

getting-started-reactive-crud/src/test/java/org/acme/reactive/crud/FruitResourceTest.java

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44
import static org.hamcrest.CoreMatchers.containsString;
55
import static org.hamcrest.core.IsNot.not;
66

7+
import org.junit.jupiter.api.MethodOrderer;
8+
import org.junit.jupiter.api.Order;
79
import org.junit.jupiter.api.Test;
810

911
import io.quarkus.test.junit.QuarkusTest;
12+
import org.junit.jupiter.api.TestMethodOrder;
1013

1114
@QuarkusTest
15+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1216
class FruitResourceTest {
17+
private static String newFruit = "Lemon";
18+
private static String updatedFruit = "Pitaya";
1319

1420
@Test
15-
public void testListAllFruits() {
21+
@Order(1)
22+
public void testAddFruit() {
1623
//List all, should have all 4 fruits the database has initially:
1724
given()
1825
.when().get("/fruits")
@@ -24,22 +31,113 @@ public void testListAllFruits() {
2431
containsString("Pomelo"),
2532
containsString("Lychee"));
2633

27-
//Delete the Kiwi:
34+
// Add new fruit
35+
given()
36+
.when().contentType("application/json")
37+
.body("{\"name\":\"" + newFruit + "\"}")
38+
.post("/fruits")
39+
.then()
40+
.statusCode(201);
41+
42+
//List all, should have 4 fruits the database has initially and 1 added fruit:
43+
given()
44+
.when().get("/fruits")
45+
.then()
46+
.statusCode(200)
47+
.body(
48+
containsString("Kiwi"),
49+
containsString("Durian"),
50+
containsString("Pomelo"),
51+
containsString("Lychee"),
52+
containsString(newFruit));
53+
}
54+
55+
@Test
56+
@Order(2)
57+
public void testUpdateFruit() {
58+
//List all, should have 4 fruits the database has initially and 1 added fruit:
59+
given()
60+
.when().get("/fruits")
61+
.then()
62+
.statusCode(200)
63+
.body(
64+
containsString("Kiwi"),
65+
containsString("Durian"),
66+
containsString("Pomelo"),
67+
containsString("Lychee"),
68+
containsString(newFruit));
69+
70+
// Update name of added fruit
71+
given()
72+
.when().contentType("application/json")
73+
.body("{\"id\":5, \"name\":\"" + updatedFruit + "\"}")
74+
.put("/fruits/5")
75+
.then()
76+
.statusCode(200);
77+
78+
//List all, should have 4 fruits the database has initially and 1 updated fruit:
79+
given()
80+
.when().get("/fruits")
81+
.then()
82+
.statusCode(200)
83+
.body(
84+
containsString("Kiwi"),
85+
containsString("Durian"),
86+
containsString("Pomelo"),
87+
containsString("Lychee"),
88+
containsString(updatedFruit));
89+
}
90+
91+
@Test
92+
@Order(3)
93+
public void testListAllFruits() {
94+
//List all, should have 5 fruits the database has initially:
95+
given()
96+
.when().get("/fruits")
97+
.then()
98+
.statusCode(200)
99+
.body(
100+
containsString("Kiwi"),
101+
containsString("Durian"),
102+
containsString("Pomelo"),
103+
containsString("Lychee"),
104+
containsString(updatedFruit));
105+
106+
//Delete the Pitaya:
28107
given()
29-
.when().delete("/fruits/1")
108+
.when().delete("/fruits/5")
30109
.then()
31110
.statusCode(204);
32111

33-
//List all, Kiwi should be missing now:
112+
//List all, Pitaya should be missing now:
34113
given()
35114
.when().get("/fruits")
36115
.then()
37116
.statusCode(200)
38117
.body(
39-
not(containsString("Kiwi")),
118+
containsString("Kiwi"),
40119
containsString("Durian"),
41120
containsString("Pomelo"),
42-
containsString("Lychee"));
121+
containsString("Lychee"),
122+
not(containsString(updatedFruit)));
123+
}
124+
125+
@Test
126+
public void testNotFoundForDelete() {
127+
given()
128+
.when().delete("/fruits/3939")
129+
.then()
130+
.statusCode(404);
131+
}
132+
133+
@Test
134+
public void testNotFoundForUpdate() {
135+
given()
136+
.when().contentType("application/json")
137+
.body("{\"id\":3939, \"name\":\"" + updatedFruit + "\"}")
138+
.put("/fruits/3939")
139+
.then()
140+
.statusCode(404);
43141
}
44142

45143
}

0 commit comments

Comments
 (0)