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  import static org.mockito.Mockito.mock;
    11  import static org.mockito.Mockito.times;
    12  import static org.mockito.Mockito.verify;
    13
    14  import java.time.LocalDate;
    15  import java.util.ArrayList;
    16  import java.util.List;
    17
    18  import org.apache.commons.lang3.RandomStringUtils;
    19  import org.apache.logging.log4j.Level;
    20  import org.apache.logging.log4j.LogManager;
    21  import org.apache.logging.log4j.core.config.Configurator;
    22  import org.example.websecurity.XssSanitizer;
    23  import org.junit.Before;
    24  import org.junit.BeforeClass;
    25  import org.junit.Test;
    26  import org.mockito.Mockito;
    27
    28  public class OrderTest
    29  {
    30      private Order testOrder = null;
    31      private XssSanitizer sanitizerMock = null;
    32
    33      @BeforeClass // this runs only once before any test
    34      public static void setup()
    35      {
    36          Configurator.setLevel(LogManager.getLogger(Order.class).getName(), Level.WARN);
    37      }
    38
    39      @Before
    40      public void setUp() throws Exception
    41      {
    42          sanitizerMock = mock(XssSanitizer.class);
    43          // tell the Mock Sanitizer to return the first argument it was passed
    44          // as a trimmed String
    45          when(sanitizerMock.sanitizeInput(anyString())).thenAnswer(i -> ((String) i.getArguments()[0]).trim());
    46
    47          testOrder = new Order(sanitizerMock);
    48      }
    49
    50      @Test
    51      public void testOrderDefaultConstructor()
    52      {
    53          Order defaultOrder = new Order();
    54          assertNotNull(defaultOrder);
    55      }
    56
    57      @Test
    58      public void testDefaultOrderDate()
    59      {
    60          LocalDate expected = LocalDate.now();
    61          LocalDate actual = testOrder.getOrderDate();
    62          assertEquals(expected, actual);
    63      }
    64
    65      @Test(expected = IllegalArgumentException.class)
    66      public void testNullOrderDate()
    67      {
    68          testOrder.setOrderDate(null);
    69      }
    70
    71      @Test(expected = IllegalArgumentException.class)
    72      public void testFutureOrderDate()
    73      {
    74          LocalDate bad = LocalDate.now().plusDays(1);
    75          testOrder.setOrderDate(bad);
    76      }
    77
    78      @Test
    79      public void testYesterdayOrderDate()
    80      {
    81          LocalDate good = LocalDate.now().minusDays(100);
    82          testOrder.setOrderDate(good);
    83      }
    84
    85      @Test
    86      public void testDefaultCustomerId()
    87      {
    88          int expected = 1;
    89          int actual = testOrder.getCustomerId();
    90          assertEquals(expected, actual);
    91      }
    92
    93      @Test
    94      public void testValidCustomerId()
    95      {
    96          int[] good = { 2, 34, 999, 22, 1, 565656 };
    97          for (int entry : good) {
    98              testOrder.setCustomerId(entry);
    99          }
    100     }
    101
    102     @Test(expected = IllegalArgumentException.class)
    103     public void testInValidCustomerId()
    104     {
    105         testOrder.setCustomerId(0);
    106     }
    107
    108     @Test
    109     public void testDefaultTotalAmount()
    110     {
    111         double expected = 0.00;
    112         double actual = testOrder.getTotalAmount();
    113         double precision = 0.001;
    114         assertEquals(expected, actual, precision);
    115     }
    116
    117     @Test
    118     public void testValidTotalAmount()
    119     {
    120         double[] good = {2.0, 0.34, 999.50, 22.25, 1.0};
    121         for (double entry : good) {
    122             testOrder.setTotalAmount(entry);
    123         }
    124     }
    125
    126     @Test(expected = IllegalArgumentException.class)
    127     public void testInValidTotalAmount()
    128     {
    129         testOrder.setTotalAmount(0.0);
    130     }
    131
    132     @Test
    133     public void testDefaultOrderItems()
    134     {
    135         int expected = 0;
    136         int actual = testOrder.getOrderItems().size();
    137         assertEquals(expected, actual);
    138     }
    139
    140     @Test(expected = IllegalArgumentException.class)
    141     public void testNullItems()
    142     {
    143         testOrder.setOrderItems(null);
    144     }
    145
    146     @Test
    147     public void testOrderItemsTotalAmount()
    148     {
    149         List<OrderItem> mockItems = new ArrayList<OrderItem>();
    150
    151         OrderItem orderItem1Mock = mock(OrderItem.class);
    152         when(orderItem1Mock.getSubTotal()).thenReturn(12.50);
    153         mockItems.add(orderItem1Mock);
    154
    155         OrderItem orderItem2Mock = Mockito.mock(OrderItem.class);
    156         when(orderItem2Mock.getSubTotal()).thenReturn(1.75);
    157        mockItems.add(orderItem2Mock);
    158
    159         OrderItem orderItem3Mock = Mockito.mock(OrderItem.class);
    160         when(orderItem3Mock.getSubTotal()).thenReturn(34.10);
    161         mockItems.add(orderItem3Mock);
    162
    163         testOrder.setOrderItems(mockItems);
    164
    165         double expected = 48.35;
    166         double actual = testOrder.getTotalAmount();
    167         double precision = 0.001;
    168         assertEquals(expected, actual, precision);
    169     }
    170
    171     @Test
    172     public void testDefaultOrderNumber()
    173     {
    174         assertNull(testOrder.getOrderNumber());
    175     }
    176
    177     @Test
    178     public void testNullOrderNumber()
    179     {
    180         testOrder.setOrderNumber(null);
    181     }
    182
    183     @Test(expected = IllegalArgumentException.class)
    184     public void OrderNumber()
    185     {
    186         testOrder.setOrderNumber("  ");
    187     }
    188
    189     @Test(expected = IllegalArgumentException.class)
    190     public void testTooLongOrderNumber()
    191     {
    192         String bad = RandomStringUtils.randomAlphabetic(11);
    193         testOrder.setOrderNumber(bad);
    194     }
    195
    196     @Test(expected = IllegalArgumentException.class)
    197     public void testTooShortOrderNumber()
    198     {
    199         String bad = RandomStringUtils.randomAlphabetic(1);
    200         testOrder.setOrderNumber(bad);
    201     }
    202
    203     @Test
    204     public void testValidOrderNumber()
    205     {
    206         String[] values = {"ABC-011", "2022-12X"};
    207         for (String value : values)
    208         {
    209             testOrder.setOrderNumber(value);
    210         }
    211     }
    212
    213     @Test
    214     public void testSanitizedOrderNumber()
    215     {
    216         String xssInput = "<script>alert('You have been Hacked!');</script>Ord23 W";
    217         String xssSanitized = "Ord23 W";
    218         // tell the Mock Sanitizer to return a specific string
    219         when(sanitizerMock.sanitizeInput(xssInput)).thenReturn(xssSanitized);
    220
    221         testOrder.setOrderNumber(xssInput);
    222
    223         // now we will test to see if the Customer class calls the sanitizer correctly
    224         String expected = "Ord23 W";
    225         String actual = testOrder.getOrderNumber();
    226         assertEquals(expected, actual);
    227
    228         verify(sanitizerMock, times(1)).sanitizeInput(xssInput);
    229     }
    230
    231     @Test
    232     public void testToString()
    233     {
    234         LocalDate today = LocalDate.now();
    235         String formattedToday = today.toString();
    236         String expected = "Order [Id=1, OrderDate="
    237                 + formattedToday + ", CustomerId=1, "
    238                 + "TotalAmount=0.0, OrderNumber=null, "
    239                 + "OrderItems=0]";
    240         String actual = testOrder.toString();
    241         assertEquals(expected, actual);
    242     }
    243
    244     @Test public void testHashCode()
    245     {
    246         Order sample = new Order();
    247         int expected = sample.hashCode();
    248         int actual = testOrder.hashCode();
    249         assertEquals(expected, actual);
    250     }
    251
    252     @Test public void testEquals()
    253     {
    254         Order sample = new Order();
    255         assertTrue(testOrder.equals(sample));
    256     }
    257
    258     @Test public void testNotEquals()
    259     {
    260         Order sample = new Order();
    261         sample.setCustomerId(2);
    262         assertFalse(testOrder.equals(sample));
    263
    264         Customer junk = new Customer();
    265         assertFalse(testOrder.equals(junk));
    266     }
    267 }