-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pablo gonzalez granados
committed
Jun 17, 2022
1 parent
afb6023
commit 27b855b
Showing
16 changed files
with
584 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.quarkus.qe</groupId> | ||
<artifactId>quarkus-test-parent</artifactId> | ||
<version>1.2.1.Beta1-SNAPSHOT</version> | ||
<relativePath>../../</relativePath> | ||
</parent> | ||
<artifactId>examples-database-oracle</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Quarkus - Test Framework - Examples - Database - Oracle</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-hibernate-orm-panache</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-hibernate-validator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-jackson</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-jdbc-oracle</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus.qe</groupId> | ||
<artifactId>quarkus-test-containers</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus.qe</groupId> | ||
<artifactId>quarkus-test-service-database</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus.qe</groupId> | ||
<artifactId>quarkus-test-openshift</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>${quarkus.platform.group-id}</groupId> | ||
<artifactId>quarkus-maven-plugin</artifactId> | ||
<version>${quarkus.platform.version}</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<profiles> | ||
<!-- Skipped on Windows as does not support Linux Containers / Testcontainers --> | ||
<profile> | ||
<id>skip-tests-on-windows</id> | ||
<activation> | ||
<os> | ||
<family>windows</family> | ||
</os> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
</project> |
28 changes: 28 additions & 0 deletions
28
...tabase-oracle/src/main/java/io/quarkus/qe/database/oracle/ApplicationExceptionMapper.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,28 @@ | ||
package io.quarkus.qe.database.oracle; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@Provider | ||
public class ApplicationExceptionMapper implements ExceptionMapper<Exception> { | ||
@Override | ||
public Response toResponse(Exception exception) { | ||
int code = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); | ||
if (exception instanceof WebApplicationException) { | ||
code = ((WebApplicationException) exception).getResponse().getStatus(); | ||
} | ||
|
||
return Response.status(code) | ||
.type(MediaType.APPLICATION_JSON) | ||
.entity(new ObjectMapper().createObjectNode() | ||
.put("code", code) | ||
.put("error", exception.getMessage()) | ||
.toString()) | ||
.build(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/database-oracle/src/main/java/io/quarkus/qe/database/oracle/Book.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,17 @@ | ||
package io.quarkus.qe.database.oracle; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Table; | ||
import javax.validation.constraints.NotBlank; | ||
|
||
import io.quarkus.hibernate.orm.panache.PanacheEntity; | ||
|
||
@Entity | ||
@Table(name = "book") | ||
public class Book extends PanacheEntity { | ||
@NotBlank(message = "book title must be set") | ||
public String title; | ||
|
||
@NotBlank(message = "book author must be set") | ||
public String author; | ||
} |
78 changes: 78 additions & 0 deletions
78
examples/database-oracle/src/main/java/io/quarkus/qe/database/oracle/BookResource.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,78 @@ | ||
package io.quarkus.qe.database.oracle; | ||
|
||
import java.util.List; | ||
|
||
import javax.transaction.Transactional; | ||
import javax.validation.Valid; | ||
import javax.ws.rs.ClientErrorException; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.NotFoundException; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import io.quarkus.panache.common.Sort; | ||
|
||
@Path("/book") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public class BookResource { | ||
|
||
@GET | ||
public List<Book> getAll() { | ||
return Book.listAll(Sort.by("title")); | ||
} | ||
|
||
@GET | ||
@Path("/{id}") | ||
public Book get(@PathParam("id") Long id) { | ||
Book book = Book.findById(id); | ||
if (book == null) { | ||
throw new NotFoundException("book '" + id + "' not found"); | ||
} | ||
return book; | ||
} | ||
|
||
@POST | ||
@Transactional | ||
public Response create(@Valid Book book) { | ||
if (book.id != null) { | ||
throw new ClientErrorException("unexpected ID in request", ValidationExceptionMapper.UNPROCESSABLE_ENTITY); | ||
} | ||
|
||
book.persist(); | ||
return Response.ok(book).status(Response.Status.CREATED).build(); | ||
} | ||
|
||
@PUT | ||
@Path("/{id}") | ||
@Transactional | ||
public Book update(@PathParam("id") Long id, @Valid Book newBook) { | ||
Book book = Book.findById(id); | ||
if (book == null) { | ||
throw new NotFoundException("book '" + id + "' not found"); | ||
} | ||
|
||
book.title = newBook.title; | ||
book.author = newBook.author; | ||
return book; | ||
} | ||
|
||
@DELETE | ||
@Path("/{id}") | ||
@Transactional | ||
public Response delete(@PathParam("id") Long id) { | ||
Book book = Book.findById(id); | ||
if (book == null) { | ||
throw new NotFoundException("book '" + id + "' not found"); | ||
} | ||
book.delete(); | ||
return Response.noContent().build(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../database-oracle/src/main/java/io/quarkus/qe/database/oracle/NotFoundExceptionMapper.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,14 @@ | ||
package io.quarkus.qe.database.oracle; | ||
|
||
import javax.ws.rs.NotFoundException; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
@Provider | ||
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> { | ||
@Override | ||
public Response toResponse(NotFoundException exception) { | ||
return new ApplicationExceptionMapper().toResponse(exception); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...atabase-oracle/src/main/java/io/quarkus/qe/database/oracle/ValidationExceptionMapper.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,36 @@ | ||
package io.quarkus.qe.database.oracle; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.ConstraintViolationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
|
||
@Provider | ||
public class ValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException> { | ||
|
||
public static final int UNPROCESSABLE_ENTITY = 422; | ||
|
||
@Override | ||
public Response toResponse(ConstraintViolationException exception) { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
ArrayNode errors = mapper.createArrayNode(); | ||
|
||
for (ConstraintViolation<?> constraintViolation : exception.getConstraintViolations()) { | ||
errors.addObject() | ||
.put("path", constraintViolation.getPropertyPath().toString()) | ||
.put("message", constraintViolation.getMessage()); | ||
} | ||
|
||
return Response.status(UNPROCESSABLE_ENTITY) | ||
.type(MediaType.APPLICATION_JSON) | ||
.entity(mapper.createObjectNode() | ||
.put("code", UNPROCESSABLE_ENTITY) | ||
.set("error", errors)) | ||
.build(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
examples/database-oracle/src/main/resources/application.properties
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,3 @@ | ||
quarkus.datasource.db-kind=oracle | ||
quarkus.hibernate-orm.sql-load-script=import.sql | ||
quarkus.hibernate-orm.database.generation=drop-and-create |
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,7 @@ | ||
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Foundation', 'Isaac Asimov'); | ||
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, '2001: A Space Odyssey', 'Arthur C. Clarke'); | ||
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Stranger in a Strange Land', 'Robert A. Heinlein'); | ||
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Ender''s Game', 'Orson Scott Card'); | ||
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Hyperion', 'Dan Simmons'); | ||
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Anathem', 'Neal Stephenson'); | ||
INSERT INTO book (id, title, author) VALUES (hibernate_sequence.NEXTVAL, 'Perdido Street Station', 'China Miéville'); |
Oops, something went wrong.