View Javadoc

    1   package org.example.customerdao;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.assertFalse;
    5   import static org.junit.Assert.assertNotNull;
    6   import static org.junit.Assert.assertNull;
    7   import static org.junit.Assert.assertTrue;
    8   import static org.junit.Assert.fail;
    9   import static org.mockito.Mockito.when;
    10
    11  import java.io.File;
    12  import java.util.List;
    13
    14  import org.apache.logging.log4j.Level;
    15  import org.apache.logging.log4j.LogManager;
    16  import org.apache.logging.log4j.core.config.Configurator;
    17  import org.example.customer.Customer;
    18  import org.example.customer.utility.CustomerEntity;
    19  import org.example.customer.utility.Location;
    20  import org.example.customer.utility.Phone;
    21  import org.example.customerdao.testcategories.SmokeTest;
    22  import org.example.customerdao.utility.NonDeleteableRecordException;
    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 CustomerDAOFastTests
    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(Customer.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 testNullCredentialsFindAllCustomers()
    67              throws Exception
    68      {
    69          CustomerDAO handler = new CustomerDAOImpl();
    70          handler.setReadOnlyDS(utility.getDataSource());
    71
    72          handler.findAllCustomers(null);
    73      }
    74
    75      @Test(expected = IllegalArgumentException.class)
    76      public void testUnauthorizedFindAllCustomers()
    77              throws Exception
    78      {
    79          CustomerDAO handler = new CustomerDAOImpl();
    80          handler.setReadOnlyDS(utility.getDataSource());
    81
    82          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    83          when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    84
    85          handler.findAllCustomers(userCredentialsMock);
    86      }
    87
    88      @Category(SmokeTest.class)
    89      @Test
    90      public void testFindAllCustomers()
    91              throws Exception
    92      {
    93          CustomerDAO handler = new CustomerDAOImpl();
    94          handler.setReadOnlyDS(utility.getDataSource());
    95
    96          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    97          when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    98
    99          List<Customer> records = handler.findAllCustomers(userCredentialsMock);
    100         assertNotNull(records);
    101         assertTrue(records.size() == 91);
    102
    103     }
    104
    105     @Test(expected = IllegalArgumentException.class)
    106     public void testNullCredentialsFindCustomerById()
    107             throws Exception
    108     {
    109         CustomerDAO handler = new CustomerDAOImpl();
    110         handler.setReadOnlyDS(utility.getDataSource());
    111
    112         int id = 1;
    113
    114         handler.findCustomerById(null, id);
    115     }
    116
    117     @Test(expected = IllegalArgumentException.class)
    118     public void testUnauthorizedFindCustomerById()
    119             throws Exception
    120     {
    121         CustomerDAO handler = new CustomerDAOImpl();
    122         handler.setReadOnlyDS(utility.getDataSource());
    123
    124         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    125         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    126
    127         int id = 1;
    128
    129         handler.findCustomerById(userCredentialsMock, id);
    130     }
    131
    132     @Category(SmokeTest.class)
    133     @Test
    134     public void testFindCustomerById()
    135             throws Exception
    136     {
    137         CustomerDAO handler = new CustomerDAOImpl();
    138         handler.setReadOnlyDS(utility.getDataSource());
    139
    140         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    141         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    142         int id = 1;
    143
    144         Customer record = handler.findCustomerById(userCredentialsMock, id);
    145         assertNotNull(record);
    146         String expected = "Anders";
    147         String actual = record.getLastName();
    148         assertEquals(expected, actual);
    149     }
    150
    151     @Test
    152     public void testFindCustomerByInvalidId()
    153             throws Exception
    154     {
    155         CustomerDAO handler = new CustomerDAOImpl();
    156         handler.setReadOnlyDS(utility.getDataSource());
    157
    158         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    159         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    160         int id = 101;
    161
    162         Customer record = handler.findCustomerById(userCredentialsMock, id);
    163         assertNull(record);
    164     }
    165
    166     @Test(expected = IllegalArgumentException.class)
    167     public void testNullCredentialsFindCustomersByName()
    168             throws Exception
    169     {
    170         CustomerDAO handler = new CustomerDAOImpl();
    171         handler.setReadOnlyDS(utility.getDataSource());
    172
    173         String firstName ="John";
    174         String lastName = "Steel";
    175
    176         handler.findCustomersByName(null, firstName, lastName);
    177     }
    178
    179     @Test(expected = IllegalArgumentException.class)
    180     public void testUnauthorizedFindCustomersByName()
    181             throws Exception
    182     {
    183         CustomerDAO handler = new CustomerDAOImpl();
    184         handler.setReadOnlyDS(utility.getDataSource());
    185
    186         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    187         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    188
    189         String firstName ="John";
    190         String lastName = "Steel";
    191
    192         handler.findCustomersByName(userCredentialsMock, firstName, lastName);
    193     }
    194
    195     @Test
    196     public void testFindCustomersByName()
    197             throws Exception
    198     {
    199         CustomerDAO handler = new CustomerDAOImpl();
    200         handler.setReadOnlyDS(utility.getDataSource());
    201
    202         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    203         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    204
    205         String firstName ="John";
    206         String lastName = "Steel";
    207
    208         List<Customer> records = handler.findCustomersByName(userCredentialsMock, firstName, lastName);
    209         assertNotNull(records);
    210         assertTrue(records.size() == 1);
    211         assertTrue(records.get(0).getId() == 43);
    212     }
    213
    214     @Test
    215     public void testFindNoCustomersByName()
    216             throws Exception
    217     {
    218         CustomerDAO handler = new CustomerDAOImpl();
    219         handler.setReadOnlyDS(utility.getDataSource());
    220
    221         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    222         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    223
    224         String firstName ="Fred";
    225         String lastName = "Flintstone";
    226
    227         List<Customer> records = handler.findCustomersByName(userCredentialsMock, firstName, lastName);
    228         assertNotNull(records);
    229         assertTrue(records.size() == 0);
    230     }
    231
    232     @Test(expected = IllegalArgumentException.class)
    233     public void testNullCredentialsAddCustomer()
    234             throws Exception
    235     {
    236         CustomerDAO handler = new CustomerDAOImpl();
    237         handler.setReadWriteDS(utility.getDataSource());
    238
    239           Customer mockCustomer = buildMockCustomer();
    240
    241         handler.addCustomer(null, mockCustomer);
    242     }
    243
    244     @Test(expected = IllegalArgumentException.class)
    245     public void testUnauthorizedAddCustomer()
    246             throws Exception
    247     {
    248         CustomerDAO handler = new CustomerDAOImpl();
    249         handler.setReadWriteDS(utility.getDataSource());
    250
    251         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    252         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    253
    254         Customer mockCustomer = buildMockCustomer();
    255
    256         handler.addCustomer(userCredentialsMock, mockCustomer);
    257     }
    258
    259     @Test(expected = IllegalArgumentException.class)
    260     public void testNullAddCustomer()
    261             throws Exception
    262     {
    263         CustomerDAO handler = new CustomerDAOImpl();
    264         handler.setReadWriteDS(utility.getDataSource());
    265
    266         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    267         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    268
    269         handler.addCustomer(userCredentialsMock, null);
    270     }
    271
    272     @Test(expected = IllegalArgumentException.class)
    273     public void testNullCredentialsUpdateCustomer()
    274             throws Exception
    275     {
    276         CustomerDAO handler = new CustomerDAOImpl();
    277         handler.setReadWriteDS(utility.getDataSource());
    278
    279         Customer mockCustomer = buildMockCustomer();
    280
    281         handler.updateCustomer(null, mockCustomer);
    282     }
    283
    284     @Test(expected = IllegalArgumentException.class)
    285     public void testUnauthorizedUpdateCustomer()
    286             throws Exception
    287     {
    288         CustomerDAO handler = new CustomerDAOImpl();
    289         handler.setReadWriteDS(utility.getDataSource());
    290
    291         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    292         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    293
    294         Customer mockCustomer = buildMockCustomer();
    295
    296         handler.updateCustomer(userCredentialsMock, mockCustomer);
    297     }
    298
    299     @Test(expected = IllegalArgumentException.class)
    300     public void testNullUpdateCustomer()
    301             throws Exception
    302     {
    303         CustomerDAO handler = new CustomerDAOImpl();
    304         handler.setReadWriteDS(utility.getDataSource());
    305
    306         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    307         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    308
    309         handler.updateCustomer(userCredentialsMock, null);
    310     }
    311
    312     @Test(expected = IllegalArgumentException.class)
    313     public void testNullCredentialsIsDeleteableCustomer()
    314             throws Exception
    315     {
    316         CustomerDAO handler = new CustomerDAOImpl();
    317         handler.setReadOnlyDS(utility.getDataSource());
    318
    319         Customer mockCustomer = buildMockCustomer();
    320
    321         handler.isDeleteable(null, mockCustomer);
    322     }
    323
    324     @Test(expected = IllegalArgumentException.class)
    325     public void testUnauthorizedIsDeleteableCustomer()
    326             throws Exception
    327     {
    328         CustomerDAO handler = new CustomerDAOImpl();
    329         handler.setReadOnlyDS(utility.getDataSource());
    330
    331         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    332         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    333
    334         Customer mockCustomer = buildMockCustomer();
    335
    336         handler.isDeleteable(userCredentialsMock, mockCustomer);
    337     }
    338
    339     @Test(expected = IllegalArgumentException.class)
    340     public void testNullIsDeleteableCustomer()
    341             throws Exception
    342     {
    343         CustomerDAO handler = new CustomerDAOImpl();
    344         handler.setReadOnlyDS(utility.getDataSource());
    345
    346         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    347         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    348
    349         handler.isDeleteable(userCredentialsMock, null);
    350     }
    351
    352     @Test
    353     public void testIsDeleteableCustomer()
    354             throws Exception
    355     {
    356         CustomerDAO handler = new CustomerDAOImpl();
    357         handler.setReadOnlyDS(utility.getDataSource());
    358
    359         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    360         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    361
    362         Customer mockCustomer = buildMockCustomer();
    363         // Customer 71 has no Orders
    364         mockCustomer.setId(71);
    365
    366         assertTrue(handler.isDeleteable(userCredentialsMock, mockCustomer));
    367     }
    368
    369     @Test(expected = IllegalArgumentException.class)
    370     public void testInvalidEntityDeleteableCustomer()
    371             throws Exception
    372     {
    373         CustomerDAO handler = new CustomerDAOImpl();
    374         handler.setReadOnlyDS(utility.getDataSource());
    375
    376         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    377         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    378
    379         CustomerEntity mockCustomerEntity = new CustomerEntity();
    380
    381         handler.deleteEntity(userCredentialsMock, mockCustomerEntity);
    382     }
    383
    384     @Test(expected = IllegalArgumentException.class)
    385     public void testInvalidEntityIsDeleteableCustomer()
    386             throws Exception
    387     {
    388         CustomerDAO handler = new CustomerDAOImpl();
    389         handler.setReadOnlyDS(utility.getDataSource());
    390
    391         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    392         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    393
    394         CustomerEntity mockCustomerEntity = new CustomerEntity();
    395
    396         assertTrue(handler.isDeleteable(userCredentialsMock, mockCustomerEntity));
    397     }
    398
    399     @Test
    400     public void testIsNotDeleteableCustomer()
    401             throws Exception
    402     {
    403         CustomerDAO handler = new CustomerDAOImpl();
    404         handler.setReadOnlyDS(utility.getDataSource());
    405
    406         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    407         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    408
    409         Customer mockCustomer = buildMockCustomer();
    410         // Customer 70 has Orders
    411         mockCustomer.setId(70);
    412
    413         assertFalse(handler.isDeleteable(userCredentialsMock, mockCustomer));
    414     }
    415
    416     @Test(expected = IllegalArgumentException.class)
    417     public void testNullCredentialsIsDeleteableId()
    418             throws Exception
    419     {
    420         CustomerDAO handler = new CustomerDAOImpl();
    421         handler.setReadOnlyDS(utility.getDataSource());
    422
    423         handler.isDeleteable(null, 21);
    424     }
    425
    426     @Test(expected = IllegalArgumentException.class)
    427     public void testUnauthorizedIsDeleteableId()
    428             throws Exception
    429     {
    430         CustomerDAO handler = new CustomerDAOImpl();
    431         handler.setReadOnlyDS(utility.getDataSource());
    432
    433         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    434         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    435
    436         handler.isDeleteable(userCredentialsMock, 21);
    437     }
    438
    439     @Test(expected = IllegalArgumentException.class)
    440     public void testNullIsDeleteableId()
    441             throws Exception
    442     {
    443         CustomerDAO handler = new CustomerDAOImpl();
    444         handler.setReadOnlyDS(utility.getDataSource());
    445
    446         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    447         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    448
    449         handler.isDeleteable(userCredentialsMock, null);
    450     }
    451
    452     @Test(expected = IllegalArgumentException.class)
    453     public void testIsDeleteableId()
    454             throws Exception
    455     {
    456         CustomerDAO handler = new CustomerDAOImpl();
    457         handler.setReadOnlyDS(utility.getDataSource());
    458
    459         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    460         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    461
    462         // Customer 71 has no Orders
    463         assertTrue(handler.isDeleteable(userCredentialsMock, 71));
    464     }
    465
    466     @Test(expected = IllegalArgumentException.class)
    467     public void testIsNotDeleteableId()
    468             throws Exception
    469     {
    470         CustomerDAO handler = new CustomerDAOImpl();
    471         handler.setReadOnlyDS(utility.getDataSource());
    472
    473         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    474         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    475
    476         assertFalse(handler.isDeleteable(userCredentialsMock, 70));
    477     }
    478
    479     @Test(expected = IllegalArgumentException.class)
    480     public void testNullCredentialsDeleteCustomer()
    481             throws Exception
    482     {
    483         CustomerDAO handler = new CustomerDAOImpl();
    484         handler.setReadOnlyDS(utility.getDataSource());
    485
    486         Customer mockCustomer = buildMockCustomer();
    487
    488         handler.deleteEntity(null, mockCustomer);
    489     }
    490
    491     @Test(expected = IllegalArgumentException.class)
    492     public void testUnauthorizedDeleteCustomer()
    493             throws Exception
    494     {
    495         CustomerDAO handler = new CustomerDAOImpl();
    496         handler.setReadOnlyDS(utility.getDataSource());
    497
    498         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    499         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    500
    501         Customer mockCustomer = buildMockCustomer();
    502
    503         handler.deleteEntity(userCredentialsMock, mockCustomer);
    504     }
    505
    506     @Test(expected = IllegalArgumentException.class)
    507     public void testNullDeleteCustomer()
    508             throws Exception
    509     {
    510         CustomerDAO handler = new CustomerDAOImpl();
    511         handler.setReadOnlyDS(utility.getDataSource());
    512
    513         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    514         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    515
    516         handler.deleteEntity(userCredentialsMock, null);
    517     }
    518
    519     @Test(expected = NonDeleteableRecordException.class)
    520     public void testNotDeleteableCustomer()
    521             throws Exception
    522     {
    523         CustomerDAO handler = new CustomerDAOImpl();
    524         handler.setReadWriteDS(utility.getDataSource());
    525         handler.setReadOnlyDS(utility.getDataSource());
    526
    527         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    528         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    529         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    530
    531         Customer mockCustomer = buildMockCustomer();
    532         // Customer 70 has Orders
    533         mockCustomer.setId(70);
    534
    535         handler.deleteEntity(userCredentialsMock, mockCustomer);
    536     }
    537
    538     private Customer buildMockCustomer()
    539     {
    540         Customer mockCustomer = new Customer();
    541         // leave the id as default, the database will generate it
    542         mockCustomer.setFirstName("Robert");
    543         mockCustomer.setLastName("Service");
    544         Location mockLocation = new Location();
    545         mockLocation.setCity("Dawson City");
    546         mockLocation.setCountry("Canada");
    547         mockCustomer.setLocation(mockLocation);
    548         Phone mockPhone = new Phone();
    549         mockPhone.setNumber("(867)993-5566");
    550         mockCustomer.setPhone(mockPhone);
    551
    552         return mockCustomer;
    553     }
    554 }