Skip to content

Commit f6e9368

Browse files
committed
Adding a new test to show databse access from a JAX-RS resource.
Jason Lee, this one's for you! :)
1 parent 092e685 commit f6e9368

File tree

8 files changed

+217
-0
lines changed

8 files changed

+217
-0
lines changed

jaxrs/db-access/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jaxrs</groupId>
6+
<artifactId>jaxrs-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>db-access</artifactId>
12+
<packaging>war</packaging>
13+
</project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.javaee7.jaxrs.dbaccess;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.NamedQueries;
11+
import javax.persistence.NamedQuery;
12+
import javax.persistence.Table;
13+
import javax.xml.bind.annotation.XmlRootElement;
14+
15+
/**
16+
* @author Arun Gupta
17+
*/
18+
@Entity
19+
@Table(name = "REST_DB_ACCESS")
20+
@NamedQueries({
21+
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
22+
})
23+
@XmlRootElement
24+
public class Employee implements Serializable {
25+
private static final long serialVersionUID = 1L;
26+
@Id
27+
@GeneratedValue(strategy = GenerationType.AUTO)
28+
private int id;
29+
30+
@Column(length=40)
31+
private String name;
32+
33+
public Employee() { }
34+
35+
public Employee(String name) {
36+
this.name = name;
37+
}
38+
39+
public int getId() {
40+
return id;
41+
}
42+
43+
public void setId(int id) {
44+
this.id = id;
45+
}
46+
47+
public String getName() {
48+
return name;
49+
}
50+
51+
public void setName(String name) {
52+
this.name = name;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return name + " " + id;
58+
}
59+
60+
@Override
61+
public boolean equals(Object obj) {
62+
if (null == obj)
63+
return false;
64+
if (!(obj instanceof Employee))
65+
return false;
66+
Employee that = (Employee)obj;
67+
if (that.name.equals(this.name) && that.id == this.id)
68+
return true;
69+
else
70+
return false;
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(this.id, this.name);
76+
}
77+
78+
79+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javaee7.jaxrs.dbaccess;
2+
3+
import javax.ejb.Stateless;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.PersistenceContext;
6+
import javax.ws.rs.GET;
7+
import javax.ws.rs.Path;
8+
import javax.ws.rs.Produces;
9+
10+
/**
11+
* @author Arun Gupta
12+
*/
13+
@Path("employee")
14+
@Stateless
15+
public class EmployeeResource {
16+
17+
@PersistenceContext
18+
EntityManager em;
19+
20+
@GET
21+
@Produces("application/xml")
22+
public Employee[] get() {
23+
return em.createNamedQuery("Employee.findAll", Employee.class).getResultList().toArray(new Employee[0]);
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.javaee7.jaxrs.dbaccess;
2+
3+
import javax.ws.rs.core.Application;
4+
5+
/**
6+
* @author Arun Gupta
7+
*/
8+
@javax.ws.rs.ApplicationPath("webresources")
9+
public class MyApplication extends Application {
10+
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
INSERT INTO REST_DB_ACCESS("ID", "NAME") VALUES (1, 'Penny')
2+
INSERT INTO REST_DB_ACCESS("ID", "NAME") VALUES (2, 'Sheldon')
3+
INSERT INTO REST_DB_ACCESS("ID", "NAME") VALUES (3, 'Amy')
4+
INSERT INTO REST_DB_ACCESS("ID", "NAME") VALUES (4, 'Leonard')
5+
INSERT INTO REST_DB_ACCESS("ID", "NAME") VALUES (5, 'Bernadette')
6+
INSERT INTO REST_DB_ACCESS("ID", "NAME") VALUES (6, 'Raj')
7+
INSERT INTO REST_DB_ACCESS("ID", "NAME") VALUES (7, 'Howard')
8+
INSERT INTO REST_DB_ACCESS("ID", "NAME") VALUES (8, 'Priya')
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence
3+
version="2.1"
4+
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
7+
<persistence-unit name="MyPU" transaction-type="JTA">
8+
<properties>
9+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
10+
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
11+
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
12+
<property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
13+
</properties>
14+
</persistence-unit>
15+
</persistence>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.javaee7.jaxrs.dbaccess;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URI;
5+
import java.net.URL;
6+
import javax.ws.rs.client.Client;
7+
import javax.ws.rs.client.ClientBuilder;
8+
import javax.ws.rs.client.WebTarget;
9+
import javax.ws.rs.core.MediaType;
10+
import org.jboss.arquillian.container.test.api.Deployment;
11+
import org.jboss.arquillian.junit.Arquillian;
12+
import org.jboss.arquillian.test.api.ArquillianResource;
13+
import org.jboss.shrinkwrap.api.ShrinkWrap;
14+
import org.jboss.shrinkwrap.api.spec.WebArchive;
15+
import org.junit.Test;
16+
import static org.junit.Assert.*;
17+
import org.junit.Before;
18+
import org.junit.runner.RunWith;
19+
20+
/**
21+
* @author Arun Gupta
22+
*/
23+
@RunWith(Arquillian.class)
24+
public class EmployeeResourceTest {
25+
26+
private WebTarget target;
27+
28+
@Deployment(testable = false)
29+
public static WebArchive createDeployment() {
30+
return ShrinkWrap.create(WebArchive.class)
31+
.addClasses(Employee.class,
32+
EmployeeResource.class,
33+
MyApplication.class)
34+
.addAsResource("META-INF/persistence.xml")
35+
.addAsResource("META-INF/load.sql");
36+
}
37+
38+
@ArquillianResource
39+
private URL base;
40+
41+
@Before
42+
public void setUp() throws MalformedURLException {
43+
Client client = ClientBuilder.newClient();
44+
target = client.target(URI.create(new URL(base, "webresources/employee").toExternalForm()));
45+
target.register(Employee.class);
46+
}
47+
48+
@Test
49+
public void testGet() {
50+
Employee[] list = target
51+
.request(MediaType.APPLICATION_XML)
52+
.get(Employee[].class);
53+
assertNotNull(list);
54+
assertEquals(8, list.length);
55+
assertFalse(list[0].equals(new Employee("Penny")));
56+
assertFalse(list[1].equals(new Employee("Sheldon")));
57+
assertFalse(list[2].equals(new Employee("Amy")));
58+
assertFalse(list[3].equals(new Employee("Leonard")));
59+
assertFalse(list[4].equals(new Employee("Bernadette")));
60+
assertFalse(list[5].equals(new Employee("Raj")));
61+
assertFalse(list[6].equals(new Employee("Howard")));
62+
assertFalse(list[7].equals(new Employee("Priya")));
63+
}
64+
65+
}

jaxrs/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
<module>singleton</module>
4242
<module>readerwriter-injection</module>
4343
<module>jaxrs-security-declarative</module>
44+
<module>db-access</module>
4445
</modules>
4546
</project>

0 commit comments

Comments
 (0)