package test.com.cjpowered.learn.inventory;

import static org.junit.Assert.*;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import com.cjpowered.learn.inventory.InventoryDatabase;
import com.cjpowered.learn.inventory.InventoryManager;
import com.cjpowered.learn.inventory.Item;
import com.cjpowered.learn.inventory.Order;
import com.cjpowered.learn.inventory.StockedItem;
import com.cjpowered.learn.inventory.ace.AceInventoryManager;

/*
 * We need to keep items in stock to prevent back orders. See the README.md
 * for the requirements.
 *
 */

public class InventoryTest {

    @Test
    public void whenNoStockItemsDoNotOrder() {
    	// given
		final InventoryDatabase db = new DatabaseTemplate() {
			
			@Override
			public List<Item> stockItems(){
				// TODO Auto-generate method stub
				return Collections.emptyList();
//				return super.stockItems();
			}
		};
    	
        final LocalDate today = LocalDate.now();
        final InventoryManager im = new AceInventoryManager(db);

        // when
        final List<Order> actual = im.getOrders(today);

        // then
        assertTrue(actual.isEmpty());

    }
    
    @Test
    public void orderEnoughStock(){
	    	// given
    		int onHand = 10;
    		int shouldHave = 16;
    		
    		Item item = new StockedItem(shouldHave);
    		final InventoryDatabase db = new DatabaseTemplate() {
    			@Override
    			public int onHand(Item item){
    				// TODO Auto-generate method stub
    				return onHand;
    			}
    			
    			@Override
    			public List<Item> stockItems(){
    				// TODO Auto-generate method stub
    				return Collections.singletonList(item);
    			}
    		};
    		
    		final InventoryManager im = new AceInventoryManager(db);
    		final LocalDate today = LocalDate.now();
    	
	    	// when
	    	final List<Order> actual = im.getOrders(today);
    		
	    	// then
	    assertEquals(1, actual.size());
	    assertEquals(item, actual.get(0).item);
	    assertEquals(shouldHave - onHand, actual.get(0).quantity);
    }
    
    @Test
    public void orderStockWithSurplus(){
	    	// given
    		int onHand = 16;
    		int shouldHave = 10;
    		
    		Item item = new StockedItem(shouldHave);
    		final InventoryDatabase db = new DatabaseTemplate() {
    			@Override
    			public int onHand(Item item){
    				// TODO Auto-generate method stub
    				return onHand;
    			}
    			
    			@Override
    			public List<Item> stockItems(){
    				// TODO Auto-generate method stub
    				return Collections.singletonList(item);
    			}
    		};
    		
    		final InventoryManager im = new AceInventoryManager(db);
    		final LocalDate today = LocalDate.now();
    	
	    	// when
	    	final List<Order> actualOrders = im.getOrders(today);
    		
	    	// then
	    assertEquals(0, actualOrders.size());
//	    assertEquals(null, actualOrders.get(0));
//	    assertEquals(0, actualOrders.get(0).quantity);
    }

}

@@ -2067,24 +2067,25 @@
 .quantity);%0A
+%0A
     %7D%0A    %0A 
@@ -2982,11 +2982,947 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void orderStockWithEqualInventory()%7B%0A%09    %09// given%0A    %09%09int onHand = 10;%0A    %09%09int shouldHave = 10;%0A    %09%09%0A    %09%09Item item = new StockedItem(shouldHave);%0A    %09%09final InventoryDatabase db = new DatabaseTemplate() %7B%0A    %09%09%09@Override%0A    %09%09%09public int onHand(Item item)%7B%0A    %09%09%09%09// TODO Auto-generate method stub%0A    %09%09%09%09return onHand;%0A    %09%09%09%7D%0A    %09%09%09%0A    %09%09%09@Override%0A    %09%09%09public List%3CItem%3E stockItems()%7B%0A    %09%09%09%09// TODO Auto-generate method stub%0A    %09%09%09%09return Collections.singletonList(item);%0A    %09%09%09%7D%0A    %09%09%7D;%0A    %09%09%0A    %09%09final InventoryManager im = new AceInventoryManager(db);%0A    %09%09final LocalDate today = LocalDate.now();%0A    %09%0A%09    %09// when%0A%09    %09final List%3COrder%3E actualOrders = im.getOrders(today);%0A    %09%09%0A%09    %09// then%0A%09%09    assertEquals(0, actualOrders.size());%0A//%09%09    assertEquals(item, actualOrders.get(0).item);%0A//%09%09    assertEquals(shouldHave - onHand, actualOrders.get(0).quantity);%0A    %7D
 %0A%7D%0A

test check create right order when onhand and shouldhave is equal