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.times;
    10  import static org.mockito.Mockito.verify;
    11  import static org.mockito.Mockito.when;
    12
    13  import org.apache.commons.lang3.RandomStringUtils;
    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.utility.Location;
    18  import org.example.customer.utility.Phone;
    19  import org.example.websecurity.XssSanitizer;
    20  import org.junit.Before;
    21  import org.junit.BeforeClass;
    22  import org.junit.Test;
    23  import org.mockito.Mockito;
    24
    25  public class SupplierTest
    26  {
    27      private Supplier testSupplier = null;
    28      private XssSanitizer sanitizerMock = null;
    29
    30      @BeforeClass // this runs only once before any test
    31      public static void setup()
    32      {
    33          Configurator.setLevel(LogManager.getLogger(Supplier.class).getName(), Level.WARN);
    34      }
    35
    36      @Before
    37      public void setUp() throws Exception
    38      {
    39          sanitizerMock = Mockito.mock(XssSanitizer.class);
    40          // tell the Mock Sanitizer to return the first argument it was passed
    41          // as a trimmed String
    42          when(sanitizerMock.sanitizeInput(anyString())).thenAnswer(i -> ((String) i.getArguments()[0]).trim());
    43
    44          testSupplier = new Supplier(sanitizerMock);
    45      }
    46
    47      @Test
    48      public void testSupplierDefaultConstructor()
    49      {
    50          Supplier defaultSupplier = new Supplier();
    51          assertNotNull(defaultSupplier);
    52      }
    53
    54      @Test
    55      public void testDefaultCompanyName()
    56      {
    57          String expected = "ABC Inc.";
    58          String actual = testSupplier.getCompanyName();
    59          assertEquals(expected, actual);
    60      }
    61
    62      @Test(expected = IllegalArgumentException.class)
    63      public void testNullCompanyName()
    64      {
    65          testSupplier.setCompanyName(null);
    66      }
    67
    68      @Test(expected = IllegalArgumentException.class)
    69      public void testEmptyCompanyName()
    70      {
    71          testSupplier.setCompanyName("  ");
    72      }
    73
    74      @Test(expected = IllegalArgumentException.class)
    75      public void testTooLongCompanyName()
    76      {
    77          String bad = RandomStringUtils.randomAlphabetic(41);
    78          testSupplier.setCompanyName(bad);
    79      }
    80
    81      @Test
    82      public void testValidCompanyName()
    83      {
    84          String[] names = {"Players Unlimited", "Will Work for Food"};
    85          for (String name : names)
    86          {
    87              testSupplier.setCompanyName(name);
    88          }
    89      }
    90
    91      @Test
    92      public void testSanitizedCompanyName()
    93      {
    94          String xssInput = "<script>alert('You have been Hacked!');</script>StarShip Enterprises";
    95          String xssSanitized = "StarShip Enterprises";
    96          // tell the Mock Sanitizer to return a specific string
    97          when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
    98
    99          testSupplier.setCompanyName(xssInput);
    100
    101         // now we will test to see if the Customer class calls the sanitizer correctly
    102         String expected = "StarShip Enterprises";
    103         String actual = testSupplier.getCompanyName();
    104         assertEquals(expected, actual);
    105
    106         verify(sanitizerMock, times(1)).sanitizeInput(xssInput);
    107     }
    108
    109     @Test
    110     public void testDefaultContactName()
    111     {
    112         String expected = "John Smith";
    113         String actual = testSupplier.getContactName();
    114         assertEquals(expected, actual);
    115     }
    116
    117     @Test
    118     public void testNullContactName()
    119     {
    120         testSupplier.setContactName(null);
    121         assertNull(testSupplier.getContactName());
    122     }
    123
    124     @Test(expected = IllegalArgumentException.class)
    125     public void testEmptyContactName()
    126     {
    127         testSupplier.setContactName("  ");
    128     }
    129
    130     @Test(expected = IllegalArgumentException.class)
    131     public void testTooLongContactName()
    132     {
    133         String bad = RandomStringUtils.randomAlphabetic(51);
    134         testSupplier.setContactName(bad);
    135     }
    136
    137     @Test(expected = IllegalArgumentException.class)
    138     public void testTooShortContactName()
    139     {
    140         String bad = RandomStringUtils.randomAlphabetic(1);
    141         testSupplier.setContactName(bad);
    142     }
    143
    144     @Test
    145     public void testValidContactName()
    146     {
    147         String[] names = {"Betty Rubble", "Sir Robert Service"};
    148         for (String name : names)
    149         {
    150             testSupplier.setContactName(name);
    151         }
    152     }
    153
    154     @Test
    155     public void testSanitizedContactName()
    156     {
    157         String xssInput = "<script>alert('You have been Hacked!');</script>Bill Norton";
    158         String xssSanitized = "Bill Norton";
    159         // tell the Mock Sanitizer to return a specific string
    160         when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
    161
    162         testSupplier.setContactName(xssInput);
    163
    164         // now we will test to see if the Customer class calls the sanitizer correctly
    165         String expected = "Bill Norton";
    166         String actual = testSupplier.getContactName();
    167         assertEquals(expected, actual);
    168
    169         verify(sanitizerMock, times(1)).sanitizeInput(xssInput);
    170     }
    171
    172     @Test
    173     public void testDefaultLocation()
    174     {
    175         Location expected = new Location();
    176         Location actual = testSupplier.getLocation();
    177         assertEquals(expected, actual);
    178     }
    179
    180     @Test(expected = IllegalArgumentException.class)
    181     public void testNullLocation()
    182     {
    183         testSupplier.setLocation(null);
    184     }
    185
    186     @Test
    187     public void testValidLocation()
    188     {
    189         Location home = new Location();
    190         home.setCity("Vancouver");
    191         home.setCountry("Canada");
    192         testSupplier.setLocation(home);
    193     }
    194
    195     @Test
    196     public void testDefaultPhone()
    197     {
    198         Phone expected = new Phone();
    199         Phone actual = testSupplier.getPhone();
    200         assertEquals(expected, actual);
    201     }
    202
    203     @Test
    204     public void testNullPhone()
    205     {
    206         testSupplier.setPhone(null);
    207     }
    208
    209     @Test
    210     public void testValidPhone()
    211     {
    212         Phone phone = new Phone();
    213         phone.setNumber("001 42-234-1234");
    214         testSupplier.setPhone(phone);
    215         assertNotNull(testSupplier.getPhone());
    216     }
    217
    218     @Test
    219     public void testDefaultFax()
    220     {
    221         Phone expected = new Phone();
    222         Phone actual = testSupplier.getFax();
    223         assertEquals(expected, actual);
    224     }
    225
    226     @Test
    227     public void testNullFax()
    228     {
    229         testSupplier.setFax(null);
    230     }
    231
    232     @Test
    233     public void testValidFax()
    234     {
    235         Phone phone = new Phone();
    236         phone.setNumber("001 42-234-1234");
    237         testSupplier.setFax(phone);
    238         assertNotNull(testSupplier.getFax());
    239     }
    240
    241     @Test
    242     public void testToString()
    243     {
    244         String expected = "Supplier [Id=1, CompanyName=ABC Inc., "
    245                 + "City=New York City, Country=United States of America, "
    246                 + "Phone=null]";
    247         String actual = testSupplier.toString();
    248         assertEquals(expected, actual);
    249     }
    250
    251     @Test public void testHashCode()
    252     {
    253         Supplier sample = new Supplier();
    254         int expected = sample.hashCode();
    255         int actual = testSupplier.hashCode();
    256         assertEquals(expected, actual);
    257     }
    258
    259     @Test public void testEquals()
    260     {
    261         Supplier sample = new Supplier();
    262         assertTrue(testSupplier.equals(sample));
    263     }
    264
    265     @Test public void testNotEquals()
    266     {
    267         Supplier sample = new Supplier();
    268         sample.setCompanyName("World Wide Unlimited");
    269         assertFalse(testSupplier.equals(sample));
    270
    271         Customer junk = new Customer();
    272         assertFalse(testSupplier.equals(junk));
    273     }
    274
    275 }