-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update bookstore test for 4.0 and add Nima app.
- Loading branch information
Showing
24 changed files
with
1,145 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# BookApplication | ||
|
||
1. Run `mvn clean install` to build your application | ||
2. Start application with `java -Dbookstore.size=10 -jar target/bookstore-nima.jar`. | ||
3. To check that your application is running enter url `http://localhost:8080/books` | ||
|
||
# JPMS | ||
|
||
You can run using modules like this: | ||
|
||
`java --module-path target/bookstore-nima.jar:target/libs -m io.helidon.tests.apps.bookstore.nima/io.helidon.tests.apps.bookstore.nima.Main` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2018, 2023 Oracle and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.helidon.applications</groupId> | ||
<artifactId>helidon-nima</artifactId> | ||
<version>4.0.0-SNAPSHOT</version> | ||
<relativePath>../../../../applications/nima/pom.xml</relativePath> | ||
</parent> | ||
<groupId>io.helidon.tests.apps.bookstore.bookstore-nima</groupId> | ||
<artifactId>bookstore-nima</artifactId> | ||
<name>Helidon Test Applications Bookstore Nima</name> | ||
|
||
<properties> | ||
<mainClass>io.helidon.tests.apps.bookstore.nima.Main</mainClass> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.helidon.nima.webserver</groupId> | ||
<artifactId>helidon-nima-webserver</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.nima.webclient</groupId> | ||
<artifactId>helidon-nima-webclient</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.nima.http2</groupId> | ||
<artifactId>helidon-nima-http2-webclient</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.nima.http2</groupId> | ||
<artifactId>helidon-nima-http2-webserver</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.nima.http.media</groupId> | ||
<artifactId>helidon-nima-http-media-jsonp</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.nima.observe</groupId> | ||
<artifactId>helidon-nima-observe-health</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.nima.observe</groupId> | ||
<artifactId>helidon-nima-observe-metrics</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.metrics</groupId> | ||
<artifactId>helidon-metrics</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.config</groupId> | ||
<artifactId>helidon-config-yaml</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.common</groupId> | ||
<artifactId>helidon-common-key-util</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.health</groupId> | ||
<artifactId>helidon-health-checks</artifactId> | ||
</dependency> | ||
<!-- does not exist yet | ||
<dependency> | ||
<groupId>io.helidon.nima.media</groupId> | ||
<artifactId>helidon-nima-media-jackson</artifactId> | ||
</dependency> | ||
--> | ||
<dependency> | ||
<groupId>io.helidon.tests.apps.bookstore.common</groupId> | ||
<artifactId>helidon-tests-apps-bookstore-common</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>3.14.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>copy-libs</id> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<compilerArgs> | ||
<compilerArg>--enable-preview</compilerArg> | ||
</compilerArgs> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<argLine>--enable-preview</argLine> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
159 changes: 159 additions & 0 deletions
159
...kstore/bookstore-nima/src/main/java/io/helidon/tests/apps/bookstore/nima/BookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright (c) 2018, 2023 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.tests.apps.bookstore.nima; | ||
|
||
import java.util.Collection; | ||
|
||
import io.helidon.common.http.Http; | ||
import io.helidon.config.Config; | ||
import io.helidon.nima.webserver.http.HttpRules; | ||
import io.helidon.nima.webserver.http.HttpService; | ||
import io.helidon.nima.webserver.http.ServerRequest; | ||
import io.helidon.nima.webserver.http.ServerResponse; | ||
import io.helidon.tests.apps.bookstore.common.Book; | ||
import io.helidon.tests.apps.bookstore.common.BookMapper; | ||
import io.helidon.tests.apps.bookstore.common.BookStore; | ||
|
||
import jakarta.json.JsonObject; | ||
|
||
/** | ||
* Implements book service. | ||
*/ | ||
public class BookService implements HttpService { | ||
|
||
private static final BookStore BOOK_STORE = new BookStore(); | ||
private static final String ISBN_PARAM = "isbn"; | ||
|
||
private final Main.JsonLibrary jsonLibrary; | ||
|
||
BookService(Config config) { | ||
jsonLibrary = Main.getJsonLibrary(config); | ||
} | ||
|
||
/** | ||
* A service registers itself by updating the routine rules. | ||
* | ||
* @param rules the routing rules. | ||
*/ | ||
@Override | ||
public void routing(HttpRules rules) { | ||
rules.get("/", this::getBooks) | ||
.post("/", this::postBook) | ||
.get("/{" + ISBN_PARAM + "}", this::getBook) | ||
.put("/{" + ISBN_PARAM + "}", this::putBook) | ||
.delete("/{" + ISBN_PARAM + "}", this::deleteBook); | ||
|
||
System.out.println("Using JSON library " + jsonLibrary); | ||
} | ||
|
||
private void getBooks(ServerRequest request, ServerResponse response) { | ||
Collection<Book> books = BOOK_STORE.getAll(); | ||
switch (jsonLibrary) { | ||
case JSONP: | ||
response.send(BookMapper.encodeJsonp(books)); | ||
break; | ||
case JSONB: | ||
case JACKSON: | ||
response.send(books); | ||
break; | ||
default: | ||
throw new RuntimeException("Unknown JSON library " + jsonLibrary); | ||
} | ||
} | ||
|
||
private void postBook(ServerRequest request, ServerResponse response) { | ||
switch (jsonLibrary) { | ||
case JSONP: | ||
JsonObject jo = request.content().as(JsonObject.class); | ||
addBook(BookMapper.decodeJsonp(jo), response); | ||
break; | ||
case JSONB: | ||
case JACKSON: | ||
Book book = request.content().as(Book.class); | ||
addBook(book, response); | ||
break; | ||
default: | ||
throw new RuntimeException("Unknown JSON library " + jsonLibrary); | ||
} | ||
} | ||
|
||
private void addBook(Book book, ServerResponse response) { | ||
if (BOOK_STORE.contains(book.getIsbn())) { | ||
response.status(Http.Status.CONFLICT_409).send(); | ||
} else { | ||
BOOK_STORE.store(book); | ||
response.status(Http.Status.OK_200).send(); | ||
} | ||
} | ||
|
||
private void getBook(ServerRequest request, ServerResponse response) { | ||
String isbn = request.path().pathParameters().value(ISBN_PARAM); | ||
Book book = BOOK_STORE.find(isbn); | ||
|
||
if (book == null) { | ||
response.status(Http.Status.NOT_FOUND_404).send(); | ||
return; | ||
} | ||
|
||
switch (jsonLibrary) { | ||
case JSONP: | ||
response.send(BookMapper.encodeJsonp(book)); | ||
break; | ||
case JSONB: | ||
case JACKSON: | ||
response.send(book); | ||
break; | ||
default: | ||
throw new RuntimeException("Unknown JSON library " + jsonLibrary); | ||
} | ||
} | ||
|
||
private void putBook(ServerRequest request, ServerResponse response) { | ||
switch (jsonLibrary) { | ||
case JSONP: | ||
JsonObject jo = request.content().as(JsonObject.class); | ||
updateBook(BookMapper.decodeJsonp(jo), response); | ||
break; | ||
case JSONB: | ||
case JACKSON: | ||
Book book = request.content().as(Book.class); | ||
updateBook(book, response); | ||
break; | ||
default: | ||
throw new RuntimeException("Unknown JSON library " + jsonLibrary); | ||
} | ||
} | ||
|
||
private void updateBook(Book book, ServerResponse response) { | ||
if (BOOK_STORE.contains(book.getIsbn())) { | ||
BOOK_STORE.store(book); | ||
response.status(Http.Status.OK_200).send(); | ||
} else { | ||
response.status(Http.Status.NOT_FOUND_404).send(); | ||
} | ||
} | ||
|
||
private void deleteBook(ServerRequest request, ServerResponse response) { | ||
String isbn = request.path().pathParameters().value(ISBN_PARAM); | ||
if (BOOK_STORE.contains(isbn)) { | ||
BOOK_STORE.remove(isbn); | ||
response.send(); | ||
} else { | ||
response.status(Http.Status.NOT_FOUND_404).send(); | ||
} | ||
} | ||
} |
Oops, something went wrong.