From a1df209a49b0418c85d25f2fb78442409ed5d9cd Mon Sep 17 00:00:00 2001 From: Alessio Soldano Date: Tue, 12 Jan 2016 13:14:38 +0100 Subject: [PATCH] More tests... --- .../ex04_3/CustomerResource20Test.java | 3 +- .../jaxrs/examples/ex05_1/InjectionTest.java | 122 ++++++++++++++ .../examples/ex05_1/domain/Customer.java | 121 ++++++++++++++ .../examples/ex05_1/services/CarResource.java | 98 +++++++++++ .../ex05_1/services/CustomerResource.java | 154 ++++++++++++++++++ .../ex05_1/services/ShoppingApplication.java | 45 +++++ .../examples/ex06_1/CustomerResourceTest.java | 104 ++++++++++++ .../examples/ex06_1/domain/Customer.java | 142 ++++++++++++++++ .../ex06_1/services/CustomerResource.java | 90 ++++++++++ .../ex06_1/services/ShoppingApplication.java | 44 +++++ .../jaxrs/examples/ex05_x/WEB-INF/web.xml | 6 + .../jaxrs/examples/ex06_x/WEB-INF/web.xml | 6 + 12 files changed, 933 insertions(+), 2 deletions(-) create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/InjectionTest.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/domain/Customer.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/CarResource.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/CustomerResource.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/ShoppingApplication.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/CustomerResourceTest.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/domain/Customer.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/services/CustomerResource.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/services/ShoppingApplication.java create mode 100644 modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex05_x/WEB-INF/web.xml create mode 100644 modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex06_x/WEB-INF/web.xml diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex04_3/CustomerResource20Test.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex04_3/CustomerResource20Test.java index 5326a9137..50df1fc4c 100644 --- a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex04_3/CustomerResource20Test.java +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex04_3/CustomerResource20Test.java @@ -125,8 +125,7 @@ public void testFirstLastCustomerResource() throws Exception .request().post(Entity.xml(xml)); if (response.getStatus() != 201) throw new RuntimeException("Failed to create"); String location = response.getLocation().toString(); -// Assert.assertTrue(location.contains("jaxrs20-examples-ex04_3/services/customers/1")); - System.out.println("* location: " + location); + Assert.assertTrue(location.contains("jaxrs20-examples-ex04_3/services/customers")); response.close(); String customer = client.target(baseURL + "services/customers/northamerica-db/Bill-Burke").request().get(String.class); diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/InjectionTest.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/InjectionTest.java new file mode 100644 index 000000000..a8e429835 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/InjectionTest.java @@ -0,0 +1,122 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex05_1; + +import java.io.File; +import java.net.URL; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.wsf.test.JBossWSTest; +import org.jboss.wsf.test.JBossWSTestHelper; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Bill Burke + * @author Alessio Soldano + */ +@RunWith(Arquillian.class) +public class InjectionTest extends JBossWSTest +{ + @ArquillianResource + private URL baseURL; + + @Deployment(testable = false) + public static WebArchive createDeployments() { + WebArchive archive = ShrinkWrap.create(WebArchive.class, "jaxrs-examples-ex05_1.war"); + archive + .addManifest() + .addClass(org.jboss.test.jaxrs.examples.ex05_1.domain.Customer.class) + .addClass(org.jboss.test.jaxrs.examples.ex05_1.services.CustomerResource.class) + .addClass(org.jboss.test.jaxrs.examples.ex05_1.services.ShoppingApplication.class) + .addClass(org.jboss.test.jaxrs.examples.ex05_1.services.CarResource.class) + .setWebXML(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxrs/examples/ex05_x/WEB-INF/web.xml")); + return archive; + } + + private static Client client; + + @BeforeClass + public static void initClient() + { + client = ClientBuilder.newClient(); + } + + @AfterClass + public static void closeClient() + { + client.close(); + client = null; + } + + @Test + @RunAsClient + public void testCarResource() throws Exception + { + String car = client.target(baseURL + "services/cars/matrix/mercedes/e55;color=black/2006").request().get(String.class); + Assert.assertEquals("A black 2006 mercedes e55", car); + + car = client.target(baseURL + "services/cars/segment/mercedes/e55;color=black/2006").request().get(String.class); + Assert.assertEquals("A black 2006 mercedes e55", car); + + car = client.target(baseURL + "services/cars/segments/mercedes/e55/amg/year/2006").request().get(String.class); + Assert.assertEquals("A 2006 mercedes e55 amg", car); + + car = client.target(baseURL + "services/cars/uriinfo/mercedes/e55;color=black/2006").request().get(String.class); + Assert.assertEquals("A black 2006 mercedes e55", car); + } + + @Test + @RunAsClient + public void testCustomerResource() throws Exception + { + String customer = client.target(baseURL + "services/customers").request().get(String.class); + Assert.assertTrue(customer.contains("Bill")); + Assert.assertTrue(customer.contains("Joe")); + + String list = client.target(baseURL + "services/customers") + .queryParam("start", "1") + .queryParam("size", "3") + .request().get(String.class); + Assert.assertTrue(list.contains("Joe")); + Assert.assertTrue(list.contains("Monica")); + Assert.assertTrue(list.contains("Steve")); + + list = client.target(baseURL + "services/customers/uriinfo") + .queryParam("start", "2") + .queryParam("size", "2") + .request().get(String.class); + Assert.assertTrue(list.contains("Monica")); + Assert.assertTrue(list.contains("Steve")); + } +} diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/domain/Customer.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/domain/Customer.java new file mode 100644 index 000000000..f8b99fffd --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/domain/Customer.java @@ -0,0 +1,121 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex05_1.domain; + +public class Customer +{ + private int id; + + private String firstName; + + private String lastName; + + private String street; + + private String city; + + private String state; + + private String zip; + + private String country; + + public int getId() + { + return id; + } + + public void setId(int id) + { + this.id = id; + } + + public String getFirstName() + { + return firstName; + } + + public void setFirstName(String firstName) + { + this.firstName = firstName; + } + + public String getLastName() + { + return lastName; + } + + public void setLastName(String lastName) + { + this.lastName = lastName; + } + + public String getStreet() + { + return street; + } + + public void setStreet(String street) + { + this.street = street; + } + + public String getCity() + { + return city; + } + + public void setCity(String city) + { + this.city = city; + } + + public String getState() + { + return state; + } + + public void setState(String state) + { + this.state = state; + } + + public String getZip() + { + return zip; + } + + public void setZip(String zip) + { + this.zip = zip; + } + + public String getCountry() + { + return country; + } + + public void setCountry(String country) + { + this.country = country; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/CarResource.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/CarResource.java new file mode 100644 index 000000000..787c90014 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/CarResource.java @@ -0,0 +1,98 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex05_1.services; + +import javax.ws.rs.GET; +import javax.ws.rs.MatrixParam; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.UriInfo; +import java.util.List; + +/** + * @author Bill Burke + */ +@Path("/cars") +public class CarResource +{ + public static enum Color + { + red, + white, + blue, + black + } + + @GET + @Path("/matrix/{make}/{model}/{year}") + @Produces("text/plain") + public String getFromMatrixParam(@PathParam("make") String make, + @PathParam("model") PathSegment car, + @MatrixParam("color") Color color, + @PathParam("year") String year) + { + return "A " + color + " " + year + " " + make + " " + car.getPath(); + } + + + @GET + @Path("/segment/{make}/{model}/{year}") + @Produces("text/plain") + public String getFromPathSegment(@PathParam("make") String make, + @PathParam("model") PathSegment car, + @PathParam("year") String year) + { + String carColor = car.getMatrixParameters().getFirst("color"); + return "A " + carColor + " " + year + " " + make + " " + car.getPath(); + } + + @GET + @Path("/segments/{make}/{model : .+}/year/{year}") + @Produces("text/plain") + public String getFromMultipleSegments(@PathParam("make") String make, + @PathParam("model") List car, + @PathParam("year") String year) + { + String output = "A " + year + " " + make; + for (PathSegment segment : car) + { + output += " " + segment.getPath(); + } + return output; + } + + @GET + @Path("/uriinfo/{make}/{model}/{year}") + @Produces("text/plain") + public String getFromUriInfo(@Context UriInfo info) + { + String make = info.getPathParameters().getFirst("make"); + String year = info.getPathParameters().getFirst("year"); + PathSegment model = info.getPathSegments().get(3); + String color = model.getMatrixParameters().getFirst("color"); + + return "A " + color + " " + year + " " + make + " " + model.getPath(); + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/CustomerResource.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/CustomerResource.java new file mode 100644 index 000000000..5e58a6464 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/CustomerResource.java @@ -0,0 +1,154 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex05_1.services; + +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.StreamingOutput; +import javax.ws.rs.core.UriInfo; + +import org.jboss.test.jaxrs.examples.ex05_1.domain.Customer; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +@Path("/customers") +public class CustomerResource +{ + private Map customerDB = Collections.synchronizedMap(new LinkedHashMap()); + + public CustomerResource() + { + Customer customer; + int id = 1; + + customer = new Customer(); + customer.setId(id); + customer.setFirstName("Bill"); + customer.setLastName("Burke"); + customer.setStreet("263 Clarendon Street"); + customer.setCity("Boston"); + customer.setState("MA"); + customer.setZip("02115"); + customer.setCountry("USA"); + customerDB.put(id++, customer); + + customer = new Customer(); + customer.setId(id); + customer.setFirstName("Joe"); + customer.setLastName("Burke"); + customer.setStreet("263 Clarendon Street"); + customer.setCity("Boston"); + customer.setState("MA"); + customer.setZip("02115"); + customer.setCountry("USA"); + customerDB.put(id++, customer); + + customer = new Customer(); + customer.setId(id); + customer.setFirstName("Monica"); + customer.setLastName("Burke"); + customer.setStreet("263 Clarendon Street"); + customer.setCity("Boston"); + customer.setState("MA"); + customer.setZip("02115"); + customer.setCountry("USA"); + customerDB.put(id++, customer); + + customer = new Customer(); + customer.setId(id); + customer.setFirstName("Steve"); + customer.setLastName("Burke"); + customer.setStreet("263 Clarendon Street"); + customer.setCity("Boston"); + customer.setState("MA"); + customer.setZip("02115"); + customer.setCountry("USA"); + customerDB.put(id++, customer); + } + + @GET + @Produces("application/xml") + public StreamingOutput getCustomers(final @QueryParam("start") int start, + final @QueryParam("size") @DefaultValue("2") int size) + { + return new StreamingOutput() + { + public void write(OutputStream outputStream) throws IOException, WebApplicationException + { + PrintStream writer = new PrintStream(outputStream); + writer.println(""); + synchronized (customerDB) + { + int i = 0; + for (Customer customer : customerDB.values()) + { + if (i >= start && i < start + size) outputCustomer(" ", writer, customer); + i++; + } + } + writer.println(""); + } + }; + } + + @GET + @Produces("application/xml") + @Path("uriinfo") + public StreamingOutput getCustomers(@Context UriInfo info) + { + int start = 0; + int size = 2; + if (info.getQueryParameters().containsKey("start")) + { + start = Integer.valueOf(info.getQueryParameters().getFirst("start")); + } + if (info.getQueryParameters().containsKey("size")) + { + size = Integer.valueOf(info.getQueryParameters().getFirst("size")); + } + return getCustomers(start, size); + } + + protected void outputCustomer(String indent, PrintStream writer, Customer cust) throws IOException + { + writer.println(indent + ""); + writer.println(indent + " " + cust.getFirstName() + ""); + writer.println(indent + " " + cust.getLastName() + ""); + writer.println(indent + " " + cust.getStreet() + ""); + writer.println(indent + " " + cust.getCity() + ""); + writer.println(indent + " " + cust.getState() + ""); + writer.println(indent + " " + cust.getZip() + ""); + writer.println(indent + " " + cust.getCountry() + ""); + writer.println(indent + ""); + } + +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/ShoppingApplication.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/ShoppingApplication.java new file mode 100644 index 000000000..431387b80 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex05_1/services/ShoppingApplication.java @@ -0,0 +1,45 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex05_1.services; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.HashSet; +import java.util.Set; + +@ApplicationPath("/services") +public class ShoppingApplication extends Application +{ + private Set singletons = new HashSet(); + + public ShoppingApplication() + { + singletons.add(new CustomerResource()); + singletons.add(new CarResource()); + } + + @Override + public Set getSingletons() + { + return singletons; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/CustomerResourceTest.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/CustomerResourceTest.java new file mode 100644 index 000000000..094425101 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/CustomerResourceTest.java @@ -0,0 +1,104 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex06_1; + +import java.io.File; +import java.net.URL; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Response; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.test.jaxrs.examples.ex06_1.domain.Customer; +import org.jboss.wsf.test.JBossWSTest; +import org.jboss.wsf.test.JBossWSTestHelper; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Bill Burke + * @author Alessio Soldano + */ +@RunWith(Arquillian.class) +public class CustomerResourceTest extends JBossWSTest +{ + @ArquillianResource + private URL baseURL; + + @Deployment(testable = false) + public static WebArchive createDeployments() { + WebArchive archive = ShrinkWrap.create(WebArchive.class, "jaxrs-examples-ex06_1.war"); + archive + .addManifest() + .addClass(org.jboss.test.jaxrs.examples.ex06_1.domain.Customer.class) + .addClass(org.jboss.test.jaxrs.examples.ex06_1.services.CustomerResource.class) + .addClass(org.jboss.test.jaxrs.examples.ex06_1.services.ShoppingApplication.class) + .setWebXML(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxrs/examples/ex06_x/WEB-INF/web.xml")); + return archive; + } + + @Test + @RunAsClient + public void testCustomerResource() throws Exception + { + Client client = ClientBuilder.newClient(); + try { + Customer newCustomer = new Customer(); + newCustomer.setFirstName("Bill"); + newCustomer.setLastName("Burke"); + newCustomer.setStreet("256 Clarendon Street"); + newCustomer.setCity("Boston"); + newCustomer.setState("MA"); + newCustomer.setZip("02115"); + newCustomer.setCountry("USA"); + + Response response = client.target(baseURL + "services/customers") + .request().post(Entity.xml(newCustomer)); + if (response.getStatus() != 201) throw new RuntimeException("Failed to create"); + String location = response.getLocation().toString(); + Assert.assertTrue(location.contains("jaxrs-examples-ex06_1/services/customers")); + response.close(); + + Customer customer = client.target(location).request().get(Customer.class); + Assert.assertEquals("Bill", customer.getFirstName()); + + customer.setFirstName("William"); + response = client.target(location).request().put(Entity.xml(customer)); + if (response.getStatus() != 204) throw new RuntimeException("Failed to update"); + + + // Show the update + customer = client.target(location).request().get(Customer.class); + Assert.assertEquals("William", customer.getFirstName()); + } finally { + client.close(); + } + } +} diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/domain/Customer.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/domain/Customer.java new file mode 100644 index 000000000..b2ac5f134 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/domain/Customer.java @@ -0,0 +1,142 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex06_1.domain; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "customer") +public class Customer +{ + private int id; + private String firstName; + private String lastName; + private String street; + private String city; + private String state; + private String zip; + private String country; + + @XmlAttribute + public int getId() + { + return id; + } + + public void setId(int id) + { + this.id = id; + } + + @XmlElement(name = "first-name") + public String getFirstName() + { + return firstName; + } + + public void setFirstName(String firstName) + { + this.firstName = firstName; + } + + @XmlElement(name = "last-name") + public String getLastName() + { + return lastName; + } + + public void setLastName(String lastName) + { + this.lastName = lastName; + } + + @XmlElement + public String getStreet() + { + return street; + } + + public void setStreet(String street) + { + this.street = street; + } + + @XmlElement + public String getCity() + { + return city; + } + + public void setCity(String city) + { + this.city = city; + } + + @XmlElement + public String getState() + { + return state; + } + + public void setState(String state) + { + this.state = state; + } + + @XmlElement + public String getZip() + { + return zip; + } + + public void setZip(String zip) + { + this.zip = zip; + } + + @XmlElement + public String getCountry() + { + return country; + } + + public void setCountry(String country) + { + this.country = country; + } + + @Override + public String toString() + { + return "Customer{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", street='" + street + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip='" + zip + '\'' + + ", country='" + country + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/services/CustomerResource.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/services/CustomerResource.java new file mode 100644 index 000000000..a0663830c --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/services/CustomerResource.java @@ -0,0 +1,90 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex06_1.services; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +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.WebApplicationException; +import javax.ws.rs.core.Response; + +import org.jboss.test.jaxrs.examples.ex06_1.domain.Customer; + +import java.net.URI; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +@Path("/customers") +public class CustomerResource +{ + private Map customerDB = new ConcurrentHashMap(); + private AtomicInteger idCounter = new AtomicInteger(); + + public CustomerResource() + { + } + + @POST + @Consumes("application/xml") + public Response createCustomer(Customer customer) + { + customer.setId(idCounter.incrementAndGet()); + customerDB.put(customer.getId(), customer); + System.out.println("Created customer " + customer.getId()); + return Response.created(URI.create("/customers/" + customer.getId())).build(); + + } + + @GET + @Path("{id}") + @Produces("application/xml") + public Customer getCustomer(@PathParam("id") int id) + { + Customer customer = customerDB.get(id); + if (customer == null) + { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + return customer; + } + + @PUT + @Path("{id}") + @Consumes("application/xml") + public void updateCustomer(@PathParam("id") int id, Customer update) + { + Customer current = customerDB.get(id); + if (current == null) throw new WebApplicationException(Response.Status.NOT_FOUND); + + current.setFirstName(update.getFirstName()); + current.setLastName(update.getLastName()); + current.setStreet(update.getStreet()); + current.setState(update.getState()); + current.setZip(update.getZip()); + current.setCountry(update.getCountry()); + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/services/ShoppingApplication.java b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/services/ShoppingApplication.java new file mode 100644 index 000000000..ef892ec9d --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/java/org/jboss/test/jaxrs/examples/ex06_1/services/ShoppingApplication.java @@ -0,0 +1,44 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2015, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.test.jaxrs.examples.ex06_1.services; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.HashSet; +import java.util.Set; + +@ApplicationPath("/services") +public class ShoppingApplication extends Application +{ + private Set singletons = new HashSet(); + + public ShoppingApplication() + { + singletons.add(new CustomerResource()); + } + + @Override + public Set getSingletons() + { + return singletons; + } +} \ No newline at end of file diff --git a/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex05_x/WEB-INF/web.xml b/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex05_x/WEB-INF/web.xml new file mode 100644 index 000000000..0f80d8e06 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex05_x/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + diff --git a/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex06_x/WEB-INF/web.xml b/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex06_x/WEB-INF/web.xml new file mode 100644 index 000000000..0f80d8e06 --- /dev/null +++ b/modules/testsuite/shared-rest-tests/src/test/resources/jaxrs/examples/ex06_x/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + +