Commits

Micha Kops committed 315c7b5

EJB integration tests added.

Comments (0)

Files changed (4)

 
 Please feel free to have a look at [my blog] for the full tutorial.
 
-## Starting the embedded GlassFish
-
-Simply run the following command from the project directory:
-
-```
-mvn
-```
-
-
-## Calling the RESTful Webservice
-
-**List all books**
-
-```
-curl -XGET http://localhost:8080/sample/ws/book/all
-```
-
-**Create new book**
-
-```
-curl -XPOST -H "Content-type: application/json" -d '{"book":{"id":,"title":"Test"}}' http://localhost:8080/sample/ws/book/
-```
-
 ----
 
 ** 2013 Micha Kops /hasCode.com**

src/main/java/com/hascode/tutorial/ws/BookService.java

-package com.hascode.tutorial.ws;
-
-import java.util.List;
-
-import javax.ejb.EJB;
-import javax.ejb.Stateless;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.GenericEntity;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
-import com.hascode.tutorial.ejb.BookEJB;
-import com.hascode.tutorial.entity.Book;
-
-@Path("/book")
-@Stateless
-public class BookService {
-	@EJB
-	private BookEJB bookEJB;
-
-	@POST
-	@Consumes(MediaType.APPLICATION_JSON)
-	public Response createBook(final Book book) {
-		bookEJB.save(book);
-		return Response.ok().build();
-	}
-
-	@Path("/all")
-	@GET
-	@Produces(MediaType.APPLICATION_JSON)
-	public Response findAllBooks() {
-		GenericEntity<List<Book>> books = new GenericEntity<List<Book>>(
-				bookEJB.findAll()) {
-		};
-		return Response.ok(books).build();
-	}
-}

src/test/java/it/BookEJBIT.java

+package it;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.util.List;
+
+import javax.ejb.EJB;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.hascode.tutorial.ejb.BookEJB;
+import com.hascode.tutorial.entity.Book;
+
+@RunWith(Arquillian.class)
+public class BookEJBIT {
+	@Deployment
+	public static JavaArchive createDeployment() {
+		return ShrinkWrap.create(JavaArchive.class)
+				.addClasses(BookEJB.class, Book.class)
+				.addAsResource("META-INF/persistence.xml")
+				.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+	}
+
+	@EJB
+	BookEJB bookEJB;
+
+	@Test
+	public void shouldCreateASingleBook() throws Exception {
+		Book book = new Book();
+		book.setTitle("First book");
+		bookEJB.save(book);
+		assertThat(book.getId(), notNullValue());
+
+		List<Book> books = bookEJB.findAll();
+		assertThat(books.size(), equalTo(1));
+		assertThat(books.get(0).getTitle(), equalTo("First book"));
+	}
+
+	@Test
+	public void shouldReturnMultipleBooks() throws Exception {
+		Book book1 = new Book();
+		book1.setTitle("Some Book");
+
+		Book book2 = new Book();
+		book2.setTitle("Another book");
+
+		bookEJB.save(book1);
+		bookEJB.save(book2);
+
+		List<Book> books = bookEJB.findAll();
+		/**
+		 * three because the test 'shouldCreateASingleBook' already created one
+		 * book
+		 */
+		assertThat(books.size(), equalTo(3));
+	}
+}

src/test/java/it/TransactionalBookEJBIT.java

+package it;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.util.List;
+
+import javax.ejb.EJB;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.transaction.api.annotation.TransactionMode;
+import org.jboss.arquillian.transaction.api.annotation.Transactional;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.hascode.tutorial.ejb.BookEJB;
+import com.hascode.tutorial.entity.Book;
+
+@RunWith(Arquillian.class)
+@Transactional
+public class TransactionalBookEJBIT {
+	@Deployment
+	public static JavaArchive createDeployment() {
+		return ShrinkWrap.create(JavaArchive.class)
+				.addClasses(BookEJB.class, Book.class)
+				.addAsResource("META-INF/persistence.xml")
+				.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+	}
+
+	@EJB
+	BookEJB bookEJB;
+
+	@Test
+	@Transactional(TransactionMode.ROLLBACK)
+	public void shouldCreateASingleBook() throws Exception {
+		Book book = new Book();
+		book.setTitle("First book");
+		bookEJB.save(book);
+		assertThat(book.getId(), notNullValue());
+
+		List<Book> books = bookEJB.findAll();
+		assertThat(books.size(), equalTo(1));
+		assertThat(books.get(0).getTitle(), equalTo("First book"));
+	}
+
+	@Test
+	public void shouldReturnMultipleBooks() throws Exception {
+		Book book1 = new Book();
+		book1.setTitle("Some Book");
+
+		Book book2 = new Book();
+		book2.setTitle("Another book");
+
+		bookEJB.save(book1);
+		bookEJB.save(book2);
+
+		List<Book> books = bookEJB.findAll();
+		/**
+		 * now its two .. because the test 'shouldCreateASingleBook' created one
+		 * book but the transaction was rolled back.
+		 */
+		assertThat(books.size(), equalTo(2));
+	}
+}