Quantcast
Viewing latest article 1
Browse Latest Browse All 26

TDD development using Arquillian together with EJB

Go back to Index

In the previous sample multiple-containers-in-tdd we added support for multiple profiles, we are going to build on this project and add support for injecting EJBs

In Step 2 we make a java class that prints out information when a user is logging in. We will leave this example for now and we will look into some other functionality.
We are going to implement a shopping cart where we can add, list, update and delete products and place an order

First step is to create a couple of POJOs called Product and Order, right click on the “src/main/java/com/bekkestad/examples/tutorial” and choose “New/Class” name the class Product.java
Lets add some properties to this object. We need id, name, description, price.

package com.bekkestad.examples.tutorial;

public class Product {
private Long id;
private String name;
private String description;
private Double price;

public String getDescription() {
return description;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Double getPrice() {
return price;
}

public void setDescription(String description) {
this.description = description;
}

public void setId(Long id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setPrice(Double price) {
this.price = price;
}
}

package com.bekkestad.examples.tutorial;

import java.util.List;

public class Order {

private Long id;
private List products;
private String status;

public Order(Long orderId, List products, String status) {
super();
this.id = orderId;
this.products = products;
this.status = status;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public List getProducts() {
return products;
}

public void setProducts(List products) {
this.products = products;
}
}

Now lets create our OrderRepository, we will be able place an order and get order.

package com.bekkestad.examples.tutorial;

import java.util.List;

import javax.ejb.Local;

@Local
public interface OrderRepository {

Long placeOrder(List order);
Order getOrder(Long orderId);
Long getOrderId();
}

Next we need to create a shopping cart that will contain our order. Create new java class ShoppingCart, we want to tie this to a specific HTTPSession so lets annotate it with @SessionScoped
You should have something looking like this

package com.bekkestad.examples.tutorial;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;

@SessionScoped
public class ShoppingCart implements Serializable {

private static final long serialVersionUID = 1L;
}

Now we need to implement some methods, we need to add to cart, remove from cart, list cart contents, place an order and get order status.
The code should now look like this.

package com.bekkestad.examples.tutorial;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;

@SessionScoped
public class ShoppingCart implements Serializable {

private static final long serialVersionUID = 1L;
private Map shoppingCartMap;

@EJB
private OrderRepository order;

public void addProduct(Product product){
shoppingCartMap.put(product.getId(), product);
}

public void removeProduct(Long productId){
shoppingCartMap.remove(productId);
}

public List getShoppingCart(){
return Collections.unmodifiableList(new ArrayList(shoppingCartMap.values()));
}

public Long placeOrder(){
Long orderId = order.placeOrder(getShoppingCart());
shoppingCartMap.clear();
return orderId;
}

public String getOrderStatus(Long orderId){
return order.getOrder(orderId).getStatus();
}

@PostConstruct
void initialize(){
shoppingCartMap = new HashMap();
}
}

So lets do our test case, create a new test class called ShoppingCartTest in “src/test/java/com/bekkestad/examples/tutorial”, tell the class to run with arquillian and create a deployment method. The code should now look like this.

package com.bekkestad.examples.tutorial;

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.runner.RunWith;

@RunWith(Arquillian.class)
public class ShoppingCartTest {

@Deployment
public static JavaArchive createDeployment(){
return ShrinkWrap.create(JavaArchive.class, “test.jar”)
.addClasses(ShoppingCart.class, OrderRepository.class, OrderRepositoryImpl.class)
.addAsManifestResource(EmptyAsset.INSTANCE, “beans.xml”);
}
}

Note that we should now have an error, this is because we have yet to implement the OrderRepositoryImpl class.
Next lets do some tests, our java class should look like this.

package com.bekkestad.examples.tutorial;

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

import javax.ejb.EJB;
import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class ShoppingCartTest {

private static final List PRODUCT_LIST = new ArrayList();
private static final int NR_OF_PRODUCTS_TO_TEST = 4;
static {

for(int i = 0; i < NR_OF_PRODUCTS_TO_TEST; i++){
Product product = new Product();

product.setId(i+1L);
product.setName(“PRODUCT” + (i + 1));
product.setDescription(“This is product ” + (i+1));
product.setPrice(10D * (i + 1));
PRODUCT_LIST.add(product);
}
}

@Deployment
public static JavaArchive createDeployment(){
return ShrinkWrap.create(JavaArchive.class, “test.jar”)
.addClasses(ShoppingCart.class, OrderRepository.class, OrderRepositoryImpl.class, Product.class)
.addAsManifestResource(EmptyAsset.INSTANCE, “beans.xml”);
}

@Inject
ShoppingCart shoppingCart;

@EJB
OrderRepository order;

@Test
@InSequence(1)
public void should_be_able_to_add_products_to_cart(){
addProductsToCart();

List productsInCart = shoppingCart.getShoppingCart();
Assert.assertEquals(NR_OF_PRODUCTS_TO_TEST, productsInCart.size());
}

@Test
@InSequence(2)
public void should_be_able_to_get_the_shopping_cart(){
addProductsToCart();

List productsInCart = shoppingCart.getShoppingCart();

int temp = 0;
for(Product product : productsInCart){
Assert.assertEquals(PRODUCT_LIST.get(temp).getId(), product.getId());
Assert.assertEquals(PRODUCT_LIST.get(temp).getName(), product.getName());
Assert.assertEquals(PRODUCT_LIST.get(temp).getDescription(), product.getDescription());
Assert.assertEquals(PRODUCT_LIST.get(temp).getPrice(), product.getPrice());
temp++;
}
if(temp == 0)
Assert.fail(“THE CART IS EMPTY”);
}

@Test
@InSequence(3)
public void should_be_able_to_remove_product_from_cart(){
addProductsToCart();

int nrOfProductsToRemove = 2;
for(int i = 0; i < nrOfProductsToRemove; i++){
shoppingCart.removeProduct((i + 1)*1L);
}
List productsInCart = shoppingCart.getShoppingCart();
Assert.assertEquals(NR_OF_PRODUCTS_TO_TEST-nrOfProductsToRemove, productsInCart.size());
}

@Test
@InSequence(4)
public void should_be_able_to_place_order(){
addProductsToCart();

Long id = shoppingCart.placeOrder();
List productsInCart = shoppingCart.getShoppingCart();

Assert.assertEquals(true, id>0);
Assert.assertEquals(0, productsInCart.size());

}

@Test
@InSequence(5)
public void should_be_able_to_get_order_status(){
Long orderId = order.getOrderId();
String status = order.getOrder(orderId).getStatus();

Assert.assertEquals(“CREATED”, status);
}

private void addProductsToCart(){
for(Product product : PRODUCT_LIST){
shoppingCart.addProduct(product);
}
}
}

The next we need to do is to create the implementation of the OrderRepositoryImpl

package com.bekkestad.examples.tutorial;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;

@Singleton
@Lock(LockType.READ)
public class OrderRepositoryImpl implements OrderRepository {

private Map orders;
private Long orderId = 0L;

@Override
public Long getOrderId(){
return orderId;
}

@Override
@Lock(LockType.WRITE)
public Long placeOrder(List order) {
orders.put(++orderId, new Order(orderId, order, “CREATED”));
return orderId;
}

@Override
public Order getOrder(Long orderId) {
return orders.get(orderId);
}

@PostConstruct
void initialize(){
orders = new HashMap();
}

}

You should now be able to run the test, if you try to weld-ee-embeded profile you will not be able to test the @EJB test cases. Any of the other profiles we have created will work just fine.
One last note is that if you look at the OrderRepositoryImpl class you will see a very ugly implementation of the orderId field, the reason for this is because we are not bothered with it in this session, since we will get that value from the database in the next one.
Get the complete code from GitHub
The next session will cover setting up JPA

Go back to Index


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing latest article 1
Browse Latest Browse All 26

Trending Articles