24 package org.lightjason.agentspeak.action.builtin;
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;
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;
70 @RunWith( DataProviderRunner.class )
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]" )
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]" )
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]" 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]" 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]" 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]" 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]" 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]" 173 Stream.of( 0, -86400 )
189 private static Object
testcaseapply(
final IAction p_action,
final String p_datetime,
final int p_value,
final String p_result )
191 return new ImmutableTriple<>( p_action,
new ImmutablePair<>( ZonedDateTime.parse( p_datetime ), p_value ), p_result );
202 private static Object
testcasebetween(
final IAction p_action,
final Stream<String> p_datetime,
final Stream<Number> p_results )
204 return new ImmutableTriple<>( p_action, p_datetime.map( ZonedDateTime::parse ).map(
CRawTerm::from ), p_results );
218 Stream.of(
"error" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
219 Collections.emptyList()
230 final List<ITerm> l_return =
new ArrayList<>();
234 Stream.of(
"",
"2007-12-03T10:15:30+01:00[Europe/Paris]",
"now" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
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 );
243 Assert.assertTrue( l_return.get( 0 ).<ZonedDateTime>raw().isBefore( l_return.get( 2 ).<ZonedDateTime>raw() ) );
253 final List<ITerm> l_return =
new ArrayList<>();
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() ),
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 );
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() );
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() );
275 Assert.assertNotEquals( l_return.get( 0 ).<ZonedDateTime>raw().getZone(), l_return.get( 1 ).<ZonedDateTime>raw().getZone() );
285 final List<ITerm> l_return =
new ArrayList<>();
288 "action execution error",
291 Stream.of(
"2007-12-03T10:15:30+03:00[Europe/Moscow]" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
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 );
310 final List<ITerm> l_return =
new ArrayList<>();
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() ),
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" );
331 @UseDataProvider(
"generateapplyminus" )
332 public final
void applysminus( final Triple<
IAction, Pair<ZonedDateTime, Integer>, String> p_value )
334 final List<ITerm> l_return =
new ArrayList<>();
336 p_value.getLeft().execute(
338 Stream.of(
"minus", p_value.getMiddle().getRight(), p_value.getMiddle().getLeft() ).map(
CRawTerm::from ).collect( Collectors.toList() ),
342 Assert.assertEquals( l_return.size(), 1 );
343 Assert.assertEquals( l_return.get( 0 ).raw(), ZonedDateTime.parse( p_value.getRight() ) );
352 @UseDataProvider(
"generateapplyplus" )
353 public final
void applyplus( final Triple<
IAction, Pair<ZonedDateTime, Integer>, String> p_value )
355 final List<ITerm> l_return =
new ArrayList<>();
357 p_value.getLeft().execute(
359 Stream.of(
"plus", p_value.getMiddle().getRight(), p_value.getMiddle().getLeft() ).map(
CRawTerm::from ).collect( Collectors.toList() ),
363 Assert.assertEquals( l_return.size(), 1 );
364 Assert.assertEquals( l_return.get( 0 ).raw(), ZonedDateTime.parse( p_value.getRight() ) );
373 @UseDataProvider(
"generatebetween" )
374 public final
void between( final Triple<
IAction, Stream<
ITerm>, Stream<Number>> p_value )
376 final List<ITerm> l_return =
new ArrayList<>();
378 p_value.getLeft().execute(
380 p_value.getMiddle().collect( Collectors.toList() ),
384 Assert.assertArrayEquals(
385 l_return.stream().map(
ITerm::<Number>raw ).mapToLong( Number::longValue ).toArray(),
386 p_value.getRight().mapToLong( Number::longValue ).toArray()
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
subtract or adds a number of minutes.
subtract or adds a number of seconds.
base test class with helpers
builds a date-time object from elements.
final void time()
test time
IContext EMPTYPLAN
empty context with plan
final void zoneid()
test zone-id
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
subtract or adds a number of years.
subtract or adds a number of hours.
execution context with local data
external action interface
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
returns the hours between two dates.
static Object [] generateapplyplus()
data provider generator of apply-plus tests
subtract or adds a number of months.
returns the days between two dates.
returns the minutes between two dates.
returns the seconds between two dates.
subtract or adds a number of days.
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
subtract or adds a number of nano-seconds.
returns the years between two dates.
static Object testcasebetween(final IAction p_action, final Stream< String > p_datetime, final Stream< Number > p_results)
action for getting the current time.
final void create()
test create
returns a date-time object.
final void build()
test build
static Object [] generateapplyminus()
data provider generator of apply-minus tests
returns the months between two dates.
final void createerror()
test create error
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
term structure for simple datatypes