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.Product;
    18  import org.example.customer.Supplier;
    19  import org.example.customer.utility.CustomerEntity;
    20  import org.example.customerdao.testcategories.SmokeTest;
    21  import org.example.customerdao.utility.NonDeleteableRecordException;
    22  import org.example.dao.testUtils.jdbc.DBUnitJDBCUtility;
    23  import org.example.websecurity.UserCredentials;
    24  import org.junit.AfterClass;
    25  import org.junit.BeforeClass;
    26  import org.junit.Test;
    27  import org.junit.experimental.categories.Category;
    28  import org.mockito.Mockito;
    29
    30  public class ProductDAOFastTests
    31  {
    32      private static String configDir = "src" + File.separator
    33              + "test" + File.separator + "resources" + File.separator + "data";
    34
    35      private static final String SCHEMA_FILE = configDir + File.separator + "Customer.sql";
    36      private static final String DATA_FILE = configDir + File.separator + "fullDB.xml";
    37
    38      private static DBUnitJDBCUtility utility = null;
    39
    40      @BeforeClass // this runs only once before any test
    41      public static void setup()
    42      {
    43          Configurator.setLevel(LogManager.getLogger(Product.class).getName(), Level.WARN);
    44
    45          try
    46          {
    47              utility = new DBUnitJDBCUtility(SCHEMA_FILE, DATA_FILE);
    48          }
    49          catch (Exception e)
    50          {
    51              fail(e.getLocalizedMessage());
    52          }
    53      }
    54
    55      @AfterClass
    56      public static void shutdown()
    57      {
    58          if (utility != null)
    59          {
    60              utility.shutdown();
    61          }
    62      }
    63
    64      @Test(expected = IllegalArgumentException.class)
    65      public void testNullCredentialsFindAllProducts()
    66              throws Exception
    67      {
    68          ProductDAO handler = new ProductDAOImpl();
    69          handler.setReadOnlyDS(utility.getDataSource());
    70
    71          handler.findAllProducts(null);
    72      }
    73
    74      @Test(expected = IllegalArgumentException.class)
    75      public void testUnauthorizedFindAllProducts()
    76              throws Exception
    77      {
    78          ProductDAO handler = new ProductDAOImpl();
    79          handler.setReadOnlyDS(utility.getDataSource());
    80
    81          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    82          when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    83
    84          handler.findAllProducts(userCredentialsMock);
    85      }
    86
    87      @Category(SmokeTest.class)
    88      @Test
    89      public void testFindAllProducts()
    90              throws Exception
    91      {
    92          ProductDAO handler = new ProductDAOImpl();
    93          handler.setReadOnlyDS(utility.getDataSource());
    94
    95          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    96          when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    97
    98          List<Product> records = handler.findAllProducts(userCredentialsMock);
    99          assertNotNull(records);
    100         assertTrue(records.size() == 80);
    101
    102     }
    103
    104     @Test(expected = IllegalArgumentException.class)
    105     public void testNullCredentialsFindAllActiveProducts()
    106             throws Exception
    107     {
    108         ProductDAO handler = new ProductDAOImpl();
    109         handler.setReadOnlyDS(utility.getDataSource());
    110
    111         handler.findAllActiveProducts(null);
    112     }
    113
    114     @Test(expected = IllegalArgumentException.class)
    115     public void testUnauthorizedFindAllActiveProducts()
    116             throws Exception
    117     {
    118         ProductDAO handler = new ProductDAOImpl();
    119         handler.setReadOnlyDS(utility.getDataSource());
    120
    121         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    122         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    123
    124         handler.findAllActiveProducts(userCredentialsMock);
    125     }
    126
    127     @Category(SmokeTest.class)
    128     @Test
    129     public void testFindAllActiveProducts()
    130             throws Exception
    131     {
    132         ProductDAO handler = new ProductDAOImpl();
    133         handler.setReadOnlyDS(utility.getDataSource());
    134
    135         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    136         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    137
    138         List<Product> records = handler.findAllActiveProducts(userCredentialsMock);
    139         assertNotNull(records);
    140         assertTrue(records.size() == 71);
    141
    142     }
    143
    144     @Test(expected = IllegalArgumentException.class)
    145     public void testNullCredentialsFindProductById()
    146             throws Exception
    147     {
    148         ProductDAO handler = new ProductDAOImpl();
    149         handler.setReadOnlyDS(utility.getDataSource());
    150
    151         int id = 1;
    152
    153         handler.findProductById(null, id);
    154     }
    155
    156     @Test(expected = IllegalArgumentException.class)
    157     public void testUnauthorizedFindProductById()
    158             throws Exception
    159     {
    160         ProductDAO handler = new ProductDAOImpl();
    161         handler.setReadOnlyDS(utility.getDataSource());
    162
    163         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    164         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    165
    166         int id = 1;
    167
    168         handler.findProductById(userCredentialsMock, id);
    169     }
    170
    171     @Category(SmokeTest.class)
    172     @Test
    173     public void testFindProductByValidId()
    174             throws Exception
    175     {
    176         ProductDAO handler = new ProductDAOImpl();
    177         handler.setReadOnlyDS(utility.getDataSource());
    178
    179         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    180         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    181         int id = 1;
    182
    183         Product record = handler.findProductById(userCredentialsMock, id);
    184         assertNotNull(record);
    185         String expected = "Chai";
    186         String actual = record.getProductName();
    187         assertEquals(expected, actual);
    188     }
    189
    190     @Test
    191     public void testFindProductByInvalidId()
    192             throws Exception
    193     {
    194         ProductDAO handler = new ProductDAOImpl();
    195         handler.setReadOnlyDS(utility.getDataSource());
    196
    197         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    198         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    199         int id = 101;
    200
    201         Product record = handler.findProductById(userCredentialsMock, id);
    202         assertNull(record);
    203     }
    204
    205     @Test(expected = IllegalArgumentException.class)
    206     public void testNullCredentialsFindProductsBySupplier()
    207             throws Exception
    208     {
    209         ProductDAO handler = new ProductDAOImpl();
    210         handler.setReadOnlyDS(utility.getDataSource());
    211
    212         Supplier supplier = new Supplier();
    213         supplier.setId(10);
    214
    215         handler.findProductsBySupplier(null, supplier);
    216     }
    217
    218     @Test(expected = IllegalArgumentException.class)
    219     public void testUnauthorizedFindProductsBySupplier()
    220             throws Exception
    221     {
    222         ProductDAO handler = new ProductDAOImpl();
    223         handler.setReadOnlyDS(utility.getDataSource());
    224
    225         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    226         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    227
    228         Supplier supplier = new Supplier();
    229         supplier.setId(10);
    230
    231         handler.findProductsBySupplier(userCredentialsMock, supplier);
    232     }
    233
    234     @Test
    235     public void testFindProductsBySupplier()
    236             throws Exception
    237     {
    238         ProductDAO handler = new ProductDAOImpl();
    239         handler.setReadOnlyDS(utility.getDataSource());
    240
    241         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    242         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    243
    244         Supplier supplier = new Supplier();
    245         supplier.setId(10);
    246
    247         List<Product> records = handler.findProductsBySupplier(userCredentialsMock, supplier);
    248         assertNotNull(records);
    249         assertTrue(records.size() == 1);
    250         assertTrue(records.get(0).getId() == 24);
    251     }
    252
    253     @Test
    254     public void testFindNoProductsBySupplier()
    255             throws Exception
    256     {
    257         ProductDAO handler = new ProductDAOImpl();
    258         handler.setReadOnlyDS(utility.getDataSource());
    259
    260         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    261         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    262
    263         Supplier supplier = new Supplier();
    264         supplier.setId(83);
    265
    266         List<Product> records = handler.findProductsBySupplier(userCredentialsMock, supplier);
    267         assertNotNull(records);
    268         assertTrue(records.size() == 0);
    269     }
    270
    271     @Test(expected = IllegalArgumentException.class)
    272     public void testNullCredentialsAddProduct()
    273             throws Exception
    274     {
    275         ProductDAO handler = new ProductDAOImpl();
    276         handler.setReadWriteDS(utility.getDataSource());
    277
    278         Product mockProduct = buildMockProduct();
    279
    280         handler.addProduct(null, mockProduct);
    281     }
    282
    283     @Test(expected = IllegalArgumentException.class)
    284     public void testUnauthorizedAddProduct()
    285             throws Exception
    286     {
    287         ProductDAO handler = new ProductDAOImpl();
    288         handler.setReadWriteDS(utility.getDataSource());
    289
    290         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    291         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    292
    293         Product mockProduct = buildMockProduct();
    294
    295         handler.addProduct(userCredentialsMock, mockProduct);
    296     }
    297
    298     @Test(expected = IllegalArgumentException.class)
    299     public void testNullAddProduct()
    300             throws Exception
    301     {
    302         ProductDAO handler = new ProductDAOImpl();
    303         handler.setReadWriteDS(utility.getDataSource());
    304
    305         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    306         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    307
    308         handler.addProduct(userCredentialsMock, null);
    309     }
    310
    311     @Test(expected = IllegalArgumentException.class)
    312     public void testNullProductUpdateProduct()
    313             throws Exception
    314     {
    315         ProductDAO handler = new ProductDAOImpl();
    316         handler.setReadWriteDS(utility.getDataSource());
    317
    318         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    319         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    320
    321        handler.updateProduct(userCredentialsMock, null);
    322     }
    323
    324     @Test(expected = IllegalArgumentException.class)
    325     public void testNullCredentialsUpdateProduct()
    326             throws Exception
    327     {
    328         ProductDAO handler = new ProductDAOImpl();
    329         handler.setReadWriteDS(utility.getDataSource());
    330
    331         Product mockProduct = buildMockProduct();
    332
    333         handler.updateProduct(null, mockProduct);
    334     }
    335
    336     @Test(expected = IllegalArgumentException.class)
    337     public void testUnauthorizedUpdateProduct()
    338             throws Exception
    339     {
    340         ProductDAO handler = new ProductDAOImpl();
    341         handler.setReadWriteDS(utility.getDataSource());
    342
    343         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    344         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    345
    346         Product mockProduct = buildMockProduct();
    347
    348         handler.updateProduct(userCredentialsMock, mockProduct);
    349     }
    350
    351     @Test(expected = IllegalArgumentException.class)
    352     public void testNullUpdateProduct()
    353             throws Exception
    354     {
    355         ProductDAO handler = new ProductDAOImpl();
    356         handler.setReadWriteDS(utility.getDataSource());
    357
    358         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    359         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    360
    361         handler.updateProduct(userCredentialsMock, null);
    362     }
    363
    364     @Test(expected = IllegalArgumentException.class)
    365     public void testNullCredentialsIsDeleteableProduct()
    366             throws Exception
    367     {
    368         ProductDAO handler = new ProductDAOImpl();
    369         handler.setReadOnlyDS(utility.getDataSource());
    370
    371         Product mockProduct = buildMockProduct();
    372
    373         handler.isDeleteable(null, mockProduct);
    374     }
    375
    376     @Test(expected = IllegalArgumentException.class)
    377     public void testUnauthorizedIsDeleteableProduct()
    378             throws Exception
    379     {
    380         ProductDAO handler = new ProductDAOImpl();
    381         handler.setReadOnlyDS(utility.getDataSource());
    382
    383         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    384         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    385
    386         Product mockProduct = buildMockProduct();
    387
    388         handler.isDeleteable(userCredentialsMock, mockProduct);
    389     }
    390
    391     @Test(expected = IllegalArgumentException.class)
    392     public void testNullIsDeleteableProduct()
    393             throws Exception
    394     {
    395         ProductDAO handler = new ProductDAOImpl();
    396         handler.setReadOnlyDS(utility.getDataSource());
    397
    398         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    399         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    400
    401         handler.isDeleteable(userCredentialsMock, null);
    402     }
    403
    404     @Test
    405     public void testIsDeleteableProduct()
    406             throws Exception
    407     {
    408         ProductDAO handler = new ProductDAOImpl();
    409         handler.setReadOnlyDS(utility.getDataSource());
    410
    411         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    412         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    413
    414         Product mockProduct = buildMockProduct();
    415         // Product 17 has no Orders
    416         mockProduct.setId(17);
    417
    418         assertTrue(handler.isDeleteable(userCredentialsMock, mockProduct));
    419     }
    420
    421     @Test
    422     public void testIsNotDeleteableProduct()
    423             throws Exception
    424     {
    425         ProductDAO handler = new ProductDAOImpl();
    426         handler.setReadOnlyDS(utility.getDataSource());
    427
    428         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    429         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    430
    431         Product mockProduct = buildMockProduct();
    432         // Product 12 has Orders
    433         mockProduct.setId(12);
    434
    435         assertFalse(handler.isDeleteable(userCredentialsMock, mockProduct));
    436     }
    437
    438     @Test(expected = IllegalArgumentException.class)
    439     public void testNullCredentialsIsDeleteableId()
    440             throws Exception
    441     {
    442         ProductDAO handler = new ProductDAOImpl();
    443         handler.setReadOnlyDS(utility.getDataSource());
    444
    445         handler.isDeleteable(null, 21);
    446     }
    447
    448     @Test(expected = IllegalArgumentException.class)
    449     public void testUnauthorizedIsDeleteableId()
    450             throws Exception
    451     {
    452         ProductDAO handler = new ProductDAOImpl();
    453         handler.setReadOnlyDS(utility.getDataSource());
    454
    455         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    456         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
    457
    458         handler.isDeleteable(userCredentialsMock, 21);
    459     }
    460
    461     @Test(expected = IllegalArgumentException.class)
    462     public void testNullIsDeleteableId()
    463             throws Exception
    464     {
    465         ProductDAO handler = new ProductDAOImpl();
    466         handler.setReadOnlyDS(utility.getDataSource());
    467
    468         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    469         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    470
    471         handler.isDeleteable(userCredentialsMock, null);
    472     }
    473
    474     @Test(expected = IllegalArgumentException.class)
    475     public void testIsDeleteableId()
    476             throws Exception
    477     {
    478         ProductDAO handler = new ProductDAOImpl();
    479         handler.setReadOnlyDS(utility.getDataSource());
    480
    481         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    482         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    483
    484         // Product 17 has no Orders
    485         assertTrue(handler.isDeleteable(userCredentialsMock, 17));
    486     }
    487
    488     @Test(expected = IllegalArgumentException.class)
    489     public void testIsNotDeleteableId()
    490             throws Exception
    491     {
    492         ProductDAO handler = new ProductDAOImpl();
    493         handler.setReadOnlyDS(utility.getDataSource());
    494
    495         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    496         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    497
    498         assertFalse(handler.isDeleteable(userCredentialsMock, 14));
    499     }
    500
    501     @Test(expected = IllegalArgumentException.class)
    502     public void testNullCredentialsDeleteProduct()
    503             throws Exception
    504     {
    505         ProductDAO handler = new ProductDAOImpl();
    506         handler.setReadOnlyDS(utility.getDataSource());
    507
    508         Product mockProduct = buildMockProduct();
    509
    510         handler.deleteEntity(null, mockProduct);
    511     }
    512
    513     @Test(expected = IllegalArgumentException.class)
    514     public void testUnauthorizedDeleteProduct()
    515             throws Exception
    516     {
    517         ProductDAO handler = new ProductDAOImpl();
    518         handler.setReadOnlyDS(utility.getDataSource());
    519
    520         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    521         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
    522
    523         Product mockProduct = buildMockProduct();
    524
    525         handler.deleteEntity(userCredentialsMock, mockProduct);
    526     }
    527
    528     @Test(expected = IllegalArgumentException.class)
    529     public void testNullDeleteProduct()
    530             throws Exception
    531     {
    532         ProductDAO handler = new ProductDAOImpl();
    533         handler.setReadOnlyDS(utility.getDataSource());
    534
    535         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    536         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    537
    538         handler.deleteEntity(userCredentialsMock, null);
    539     }
    540
    541     @Test(expected = NonDeleteableRecordException.class)
    542     public void testNotDeleteableProduct()
    543             throws Exception
    544     {
    545         ProductDAO handler = new ProductDAOImpl();
    546         handler.setReadWriteDS(utility.getDataSource());
    547         handler.setReadOnlyDS(utility.getDataSource());
    548
    549         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    550         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
    551         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    552
    553         Product mockProduct = buildMockProduct();
    554         // Product 12 has Orders
    555         mockProduct.setId(12);
    556
    557         handler.deleteEntity(userCredentialsMock, mockProduct);
    558     }
    559
    560     @Test(expected = IllegalArgumentException.class)
    561     public void testInvalidEntityDeleteableProduct()
    562             throws Exception
    563     {
    564         ProductDAO handler = new ProductDAOImpl();
    565         handler.setReadOnlyDS(utility.getDataSource());
    566
    567         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    568         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    569
    570         CustomerEntity mockCustomerEntity = new CustomerEntity();
    571
    572         handler.deleteEntity(userCredentialsMock, mockCustomerEntity);
    573     }
    574
    575     @Test(expected = IllegalArgumentException.class)
    576     public void testInvalidEntityIsDeleteableCustomer()
    577             throws Exception
    578     {
    579         ProductDAO handler = new ProductDAOImpl();
    580         handler.setReadOnlyDS(utility.getDataSource());
    581
    582         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
    583         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
    584
    585         CustomerEntity mockCustomerEntity = new CustomerEntity();
    586
    587         assertTrue(handler.isDeleteable(userCredentialsMock, mockCustomerEntity));
    588     }
    589
    590     private Product buildMockProduct()
    591     {
    592         Product mockProduct = new Product();
    593         // leave the id as default, the database will generate it
    594         mockProduct.setProductName("Salted Peanuts");
    595         mockProduct.setSupplierId(7);
    596         mockProduct.setUnitPrice(0.50);
    597         mockProduct.setPackaging("1 kg pkg");
    598         mockProduct.setDiscontinued(false);
    599
    600         return mockProduct;
    601     }
    602 }