View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.appointment.service;
35  
36  import java.time.LocalDate;
37  import java.time.LocalDateTime;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import fr.paris.lutece.test.LuteceTestCase;
42  import java.time.format.DateTimeFormatter;
43  import java.util.Locale;
44  import org.junit.Test;
45  
46  public class UtilitiesTest extends LuteceTestCase
47  {
48  
49      /**
50       * Return the closest date in past a list of date with the given date
51       */
52      @Test
53      public void testGetClosestDateInPast( )
54      {
55          LocalDate localDate1 = LocalDate.parse( "2018-06-05" );
56          LocalDate localDate2 = LocalDate.parse( "2018-06-10" );
57          LocalDate localDate3 = LocalDate.parse( "2018-06-25" );
58  
59          List<LocalDate> listDates = new ArrayList<>( );
60          listDates.add( localDate1 );
61          listDates.add( localDate2 );
62          listDates.add( localDate3 );
63          assertEquals( localDate2, Utilities.getClosestDateInPast( listDates, LocalDate.parse( "2018-06-15" ) ) );
64      }
65  
66      /**
67       * Return the closest date time in future in a list of date time and a given date time
68       */
69      @Test
70      public void testGetClosestDateTimeInFuture( )
71      {
72          LocalDateTime localDateTime1 = LocalDateTime.parse( "2018-06-05T10:15" );
73          LocalDateTime localDateTime2 = LocalDateTime.parse( "2018-06-10T10:30" );
74          LocalDateTime localDateTime3 = LocalDateTime.parse( "2018-06-25T11:15" );
75  
76          List<LocalDateTime> listDateTime = new ArrayList<>( );
77          listDateTime.add( localDateTime1 );
78          listDateTime.add( localDateTime2 );
79          listDateTime.add( localDateTime3 );
80          assertEquals( localDateTime3, Utilities.getClosestDateTimeInFuture( listDateTime, LocalDateTime.parse( "2018-06-10T12:30" ) ) );
81      }
82  
83      /**
84       * Test of getFormatter method, of class Utilities.
85       */
86      @Test
87      public void testGetFormatter( )
88      {
89          System.out.println( "getFormatter" );
90  
91          AppointmentPlugin.setPluginLocale( Locale.ENGLISH );
92          Utilities.resetFormatter( );
93          DateTimeFormatter formatterEn = Utilities.getFormatter( );
94          LocalDateTime localDateTimeEn = LocalDateTime.parse( "2018-06-25T00:00" );
95          String strDateEn = localDateTimeEn.format( formatterEn );
96          assertEquals( "6/25/18", strDateEn );
97  
98          AppointmentPlugin.setPluginLocale( Locale.FRENCH );
99          Utilities.resetFormatter( );
100         DateTimeFormatter formatterFr = Utilities.getFormatter( );
101         LocalDateTime localDateTimeFr = LocalDateTime.parse( "2018-06-25T00:00" );
102         String strDateFr = localDateTimeFr.format( formatterFr );
103         assertEquals( "25/06/2018", strDateFr );
104 
105     }
106 
107 }