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.assertTrue;
7   import static org.mockito.ArgumentMatchers.anyString;
8   import static org.mockito.Mockito.when;
9   
10  import org.apache.commons.lang3.RandomStringUtils;
11  import org.apache.logging.log4j.Level;
12  import org.apache.logging.log4j.LogManager;
13  import org.apache.logging.log4j.core.config.Configurator;
14  import org.example.customer.utility.Location;
15  import org.example.customer.utility.Phone;
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 CustomerTest 
23  {
24      private Customer testCustomer = 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(Customer.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          testCustomer = new Customer(sanitizerMock);
42      }
43  
44      @Test
45      public void testCustomerDefaultConstructor()
46      {
47          Customer defaultCustomer = new Customer();
48          assertNotNull(defaultCustomer);
49      }
50  
51      @Test
52      public void testDefaultFirstName()
53      {
54          String expected = "John";
55          String actual = testCustomer.getFirstName();
56          assertEquals(expected, actual);
57      }
58  
59      @Test(expected = IllegalArgumentException.class)
60      public void testNullFirstName()
61      {
62          testCustomer.setFirstName(null);
63      }
64  
65      @Test(expected = IllegalArgumentException.class)
66      public void testEmptyFirstName()
67      {
68          testCustomer.setFirstName("  ");
69      }
70  
71      @Test(expected = IllegalArgumentException.class)
72      public void testTooLongFirstName()
73      {
74          String bad = RandomStringUtils.randomAlphabetic(41);
75          testCustomer.setFirstName(bad);
76      }
77      
78      @Test
79      public void testValidFirstName()
80      {
81          String[] names = {"Barney", "Fred", "betty", "WILMA"};
82          for (String name : names)
83          {
84              testCustomer.setFirstName(name);
85          }
86      }
87  
88      @Test
89      public void testSanitizedFirstName()
90      {
91          String xssInput = "<script>alert('You have been Hacked!');</script>Jonathan";
92          String xssSanitized = "Jonathan";
93          // tell the Mock Sanitizer to return a specific string
94          when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
95  
96          testCustomer.setFirstName(xssInput);
97  
98          // now we will test to see if the Customer class calls the sanitizer correctly
99          String expected = "Jonathan";
100         String actual = testCustomer.getFirstName();
101         assertEquals(expected, actual);
102     }
103 
104     @Test
105     public void testDefaultLastName()
106     {
107         String expected = "Smith";
108         String actual = testCustomer.getLastName();
109         assertEquals(expected, actual);
110     }
111 
112     @Test(expected = IllegalArgumentException.class)
113     public void testNullLastName()
114     {
115         testCustomer.setLastName(null);
116     }
117 
118     @Test(expected = IllegalArgumentException.class)
119     public void testEmptyLastName()
120     {
121         testCustomer.setLastName("  ");
122     }
123 
124     @Test(expected = IllegalArgumentException.class)
125     public void testTooLongLastName()
126     {
127         String bad = RandomStringUtils.randomAlphabetic(41);
128         testCustomer.setLastName(bad);
129     }
130     
131     @Test(expected = IllegalArgumentException.class)
132     public void testTooShortLastName()
133     {
134         String bad = RandomStringUtils.randomAlphabetic(1);
135         testCustomer.setLastName(bad);
136     }
137     
138     @Test
139     public void testValidLastName()
140     {
141         String[] names = {"Rubble", "Flintstone"};
142         for (String name : names)
143         {
144             testCustomer.setLastName(name);
145         }
146     }
147 
148     @Test
149     public void testSanitizedLastName()
150     {
151         String xssInput = "<script>alert('You have been Hacked!');</script>Earl";
152         String xssSanitized = "Earl";
153         // tell the Mock Sanitizer to return a specific string
154         when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
155 
156         testCustomer.setLastName(xssInput);
157 
158         // now we will test to see if the Customer class calls the sanitizer correctly
159         String expected = "Earl";
160         String actual = testCustomer.getLastName();
161         assertEquals(expected, actual);
162     }
163     
164     @Test 
165     public void testDefaultLocation()
166     {
167         Location expected = new Location();
168         Location actual = testCustomer.getLocation();
169         assertEquals(expected, actual);
170     }
171 
172     @Test(expected = IllegalArgumentException.class)
173     public void testNullLocation()
174     {
175         testCustomer.setLocation(null);
176     }
177     
178     @Test 
179     public void testValidLocation()
180     {
181         Location home = new Location();
182         home.setCity("Vancouver");
183         home.setCountry("Canada");
184         testCustomer.setLocation(home);
185     }
186     
187     @Test 
188     public void testDefaultPhone()
189     {
190         Phone expected = new Phone();
191         Phone actual = testCustomer.getPhone();
192         assertEquals(expected, actual);
193     }
194     
195     @Test
196     public void testNullPhone()
197     {
198         testCustomer.setPhone(null);
199     }
200 
201     @Test
202     public void testValidPhone()
203     {
204         Phone phone = new Phone();
205         phone.setNumber("001 42-234-1234");
206         testCustomer.setPhone(phone);
207         assertNotNull(testCustomer.getPhone());
208     }
209 
210     @Test  
211     public void testToString()
212     {
213         String expected = "Customer [Id=1, FirstName=John, "
214                 + "LastName=Smith, City=New York City, "
215                 + "Country=United States of America, Phone=null]";
216         String actual = testCustomer.toString();
217         assertEquals(expected, actual);
218     }
219     
220     @Test public void testHashCode()
221     {
222         Customer sample = new Customer();
223         int expected = sample.hashCode();
224         int actual = testCustomer.hashCode();
225         assertEquals(expected, actual);
226     }
227     
228     @Test public void testEquals()
229     {
230         Customer sample = new Customer();
231         assertTrue(testCustomer.equals(sample));
232     }
233     
234     @Test public void testNotEquals()
235     {
236         Customer sample = new Customer();
237         sample.setLastName("Jones");
238         assertFalse(testCustomer.equals(sample));
239         
240         Object junk = new Object();
241         assertFalse(testCustomer.equals(junk));
242     }
243 }