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 LocationTest
    22  {
    23      private Location testLocation = null;
    24      private XssSanitizer sanitizerMock = null;
    25
    26      @BeforeClass // this runs only once before any test
    27      public static void setup()
    28      {
    29          Configurator.setLevel(LogManager.getLogger(Location.class).getName(), Level.WARN);
    30      }
    31
    32      @Before
    33      public void setUp() throws Exception
    34      {
    35          sanitizerMock = Mockito.mock(XssSanitizer.class);
    36          // tell the Mock Sanitizer to return the first argument it was passed
    37          // as a trimmed String
    38          when(sanitizerMock.sanitizeInput(anyString())).thenAnswer(i -> ((String) i.getArguments()[0]).trim());
    39
    40          testLocation = new Location(sanitizerMock);
    41      }
    42
    43      @Test
    44      public void testLocationDefaultConstructor()
    45      {
    46          Location defaultLocation = new Location();
    47          assertNotNull(defaultLocation);
    48      }
    49
    50      @Test
    51      public void testDefaultCity()
    52      {
    53          String expected = "New York City";
    54          String actual = testLocation.getCity();
    55          assertEquals(expected, actual);
    56      }
    57
    58      @Test
    59      public void testNullCity()
    60      {
    61          testLocation.setCity(null);
    62          assertNull(testLocation.getCity());
    63      }
    64
    65      @Test(expected = IllegalArgumentException.class)
    66      public void testEmptyCity()
    67      {
    68          testLocation.setCity("  ");
    69      }
    70
    71      @Test(expected = IllegalArgumentException.class)
    72      public void testTooLongCity()
    73      {
    74          String bad = RandomStringUtils.randomAlphabetic(41);
    75          testLocation.setCity(bad);
    76      }
    77
    78      @Test(expected = IllegalArgumentException.class)
    79      public void testTooShortCity()
    80      {
    81          String bad = RandomStringUtils.randomAlphabetic(1);
    82          testLocation.setCity(bad);
    83      }
    84
    85      @Test
    86      public void testValidCity()
    87      {
    88          String[] names = {"Boulder", "London"};
    89          for (String name : names)
    90          {
    91              testLocation.setCity(name);
    92          }
    93      }
    94
    95      @Test
    96      public void testSanitizedCity()
    97      {
    98          String xssInput = "<script>alert('You have been Hacked!');</script>Paris ";
    99          String xssSanitized = "Paris";
    100         // tell the Mock Sanitizer to return a specific string
    101         when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
    102
    103         testLocation.setCity(xssInput);
    104
    105         // now we will test to see if the Customer class calls the sanitizer correctly
    106         String expected = "Paris";
    107         String actual = testLocation.getCity();
    108         assertEquals(expected, actual);
    109     }
    110
    111     @Test
    112     public void testDefaultCountry()
    113     {
    114         String expected = "United States of America";
    115         String actual = testLocation.getCountry();
    116         assertEquals(expected, actual);
    117     }
    118
    119     @Test
    120     public void testNullCountry()
    121     {
    122         testLocation.setCountry(null);
    123         assertNull(testLocation.getCountry());
    124     }
    125
    126     @Test(expected = IllegalArgumentException.class)
    127     public void testEmptyCountry()
    128     {
    129         testLocation.setCountry("  ");
    130     }
    131
    132     @Test(expected = IllegalArgumentException.class)
    133     public void testTooLongCountry()
    134     {
    135         String bad = RandomStringUtils.randomAlphabetic(41);
    136         testLocation.setCountry(bad);
    137     }
    138
    139     @Test(expected = IllegalArgumentException.class)
    140     public void testTooShortCountry()
    141     {
    142         String bad = RandomStringUtils.randomAlphabetic(1);
    143         testLocation.setCountry(bad);
    144     }
    145
    146     @Test
    147     public void testValidCountry()
    148     {
    149         String[] names = {"Canada", "Japan", "Brazil"};
    150         for (String name : names)
    151         {
    152             testLocation.setCountry(name);
    153         }
    154     }
    155
    156     @Test
    157     public void testToString()
    158     {
    159         String expected = "Location [City=New York City, Country=United States of America]";
    160         String actual = testLocation.toString();
    161         assertEquals(expected, actual);
    162     }
    163
    164     @Test public void testHashCode()
    165     {
    166         Location sample = new Location();
    167         int expected = sample.hashCode();
    168         int actual = testLocation.hashCode();
    169         assertEquals(expected, actual);
    170     }
    171
    172     @Test public void testEquals()
    173     {
    174         Location sample = new Location();
    175         assertTrue(testLocation.equals(sample));
    176     }
    177
    178     @Test public void testNotEquals()
    179     {
    180         Location sample = new Location();
    181         sample.setCity("Boston");
    182         assertFalse(testLocation.equals(sample));
    183
    184         Object junk = new Object();
    185         assertFalse(testLocation.equals(junk));
    186     }
    187
    188 }