Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,50 @@

package org.javaee7.jaxrs.mapping.exceptions;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.net.MalformedURLException;
import java.net.URL;

import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.jboss.arquillian.container.test.api.Deployment;
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.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;

/**
*
* @author argupta
*/
@RunWith(Arquillian.class)
public class MyResourceTest {
WebTarget target;

@Deployment(testable = false)
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(
MyApplication.class, MyResource.class,
OrderNotFoundException.class, OrderNotFoundExceptionMapper.class);
}
@ArquillianResource
private URL base;

private WebTarget target;

@Before
public void setUp() {
public void setUp() throws MalformedURLException {
Client client = ClientBuilder.newClient();
target = client
.target("http://localhost:8080/mapping-exceptions/webresources/order");
.target(new URL(base, "webresources/order").toExternalForm());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
package org.javaee7.jaxrs.server.negotiation;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -51,11 +53,11 @@ public class MyResource {
@GET
@Produces({"application/xml; qs=0.75", "application/json; qs=1.0"})
// @Produces({"application/xml", "application/json"})
public Person[] getList() {
Person[] list = new Person[3];
list[0] = new Person("Penny", 1);
list[1] = new Person("Leonard", 2);
list[2] = new Person("Sheldon", 3);
public List<Person> getList() {
People list = new People();
list.add(new Person("Penny", 1));
list.add(new Person("Leonard", 2));
list.add(new Person("Sheldon", 3));

return list;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.javaee7.jaxrs.server.negotiation;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class People extends ArrayList<Person> {

private static final long serialVersionUID = 1L;

@XmlElement(name = "person")
public List<Person> getPeople() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,48 @@
package org.javaee7.jaxrs.server.negotiation;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.custommonkey.xmlunit.XMLAssert;
import org.jboss.arquillian.container.test.api.Deployment;
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.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.xml.sax.SAXException;

/**
* @author Arun Gupta
*/
@RunWith(Arquillian.class)
public class MyResourceTest {

WebTarget target;

@Deployment(testable = false)
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(MyApplication.class, MyResource.class, People.class, Person.class);
}

@ArquillianResource
private URL base;

private WebTarget target;

@Before
public void setUp() {
public void setUp() throws MalformedURLException {
Client client = ClientBuilder.newClient();
target = client
.target("http://localhost:8080/server-negotiation/webresources/persons");
target = client.target(new URL(base, "webresources/persons").toExternalForm());
}

@Test
Expand All @@ -55,7 +70,7 @@ public void testJson2() throws JSONException {
@Test
public void testXml() throws JSONException, SAXException, IOException {
String response = target.request().accept("application/xml").get(String.class);
XMLAssert.assertXMLEqual("<collection><person><age>1</age><name>Penny</name></person><person><age>2</age><name>Leonard</name></person><person><age>3</age><name>Sheldon</name></person></collection>",
XMLAssert.assertXMLEqual("<people><person><age>1</age><name>Penny</name></person><person><age>2</age><name>Leonard</name></person><person><age>3</age><name>Sheldon</name></person></people>",
response);
}

Expand Down