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.Supplier;
    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 SupplierDAOSlowTests
    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 SUPPLIER_ADD_FILE = configDir + File.separator + "addSupplier.xml";
    34      private static final String SUPPLIER_UPDATE_FILE = configDir + File.separator + "updateSupplier.xml";
    35      private static final String SUPPLIER_DELETE_FILE = configDir + File.separator + "deleteSupplier.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(Supplier.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 testAddSupplier()
    66              throws Exception
    67      {
    68          SupplierDAO handler = new SupplierDAOImpl();
    69          handler.setReadWriteDS(utility.getDataSource());
    70
    71          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    72          when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    73
    74          Supplier mockSupplier = buildMockSupplier();
    75
    76          int expected = 30;
    77          int actual = handler.addSupplier(userCredentialsMock, mockSupplier);
    78          assertEquals(expected, actual);
    79
    80          try
    81          {
    82              // Fetch database data after executing your code
    83              ITable actualTable = utility.getTableFromDatabase("SUPPLIER");
    84              ITable expectedTable = utility.getTableFromFile(SUPPLIER_ADD_FILE, "SUPPLIER");
    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 = 3000)
    97      public void testUpdateSupplier()
    98              throws Exception
    99      {
    100         SupplierDAO handler = new SupplierDAOImpl();
    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         Supplier mockSupplier3 = handler.findSupplierById(userCredentialsMock, 3);
    109         mockSupplier3.getLocation().setCity("Detroit");
    110
    111         handler.updateSupplier(userCredentialsMock, mockSupplier3);
    112
    113         Supplier mockSupplier14 = handler.findSupplierById(userCredentialsMock, 14);
    114         mockSupplier14.getFax().setNumber("(0544) 60604");
    115
    116         handler.updateSupplier(userCredentialsMock, mockSupplier14);
    117
    118         Supplier mockSupplier25 = handler.findSupplierById(userCredentialsMock, 25);
    119         mockSupplier25.setCompanyName("Ma Maison - Montréal");
    120
    121         handler.updateSupplier(userCredentialsMock, mockSupplier25);
    122
    123         try
    124         {
    125             // Fetch database data after executing your code
    126             ITable actualTable = utility.getTableFromDatabase("SUPPLIER");
    127             ITable expectedTable = utility.getTableFromFile(SUPPLIER_UPDATE_FILE, "SUPPLIER");
    128
    129             // Assert actual database table match expected table
    130             // This will check every row, and every column of the table
    131             Assertion.assertEquals(expectedTable, actualTable);
    132         }
    133         catch (Exception e1)
    134         {
    135             fail(e1.getLocalizedMessage());
    136         }
    137     }
    138
    139     @Test(timeout = 1000) // 1 sec (1000) millisec timeout
    140     public void testDeleteSupplier()
    141             throws Exception
    142     {
    143         SupplierDAO handler = new SupplierDAOImpl();
    144         handler.setReadWriteDS(utility.getDataSource());
    145         handler.setReadOnlyDS(utility.getDataSource());
    146
    147         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    148         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    149         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    150
    151         Supplier mockSupplier = buildMockSupplier();
    152         // Supplier 14 has no Products
    153         mockSupplier.setId(14);
    154
    155         handler.deleteEntity(userCredentialsMock, mockSupplier);
    156
    157         try
    158         {
    159             // Fetch database data after executing your code
    160             ITable actualTable = utility.getTableFromDatabase("SUPPLIER");
    161             ITable expectedTable = utility.getTableFromFile(SUPPLIER_DELETE_FILE, "SUPPLIER");
    162
    163             // Assert actual database table match expected table
    164             // This will check every row, and every column of the table
    165             Assertion.assertEquals(expectedTable, actualTable);
    166         }
    167         catch (Exception e1)
    168         {
    169             fail(e1.getLocalizedMessage());
    170         }
    171     }
    172
    173     private Supplier buildMockSupplier()
    174     {
    175         Supplier mockSupplier = new Supplier();
    176         // leave the id as default, the database will generate it
    177         mockSupplier.setCompanyName("Service Unlimited");
    178         mockSupplier.setContactName("George Robers Jr.");
    179         Location mockLocation = new Location();
    180         mockLocation.setCity("Memphis");
    181         mockLocation.setCountry("USA");
    182         mockSupplier.setLocation(mockLocation);
    183         Phone mockPhone = new Phone();
    184         mockPhone.setNumber("(389) 555-1212");
    185         mockSupplier.setPhone(mockPhone);
    186
    187         return mockSupplier;
    188     }
    189 }