View Javadoc
1   package org.example.customer.utility;
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.websecurity.XssSanitizer;
16  import org.junit.Before;
17  import org.junit.BeforeClass;
18  import org.junit.Test;
19  import org.mockito.Mockito;
20  
21  public class PhoneTest
22  {
23      
24      private Phone testPhone = 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(Phone.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          testPhone = new Phone(sanitizerMock);
42      }
43      
44      @Test
45      public void testPhoneDefaultConstructor()
46      {
47          Phone defaultLocation = new Phone();
48          assertNotNull(defaultLocation);
49      }
50      
51      @Test
52      public void testDefaultNumber()
53      {
54          assertNull(testPhone.getNumber());
55      }
56  
57      @Test
58      public void testNullNumber()
59      {
60          testPhone.setNumber(null);
61          assertNull(testPhone.getNumber());
62      }
63  
64      @Test(expected = IllegalArgumentException.class)
65      public void testEmptyNumber()
66      {
67          testPhone.setNumber("  ");
68      }
69  
70      @Test(expected = IllegalArgumentException.class)
71      public void testTooLongNumber()
72      {
73          String bad = RandomStringUtils.randomAlphabetic(31);
74          testPhone.setNumber(bad);
75      }
76      
77      @Test(expected = IllegalArgumentException.class)
78      public void testTooShortNumber()
79      {
80          String bad = RandomStringUtils.randomAlphabetic(1);
81          testPhone.setNumber(bad);
82      }
83      
84      @Test
85      public void testValidNumber()
86      {
87          String[] values = {"212.555.1212", "(303)555-1212"};
88          for (String value : values)
89          {
90              testPhone.setNumber(value);
91          }
92      }
93      
94      @Test
95      public void testSanitizedNumber()
96      {
97          String xssInput = "<script>alert('You have been Hacked!');</script>202.555.1212 ";
98          String xssSanitized = "202.555.1212";
99          // tell the Mock Sanitizer to return a specific string
100         when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
101 
102         testPhone.setNumber(xssInput);
103 
104         // now we will test to see if the Customer class calls the sanitizer correctly
105         String expected = "202.555.1212";
106         String actual = testPhone.getNumber();
107         assertEquals(expected, actual);
108     }
109 
110     @Test  
111     public void testToString()
112     {
113         String expected = "Phone [Number=null]";
114         String actual = testPhone.toString();
115         assertEquals(expected, actual);
116     }
117     
118     @Test public void testHashCode()
119     {
120         Phone sample = new Phone();
121         int expected = sample.hashCode();
122         int actual = testPhone.hashCode();
123         assertEquals(expected, actual);
124     }
125     
126     @Test public void testEquals()
127     {
128         Phone sample = new Phone();
129         sample.setNumber("202.555.1212");
130         testPhone.setNumber("202.555.1212");
131         assertTrue(testPhone.equals(sample));
132     }
133     
134     @Test public void testNotEquals()
135     {
136         Phone sample = new Phone();
137         sample.setNumber("312.101.3200");
138         assertFalse(testPhone.equals(sample));
139         
140         Object junk = new Object();
141         assertFalse(testPhone.equals(junk));
142     }
143 
144 }