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.Supplier;
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 SupplierDAOFastTests
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(Supplier.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 testNullCredentialsFindAllSuppliers() 
67              throws Exception
68      {
69          SupplierDAO handler = new SupplierDAOImpl();
70          handler.setReadOnlyDS(utility.getDataSource());
71             
72          handler.findAllSuppliers(null);
73      }
74      
75      @Test(expected = IllegalArgumentException.class)  
76      public void testUnauthorizedFindAllSuppliers() 
77              throws Exception
78      {
79          SupplierDAO handler = new SupplierDAOImpl();
80          handler.setReadOnlyDS(utility.getDataSource());
81          
82          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
83          when(userCredentialsMock.hasRole("worker")).thenReturn(false);
84      
85          handler.findAllSuppliers(userCredentialsMock);
86      }
87      
88      @Category(SmokeTest.class)
89      @Test
90      public void testFindAllSuppliers() 
91              throws Exception
92      {
93          SupplierDAO handler = new SupplierDAOImpl();
94          handler.setReadOnlyDS(utility.getDataSource());
95          
96          UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
97          when(userCredentialsMock.hasRole("worker")).thenReturn(true);
98      
99          List<Supplier> records = handler.findAllSuppliers(userCredentialsMock);
100         assertNotNull(records);
101         assertTrue(records.size() == 29);
102 
103     }
104     
105     @Test(expected = IllegalArgumentException.class)  
106     public void testNullCredentialsFindSupplierById() 
107             throws Exception
108     {
109         SupplierDAO handler = new SupplierDAOImpl();
110         handler.setReadOnlyDS(utility.getDataSource());
111                
112         int id = 1;
113     
114         handler.findSupplierById(null, id);
115     }
116     
117     @Test(expected = IllegalArgumentException.class)  
118     public void testUnauthorizedFindSupplierById() 
119             throws Exception
120     {
121         SupplierDAO handler = new SupplierDAOImpl();
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.findSupplierById(userCredentialsMock, id);
130     }
131     
132     @Category(SmokeTest.class)
133     @Test
134     public void testFindSupplierByValidId() 
135             throws Exception
136     {
137         SupplierDAO handler = new SupplierDAOImpl();
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         Supplier record = handler.findSupplierById(userCredentialsMock, id);
145         assertNotNull(record);
146         String expected = "Exotic Liquids";
147         String actual = record.getCompanyName();
148         assertEquals(expected, actual);
149     }
150     
151     @Test
152     public void testFindSupplierByInvalidId() 
153             throws Exception
154     {
155         SupplierDAO handler = new SupplierDAOImpl();
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         Supplier record = handler.findSupplierById(userCredentialsMock, id);
163         assertNull(record);
164     }
165     
166     @Test(expected = IllegalArgumentException.class)  
167     public void testNullCredentialsFindSuppliersByName() 
168             throws Exception
169     {
170         SupplierDAO handler = new SupplierDAOImpl();
171         handler.setReadOnlyDS(utility.getDataSource());
172       
173         String company ="John";
174     
175         handler.findSuppliersByCompanyName(null, company);
176     }
177     
178     @Test(expected = IllegalArgumentException.class)  
179     public void testUnauthorizedFindSuppliersByName() 
180             throws Exception
181     {
182         SupplierDAO handler = new SupplierDAOImpl();
183         handler.setReadOnlyDS(utility.getDataSource());
184         
185         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
186         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
187     
188         String company ="John";
189         
190         handler.findSuppliersByCompanyName(userCredentialsMock, company);
191     }
192     
193     @Test
194     public void testFindSuppliersByName() 
195             throws Exception
196     {
197         SupplierDAO handler = new SupplierDAOImpl();
198         handler.setReadOnlyDS(utility.getDataSource());
199         
200         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
201         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
202         
203         String company ="Trad";
204     
205         List<Supplier> records = handler.findSuppliersByCompanyName(userCredentialsMock,company);
206         assertNotNull(records);
207         assertTrue(records.size() == 2);
208         assertTrue(records.get(0).getId() == 4);
209     }
210     
211     @Test
212     public void testFindNoSuppliersByName() 
213             throws Exception
214     {
215         SupplierDAO handler = new SupplierDAOImpl();
216         handler.setReadOnlyDS(utility.getDataSource());
217         
218         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
219         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
220         
221         String company ="John";
222     
223         List<Supplier> records = handler.findSuppliersByCompanyName(userCredentialsMock, company);
224         assertNotNull(records);
225         assertTrue(records.size() == 0);
226     }
227     
228     @Test(expected = IllegalArgumentException.class)  
229     public void testNullCredentialsAddSupplier() 
230             throws Exception
231     {
232         SupplierDAO handler = new SupplierDAOImpl();
233         handler.setReadWriteDS(utility.getDataSource());
234         
235         Supplier mockSupplier = buildMockSupplier();
236         
237         handler.addSupplier(null, mockSupplier);
238     }
239     
240     @Test(expected = IllegalArgumentException.class)  
241     public void testUnauthorizedAddSupplier() 
242             throws Exception
243     {
244         SupplierDAO handler = new SupplierDAOImpl();
245         handler.setReadWriteDS(utility.getDataSource());
246         
247         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
248         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
249         
250         Supplier mockSupplier = buildMockSupplier();
251         
252         handler.addSupplier(userCredentialsMock, mockSupplier);
253     }
254     
255     @Test(expected = IllegalArgumentException.class)  
256     public void testNullAddSupplier() 
257             throws Exception
258     {
259         SupplierDAO handler = new SupplierDAOImpl();
260         handler.setReadWriteDS(utility.getDataSource());
261         
262         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
263         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
264              
265         handler.addSupplier(userCredentialsMock, null);
266     }
267     
268     @Test(expected = IllegalArgumentException.class)  
269     public void testNullSupplierUpdateSupplier() 
270             throws Exception
271     {
272         SupplierDAO handler = new SupplierDAOImpl();
273         handler.setReadWriteDS(utility.getDataSource());
274         
275         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
276         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
277        
278        handler.updateSupplier(userCredentialsMock, null);
279     }
280     
281     @Test(expected = IllegalArgumentException.class)  
282     public void testNullCredentialsUpdateSupplier() 
283             throws Exception
284     {
285         SupplierDAO handler = new SupplierDAOImpl();
286         handler.setReadWriteDS(utility.getDataSource());
287           
288         Supplier mockSupplier = buildMockSupplier();
289         
290         handler.updateSupplier(null, mockSupplier);
291     }
292     
293     @Test(expected = IllegalArgumentException.class)  
294     public void testUnauthorizedUpdateSupplier() 
295             throws Exception
296     {
297         SupplierDAO handler = new SupplierDAOImpl();
298         handler.setReadWriteDS(utility.getDataSource());
299         
300         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
301         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
302         
303         Supplier mockSupplier = buildMockSupplier();
304         
305         handler.updateSupplier(userCredentialsMock, mockSupplier);
306     }
307     
308     @Test(expected = IllegalArgumentException.class)  
309     public void testNullUpdateSupplier() 
310             throws Exception
311     {
312         SupplierDAO handler = new SupplierDAOImpl();
313         handler.setReadWriteDS(utility.getDataSource());
314         
315         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
316         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
317              
318         handler.updateSupplier(userCredentialsMock, null);
319     }
320     
321     @Test(expected = IllegalArgumentException.class)  
322     public void testNullCredentialsIsDeleteableSupplier() 
323             throws Exception
324     {
325         SupplierDAO handler = new SupplierDAOImpl();
326         handler.setReadOnlyDS(utility.getDataSource());
327                 
328         Supplier mockSupplier = buildMockSupplier();
329         
330         handler.isDeleteable(null, mockSupplier);
331     }
332     
333     @Test(expected = IllegalArgumentException.class)  
334     public void testUnauthorizedIsDeleteableSupplier() 
335             throws Exception
336     {
337         SupplierDAO handler = new SupplierDAOImpl();
338         handler.setReadOnlyDS(utility.getDataSource());
339         
340         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
341         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
342         
343         Supplier mockSupplier = buildMockSupplier();
344         
345         handler.isDeleteable(userCredentialsMock, mockSupplier);
346     }
347     
348     @Test(expected = IllegalArgumentException.class)  
349     public void testNullIsDeleteableSupplier() 
350             throws Exception
351     {
352         SupplierDAO handler = new SupplierDAOImpl();
353         handler.setReadOnlyDS(utility.getDataSource());
354         
355         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
356         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
357              
358         handler.isDeleteable(userCredentialsMock, null);
359     }
360     
361     @Test
362     public void testIsDeleteableSupplier() 
363             throws Exception
364     {
365         SupplierDAO handler = new SupplierDAOImpl();
366         handler.setReadOnlyDS(utility.getDataSource());
367         
368         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
369         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
370              
371         Supplier mockSupplier = buildMockSupplier();
372         // Supplier 14 has no Products
373         mockSupplier.setId(14);
374          
375         assertTrue(handler.isDeleteable(userCredentialsMock, mockSupplier));
376     }
377     
378     @Test
379     public void testIsNotDeleteableSupplier() 
380             throws Exception
381     {
382         SupplierDAO handler = new SupplierDAOImpl();
383         handler.setReadOnlyDS(utility.getDataSource());
384         
385         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
386         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
387              
388         Supplier mockSupplier = buildMockSupplier();
389         // Supplier 12 has Products
390         mockSupplier.setId(12);
391         
392         assertFalse(handler.isDeleteable(userCredentialsMock, mockSupplier));
393     }
394     
395     @Test(expected = IllegalArgumentException.class)  
396     public void testNullCredentialsIsDeleteableId() 
397             throws Exception
398     {
399         SupplierDAO handler = new SupplierDAOImpl();
400         handler.setReadOnlyDS(utility.getDataSource());
401                
402         handler.isDeleteable(null, 21);
403     }
404     
405     @Test(expected = IllegalArgumentException.class)  
406     public void testUnauthorizedIsDeleteableId() 
407             throws Exception
408     {
409         SupplierDAO handler = new SupplierDAOImpl();
410         handler.setReadOnlyDS(utility.getDataSource());
411         
412         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
413         when(userCredentialsMock.hasRole("worker")).thenReturn(false);
414               
415         handler.isDeleteable(userCredentialsMock, 21);
416     }
417     
418     @Test(expected = IllegalArgumentException.class)  
419     public void testNullIsDeleteableId() 
420             throws Exception
421     {
422         SupplierDAO handler = new SupplierDAOImpl();
423         handler.setReadOnlyDS(utility.getDataSource());
424         
425         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
426         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
427              
428         handler.isDeleteable(userCredentialsMock, null);
429     }
430     
431     @Test(expected = IllegalArgumentException.class)  
432     public void testIsDeleteableId() 
433             throws Exception
434     {
435         SupplierDAO handler = new SupplierDAOImpl();
436         handler.setReadOnlyDS(utility.getDataSource());
437         
438         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
439         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
440              
441         // Supplier 12 has no Products
442         assertTrue(handler.isDeleteable(userCredentialsMock, 12));
443     }
444     
445     @Test(expected = IllegalArgumentException.class)  
446     public void testIsNotDeleteableId() 
447             throws Exception
448     {
449         SupplierDAO handler = new SupplierDAOImpl();
450         handler.setReadOnlyDS(utility.getDataSource());
451         
452         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
453         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
454                   
455         assertFalse(handler.isDeleteable(userCredentialsMock, 14));
456     }
457     
458     @Test(expected = IllegalArgumentException.class)  
459     public void testNullCredentialsDeleteSupplier() 
460             throws Exception
461     {
462         SupplierDAO handler = new SupplierDAOImpl();
463         handler.setReadOnlyDS(utility.getDataSource());
464                
465         Supplier mockSupplier = buildMockSupplier();
466         
467         handler.deleteEntity(null, mockSupplier);
468     }
469     
470     @Test(expected = IllegalArgumentException.class)  
471     public void testUnauthorizedDeleteSupplier() 
472             throws Exception
473     {
474         SupplierDAO handler = new SupplierDAOImpl();
475         handler.setReadOnlyDS(utility.getDataSource());
476         
477         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
478         when(userCredentialsMock.hasRole("manager")).thenReturn(false);
479         
480         Supplier mockSupplier = buildMockSupplier();
481         
482         handler.deleteEntity(userCredentialsMock, mockSupplier);
483     }
484     
485     @Test(expected = IllegalArgumentException.class)  
486     public void testNullDeleteSupplier() 
487             throws Exception
488     {
489         SupplierDAO handler = new SupplierDAOImpl();
490         handler.setReadOnlyDS(utility.getDataSource());
491         
492         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
493         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
494              
495         handler.deleteEntity(userCredentialsMock, null);
496     }
497     
498     @Test(expected = NonDeleteableRecordException.class)  
499     public void testNotDeleteableSupplier() 
500             throws Exception
501     {
502         SupplierDAO handler = new SupplierDAOImpl();
503         handler.setReadWriteDS(utility.getDataSource());
504         handler.setReadOnlyDS(utility.getDataSource());
505         
506         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
507         when(userCredentialsMock.hasRole("worker")).thenReturn(true);
508         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
509              
510         Supplier mockSupplier = buildMockSupplier();
511         // Supplier 12 has Products
512         mockSupplier.setId(12);
513         
514         handler.deleteEntity(userCredentialsMock, mockSupplier);
515     }
516     
517     @Test(expected = IllegalArgumentException.class)  
518     public void testInvalidEntityDeleteableProduct() 
519             throws Exception
520     {
521         SupplierDAO handler = new SupplierDAOImpl();
522         handler.setReadOnlyDS(utility.getDataSource());
523         
524         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
525         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
526              
527         CustomerEntity mockCustomerEntity = new CustomerEntity();
528          
529         handler.deleteEntity(userCredentialsMock, mockCustomerEntity);
530     }
531     
532     @Test(expected = IllegalArgumentException.class)  
533     public void testInvalidEntityIsDeleteableCustomer() 
534             throws Exception
535     {
536         SupplierDAO handler = new SupplierDAOImpl();
537         handler.setReadOnlyDS(utility.getDataSource());
538         
539         UserCredentials userCredentialsMock = Mockito.mock(UserCredentials.class);
540         when(userCredentialsMock.hasRole("manager")).thenReturn(true);
541              
542         CustomerEntity mockCustomerEntity = new CustomerEntity();
543          
544         assertTrue(handler.isDeleteable(userCredentialsMock, mockCustomerEntity));
545     }
546    
547     private Supplier buildMockSupplier()
548     {
549         Supplier mockSupplier = new Supplier();
550         // leave the id as default, the database will generate it
551         mockSupplier.setCompanyName("ABC Inc.");
552         mockSupplier.setContactName("John Smith");
553         Location mockLocation = new Location();
554         mockLocation.setCity("Dawson City");
555         mockLocation.setCountry("Canada");
556         mockSupplier.setLocation(mockLocation);
557         Phone mockPhone = new Phone();
558         mockPhone.setNumber("(867)993-5566");
559         mockSupplier.setPhone(mockPhone);
560         
561         return mockSupplier;
562     }
563 }