LightJason - AgentSpeak(L++)
TestCActionDateTime.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin;
25 
26 import com.tngtech.java.junit.dataprovider.DataProvider;
27 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
28 import com.tngtech.java.junit.dataprovider.UseDataProvider;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.apache.commons.lang3.tuple.ImmutableTriple;
31 import org.apache.commons.lang3.tuple.Pair;
32 import org.apache.commons.lang3.tuple.Triple;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
58 
59 import java.time.ZonedDateTime;
60 import java.util.ArrayList;
61 import java.util.Collections;
62 import java.util.List;
63 import java.util.stream.Collectors;
64 import java.util.stream.Stream;
65 
66 
70 @RunWith( DataProviderRunner.class )
71 public final class TestCActionDateTime extends IBaseTest
72 {
73 
78  @DataProvider
79  public static Object[] generateapplyminus()
80  {
81  return Stream.of(
82  testcaseapply( new CApplyYears(), "2010-05-04T10:17:13Z[America/New_York]", 6, "2004-05-04T10:17:13Z[America/New_York]" ),
83  testcaseapply( new CApplyMonths(), "2009-05-04T10:17:13Z[America/New_York]", 7, "2008-10-04T10:17:13Z[America/New_York]" ),
84  testcaseapply( new CApplyDays(), "2008-05-04T10:17:13Z[America/New_York]", 5, "2008-04-29T10:17:13Z[America/New_York]" ),
85  testcaseapply( new CApplyHours(), "2009-01-15T15:16:13Z[Europe/London]", 36, "2009-01-14T03:16:13Z[Europe/London]" ),
86  testcaseapply( new CApplyMinutes(), "2007-01-15T10:23:13Z[Europe/London]", 187, "2007-01-15T07:16:13Z[Europe/London]" ),
87  testcaseapply( new CApplySeconds(), "2006-01-15T10:23:13Z[Europe/London]", 4200, "2006-01-15T09:13:13Z[Europe/London]" ),
88  testcaseapply( new CApplyNanoSeconds(), "2005-01-15T10:23:13Z[Europe/London]", 10200, "2005-01-15T10:23:12.999989800Z[Europe/London]" )
89  ).toArray();
90  }
91 
92 
97  @DataProvider
98  public static Object[] generateapplyplus()
99  {
100  return Stream.of(
101  testcaseapply( new CApplyYears(), "2010-05-04T10:17:13Z[America/New_York]", 12, "2022-05-04T10:17:13Z[America/New_York]" ),
102  testcaseapply( new CApplyMonths(), "2009-05-04T10:17:13Z[America/New_York]", 8, "2010-01-04T06:17:13-05:00[America/New_York]" ),
103  testcaseapply( new CApplyDays(), "2008-05-04T10:17:13Z[America/New_York]", 3, "2008-05-07T10:17:13Z[America/New_York]" ),
104  testcaseapply( new CApplyHours(), "2009-01-15T10:16:13Z[Europe/London]", 120, "2009-01-20T10:16:13Z[Europe/London]" ),
105  testcaseapply( new CApplyMinutes(), "2007-01-15T10:23:13Z[Europe/London]", 240, "2007-01-15T14:23:13Z[Europe/London]" ),
106  testcaseapply( new CApplySeconds(), "2006-01-15T10:23:13Z[Europe/London]", 7205, "2006-01-15T12:23:18Z[Europe/London]" ),
107  testcaseapply( new CApplyNanoSeconds(), "2005-01-15T10:23:13Z[Europe/London]", 15715, "2005-01-15T10:23:13.000015715Z[Europe/London]" )
108  ).toArray();
109  }
110 
111 
117  @DataProvider
118  public static Object[] generatebetween()
119  {
120  return Stream.of(
121  testcasebetween(
122  new CYearsBetween(),
123  Stream.of(
124  "2000-01-15T10:16:13Z[Europe/London]", "2000-01-15T10:16:13Z[Europe/London]",
125  "2000-01-15T10:23:13Z[Europe/London]", "2020-05-04T10:17:13Z[America/New_York]"
126  ),
127  Stream.of( 0, 20 )
128  ),
129 
130  testcasebetween(
131  new CMonthsBetween(),
132  Stream.of(
133  "1999-01-15T10:16:13Z[Europe/London]", "1999-01-15T10:16:13Z[Europe/London]",
134  "1999-01-15T10:16:13Z[Europe/London]", "2001-01-15T10:16:13Z[Europe/London]"
135  ),
136  Stream.of( 0, 24 )
137  ),
138 
139  testcasebetween(
140  new CDaysBetween(),
141  Stream.of(
142  "1998-01-15T10:23:13Z[Europe/London]", "1998-01-15T10:23:13Z[Europe/London]",
143  "1998-06-15T10:23:13Z[Europe/London]", "1998-01-15T10:23:13Z[Europe/London]"
144  ),
145  Stream.of( 0, -151 )
146  ),
147 
148  testcasebetween(
149  new CHoursBetween(),
150  Stream.of(
151  "1997-05-04T10:17:13Z[America/New_York]", "1997-05-04T10:17:13Z[America/New_York]",
152  "1997-05-04T18:12:13Z[America/New_York]", "1997-05-04T10:17:13Z[America/New_York]"
153  ),
154  Stream.of( 0, -7 )
155  ),
156 
157  testcasebetween(
158  new CMinutesBetween(),
159  Stream.of(
160  "1996-01-15T10:23:13Z[Europe/Paris]", "1996-01-15T10:23:13Z[Europe/Paris]",
161  "1996-01-15T10:23:13Z[Europe/Paris]", "1996-01-15T16:23:13Z[Europe/Paris]"
162 
163  ),
164  Stream.of( 0, 360 )
165  ),
166 
167  testcasebetween(
168  new CSecondsBetween(),
169  Stream.of(
170  "1995-01-15T10:23:13Z[Europe/Madrid]", "1995-01-15T10:23:13Z[Europe/Madrid]",
171  "1995-02-15T10:23:13Z[Europe/Madrid]", "1995-02-14T10:23:13Z[Europe/Madrid]"
172  ),
173  Stream.of( 0, -86400 )
174  )
175 
176  ).toArray();
177  }
178 
179 
189  private static Object testcaseapply( final IAction p_action, final String p_datetime, final int p_value, final String p_result )
190  {
191  return new ImmutableTriple<>( p_action, new ImmutablePair<>( ZonedDateTime.parse( p_datetime ), p_value ), p_result );
192  }
193 
194 
202  private static Object testcasebetween( final IAction p_action, final Stream<String> p_datetime, final Stream<Number> p_results )
203  {
204  return new ImmutableTriple<>( p_action, p_datetime.map( ZonedDateTime::parse ).map( CRawTerm::from ), p_results );
205  }
206 
207 
208 
212  @Test
213  public final void createerror()
214  {
215  Assert.assertFalse(
216  new CCreate().execute(
217  false, IContext.EMPTYPLAN,
218  Stream.of( "error" ).map( CRawTerm::from ).collect( Collectors.toList() ),
219  Collections.emptyList()
220  ).value()
221  );
222  }
223 
227  @Test
228  public final void create()
229  {
230  final List<ITerm> l_return = new ArrayList<>();
231 
232  new CCreate().execute(
233  false, IContext.EMPTYPLAN,
234  Stream.of( "", "2007-12-03T10:15:30+01:00[Europe/Paris]", "now" ).map( CRawTerm::from ).collect( Collectors.toList() ),
235  l_return
236  );
237 
238  Assert.assertEquals( l_return.size(), 3 );
239  Assert.assertTrue( l_return.get( 0 ).raw() instanceof ZonedDateTime );
240  Assert.assertTrue( l_return.get( 1 ).raw() instanceof ZonedDateTime );
241  Assert.assertTrue( l_return.get( 2 ).raw() instanceof ZonedDateTime );
242 
243  Assert.assertTrue( l_return.get( 0 ).<ZonedDateTime>raw().isBefore( l_return.get( 2 ).<ZonedDateTime>raw() ) );
244  }
245 
246 
250  @Test
251  public final void build()
252  {
253  final List<ITerm> l_return = new ArrayList<>();
254 
255  new CBuild().execute(
256  false, IContext.EMPTYPLAN,
257  Stream.of( 2013, 3, 13, 12, 11, 10, 9, "current", 2013, 3, 13, 12, 11, 10, 9, "Europe/Moscow" ).map( CRawTerm::from )
258  .collect( Collectors.toList() ),
259  l_return
260  );
261 
262  Assert.assertEquals( l_return.size(), 2 );
263  Assert.assertTrue( l_return.get( 0 ).raw() instanceof ZonedDateTime );
264  Assert.assertTrue( l_return.get( 1 ).raw() instanceof ZonedDateTime );
265 
266  Assert.assertEquals( l_return.get( 0 ).<ZonedDateTime>raw().getYear(), l_return.get( 1 ).<ZonedDateTime>raw().getYear() );
267  Assert.assertEquals( l_return.get( 0 ).<ZonedDateTime>raw().getMonthValue(), l_return.get( 1 ).<ZonedDateTime>raw().getMonthValue() );
268  Assert.assertEquals( l_return.get( 0 ).<ZonedDateTime>raw().getDayOfMonth(), l_return.get( 1 ).<ZonedDateTime>raw().getDayOfMonth() );
269 
270  Assert.assertEquals( l_return.get( 0 ).<ZonedDateTime>raw().getHour(), l_return.get( 1 ).<ZonedDateTime>raw().getHour() );
271  Assert.assertEquals( l_return.get( 0 ).<ZonedDateTime>raw().getMinute(), l_return.get( 1 ).<ZonedDateTime>raw().getMinute() );
272  Assert.assertEquals( l_return.get( 0 ).<ZonedDateTime>raw().getSecond(), l_return.get( 1 ).<ZonedDateTime>raw().getSecond() );
273  Assert.assertEquals( l_return.get( 0 ).<ZonedDateTime>raw().getNano(), l_return.get( 1 ).<ZonedDateTime>raw().getNano() );
274 
275  Assert.assertNotEquals( l_return.get( 0 ).<ZonedDateTime>raw().getZone(), l_return.get( 1 ).<ZonedDateTime>raw().getZone() );
276  }
277 
278 
282  @Test
283  public final void time()
284  {
285  final List<ITerm> l_return = new ArrayList<>();
286 
287  Assert.assertTrue(
288  "action execution error",
289  new CTime().execute(
290  false, IContext.EMPTYPLAN,
291  Stream.of( "2007-12-03T10:15:30+03:00[Europe/Moscow]" ).map( CRawTerm::from ).collect( Collectors.toList() ),
292  l_return
293  ).value()
294  );
295 
296  Assert.assertEquals( l_return.size(), 4 );
297  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 10 );
298  Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 15 );
299  Assert.assertEquals( l_return.get( 2 ).<Number>raw(), 30 );
300  Assert.assertEquals( l_return.get( 3 ).<Number>raw(), 0 );
301  }
302 
303 
307  @Test
308  public final void zoneid()
309  {
310  final List<ITerm> l_return = new ArrayList<>();
311 
312  new CZoneid().execute(
313  false, IContext.EMPTYPLAN,
314  Stream.of( "2006-10-04T10:17:13-05:00[America/New_York]", "2006-10-04T10:17:13+00:00[Europe/London]" ).map( CRawTerm::from )
315  .collect( Collectors.toList() ),
316  l_return
317  );
318 
319  Assert.assertEquals( l_return.size(), 2 );
320  Assert.assertEquals( l_return.get( 0 ).raw(), "America/New_York" );
321  Assert.assertEquals( l_return.get( 1 ).raw(), "Europe/London" );
322  }
323 
324 
330  @Test
331  @UseDataProvider( "generateapplyminus" )
332  public final void applysminus( final Triple<IAction, Pair<ZonedDateTime, Integer>, String> p_value )
333  {
334  final List<ITerm> l_return = new ArrayList<>();
335 
336  p_value.getLeft().execute(
337  false, IContext.EMPTYPLAN,
338  Stream.of( "minus", p_value.getMiddle().getRight(), p_value.getMiddle().getLeft() ).map( CRawTerm::from ).collect( Collectors.toList() ),
339  l_return
340  );
341 
342  Assert.assertEquals( l_return.size(), 1 );
343  Assert.assertEquals( l_return.get( 0 ).raw(), ZonedDateTime.parse( p_value.getRight() ) );
344  }
345 
351  @Test
352  @UseDataProvider( "generateapplyplus" )
353  public final void applyplus( final Triple<IAction, Pair<ZonedDateTime, Integer>, String> p_value )
354  {
355  final List<ITerm> l_return = new ArrayList<>();
356 
357  p_value.getLeft().execute(
358  false, IContext.EMPTYPLAN,
359  Stream.of( "plus", p_value.getMiddle().getRight(), p_value.getMiddle().getLeft() ).map( CRawTerm::from ).collect( Collectors.toList() ),
360  l_return
361  );
362 
363  Assert.assertEquals( l_return.size(), 1 );
364  Assert.assertEquals( l_return.get( 0 ).raw(), ZonedDateTime.parse( p_value.getRight() ) );
365  }
366 
372  @Test
373  @UseDataProvider( "generatebetween" )
374  public final void between( final Triple<IAction, Stream<ITerm>, Stream<Number>> p_value )
375  {
376  final List<ITerm> l_return = new ArrayList<>();
377 
378  p_value.getLeft().execute(
379  false, IContext.EMPTYPLAN,
380  p_value.getMiddle().collect( Collectors.toList() ),
381  l_return
382  );
383 
384  Assert.assertArrayEquals(
385  l_return.stream().map( ITerm::<Number>raw ).mapToLong( Number::longValue ).toArray(),
386  p_value.getRight().mapToLong( Number::longValue ).toArray()
387  );
388  }
389 
390 }
static Object testcaseapply(final IAction p_action, final String p_datetime, final int p_value, final String p_result)
generate test-case of apply definition
base test class with helpers
Definition: IBaseTest.java:33
builds a date-time object from elements.
Definition: CBuild.java:51
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
static Object [] generatebetween()
data provider generator of between tests
execution context with local data
Definition: IContext.java:42
external action interface
Definition: IAction.java:38
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: IDateTime.java:59
static Object [] generateapplyplus()
data provider generator of apply-plus tests
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static Object testcasebetween(final IAction p_action, final Stream< String > p_datetime, final Stream< Number > p_results)
action for getting the current time.
Definition: CTime.java:44
static Object [] generateapplyminus()
data provider generator of apply-minus tests
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CBuild.java:67
term structure for simple datatypes
Definition: CRawTerm.java:45