Enterprise Java Development@TOPIC@

Business Logic

Revision: v2013-09-09

Built on: 2014-03-06 23:54 EST

Abstract

This presentation introduces the concept of the business logic tier, using it to form a user facade around the capabilities of the service, and to leverage business logic interfaces and tests as a form of executable requirements.


Purpose
1. Goals
2. Objectives
1. Session Facade Pattern
1.1. Context
1.2. Problem
1.3. Forces
1.4. Solution
1.5. Consequences
2. Using Business Logic for Requirements
2.1. Business Interfaces Capture Functional Requirements
2.2. Business Objects Capture Data Requirements
2.3. Test Cases as Executable Requirements
2.4. Sample Project Layout


/**
 * Purchasing handles payment of purchased products.
 */
public interface Purchasing {
    /**
     * Creates an account for the user to use in purchasing products.
     * @param email
     * @param firstName
     * @param lastName
     * @return
     */
    Account createAccount(String email, String firstName, String lastName);
    /**
     * Completes the purchase of the items in the user's shopping cart,
     * empties the cart, and returns the total cost paid.<p/>
     * 
     * Note that this capability is not yet fully defined.
     * @param email
     * @param password
     * @return
     */
    double checkout(String email, String password);
}
/**

 * The catalog maintains a view of the inventory known to our application. 
 */
public interface Catalog {
    /**
     * Returns a list of products in the catalog chunked into page sizes.
     * @param offset
     * @param limit
     * @return
     */
    List<Product> getProducts(int offset, int limit);
    /**
     * Adds the selected product to the users' shopping cart and returns
     * the count of items.
     * @param id
     * @param validEmail
     */
    int addToCart(int id, String validEmail);
}
/**

 * A user shall be able to establish an account with just a first 
 * and last name and a unique email address.
 */
@Test
public void establishAccount() {
    log.info("*** establishAccount ***");
    
    //the user will supply their email address, first and last name
    String email="jharb@ravens.com";
    String firstName="john";
    String lastName="harbaugh";
    Account account = purchasing.createAccount(email, firstName, lastName);
    
    //they will get back a generated password to use as a login for the account
    assertNotNull("no account returned", account);
    assertNotNull("no password assigned", account.getPassword());
}
/**

 * A user shall be able browse products in the catalog.
 */
@Test
public void browseCatalog() {
    log.info("*** browseCatalog ***");
    
        //the user will ask for product summaries in pages
    int pageSize=10;
    int offset=0;
    List<Product> products = catalog.getProducts(offset, pageSize);
    
        //they will receive <= a page size of product information
    assertNotNull("no products returned", products);
    assertTrue("no products provided", products.size() > 0);
    
        //they can page thru the entire set
    for (int i=0; products.size() != 0; i++) {
        offset += products.size();
        products = catalog.getProducts(offset, pageSize);
        assertTrue("this catalog never ends!!!", i<100);
    }
}
/**

 * A user shall be able to purchase a product in the catalog.
 */
@Test
public void purchaseProduct() {
    log.info("*** purchaseProduct ***");
    
        //the user selects a product
    Product product=null;
    Random random=new Random();
    for (int i=0; product == null; i++) {
        List<Product> products=catalog.getProducts(random.nextInt(100), 1);
        product=products.iterator().next();
        assertTrue("I can't find anything to buy!!!", i<1000);
    }
    
        //the user adds the product to their shopping cart by providing the 
        //product id and their credentials
    int count=catalog.addToCart(product.getId(), validEmail);
        //the user receives a count of the items in the cart
    assertEquals("somebody tweeked my cart!!!!", 1, count);
    
        //the user checks out with the cashier -- payment not yet implemented
    double total=purchasing.checkout(validEmail, validPassword);
    
        //the user gets a total amount back as their receipt
    assertEquals("price doesn't add up", product.getPrice(), total, .01);
}