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
    9   import org.apache.logging.log4j.Level;
    10  import org.apache.logging.log4j.LogManager;
    11  import org.apache.logging.log4j.core.config.Configurator;
    12  import org.dbunit.Assertion;
    13  import org.dbunit.dataset.ITable;
    14  import org.example.customer.Product;
    15  import org.example.customerdao.testcategories.SmokeTest;
    16  import org.example.dao.testUtils.jdbc.DBUnitJDBCUtility;
    17  import org.example.websecurity.UserCredentials;
    18  import org.junit.After;
    19  import org.junit.Before;
    20  import org.junit.Test;
    21  import org.junit.experimental.categories.Category;
    22  import org.mockito.Mockito;
    23
    24  public class ProductDAOSlowTests
    25  {
    26      private static String configDir = "src" + File.separator
    27              + "test" + File.separator + "resources" + File.separator + "data";
    28
    29      private static final String SCHEMA_FILE = configDir + File.separator + "Customer.sql";
    30      private static final String DATA_FILE = configDir + File.separator + "fullDB.xml";
    31      private static final String Product_ADD_FILE = configDir + File.separator + "addProduct.xml";
    32      private static final String Product_UPDATE_FILE = configDir + File.separator + "updateProduct.xml";
    33      private static final String Product_DELETE_FILE = configDir + File.separator + "deleteProduct.xml";
    34
    35      private static DBUnitJDBCUtility utility = null;
    36
    37      @Before  // (re)build the Database before each test
    38      public void setup()
    39      {
    40          Configurator.setLevel(LogManager.getLogger(Product.class).getName(), Level.WARN);
    41
    42          try
    43          {
    44              utility = new DBUnitJDBCUtility(SCHEMA_FILE, DATA_FILE);
    45          }
    46          catch (Exception e)
    47          {
    48              fail(e.getLocalizedMessage());
    49          }
    50      }
    51
    52      @After
    53      public void shutdown()
    54      {
    55          if (utility != null)
    56          {
    57              utility.shutdown();
    58          }
    59      }
    60
    61      @Category(SmokeTest.class)
    62      @Test(timeout = 3000) // 3 sec (3000) millisec timeout
    63      public void testAddProduct()
    64              throws Exception
    65      {
    66          ProductDAO handler = new ProductDAOImpl();
    67          handler.setReadWriteDS(utility.getDataSource());
    68
    69          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    70          when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    71
    72          Product mockProduct = buildMockProduct();
    73
    74          int expected = 81;
    75          int actual = handler.addProduct(userCredentialsMock, mockProduct);
    76          assertEquals(expected, actual);
    77
    78          try
    79          {
    80              // Fetch database data after executing your code
    81              ITable actualTable = utility.getTableFromDatabase("Product");
    82              ITable expectedTable = utility.getTableFromFile(Product_ADD_FILE, "Product");
    83
    84              // Assert actual database table match expected table
    85              // This will check every row, and every column of the table
    86              Assertion.assertEquals(expectedTable, actualTable);
    87          }
    88          catch (Exception e1)
    89          {
    90              fail(e1.getLocalizedMessage());
    91          }
    92      }
    93
    94      @Test(timeout = 1000)
    95      public void testUpdateProduct()
    96              throws Exception
    97      {
    98          ProductDAO handler = new ProductDAOImpl();
    99          handler.setReadWriteDS(utility.getDataSource());
    100         handler.setReadOnlyDS(utility.getDataSource());
    101
    102         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    103         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    104         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    105
    106         Product mockProduct14 = handler.findProductById(userCredentialsMock, 14);
    107         mockProduct14.setProductName("Firm Tofu");
    108
    109         handler.updateProduct(userCredentialsMock, mockProduct14);
    110
    111         Product mockProduct55 = handler.findProductById(userCredentialsMock, 55);
    112         mockProduct55.setDiscontinued(true);
    113
    114         handler.updateProduct(userCredentialsMock, mockProduct55);
    115
    116         Product mockProduct71 = handler.findProductById(userCredentialsMock, 71);
    117         mockProduct71.setUnitPrice(23.75);
    118
    119         handler.updateProduct(userCredentialsMock, mockProduct71);
    120
    121         try
    122         {
    123             // Fetch database data after executing your code
    124             ITable actualTable = utility.getTableFromDatabase("Product");
    125             ITable expectedTable = utility.getTableFromFile(Product_UPDATE_FILE, "Product");
    126
    127             // Assert actual database table match expected table
    128             // This will check every row, and every column of the table
    129             Assertion.assertEquals(expectedTable, actualTable);
    130         }
    131         catch (Exception e1)
    132         {
    133             fail(e1.getLocalizedMessage());
    134         }
    135     }
    136
    137     @Test(timeout = 1000) // 1 sec (1000) millisec timeout
    138     public void testDeleteProduct()
    139             throws Exception
    140     {
    141         ProductDAO handler = new ProductDAOImpl();
    142         handler.setReadWriteDS(utility.getDataSource());
    143         handler.setReadOnlyDS(utility.getDataSource());
    144
    145         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    146         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    147         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    148
    149         Product mockProduct = buildMockProduct();
    150         // Product 17 has no orders
    151         mockProduct.setId(17);
    152
    153         handler.deleteEntity(userCredentialsMock, mockProduct);
    154
    155         try
    156         {
    157             // Fetch database data after executing your code
    158             ITable actualTable = utility.getTableFromDatabase("Product");
    159             ITable expectedTable = utility.getTableFromFile(Product_DELETE_FILE, "Product");
    160
    161             // Assert actual database table match expected table
    162             // This will check every row, and every column of the table
    163             Assertion.assertEquals(expectedTable, actualTable);
    164         }
    165         catch (Exception e1)
    166         {
    167             fail(e1.getLocalizedMessage());
    168         }
    169     }
    170
    171     private Product buildMockProduct()
    172     {
    173         Product mockProduct = new Product();
    174         // leave the id as default, the database will generate it
    175         mockProduct.setProductName("Salted Peanuts");
    176         mockProduct.setSupplierId(7);
    177         mockProduct.setUnitPrice(0.50);
    178         mockProduct.setPackaging("1 kg pkg");
    179         mockProduct.setDiscontinued(false);
    180
    181         return mockProduct;
    182     }
    183 }