View Javadoc

    1   package org.example.customerdao;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.fail;
    5   import static org.mockito.Mockito.when;
    6
    7   import java.io.File;
    8   import java.time.LocalDate;
    9   import java.util.ArrayList;
    10  import java.util.List;
    11
    12  import org.apache.logging.log4j.Level;
    13  import org.apache.logging.log4j.LogManager;
    14  import org.apache.logging.log4j.core.config.Configurator;
    15  import org.dbunit.Assertion;
    16  import org.dbunit.dataset.ITable;
    17  import org.example.customer.Order;
    18  import org.example.customer.OrderItem;
    19  import org.example.customerdao.testcategories.SmokeTest;
    20  import org.example.dao.testUtils.jdbc.DBUnitJDBCUtility;
    21  import org.example.websecurity.UserCredentials;
    22  import org.junit.After;
    23  import org.junit.Before;
    24  import org.junit.Test;
    25  import org.junit.experimental.categories.Category;
    26  import org.mockito.Mockito;
    27
    28  public class OrderDAOSlowTests
    29  {
    30      private static String configDir = "src" + File.separator
    31              + "test" + File.separator + "resources" + File.separator + "data";
    32
    33      private static final String SCHEMA_FILE = configDir + File.separator + "Customer.sql";
    34      private static final String DATA_FILE = configDir + File.separator + "fullDB.xml";
    35      private static final String Order_ADD_FILE = configDir + File.separator + "addOrder.xml";
    36      private static final String Order_UPDATE_FILE = configDir + File.separator + "updateOrder.xml";
    37      private static final String Order_DELETE_FILE = configDir + File.separator + "deleteOrder.xml";
    38
    39      private static DBUnitJDBCUtility utility = null;
    40
    41      @Before  // (re)build the Database before each test
    42      public 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      @After
    57      public void shutdown()
    58      {
    59          if (utility != null)
    60          {
    61              utility.shutdown();
    62          }
    63      }
    64
    65      @Category(SmokeTest.class)
    66      @Test(timeout = 3000)
    67      public void testAddOrder()
    68              throws Exception
    69      {
    70          OrderDAO handler = new OrderDAOImpl();
    71          handler.setReadWriteDS(utility.getDataSource());
    72
    73          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    74          when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    75
    76          Order mockOrder = buildMockOrder();
    77
    78          int expected = 831;
    79          int actual = handler.addOrder(userCredentialsMock, mockOrder);
    80          assertEquals(expected, actual);
    81
    82          try
    83          {
    84              // Fetch database data after executing your code
    85              ITable actualTable = utility.getTableFromDatabase("Orders");
    86              ITable expectedTable = utility.getTableFromFile(Order_ADD_FILE, "Orders");
    87
    88              // Assert actual database table match expected table
    89              // This will check every row, and every column of the table
    90              Assertion.assertEquals(expectedTable, actualTable);
    91          }
    92          catch (Exception e1)
    93          {
    94              fail(e1.getLocalizedMessage());
    95          }
    96      }
    97
    98      @Test(timeout = 1000)
    99      public void testUpdateOrder()
    100             throws Exception
    101     {
    102         OrderDAO handler = new OrderDAOImpl();
    103         handler.setReadWriteDS(utility.getDataSource());
    104         handler.setReadOnlyDS(utility.getDataSource());
    105
    106         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    107         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    108         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    109
    110         Order mockOrder140 = handler.findOrderById(userCredentialsMock, 140);
    111         // Original data
    112         // <ORDERS ID="140" ORDER_DATE="2012-12-18" CUSTOMER_ID="70" TOTAL_AMOUNT="1058.40" ORDER_NUMBER="542517"/>
    113         // <ORDER_ITEM ID="368" ORDER_ID="140" PRODUCT_ID="24" UNIT_PRICE="3.60" QUANTITY="15"/>
    114         // <ORDER_ITEM ID="369" ORDER_ID="140" PRODUCT_ID="28" UNIT_PRICE="36.40" QUANTITY="6"/>
    115         // <ORDER_ITEM ID="370" ORDER_ID="140" PRODUCT_ID="59" UNIT_PRICE="44.00" QUANTITY="12"/>
    116         // <ORDER_ITEM ID="371" ORDER_ID="140" PRODUCT_ID="71" UNIT_PRICE="17.20" QUANTITY="15"/>
    117
    118         mockOrder140.setTotalAmount(1097.40);
    119
    120         // Modified data
    121         // <ORDERS ID="140" ORDER_DATE="2012-12-18" CUSTOMER_ID="70" TOTAL_AMOUNT="1097.40" ORDER_NUMBER="542517"/>
    122         // <ORDER_ITEM ID="2156" ORDER_ID="140" PRODUCT_ID="24" UNIT_PRICE="3.60" QUANTITY="15"/> 54.00
    123         // <ORDER_ITEM ID="2157" ORDER_ID="140" PRODUCT_ID="28" UNIT_PRICE="36.40" QUANTITY="6"/> 218.40
    124         // <ORDER_ITEM ID="2158" ORDER_ID="140" PRODUCT_ID="59" UNIT_PRICE="44.00" QUANTITY="12"/> 528.00
    125         // <ORDER_ITEM ID="2159" ORDER_ID="140" PRODUCT_ID="71" UNIT_PRICE="17.20" QUANTITY="15"/> 258.00
    126         // <ORDER_ITEM ID="2160" ORDER_ID="140" PRODUCT_ID="17" UNIT_PRICE="39.00" QUANTITY="1"/> 258.00
    127
    128         List<OrderItem> mockOrder140items = mockOrder140.getOrderItems();
    129         OrderItem mockOrderItem = new OrderItem();
    130         mockOrderItem.setOrderId(140);
    131         mockOrderItem.setProductId(17);
    132         mockOrderItem.setUnitPrice(39.00);
    133         mockOrderItem.setQuantity(1);
    134         mockOrder140items.add(mockOrderItem);
    135
    136         handler.updateOrder(userCredentialsMock, mockOrder140);
    137
    138         try
    139         {
    140             // Fetch database data after executing your code
    141             ITable actualTable = utility.getTableFromDatabase("Orders");
    142             ITable expectedTable = utility.getTableFromFile(Order_UPDATE_FILE, "Orders");
    143
    144             // Assert actual database table match expected table
    145             // This will check every row, and every column of the table
    146             Assertion.assertEquals(expectedTable, actualTable);
    147         }
    148         catch (Exception e1)
    149         {
    150             fail(e1.getLocalizedMessage());
    151         }
    152     }
    153
    154     @Test(timeout = 3000)
    155     public void testDeleteOrder()
    156             throws Exception
    157     {
    158         OrderDAO handler = new OrderDAOImpl();
    159         handler.setReadWriteDS(utility.getDataSource());
    160         handler.setReadOnlyDS(utility.getDataSource());
    161
    162         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    163         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    164
    165         Order mockOrder = buildMockOrder();
    166         mockOrder.setId(117);
    167
    168         handler.deleteEntity(userCredentialsMock, mockOrder);
    169
    170         try
    171         {
    172             // Fetch database data after executing your code
    173             ITable actualTable = utility.getTableFromDatabase("Orders");
    174             ITable expectedTable = utility.getTableFromFile(Order_DELETE_FILE, "Orders");
    175
    176             // Assert actual database table match expected table
    177             // This will check every row, and every column of the table
    178             Assertion.assertEquals(expectedTable, actualTable);
    179         }
    180         catch (Exception e1)
    181         {
    182             fail(e1.getLocalizedMessage());
    183         }
    184     }
    185
    186     private Order buildMockOrder()
    187     {
    188         // <ORDERS ID="831" ORDER_DATE="2020-02-17" CUSTOMER_ID="12" TOTAL_AMOUNT="266.00" ORDER_NUMBER="632901"/>
    189
    190         Order mockOrder = new Order();
    191         // leave id blank the DB will generate it
    192         mockOrder.setOrderDate(LocalDate.of(2020, 2, 17));
    193         mockOrder.setCustomerId(12);
    194         mockOrder.setTotalAmount(266.00);
    195         mockOrder.setOrderNumber("632901");
    196         List<OrderItem> mockOrderItems = buildOrderItems();
    197         mockOrder.setOrderItems(mockOrderItems);
    198
    199         return mockOrder;
    200     }
    201
    202
    203     private List<OrderItem> buildOrderItems()
    204     {
    205      //   <ORDER_ITEM ID="2156" ORDER_ID="831" PRODUCT_ID="3" UNIT_PRICE="10.00" QUANTITY="2"/>
    206      //   <ORDER_ITEM ID="2157" ORDER_ID="831" PRODUCT_ID="52" UNIT_PRICE="7.00" QUANTITY="12"/>
    207      //   <ORDER_ITEM ID="2158" ORDER_ID="831" PRODUCT_ID="762" UNIT_PRICE="18.00" QUANTITY="9"/>
    208
    209         List<OrderItem> data = new ArrayList<OrderItem>();
    210
    211         OrderItem mockOrderItem1 = new OrderItem();
    212         mockOrderItem1.setOrderId(831);
    213         mockOrderItem1.setProductId(3);
    214         mockOrderItem1.setUnitPrice(10.00);
    215         mockOrderItem1.setQuantity(2);
    216         data.add(mockOrderItem1);
    217
    218         OrderItem mockOrderItem2 = new OrderItem();
    219         mockOrderItem2.setOrderId(831);
    220         mockOrderItem2.setProductId(52);
    221         mockOrderItem2.setUnitPrice(7.00);
    222         mockOrderItem2.setQuantity(12);
    223         data.add(mockOrderItem2);
    224
    225         OrderItem mockOrderItem3 = new OrderItem();
    226         mockOrderItem3.setOrderId(831);
    227         mockOrderItem3.setProductId(762);
    228         mockOrderItem3.setUnitPrice(18.00);
    229         mockOrderItem3.setQuantity(9);
    230         data.add(mockOrderItem3);
    231
    232         return data;
    233     }
    234 }