Skip to content

Commit

Permalink
More tests...
Browse files Browse the repository at this point in the history
  • Loading branch information
asoldano committed Jan 12, 2016
1 parent d8d3416 commit 4a457a1
Show file tree
Hide file tree
Showing 21 changed files with 1,991 additions and 0 deletions.
@@ -0,0 +1,103 @@
/*
* 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.ex09_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.MediaType;
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 <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @author <a href="mailto:alessio.soldano@jboss.com">Alessio Soldano</a>
*/
@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-ex09_1.war");
archive
.addManifest()
.addClass(org.jboss.test.jaxrs.examples.ex09_1.domain.Customer.class)
.addClass(org.jboss.test.jaxrs.examples.ex09_1.services.CustomerResource.class)
.addClass(org.jboss.test.jaxrs.examples.ex09_1.services.ShoppingApplication.class)
.setWebXML(new File(JBossWSTestHelper.getTestResourcesDir() + "/jaxrs/examples/ex09_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-ex09_1/services/customers"));
response.close();

String xml = client.target(location).request()
.accept(MediaType.APPLICATION_XML_TYPE)
.get(String.class);
Assert.assertTrue(xml.contains("first-name>Bill</first-name>"));

String json = client.target(location).request()
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(String.class);
Assert.assertTrue(json.contains("\"first-name\":\"Bill\""));
} finally {
client.close();
}
}
}
@@ -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.ex09_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 + '\'' +
'}';
}
}
@@ -0,0 +1,83 @@
/*
* 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.ex09_1.services;


import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
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.ex09_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<Integer, Customer> customerDB = new ConcurrentHashMap<Integer, Customer>();
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", "application/json"})
public Customer getCustomer(@PathParam("id") int id)
{
Customer customer = customerDB.get(id);
if (customer == null)
{
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return customer;
}


@GET
@Path("{id}")
@Produces("text/plain")
public String getCustomerString(@PathParam("id") int id)
{
return getCustomer(id).toString();
}
}
@@ -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.ex09_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<Object> singletons = new HashSet<Object>();

public ShoppingApplication()
{
singletons.add(new CustomerResource());
}

@Override
public Set<Object> getSingletons()
{
return singletons;
}
}

0 comments on commit 4a457a1

Please sign in to comment.