View Javadoc
1   package org.example.customer;
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.mockito.ArgumentMatchers.anyString;
9   import static org.mockito.Mockito.when;
10  
11  import org.apache.commons.lang3.RandomStringUtils;
12  import org.apache.logging.log4j.Level;
13  import org.apache.logging.log4j.LogManager;
14  import org.apache.logging.log4j.core.config.Configurator;
15  import org.example.customer.utility.CustomerEntity;
16  import org.example.websecurity.XssSanitizer;
17  import org.junit.Before;
18  import org.junit.BeforeClass;
19  import org.junit.Test;
20  import org.mockito.Mockito;
21  
22  public class ProductTest
23  {
24      private Product testProduct = null;
25      private XssSanitizer sanitizerMock = null;
26  
27      @BeforeClass // this runs only once before any test
28      public static void setup()
29      {
30          Configurator.setLevel(LogManager.getLogger(Product.class).getName(), Level.WARN);
31      }
32  
33      @Before
34      public void setUp() throws Exception
35      {
36          sanitizerMock = Mockito.mock(XssSanitizer.class);
37          // tell the Mock Sanitizer to return the first argument it was passed
38          // as a trimmed String
39          when(sanitizerMock.sanitizeInput(anyString())).thenAnswer(i -> ((String) i.getArguments()[0]).trim());
40  
41          testProduct = new Product(sanitizerMock);
42      }
43  
44      @Test
45      public void testCustomerDefaultConstructor()
46      {
47          Product defaultProduct = new Product();
48          assertNotNull(defaultProduct);
49      }
50  
51      @Test
52      public void testDefaultProductName()
53      {
54          String expected = "Paper Envelope";
55          String actual = testProduct.getProductName();
56          assertEquals(expected, actual);
57      }
58  
59      @Test(expected = IllegalArgumentException.class)
60      public void testNullProductName()
61      {
62          testProduct.setProductName(null);
63      }
64  
65      @Test(expected = IllegalArgumentException.class)
66      public void testEmptyProductName()
67      {
68          testProduct.setProductName("  ");
69      }
70  
71      @Test(expected = IllegalArgumentException.class)
72      public void testTooLongProductName()
73      {
74          String bad = RandomStringUtils.randomAlphabetic(51);
75          testProduct.setProductName(bad);
76      }
77  
78      @Test
79      public void testValidProductName()
80      {
81          String[] names = {"Copy Paper", "Flour", "Sugar", "10in Frying Pan"};
82          for (String name : names)
83          {
84              testProduct.setProductName(name);
85          }
86      }
87  
88      @Test
89      public void testSanitizedProductName()
90      {
91          String xssInput = "<script>alert('You have been Hacked!');</script>Frozen Strawberries";
92          String xssSanitized = "Frozen Strawberries";
93          // tell the Mock Sanitizer to return a specific string
94          when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
95  
96          testProduct.setProductName(xssInput);
97  
98          // now we will test to see if the Product class calls the sanitizer correctly
99          String expected = "Frozen Strawberries";
100         String actual = testProduct.getProductName();
101         assertEquals(expected, actual);
102     }
103 
104     @Test
105     public void testDefaultSupplierId()
106     {
107         int expected = 1;
108         int actual = testProduct.getSupplierId();
109         assertEquals(expected, actual);
110     }
111 
112     @Test
113     public void testValidSupplierId()
114     {
115         int[] good = { 2, 34, 999, 22, 1, 565656 };
116         for (int entry : good) {
117             testProduct.setSupplierId(entry);
118         }
119     }
120 
121     @Test(expected = IllegalArgumentException.class)
122     public void testInValidSupplierId()
123     {
124         testProduct.setSupplierId(0);
125     }
126 
127     @Test
128     public void testDefaultUnitPrice()
129     {
130         double expected = 0.01;
131         double actual = testProduct.getUnitPrice();
132         double precision = 0.001;
133         assertEquals(expected, actual, precision);
134     }
135 
136     @Test
137     public void testValidUnitPrice()
138     {
139         double[] good = {2.0, 0.34, 999.50, 22.25, 1.0};
140         for (double entry : good) {
141             testProduct.setUnitPrice(entry);
142         }
143     }
144 
145     @Test(expected = IllegalArgumentException.class)
146     public void testInValidUnitPrice()
147     {
148         testProduct.setUnitPrice(0.0);
149     }
150 
151     @Test
152     public void testDefaultPackaging()
153     {
154         String expected = "single";
155         String actual = testProduct.getPackaging();
156         assertEquals(expected, actual);
157     }
158 
159     @Test
160     public void testNullPackaging()
161     {
162         testProduct.setPackaging(null);
163         assertNull(testProduct.getPackaging());
164     }
165 
166     @Test(expected = IllegalArgumentException.class)
167     public void testWhitespacePackaging()
168     {
169         testProduct.setPackaging("  ");
170     }
171     
172     @Test(expected = IllegalArgumentException.class)
173     public void testEmptyPackaging()
174     {
175         testProduct.setPackaging("");
176     }
177 
178     @Test(expected = IllegalArgumentException.class)
179     public void testTooLongPackaging()
180     {
181         String bad = RandomStringUtils.randomAlphabetic(31);
182         testProduct.setPackaging(bad);
183     }
184 
185     @Test(expected = IllegalArgumentException.class)
186     public void testTooShortPackaging()
187     {
188         String bad = RandomStringUtils.randomAlphabetic(1);
189         testProduct.setPackaging(bad);
190     }
191 
192     @Test
193     public void testValidPackaging()
194     {
195         String[] values = {"ream", "dozen", "5 count"};
196         for (String value : values)
197         {
198             testProduct.setPackaging(value);
199         }
200     }
201 
202     @Test
203     public void testSanitizedPackaging()
204     {
205         String xssInput = "<script>alert('You have been Hacked!');</script>Case ";
206         String xssSanitized = "Case";
207         // tell the Mock Sanitizer to return a specific string
208         when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
209 
210         testProduct.setPackaging(xssInput);
211 
212         // now we will test to see if the Product class calls the sanitizer correctly
213         String expected = "Case";
214         String actual = testProduct.getPackaging();
215         assertEquals(expected, actual);
216     }
217 
218     @Test
219     public void testDefaultDiscontinued()
220     {
221         assertFalse(testProduct.isDiscontinued());
222     }
223 
224     @Test
225     public void testSettingDiscontinued()
226     {
227         testProduct.setDiscontinued(true);
228         assertTrue(testProduct.isDiscontinued());
229     }
230 
231     @Test
232     public void testToString()
233     {
234         String expected = "Product [Id=1, "
235                 + "ProductName=Paper Envelope, "
236                 + "SupplierId=1, UnitPrice=0.01, "
237                 + "Packaging=single, Discontinued=false]";
238         String actual = testProduct.toString();
239         assertEquals(expected, actual);
240     }
241 
242     @Test public void testHashCode()
243     {
244         Product sample = new Product();
245         int expected = sample.hashCode();
246         int actual = testProduct.hashCode();
247         assertEquals(expected, actual);
248     }
249 
250     @Test public void testEquals()
251     {
252         Product sample = new Product();
253         assertTrue(testProduct.equals(sample));
254     }
255 
256     @Test public void testNotEquals()
257     {
258         Product sample = new Product();
259         sample.setSupplierId(2);
260         assertFalse(testProduct.equals(sample));
261 
262         CustomerEntity junk = new Customer();
263         assertFalse(testProduct.equals(junk));
264     }
265 
266 }