LightJason - AgentSpeak(L++)
TestCActionGeneric.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 org.junit.Assert;
27 import org.junit.Test;
36 
37 import java.io.ByteArrayOutputStream;
38 import java.io.PrintStream;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.stream.Collectors;
43 import java.util.stream.Stream;
44 
45 
49 public final class TestCActionGeneric extends IBaseTest
50 {
51 
55  @Test( expected = CRuntimeException.class )
56  public final void throwparameter()
57  {
58  new CThrow().execute(
59  false, IContext.EMPTYPLAN,
60  Stream.of( true, "test message" ).map( CRawTerm::from ).collect( Collectors.toList() ),
61  Collections.emptyList()
62  );
63  }
64 
65 
69  @Test( expected = CRuntimeException.class )
70  public final void throwwithoutparameter()
71  {
72  new CThrow().execute(
73  false, IContext.EMPTYPLAN,
74  Stream.of( true ).map( CRawTerm::from ).collect( Collectors.toList() ),
75  Collections.emptyList()
76  );
77  }
78 
79 
83  @Test
84  public final void thrownot()
85  {
86  new CThrow().execute(
87  false, IContext.EMPTYPLAN,
88  Stream.of( false, "this should not be thrown" ).map( CRawTerm::from ).collect( Collectors.toList() ),
89  Collections.emptyList()
90  );
91  }
92 
93 
99  @Test
100  public final void print() throws Exception
101  {
102  final ByteArrayOutputStream l_output = new ByteArrayOutputStream();
103 
104  new CPrint( () -> new PrintStream( l_output, false, "utf-8" ), "-" ).execute(
105  false, IContext.EMPTYPLAN,
106  Stream.of( "foobar", 1234, true ).map( CRawTerm::from ).collect( Collectors.toList() ),
107  Collections.emptyList()
108  );
109 
110  Assert.assertEquals( l_output.toString( "utf-8" ), "foobar-1234-true\n" );
111  }
112 
118  @Test
119  public final void printformatter() throws Exception
120  {
121  final CPrint.IFormatter<?> l_format1 = new CStringFormatter();
122  final CPrint.IFormatter<?> l_format2 = new CBooleanFormatter();
123 
124  Assert.assertFalse( l_format1.equals( l_format2 ) );
125 
126 
127  final ByteArrayOutputStream l_output = new ByteArrayOutputStream();
128  final CPrint l_print = new CPrint( () -> new PrintStream( l_output, false, "utf-8" ), "-" );
129 
130  l_print.formatter().add( l_format1 );
131  l_print.formatter().add( l_format2 );
132 
133  l_print.execute(
134  false, IContext.EMPTYPLAN,
135  Stream.of( "foobar", 1234, true ).map( CRawTerm::from ).collect( Collectors.toList() ),
136  Collections.emptyList()
137  );
138 
139  Assert.assertEquals( l_output.toString( "utf-8" ), "FOOBAR-1234-yes\n" );
140  }
141 
145  @Test
146  public final void uuid()
147  {
148  final List<ITerm> l_return = new ArrayList<>();
149 
150  new CUuid().execute(
151  false, IContext.EMPTYPLAN,
152  Collections.emptyList(),
153  l_return
154  );
155 
156  Assert.assertEquals( l_return.size(), 1 );
157  Assert.assertEquals( l_return.get( 0 ).raw().getClass(), String.class );
158  Assert.assertTrue( !l_return.get( 0 ).<String>raw().isEmpty() );
159  }
160 
161  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
162 
166  private static final class CStringFormatter extends CPrint.IFormatter<String>
167  {
171  private static final long serialVersionUID = -8753365775971744734L;
172 
173  @Override
174  protected final Class<?> getType()
175  {
176  return String.class;
177  }
178 
179  @Override
180  protected final String format( final String p_data )
181  {
182  return p_data.toUpperCase();
183  }
184  }
185 
189  private static final class CBooleanFormatter extends CPrint.IFormatter<Boolean>
190  {
194  private static final long serialVersionUID = -1447139962315061035L;
195 
196  @Override
197  protected final Class<?> getType()
198  {
199  return Boolean.class;
200  }
201 
202  @Override
203  protected final String format( final Boolean p_data )
204  {
205  return p_data ? "yes" : "no";
206  }
207  }
208 
209 }
final void printformatter()
test print action with formatter
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: CPrint.java:146
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
test formatter for boolean (translate each boolean to an yes/no string)
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: CUuid.java:56
test formatter for strings (translate each string to an upper-case string)
action for throwing a runtime execption.
Definition: CThrow.java:46
execution context with local data
Definition: IContext.java:42
final Set< IFormatter<?> > formatter()
returns the formatter list
Definition: CPrint.java:139
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
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: CThrow.java:62
term structure for simple datatypes
Definition: CRawTerm.java:45