View Javadoc

    1   package org.example.customerdao;
    2
    3   import static org.junit.jupiter.api.Assertions.assertEquals;
    4   import static org.junit.jupiter.api.Assertions.assertNotNull;
    5   import static org.junit.jupiter.api.Assertions.assertNull;
    6   import static org.junit.jupiter.api.Assertions.assertTrue;
    7   import static org.junit.jupiter.api.Assertions.fail;
    8   import static org.mockito.Mockito.when;
    9
    10  import java.io.File;
    11  import java.time.LocalDate;
    12  import java.util.ArrayList;
    13  import java.util.List;
    14
    15  import org.apache.logging.log4j.Level;
    16  import org.apache.logging.log4j.LogManager;
    17  import org.apache.logging.log4j.core.config.Configurator;
    18  import org.example.customer.Customer;
    19  import org.example.customer.Order;
    20  import org.example.customer.OrderItem;
    21  import org.example.customer.utility.CustomerEntity;
    22  import org.example.customerdao.testcategories.SmokeTest;
    23  import org.example.dao.testUtils.jdbc.DBUnitJDBCUtility;
    24  import org.example.websecurity.UserCredentials;
    25  import org.junit.AfterClass;
    26  import org.junit.BeforeClass;
    27  import org.junit.Test;
    28  import org.junit.experimental.categories.Category;
    29  import org.mockito.Mockito;
    30
    31  public class OrderDAOFastTests
    32  {
    33      private static String configDir = "src" + File.separator
    34              + "test" + File.separator + "resources" + File.separator + "data";
    35
    36      private static final String SCHEMA_FILE = configDir + File.separator + "Customer.sql";
    37      private static final String DATA_FILE = configDir + File.separator + "fullDB.xml";
    38
    39      private static DBUnitJDBCUtility utility = null;
    40
    41      @BeforeClass // this runs only once before any test
    42      public static void setup()
    43      {
    44          Configurator.setLevel(LogManager.getLogger(Order.class).getName(), Level.WARN);
    45
    46          try
    47          {
    48              utility = new DBUnitJDBCUtility(SCHEMA_FILE, DATA_FILE);
    49          }
    50          catch (Exception e)
    51          {
    52              fail(e.getLocalizedMessage());
    53          }
    54      }
    55
    56      @AfterClass
    57      public static void shutdown()
    58      {
    59          if (utility != null)
    60          {
    61              utility.shutdown();
    62          }
    63      }
    64
    65      @Test(expected = IllegalArgumentException.class)
    66      public void testNullCredentialsFindAllOrders()
    67              throws Exception
    68      {
    69          OrderDAO handler = new OrderDAOImpl();
    70          handler.setReadOnlyDS(utility.getDataSource());
    71
    72          handler.findAllOrders(null);
    73      }
    74
    75      @Test(expected = IllegalArgumentException.class)
    76      public void testUnauthorizedFindAllOrders()
    77              throws Exception
    78      {
    79          OrderDAO handler = new OrderDAOImpl();
    80          handler.setReadOnlyDS(utility.getDataSource());
    81
    82          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    83          when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    84
    85          handler.findAllOrders(userCredentialsMock);
    86      }
    87
    88      @Category(SmokeTest.class)
    89      @Test
    90      public void testFindAllOrders()
    91              throws Exception
    92      {
    93          OrderDAO handler = new OrderDAOImpl();
    94          handler.setReadOnlyDS(utility.getDataSource());
    95
    96          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    97          when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    98
    99          List<Order> records = handler.findAllOrders(userCredentialsMock);
    100         assertNotNull(records);
    101         assertTrue(records.size() == 830);
    102         Order order323 = records.get(322);
    103         // <ORDERS ID="323" ORDER_DATE="2013-06-17" CUSTOMER_ID="51" TOTAL_AMOUNT="2595.00" ORDER_NUMBER="542700"/>
    104         // <ORDER_ITEM ID="861" ORDER_ID="323" PRODUCT_ID="11" UNIT_PRICE="21.00" QUANTITY="15"/>
    105         // <ORDER_ITEM ID="862" ORDER_ID="323" PRODUCT_ID="56" UNIT_PRICE="38.00" QUANTITY="60"/>
    106
    107         String expectedOrderNumber = "542700";
    108         String acutalOrderNumber = order323.getOrderNumber();
    109         assertEquals(expectedOrderNumber, acutalOrderNumber);
    110
    111         List<OrderItem> order323OrderItems = order323.getOrderItems();
    112
    113         int expectedOrderItemCount = 2;
    114         int actualOrderItemCount = order323OrderItems.size();
    115         assertEquals(expectedOrderItemCount, actualOrderItemCount);
    116
    117         double expectedSubTotalCost = 21.00 * 15;
    118         double acutalSubTotalCost = order323OrderItems.get(0).getSubTotal();
    119         double precision = 0.001;
    120         assertEquals(expectedSubTotalCost, acutalSubTotalCost, precision);
    121
    122     }
    123
    124     @Test(expected = IllegalArgumentException.class)
    125     public void testNullCredentialsFindAllOrderItems()
    126             throws Exception
    127     {
    128         OrderDAO handler = new OrderDAOImpl();
    129         handler.setReadOnlyDS(utility.getDataSource());
    130
    131         Order mockOrder = buildMockOrder();
    132
    133         handler.findAllOrderItems(null, mockOrder);
    134     }
    135
    136     @Test(expected = IllegalArgumentException.class)
    137     public void testUnauthorizedFindAllOrderItemss()
    138             throws Exception
    139     {
    140         OrderDAO handler = new OrderDAOImpl();
    141         handler.setReadOnlyDS(utility.getDataSource());
    142
    143         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    144         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    145
    146         Order mockOrder = buildMockOrder();
    147
    148         handler.findAllOrderItems(userCredentialsMock, mockOrder);
    149     }
    150
    151     @Test(expected = IllegalArgumentException.class)
    152     public void testNullOrderFindAllOrderItems()
    153             throws Exception
    154     {
    155         OrderDAO handler = new OrderDAOImpl();
    156         handler.setReadOnlyDS(utility.getDataSource());
    157
    158         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    159         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    160
    161         handler.findAllOrderItems(userCredentialsMock, null);
    162     }
    163
    164     @Test
    165     public void testFindAllOrderItems()
    166             throws Exception
    167     {
    168         OrderDAO handler = new OrderDAOImpl();
    169         handler.setReadOnlyDS(utility.getDataSource());
    170
    171         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    172         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    173
    174         Order mockOrder = buildMockOrder();
    175
    176         List<OrderItem> records = handler.findAllOrderItems(userCredentialsMock, mockOrder);
    177
    178        // <ORDER_ITEM ID="56" ORDER_ID="21" PRODUCT_ID="29" UNIT_PRICE="99.00" QUANTITY="10"/>
    179        // <ORDER_ITEM ID="57" ORDER_ID="21" PRODUCT_ID="72" UNIT_PRICE="27.80" QUANTITY="4"/>
    180
    181         assertNotNull(records);
    182         assertTrue(records.size() == 2);
    183         OrderItem orderItem56 = records.get(0);
    184
    185         int expectedProductId = 29;
    186         int acutalProductId = orderItem56.getProductId();
    187         assertEquals(expectedProductId, acutalProductId);
    188
    189         double expectedSubTotalCost = 99.00 * 10;
    190         double acutalSubTotalCost = orderItem56.getSubTotal();
    191         double precision = 0.001;
    192         assertEquals(expectedSubTotalCost, acutalSubTotalCost, precision);
    193
    194     }
    195
    196     @Test(expected = IllegalArgumentException.class)
    197     public void testNullCredentialsFindOrderById()
    198             throws Exception
    199     {
    200         OrderDAO handler = new OrderDAOImpl();
    201         handler.setReadOnlyDS(utility.getDataSource());
    202
    203         handler.findOrderById(null, 1);
    204     }
    205
    206     @Test(expected = IllegalArgumentException.class)
    207     public void testUnauthorizedFindOrderById()
    208             throws Exception
    209     {
    210         OrderDAO handler = new OrderDAOImpl();
    211         handler.setReadOnlyDS(utility.getDataSource());
    212
    213         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    214         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    215
    216         handler.findOrderById(userCredentialsMock, 1);
    217     }
    218
    219     @Category(SmokeTest.class)
    220     @Test
    221     public void testFindOrderById()
    222             throws Exception
    223     {
    224         OrderDAO handler = new OrderDAOImpl();
    225         handler.setReadOnlyDS(utility.getDataSource());
    226
    227         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    228         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    229
    230         Order order323 = handler.findOrderById(userCredentialsMock, 323);
    231
    232         // <ORDERS ID="323" ORDER_DATE="2013-06-17" CUSTOMER_ID="51" TOTAL_AMOUNT="2595.00" ORDER_NUMBER="542700"/>
    233         // <ORDER_ITEM ID="861" ORDER_ID="323" PRODUCT_ID="11" UNIT_PRICE="21.00" QUANTITY="15"/>
    234         // <ORDER_ITEM ID="862" ORDER_ID="323" PRODUCT_ID="56" UNIT_PRICE="38.00" QUANTITY="60"/>
    235
    236         String expectedOrderNumber = "542700";
    237         String acutalOrderNumber = order323.getOrderNumber();
    238         assertEquals(expectedOrderNumber, acutalOrderNumber);
    239
    240         List<OrderItem> order323OrderItems = order323.getOrderItems();
    241
    242         int expectedOrderItemCount = 2;
    243         int actualOrderItemCount = order323OrderItems.size();
    244         assertEquals(expectedOrderItemCount, actualOrderItemCount);
    245
    246         double expectedSubTotalCost = 21.00 * 15;
    247         double acutalSubTotalCost = order323OrderItems.get(0).getSubTotal();
    248         double precision = 0.001;
    249         assertEquals(expectedSubTotalCost, acutalSubTotalCost, precision);
    250
    251     }
    252
    253     @Test
    254     public void testInvalidFindOrderById()
    255             throws Exception
    256     {
    257         OrderDAO handler = new OrderDAOImpl();
    258         handler.setReadOnlyDS(utility.getDataSource());
    259
    260         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    261         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    262
    263         assertNull(handler.findOrderById(userCredentialsMock, 921));
    264     }
    265
    266     @Test(expected = IllegalArgumentException.class)
    267     public void testNullCredentialsFindOrderByCustomer()
    268             throws Exception
    269     {
    270         OrderDAO handler = new OrderDAOImpl();
    271         handler.setReadOnlyDS(utility.getDataSource());
    272
    273         Customer mockCustomer = new Customer();
    274         mockCustomer.setId(6);
    275
    276         handler.findOrdersByCustomer(null, mockCustomer);
    277     }
    278
    279
    280     @Test(expected = IllegalArgumentException.class)
    281     public void testUnauthorizedFindOrdersByCustomer()
    282             throws Exception
    283     {
    284         OrderDAO handler = new OrderDAOImpl();
    285         handler.setReadOnlyDS(utility.getDataSource());
    286
    287         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    288         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    289
    290         Customer mockCustomer = new Customer();
    291         mockCustomer.setId(6);
    292
    293         handler.findOrdersByCustomer(userCredentialsMock, mockCustomer);
    294     }
    295
    296
    297     @Test(expected = IllegalArgumentException.class)
    298     public void testNullFindOrdersByCustomer()
    299             throws Exception
    300     {
    301         OrderDAO handler = new OrderDAOImpl();
    302         handler.setReadOnlyDS(utility.getDataSource());
    303
    304         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    305         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    306
    307         handler.findOrdersByCustomer(userCredentialsMock, null);
    308     }
    309
    310     @Test
    311     public void testFindOrdersByCustomer()
    312             throws Exception
    313     {
    314         OrderDAO handler = new OrderDAOImpl();
    315         handler.setReadOnlyDS(utility.getDataSource());
    316
    317         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    318         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    319
    320         Customer mockCustomer = new Customer();
    321         mockCustomer.setId(6);
    322
    323         List<Order> records = handler.findOrdersByCustomer(userCredentialsMock, mockCustomer);
    324
    325         //   <ORDERS ID="254" ORDER_DATE="2013-04-09" CUSTOMER_ID="6" TOTAL_AMOUNT="149.00" ORDER_NUMBER="542631"/>
    326         //   <ORDERS ID="262" ORDER_DATE="2013-04-17" CUSTOMER_ID="6" TOTAL_AMOUNT="136.80" ORDER_NUMBER="542639"/>
    327         //   <ORDERS ID="335" ORDER_DATE="2013-06-27" CUSTOMER_ID="6" TOTAL_AMOUNT="330.00" ORDER_NUMBER="542712"/>
    328         //   <ORDERS ID="367" ORDER_DATE="2013-07-29" CUSTOMER_ID="6" TOTAL_AMOUNT="464.00" ORDER_NUMBER="542744"/>
    329         //   <ORDERS ID="606" ORDER_DATE="2014-01-27" CUSTOMER_ID="6" TOTAL_AMOUNT="625.00" ORDER_NUMBER="542983"/>
    330         //   <ORDERS ID="709" ORDER_DATE="2014-03-17" CUSTOMER_ID="6" TOTAL_AMOUNT="677.00" ORDER_NUMBER="543086"/>
    331         //   <ORDERS ID="811" ORDER_DATE="2014-04-29" CUSTOMER_ID="6" TOTAL_AMOUNT="858.00" ORDER_NUMBER="543188"/>
    332
    333         assertNotNull(records);
    334         assertTrue(records.size() == 7);
    335         Order order367 = records.get(3);
    336
    337         LocalDate expectedOrderDate = LocalDate.of(2013, 7, 29);
    338         LocalDate acutalOrderDate = order367.getOrderDate();
    339         assertEquals(expectedOrderDate, acutalOrderDate);
    340
    341         double expectedTotalAmount = 464.00;
    342         double acutalTotalAmount = order367.getTotalAmount();
    343         double precision = 0.001;
    344         assertEquals(expectedTotalAmount, acutalTotalAmount, precision);
    345
    346     }
    347
    348     @Test
    349     public void testInvalidFindOrdersByCustomer()
    350             throws Exception
    351     {
    352         OrderDAO handler = new OrderDAOImpl();
    353         handler.setReadOnlyDS(utility.getDataSource());
    354
    355         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    356         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    357
    358         Customer mockCustomer = new Customer();
    359         mockCustomer.setId(930);
    360
    361         List<Order> records = handler.findOrdersByCustomer(userCredentialsMock, mockCustomer);
    362
    363         assertNotNull(records);
    364         assertTrue(records.size() == 0);
    365     }
    366
    367     @Test(expected = IllegalArgumentException.class)
    368     public void testNullCredentialsDeleteOrderById()
    369             throws Exception
    370     {
    371         OrderDAO handler = new OrderDAOImpl();
    372         handler.setReadOnlyDS(utility.getDataSource());
    373
    374        handler.deleteEntity(null, 1);
    375     }
    376
    377     @Test(expected = IllegalArgumentException.class)
    378     public void testNullCredentialsDeleteOrder()
    379             throws Exception
    380     {
    381         OrderDAO handler = new OrderDAOImpl();
    382         handler.setReadOnlyDS(utility.getDataSource());
    383
    384         Order mockOrder = buildMockOrder();
    385
    386         handler.deleteEntity(null, mockOrder);
    387     }
    388
    389     @Test(expected = IllegalArgumentException.class)
    390     public void testUnauthorizedDeleteOrderById()
    391             throws Exception
    392     {
    393         OrderDAO handler = new OrderDAOImpl();
    394         handler.setReadOnlyDS(utility.getDataSource());
    395
    396         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    397         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    398
    399         handler.deleteEntity(userCredentialsMock, 1);
    400     }
    401
    402     @Test(expected = IllegalArgumentException.class)
    403     public void testUnauthorizedDeleteOrder()
    404             throws Exception
    405     {
    406         OrderDAO handler = new OrderDAOImpl();
    407         handler.setReadOnlyDS(utility.getDataSource());
    408
    409         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    410         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    411
    412         Order mockOrder = buildMockOrder();
    413
    414         handler.deleteEntity(userCredentialsMock, mockOrder);
    415     }
    416
    417
    418     @Test(expected = IllegalArgumentException.class)
    419     public void testNullOrderDeleteOrder()
    420             throws Exception
    421     {
    422         OrderDAO handler = new OrderDAOImpl();
    423         handler.setReadOnlyDS(utility.getDataSource());
    424
    425         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    426         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    427
    428         handler.deleteEntity(userCredentialsMock, null);
    429     }
    430
    431     @Test(expected = IllegalArgumentException.class)
    432     public void testUnauthorizedIsDeleteableById()
    433             throws Exception
    434     {
    435         OrderDAO handler = new OrderDAOImpl();
    436         handler.setReadOnlyDS(utility.getDataSource());
    437
    438         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    439         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    440
    441         handler.isDeleteable(userCredentialsMock, 1);
    442     }
    443
    444     @Test(expected = IllegalArgumentException.class)
    445     public void testUnauthorizedIsDeleteable()
    446             throws Exception
    447     {
    448         OrderDAO handler = new OrderDAOImpl();
    449         handler.setReadOnlyDS(utility.getDataSource());
    450
    451         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    452         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    453
    454         Order mockOrder = buildMockOrder();
    455         handler.isDeleteable(userCredentialsMock, mockOrder);
    456     }
    457
    458     @Test(expected = IllegalArgumentException.class)
    459     public void testNullCredentialsIsDeleteableById()
    460             throws Exception
    461     {
    462         OrderDAO handler = new OrderDAOImpl();
    463         handler.setReadOnlyDS(utility.getDataSource());
    464
    465         handler.isDeleteable(null, 1);
    466     }
    467
    468     @Test(expected = IllegalArgumentException.class)
    469     public void testNullCredentialsIsDeleteable()
    470             throws Exception
    471     {
    472         OrderDAO handler = new OrderDAOImpl();
    473         handler.setReadOnlyDS(utility.getDataSource());
    474
    475         Order mockOrder = buildMockOrder();
    476         handler.isDeleteable(null, mockOrder);
    477     }
    478
    479     @Test(expected = IllegalArgumentException.class)
    480     public void testNullOrderIsDeleteable()
    481             throws Exception
    482     {
    483         OrderDAO handler = new OrderDAOImpl();
    484         handler.setReadOnlyDS(utility.getDataSource());
    485
    486         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    487         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    488
    489         handler.isDeleteable(userCredentialsMock, null);
    490     }
    491
    492     @Test(expected = IllegalArgumentException.class)
    493     public void testWrongEntityIsDeleteable()
    494             throws Exception
    495     {
    496         OrderDAO handler = new OrderDAOImpl();
    497         handler.setReadOnlyDS(utility.getDataSource());
    498
    499         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    500         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    501
    502         CustomerEntity entity = new CustomerEntity();
    503
    504         handler.isDeleteable(userCredentialsMock, entity);
    505     }
    506
    507     @Test
    508     public void testIsDeleteableById()
    509             throws Exception
    510     {
    511         OrderDAO handler = new OrderDAOImpl();
    512         handler.setReadOnlyDS(utility.getDataSource());
    513
    514         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    515         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    516
    517         assertTrue(handler.isDeleteable(userCredentialsMock, 1));
    518     }
    519
    520     @Test
    521     public void testIsDeleteable()
    522             throws Exception
    523     {
    524         OrderDAO handler = new OrderDAOImpl();
    525         handler.setReadOnlyDS(utility.getDataSource());
    526
    527         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    528         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    529
    530         Order mockOrder = buildMockOrder();
    531
    532         assertTrue(handler.isDeleteable(userCredentialsMock, mockOrder));
    533     }
    534
    535     @Test(expected = IllegalArgumentException.class)
    536     public void testNullCredentialsAddOrder()
    537             throws Exception
    538     {
    539         OrderDAO handler = new OrderDAOImpl();
    540         handler.setReadOnlyDS(utility.getDataSource());
    541
    542         Order mockOrder = buildMockOrder();
    543
    544         handler.addOrder(null, mockOrder);
    545     }
    546
    547
    548     @Test(expected = IllegalArgumentException.class)
    549     public void testUnauthorizedAddOrder()
    550             throws Exception
    551     {
    552         OrderDAO handler = new OrderDAOImpl();
    553         handler.setReadOnlyDS(utility.getDataSource());
    554
    555         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    556         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    557
    558         Order mockOrder = buildMockOrder();
    559
    560         handler.addOrder(userCredentialsMock, mockOrder);
    561     }
    562
    563
    564     @Test(expected = IllegalArgumentException.class)
    565     public void testNullOrderAddOrder()
    566             throws Exception
    567     {
    568         OrderDAO handler = new OrderDAOImpl();
    569         handler.setReadOnlyDS(utility.getDataSource());
    570
    571         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    572         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    573
    574         handler.addOrder(userCredentialsMock, null);
    575     }
    576
    577     @Test(expected = IllegalArgumentException.class)
    578     public void testUnauthorizedUpdateOrder()
    579             throws Exception
    580     {
    581         OrderDAO handler = new OrderDAOImpl();
    582         handler.setReadOnlyDS(utility.getDataSource());
    583
    584         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    585         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    586
    587         Order mockOrder = buildMockOrder();
    588
    589         handler.updateOrder(userCredentialsMock, mockOrder);
    590     }
    591
    592     @Test(expected = IllegalArgumentException.class)
    593     public void testNullCredentialsUpdateOrder()
    594             throws Exception
    595     {
    596         OrderDAO handler = new OrderDAOImpl();
    597         handler.setReadOnlyDS(utility.getDataSource());
    598
    599         Order mockOrder = buildMockOrder();
    600
    601         handler.updateOrder(null, mockOrder);
    602     }
    603
    604     @Test(expected = IllegalArgumentException.class)
    605     public void testNullOrderUpdateOrder()
    606             throws Exception
    607     {
    608         OrderDAO handler = new OrderDAOImpl();
    609         handler.setReadOnlyDS(utility.getDataSource());
    610
    611         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    612         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    613
    614         handler.updateOrder(userCredentialsMock, null);
    615     }
    616
    617     @Test(expected = IllegalArgumentException.class)
    618     public void testInvalidOrderUpdateOrder()
    619             throws Exception
    620     {
    621         OrderDAO handler = new OrderDAOImpl();
    622         handler.setReadWriteDS(utility.getDataSource());
    623         handler.setReadOnlyDS(utility.getDataSource());
    624
    625         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    626         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    627         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    628
    629         Order mockOrder = buildMockOrder();
    630         mockOrder.setId(1299);
    631
    632         handler.updateOrder(userCredentialsMock, mockOrder);
    633     }
    634
    635     private Order buildMockOrder()
    636     {
    637         Order mockOrder = new Order();
    638         mockOrder.setId(21);
    639         mockOrder.setOrderDate(LocalDate.now());
    640         mockOrder.setCustomerId(7);
    641         mockOrder.setTotalAmount(20.50);
    642         mockOrder.setOrderNumber("0292929");
    643         List<OrderItem> mockOrderItems = buildOrderItems();
    644         mockOrder.setOrderItems(mockOrderItems);
    645
    646         return mockOrder;
    647     }
    648
    649
    650     private List<OrderItem> buildOrderItems()
    651     {
    652         List<OrderItem> data = new ArrayList<OrderItem>();
    653         for (int counter = 1; counter < 3; counter++)
    654         {
    655             OrderItem mockOrderItem = new OrderItem();
    656             mockOrderItem.setId(counter);
    657             mockOrderItem.setOrderId(21);
    658             mockOrderItem.setProductId(12);
    659             mockOrderItem.setUnitPrice(10.25);
    660             mockOrderItem.setQuantity(2);
    661             data.add(mockOrderItem);
    662         }
    663
    664         return data;
    665     }
    666 }