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.Customer;
    15  import org.example.customer.utility.Location;
    16  import org.example.customer.utility.Phone;
    17  import org.example.customerdao.testcategories.SmokeTest;
    18  import org.example.dao.testUtils.jdbc.DBUnitJDBCUtility;
    19  import org.example.websecurity.UserCredentials;
    20  import org.junit.After;
    21  import org.junit.Before;
    22  import org.junit.Test;
    23  import org.junit.experimental.categories.Category;
    24  import org.mockito.Mockito;
    25
    26  public class CustomerDAOSlowTests
    27  {
    28      private static String configDir = "src" + File.separator
    29              + "test" + File.separator + "resources" + File.separator + "data";
    30
    31      private static final String SCHEMA_FILE = configDir + File.separator + "Customer.sql";
    32      private static final String DATA_FILE = configDir + File.separator + "fullDB.xml";
    33      private static final String CUSTOMER_ADD_FILE = configDir + File.separator + "addCustomer.xml";
    34      private static final String CUSTOMER_UPDATE_FILE = configDir + File.separator + "updateCustomer.xml";
    35      private static final String CUSTOMER_DELETE_FILE = configDir + File.separator + "deleteCustomer.xml";
    36
    37      private static DBUnitJDBCUtility utility = null;
    38
    39      @Before  // (re)build the Database before each test
    40      public void setup()
    41      {
    42          Configurator.setLevel(LogManager.getLogger(Customer.class).getName(), Level.WARN);
    43
    44          try
    45          {
    46              utility = new DBUnitJDBCUtility(SCHEMA_FILE, DATA_FILE);
    47          }
    48          catch (Exception e)
    49          {
    50              fail(e.getLocalizedMessage());
    51          }
    52      }
    53
    54      @After
    55      public void shutdown()
    56      {
    57          if (utility != null)
    58          {
    59              utility.shutdown();
    60          }
    61      }
    62
    63      @Category(SmokeTest.class)
    64      @Test(timeout = 3000) // 3 sec (3000) millisec timeout
    65      public void testAddCustomer()
    66              throws Exception
    67      {
    68          CustomerDAO handler = new CustomerDAOImpl();
    69          handler.setReadWriteDS(utility.getDataSource());
    70
    71          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    72          when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    73
    74          Customer mockCustomer = buildMockCustomer();
    75
    76          int expected = 92;
    77          int actual = handler.addCustomer(userCredentialsMock, mockCustomer);
    78          assertEquals(expected, actual);
    79
    80          try
    81          {
    82              // Fetch database data after executing your code
    83              ITable actualTable = utility.getTableFromDatabase("CUSTOMER");
    84              ITable expectedTable = utility.getTableFromFile(CUSTOMER_ADD_FILE, "CUSTOMER");
    85
    86              // Assert actual database table match expected table
    87              // This will check every row, and every column of the table
    88              Assertion.assertEquals(expectedTable, actualTable);
    89          }
    90          catch (Exception e1)
    91          {
    92              fail(e1.getLocalizedMessage());
    93          }
    94      }
    95
    96      @Test(timeout = 1000)
    97      public void testUpdateCustomer()
    98              throws Exception
    99      {
    100         CustomerDAO handler = new CustomerDAOImpl();
    101         handler.setReadWriteDS(utility.getDataSource());
    102         handler.setReadOnlyDS(utility.getDataSource());
    103
    104         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    105         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    106         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    107
    108         Customer mockCustomer68 = handler.findCustomerById(userCredentialsMock, 68);
    109         mockCustomer68.setFirstName("Penny");
    110         mockCustomer68.setLastName("Miller");
    111
    112         handler.updateCustomer(userCredentialsMock, mockCustomer68);
    113
    114         Customer mockCustomer8 = handler.findCustomerById(userCredentialsMock, 8);
    115         mockCustomer8.getLocation().setCity("Barcelona");
    116
    117         handler.updateCustomer(userCredentialsMock, mockCustomer8);
    118
    119         Customer mockCustomer25 = handler.findCustomerById(userCredentialsMock, 25);
    120         mockCustomer25.setPhone(null);
    121
    122         handler.updateCustomer(userCredentialsMock, mockCustomer25);
    123
    124         try
    125         {
    126             // Fetch database data after executing your code
    127             ITable actualTable = utility.getTableFromDatabase("CUSTOMER");
    128             ITable expectedTable = utility.getTableFromFile(CUSTOMER_UPDATE_FILE, "CUSTOMER");
    129
    130             // Assert actual database table match expected table
    131             // This will check every row, and every column of the table
    132             Assertion.assertEquals(expectedTable, actualTable);
    133         }
    134         catch (Exception e1)
    135         {
    136             fail(e1.getLocalizedMessage());
    137         }
    138     }
    139
    140     @Test(timeout = 1000) // 1 sec (1000) millisec timeout
    141     public void testDeleteCustomer()
    142             throws Exception
    143     {
    144         CustomerDAO handler = new CustomerDAOImpl();
    145         handler.setReadWriteDS(utility.getDataSource());
    146         handler.setReadOnlyDS(utility.getDataSource());
    147
    148         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    149         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    150         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    151
    152         Customer mockCustomer = buildMockCustomer();
    153         // Customer 71 has no Orders
    154         mockCustomer.setId(71);
    155
    156         handler.deleteEntity(userCredentialsMock, mockCustomer);
    157
    158         try
    159         {
    160             // Fetch database data after executing your code
    161             ITable actualTable = utility.getTableFromDatabase("CUSTOMER");
    162             ITable expectedTable = utility.getTableFromFile(CUSTOMER_DELETE_FILE, "CUSTOMER");
    163
    164             // Assert actual database table match expected table
    165             // This will check every row, and every column of the table
    166             Assertion.assertEquals(expectedTable, actualTable);
    167         }
    168         catch (Exception e1)
    169         {
    170             fail(e1.getLocalizedMessage());
    171         }
    172     }
    173
    174     private Customer buildMockCustomer()
    175     {
    176         Customer mockCustomer = new Customer();
    177         // leave the id as default, the database will generate it
    178         mockCustomer.setFirstName("Robert");
    179         mockCustomer.setLastName("Service");
    180         Location mockLocation = new Location();
    181         mockLocation.setCity("Dawson City");
    182         mockLocation.setCountry("Canada");
    183         mockCustomer.setLocation(mockLocation);
    184         Phone mockPhone = new Phone();
    185         mockPhone.setNumber("(867)993-5566");
    186         mockCustomer.setPhone(mockPhone);
    187
    188         return mockCustomer;
    189     }
    190 }