package com.sepoe.wbitdd.pos;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * Class to test the respository handling.
 *
 * @author lumiha
 * @since 08/05/15.
 * <p>
 * ToTest: output invalid barcode
 * ToTest: output of not found item for barcode
 * ToRefactor: generate random valid barcodes - next
 * ToRefactor: output should write string to output errors
 * ToRefactor: price to price object
 * ToRefactor: wrap Barcode into a wrapper class (inspired by object calisthenics)
 */
public class PointOfSaleRepositoryHandlingTest {

    private MockItemRepository itemRepository = new MockItemRepository();
    private MockOutputDevice outputDevice = new MockOutputDevice();
    private PointOfSale pointOfSale = new PointOfSale(itemRepository, outputDevice);

    @Test
    public void onBarcode_withValidBarcode_looksUpRepository() {
        final String barcode1 = "123456789012";
        pointOfSale.onBarcode(barcode1);
        assertThat(itemRepository.getLookupBarcode(), is(barcode1));

        final String barcode2 = "234567890123";
        pointOfSale.onBarcode(barcode2);
        assertThat(itemRepository.getLookupBarcode(), is(barcode2));
    }

    @Test
    public void onBarcode_withExistingItem_passesPriceToOutputDevice() {
        final String barcode1 = "123456789012";
        final double price1 = 42.42;
        itemRepository.when(barcode1, price1);

        pointOfSale.onBarcode(barcode1);
        assertThat(outputDevice.getWrittenPrice(), is(price1));

        final String barcode2 = "234567890123";
        final double price2 = 24.24;
        itemRepository.when(barcode2, price2);

        pointOfSale.onBarcode(barcode2);
        assertThat(outputDevice.getWrittenPrice(), is(price2));
    }
}

@@ -48,16 +48,42 @@
 .Test;%0A%0A
+import java.util.Random;%0A%0A
 import s
@@ -118,16 +118,16 @@
 ers.is;%0A
-
 import s
@@ -343,61 +343,8 @@
 ode%0A
- * ToRefactor: generate random valid barcodes - next%0A
  * T
@@ -903,38 +903,41 @@
  barcode1 = 
-%22123456789012%22
+generateBarCode()
 ;%0A        po
@@ -1065,38 +1065,41 @@
  barcode2 = 
-%22234567890123%22
+generateBarCode()
 ;%0A        po
@@ -1324,22 +1324,25 @@
 1 = 
-%22123456789012%22
+generateBarCode()
 ;%0A  
@@ -1566,22 +1566,25 @@
 2 = 
-%22234567890123%22
+generateBarCode()
 ;%0A  
@@ -1771,16 +1771,314 @@
 ice2));%0A
-
     %7D%0A
+%0A    private String generateBarCode() %7B%0A        char%5B%5D digitArray = new char%5B12%5D;%0A%0A        for (int i = 0; i %3C 12; i++) %7B%0A            final int digit = new Random().nextInt(10);%0A            digitArray%5Bi%5D = Integer.toString(digit).charAt(0);%0A        %7D%0A%0A        return new String(digitArray);%0A    %7D%0A%0A
 %7D%0A

generated random barcodes