Repository: tomee Updated Branches: refs/heads/master 0fa46129c -> e7efcb6c8
TOMEE-2297 - Added mp-rest-client example maven project Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/d4761311 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/d4761311 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/d4761311 Branch: refs/heads/master Commit: d4761311aa93ad747c9e778daa21da0041724c40 Parents: 0fa4612 Author: CesarHernandezGt <[email protected]> Authored: Tue Nov 20 16:29:10 2018 -0600 Committer: Roberto Cortez <[email protected]> Committed: Mon Dec 17 22:19:42 2018 +0000 ---------------------------------------------------------------------- examples/mp-rest-client/README.md | 1 + examples/mp-rest-client/pom.xml | 79 ++++++++++++++++++++ .../org/superbiz/rest/ApplicationConfig.java | 9 +++ .../src/main/java/org/superbiz/rest/Book.java | 33 ++++++++ .../main/java/org/superbiz/rest/BookBean.java | 43 +++++++++++ .../java/org/superbiz/rest/BookResource.java | 54 +++++++++++++ .../org/superbiz/rest/BookResourceClient.java | 40 ++++++++++ .../org/superbiz/rest/BookResourceTest.java | 60 +++++++++++++++ .../src/test/resources/META-INF/beans.xml | 1 + .../META-INF/microprofile-config.properties | 1 + .../src/test/resources/arquillian.xml | 39 ++++++++++ 11 files changed, 360 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/README.md ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/README.md b/examples/mp-rest-client/README.md new file mode 100755 index 0000000..cedeb35 --- /dev/null +++ b/examples/mp-rest-client/README.md @@ -0,0 +1 @@ +# Microprofile Rest client http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/pom.xml ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/pom.xml b/examples/mp-rest-client/pom.xml new file mode 100755 index 0000000..0b46d37 --- /dev/null +++ b/examples/mp-rest-client/pom.xml @@ -0,0 +1,79 @@ +<?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"> + <parent> + <artifactId>examples</artifactId> + <groupId>org.apache.tomee</groupId> + <version>8.0.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>mp-rest-client-example</artifactId> + <packaging>war</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.tomee</groupId> + <artifactId>javaee-api</artifactId> + <version>8.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.eclipse.microprofile</groupId> + <artifactId>microprofile</artifactId> + <version>1.3</version> + <type>pom</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.tomee</groupId> + <artifactId>openejb-cxf-rs</artifactId> + <version>${tomee.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jboss.arquillian.junit</groupId> + <artifactId>arquillian-junit-container</artifactId> + <version>1.0.3.Final</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.tomee</groupId> + <artifactId>arquillian-tomee-remote</artifactId> + <version>${tomee.version}</version> + </dependency> + <dependency> + <groupId>org.apache.tomee</groupId> + <artifactId>apache-tomee</artifactId> + <version>${tomee.version}</version> + <type>zip</type> + <classifier>microprofile</classifier> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.tomee.maven</groupId> + <artifactId>tomee-maven-plugin</artifactId> + <version>${tomee.version}</version> + <configuration> + <tomeeClassifier>microprofile</tomeeClassifier> + <context>${artifactId}</context> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <reuseForks>false</reuseForks> + <!-- need it in order to see complete logs on terminal during: mvn clean test--> + <forkCount>0</forkCount> + </configuration> + </plugin> + </plugins> + </build> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/main/java/org/superbiz/rest/ApplicationConfig.java ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/main/java/org/superbiz/rest/ApplicationConfig.java b/examples/mp-rest-client/src/main/java/org/superbiz/rest/ApplicationConfig.java new file mode 100644 index 0000000..ec64bd1 --- /dev/null +++ b/examples/mp-rest-client/src/main/java/org/superbiz/rest/ApplicationConfig.java @@ -0,0 +1,9 @@ +package org.superbiz.rest; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/api") +public class ApplicationConfig extends Application { + // let the server discover the endpoints +} http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/main/java/org/superbiz/rest/Book.java ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/main/java/org/superbiz/rest/Book.java b/examples/mp-rest-client/src/main/java/org/superbiz/rest/Book.java new file mode 100644 index 0000000..f3eb767 --- /dev/null +++ b/examples/mp-rest-client/src/main/java/org/superbiz/rest/Book.java @@ -0,0 +1,33 @@ +package org.superbiz.rest; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class Book { + + private int id; + private String name; + + public Book() {} + + public Book(int bookId, String name) { + this.id = bookId; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/main/java/org/superbiz/rest/BookBean.java ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/main/java/org/superbiz/rest/BookBean.java b/examples/mp-rest-client/src/main/java/org/superbiz/rest/BookBean.java new file mode 100644 index 0000000..76f7d88 --- /dev/null +++ b/examples/mp-rest-client/src/main/java/org/superbiz/rest/BookBean.java @@ -0,0 +1,43 @@ +package org.superbiz.rest; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.ApplicationScoped; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; + +@ApplicationScoped +public class BookBean { + + private HashMap<Integer,Book> bookStore; + + + @PostConstruct + public void bookBean() { + bookStore = new HashMap(); + } + + public void addBook(Book newBook) { + bookStore.put(newBook.getId(), newBook); + } + + public void deleteBook(int id) { + bookStore.remove(id); + } + + public void updateBook(Book updatedBook) { + bookStore.put(updatedBook.getId(),updatedBook); + } + + public Book getBook(int id) { + return bookStore.get(id); + } + + public List getBooks() { + Collection<Book> books = bookStore.values(); + return new ArrayList<Book>(books); + + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/main/java/org/superbiz/rest/BookResource.java ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/main/java/org/superbiz/rest/BookResource.java b/examples/mp-rest-client/src/main/java/org/superbiz/rest/BookResource.java new file mode 100644 index 0000000..5885097 --- /dev/null +++ b/examples/mp-rest-client/src/main/java/org/superbiz/rest/BookResource.java @@ -0,0 +1,54 @@ +package org.superbiz.rest; + + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import java.util.List; + +@Path("/library") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@ApplicationScoped +public class BookResource { + + @Inject + BookBean bookBean; + + @GET + public String status() { + return "ok"; + } + + @POST + @Path("/books") + public void addBook(Book newBook) { + bookBean.addBook(newBook); + } + + @DELETE + @Path("/books/{id}") + public void deleteBook(@PathParam("id") int id) { + bookBean.deleteBook(id); + } + + @PUT + @Path("/books") + public void updateBook(Book updatedBook) { + bookBean.updateBook(updatedBook); + } + + @GET + @Path("/books/{id}") + public Book getBook(@PathParam("id") int id) { + return bookBean.getBook(id); + } + + @GET + @Path("/books") + public List<Book> getListOfBooks() { + return bookBean.getBooks(); + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/test/java/org/superbiz/rest/BookResourceClient.java ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/test/java/org/superbiz/rest/BookResourceClient.java b/examples/mp-rest-client/src/test/java/org/superbiz/rest/BookResourceClient.java new file mode 100644 index 0000000..5ce735f --- /dev/null +++ b/examples/mp-rest-client/src/test/java/org/superbiz/rest/BookResourceClient.java @@ -0,0 +1,40 @@ +package org.superbiz.rest; + + +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; +import javax.enterprise.context.Dependent; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import java.util.List; + +@Dependent +@RegisterRestClient +@Path("/test/api/library") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public interface BookResourceClient { + + @GET + String status(); + + @POST + @Path("/books") + void addBook(Book newBook); + + @DELETE + @Path("/books/{id}") + void deleteBook(@PathParam("id") int id); + + @PUT + @Path("/books") + void updateBook(Book updatedBook); + + @GET + @Path("/books/{id}") + Book getBook(@PathParam("id") int id); + + @GET + @Path("/books") + List<Book> getListOfBooks(); + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/test/java/org/superbiz/rest/BookResourceTest.java ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/test/java/org/superbiz/rest/BookResourceTest.java b/examples/mp-rest-client/src/test/java/org/superbiz/rest/BookResourceTest.java new file mode 100644 index 0000000..8816c80 --- /dev/null +++ b/examples/mp-rest-client/src/test/java/org/superbiz/rest/BookResourceTest.java @@ -0,0 +1,60 @@ +package org.superbiz.rest; + +import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; + +import static org.junit.Assert.assertTrue; + +@RunWith(Arquillian.class) +public class BookResourceTest { + + @Deployment() + public static WebArchive createDeployment() { + final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test.war") + .addClass(BookResource.class) + .addClass(Book.class) + .addClass(BookBean.class) + .addClass(BookResourceClient.class) + .addClass(ApplicationConfig.class) + .addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml") + .addAsResource("META-INF/microprofile-config.properties"); + return webArchive; + } + + + @Inject + @RestClient + private BookResourceClient bookResourceClient; + + @Test() + public void testServerStatus(){ + bookResourceClient.addBook(new Book(1,"TomEE Book")); + } + + @Test + public void testBookResource(){ + bookResourceClient.addBook(new Book(1, "TomEE and MicroProfile Adventures")); + bookResourceClient.addBook(new Book(2, "Top 10 Tomee Configuraiton Tips")); + + + assertTrue(bookResourceClient.getListOfBooks().size() == 2); + assertTrue(bookResourceClient.getBook(1).getName().equalsIgnoreCase("TomEE and MicroProfile Adventures")); + + bookResourceClient.deleteBook(1); + assertTrue(bookResourceClient.getListOfBooks().size() == 1); + assertTrue(bookResourceClient.getBook(2).getName().equalsIgnoreCase("Top 10 Tomee Configuraiton Tips")); + + bookResourceClient.updateBook(new Book(2, "Top 3 Tomee Configuraiton Tips")); + assertTrue(bookResourceClient.getListOfBooks().size() == 1); + assertTrue(bookResourceClient.getBook(2).getName().equalsIgnoreCase("Top 3 Tomee Configuraiton Tips")); + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/test/resources/META-INF/beans.xml ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/test/resources/META-INF/beans.xml b/examples/mp-rest-client/src/test/resources/META-INF/beans.xml new file mode 100644 index 0000000..e71e480 --- /dev/null +++ b/examples/mp-rest-client/src/test/resources/META-INF/beans.xml @@ -0,0 +1 @@ +<beans></beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/test/resources/META-INF/microprofile-config.properties ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/test/resources/META-INF/microprofile-config.properties b/examples/mp-rest-client/src/test/resources/META-INF/microprofile-config.properties new file mode 100644 index 0000000..df8dfa1 --- /dev/null +++ b/examples/mp-rest-client/src/test/resources/META-INF/microprofile-config.properties @@ -0,0 +1 @@ +org.superbiz.rest.BookResourceClient/mp-rest/url=http://localhost:4444 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tomee/blob/d4761311/examples/mp-rest-client/src/test/resources/arquillian.xml ---------------------------------------------------------------------- diff --git a/examples/mp-rest-client/src/test/resources/arquillian.xml b/examples/mp-rest-client/src/test/resources/arquillian.xml new file mode 100755 index 0000000..5fd386b --- /dev/null +++ b/examples/mp-rest-client/src/test/resources/arquillian.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--> +<arquillian xmlns="http://jboss.org/schema/arquillian" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://jboss.org/schema/arquillian + http://jboss.org/schema/arquillian/arquillian_1_0.xsd"> + + <container qualifier="server" default="true"> + <configuration> + <property name="httpsPort">-1</property> + <property name="httpPort">4444</property> + <property name="stopPort">-1</property> + <property name="ajpPort">-1</property> + <property name="classifier">microprofile</property> + <property name="simpleLog">true</property> + <property name="cleanOnStartUp">true</property> + <property name="additionalLibs">mvn:com.github.javafaker:javafaker:0.15</property> + <property name="dir">target/server</property> + <property name="appWorkingDir">target/arquillian</property> + </configuration> + </container> +</arquillian> \ No newline at end of file
