Skip to content

Commit 217260e

Browse files
committed
refactoring sample/test for a cleaner usage
1 parent 2bbf674 commit 217260e

File tree

6 files changed

+92
-50
lines changed

6 files changed

+92
-50
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@
9393
<version>3.0.5.Final</version>
9494
<scope>test</scope>
9595
</dependency>
96+
<dependency>
97+
<groupId>org.jboss.resteasy</groupId>
98+
<artifactId>resteasy-jaxb-provider</artifactId>
99+
<version>3.0.5.Final</version>
100+
<scope>test</scope>
101+
</dependency>
96102
</dependencies>
97103
</profile>
98104
</profiles>
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
package org.javaee7.sample;
22

3-
import javax.enterprise.context.RequestScoped;
4-
import javax.inject.Inject;
5-
import javax.ws.rs.GET;
6-
import javax.ws.rs.Path;
7-
import javax.ws.rs.PathParam;
3+
import javax.xml.bind.annotation.XmlRootElement;
84

9-
@RequestScoped
10-
@Path("persons")
5+
/**
6+
* @author arungupta
7+
*/
8+
@XmlRootElement
119
public class Person {
10+
private String name;
1211

13-
@Inject
14-
PersonDatabase database;
12+
public Person() {
13+
}
14+
15+
public Person(String name) {
16+
this.name = name;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
1522

16-
@GET
17-
public String get() {
18-
return database.currentList().toString();
23+
public void setName(String name) {
24+
this.name = name;
1925
}
2026

21-
@GET
22-
@Path("{id}")
23-
public String get(@PathParam("id") int id) {
24-
return database.getPerson(id);
27+
@Override
28+
public String toString() {
29+
return name;
2530
}
26-
}
31+
}

src/main/java/org/javaee7/sample/PersonDatabase.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,30 @@
1010
@Singleton
1111
public class PersonDatabase {
1212

13-
List<String> persons;
13+
List<Person> persons;
1414

1515
@PostConstruct
1616
public void init() {
17-
persons = Arrays.asList("Penny", "Leonard", "Sheldon", "Amy", "Howard", "Bernadette", "Raj", "Priya");
17+
persons = Arrays.asList(
18+
new Person("Penny"),
19+
new Person("Leonard"),
20+
new Person("Sheldon"),
21+
new Person("Amy"),
22+
new Person("Howard"),
23+
new Person("Bernadette"),
24+
new Person("Raj"),
25+
new Person("Priya"));
1826
}
1927

20-
public List<String> currentList() {
21-
return persons;
28+
public Person[] currentList() {
29+
return persons.toArray(new Person[0]);
2230
}
2331

24-
public String getPerson(int id) {
32+
public Person getPerson(int id) {
2533
if (id < persons.size()) {
2634
return persons.get(id);
2735
}
2836

2937
throw new NotFoundException("Person with id \"" + id + "\" not found.");
3038
}
31-
32-
public void addPerson(String name) {
33-
persons.add(name);
34-
}
35-
36-
public void deletePerson(String name) {
37-
if (persons.contains(name)) {
38-
persons.remove(name);
39-
}
40-
41-
throw new NotFoundException("Person with name \"" + name + "\" not found.");
42-
}
43-
4439
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.javaee7.sample;
2+
3+
import javax.enterprise.context.RequestScoped;
4+
import javax.inject.Inject;
5+
import javax.ws.rs.GET;
6+
import javax.ws.rs.Path;
7+
import javax.ws.rs.PathParam;
8+
import javax.ws.rs.Produces;
9+
10+
//@RequestScoped
11+
@Path("persons")
12+
public class PersonResource {
13+
14+
@Inject
15+
PersonDatabase database;
16+
17+
@GET
18+
@Produces("application/xml")
19+
public Person[] get() {
20+
return database.currentList();
21+
}
22+
23+
@GET
24+
@Path("{id}")
25+
@Produces("application/xml")
26+
public Person get(@PathParam("id") int id) {
27+
return database.getPerson(id);
28+
}
29+
}

src/test/java/org/javaee7/sample/PersonTest.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.net.MalformedURLException;
44
import java.net.URI;
55
import java.net.URL;
6+
import java.util.StringTokenizer;
67
import javax.ws.rs.client.Client;
78
import javax.ws.rs.client.ClientBuilder;
89
import javax.ws.rs.client.WebTarget;
10+
import javax.ws.rs.core.MediaType;
911
import org.jboss.arquillian.container.test.api.Deployment;
1012
import org.jboss.arquillian.junit.Arquillian;
1113
import org.jboss.arquillian.test.api.ArquillianResource;
@@ -37,24 +39,25 @@ public static WebArchive createDeployment() {
3739
public void setUp() throws MalformedURLException {
3840
Client client = ClientBuilder.newClient();
3941
target = client.target(URI.create(new URL(base, "resources/persons").toExternalForm()));
42+
target.register(Person.class);
4043
}
4144

4245
/**
4346
* Test of get method, of class Person.
4447
*/
45-
// @Test
48+
@Test
4649
public void testGetAll() {
47-
String[] list = target.request().get(String[].class);
48-
assertEquals(8, list.length);
50+
Person[] persons = target.request().get(Person[].class);
51+
assertEquals(8, persons.length);
4952

50-
assertEquals("Penny", list[0]);
51-
assertEquals("Leonard", list[1]);
52-
assertEquals("Sheldon", list[2]);
53-
assertEquals("Amy", list[3]);
54-
assertEquals("Howard", list[4]);
55-
assertEquals("Bernadette", list[5]);
56-
assertEquals("Raj", list[6]);
57-
assertEquals("Priya", list[7]);
53+
assertEquals("Penny", persons[0].getName());
54+
assertEquals("Leonard", persons[1].getName());
55+
assertEquals("Sheldon", persons[2].getName());
56+
assertEquals("Amy", persons[3].getName());
57+
assertEquals("Howard", persons[4].getName());
58+
assertEquals("Bernadette", persons[5].getName());
59+
assertEquals("Raj", persons[6].getName());
60+
assertEquals("Priya", persons[7].getName());
5861
}
5962

6063
/**
@@ -64,11 +67,11 @@ public void testGetAll() {
6467
public void testGetOne() {
6568
WebTarget target2 = target.path("{id}");
6669

67-
String response = target2.resolveTemplate("id", 0).request().get(String.class);
68-
assertEquals("Penny", response);
70+
Person response = target2.resolveTemplate("id", 0).request().get(Person.class);
71+
assertEquals("Penny", response.getName());
6972

70-
response = target2.resolveTemplate("id", 1).request().get(String.class);
71-
assertEquals("Leonard", response);
73+
response = target2.resolveTemplate("id", 1).request().get(Person.class);
74+
assertEquals("Leonard", response.getName());
7275
}
7376

7477
}

src/test/resources/arquillian.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<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">
3+
<container qualifier="arquillian-wildfly-remote"/>
4+
</arquillian>

0 commit comments

Comments
 (0)