LightJason - AgentSpeak(L++)
TestCActionWebRest.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.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.stream.Collectors;
41 import java.util.stream.Stream;
42 
43 
47 public final class TestCActionWebRest extends IBaseTest
48 {
49 
53  @Test
54  public final void jsonlisterror()
55  {
56  Assert.assertFalse(
57  new CJsonList().execute(
58  false, IContext.EMPTYPLAN,
59  Stream.of( "jsonlist", "testjsonlist" )
60  .map( CRawTerm::from )
61  .collect( Collectors.toList() ),
62  Collections.emptyList()
63  ).value()
64  );
65  }
66 
70  @Test
71  public final void jsonlistsingle()
72  {
73  final List<ITerm> l_return = new ArrayList<>();
74 
75  new CJsonList().execute(
76  false, IContext.EMPTYPLAN,
77  Stream.of( "https://api.github.com/repos/LightJason/Examples/commits", "testjsonlist" )
78  .map( CRawTerm::from )
79  .collect( Collectors.toList() ),
80  l_return
81  );
82 
83  Assert.assertFalse( l_return.isEmpty() );
84  Assert.assertTrue( l_return.stream().map( ITerm::raw ).allMatch( i -> i instanceof ILiteral ) );
85  Assert.assertTrue( l_return.stream().map( ITerm::<ILiteral>raw ).map( ITerm::functor ).allMatch( i -> i.equals( "testjsonlist" ) ) );
86  }
87 
88 
92  @Test
93  public final void jsonlistmultiple()
94  {
95  final List<ITerm> l_return = new ArrayList<>();
96 
97  new CJsonList().execute(
98  false, IContext.EMPTYPLAN,
99  Stream.of( "https://api.github.com/repos/LightJason/Examples/commits", "testjsonlist", "item" )
100  .map( CRawTerm::from )
101  .collect( Collectors.toList() ),
102  l_return
103  );
104 
105  Assert.assertEquals( l_return.size(), 1 );
106  Assert.assertTrue( l_return.get( 0 ).raw() instanceof ILiteral );
107  Assert.assertEquals( l_return.get( 0 ).<ILiteral>raw().functor(), "testjsonlist" );
108  Assert.assertFalse( l_return.get( 0 ).<ILiteral>raw().values().map( ITerm::functor ).noneMatch( i -> i.equals( "item" ) ) );
109  }
110 
111 
115  @Test
116  public final void jsonobjecterror()
117  {
118  Assert.assertFalse(
119  new CJsonObject().execute(
120  false, IContext.EMPTYPLAN,
121  Stream.of( "jsonobject", "testjsonobject" )
122  .map( CRawTerm::from )
123  .collect( Collectors.toList() ),
124  Collections.emptyList()
125  ).value()
126  );
127  }
128 
129 
133  @Test
134  public final void jsonobjectsingle()
135  {
136  final List<ITerm> l_return = new ArrayList<>();
137 
138  new CJsonObject().execute(
139  false, IContext.EMPTYPLAN,
140  Stream.of( "https://maps.googleapis.com/maps/api/geocode/json?address=Frankfurt", "testjsonobject" )
141  .map( CRawTerm::from )
142  .collect( Collectors.toList() ),
143  l_return
144  );
145 
146  Assert.assertEquals( l_return.size(), 1 );
147  Assert.assertTrue( l_return.get( 0 ).raw() instanceof ILiteral );
148  Assert.assertEquals( l_return.get( 0 ).<ILiteral>raw().functor(), "testjsonobject" );
149  }
150 
151 
155  @Test
156  public final void jsonobjectmultiple()
157  {
158  final List<ITerm> l_return = new ArrayList<>();
159 
160  new CJsonObject().execute(
161  false, IContext.EMPTYPLAN,
162  Stream.of( "https://maps.googleapis.com/maps/api/geocode/json?address=Frankfurt", "testjsonobject", "loc" )
163  .map( CRawTerm::from )
164  .collect( Collectors.toList() ),
165  l_return
166  );
167 
168  Assert.assertEquals( l_return.size(), 1 );
169  Assert.assertTrue( l_return.get( 0 ).raw() instanceof ILiteral );
170  Assert.assertEquals( l_return.get( 0 ).<ILiteral>raw().functor(), "testjsonobject" );
171  Assert.assertTrue( l_return.get( 0 ).<ILiteral>raw().values().findFirst().isPresent() );
172  Assert.assertEquals(
173  l_return.get( 0 ).<ILiteral>raw().values().findFirst().map( ITerm::functor ).orElseThrow( IllegalArgumentException::new ),
174  "loc"
175  );
176  }
177 
178 
182  @Test
183  public final void xmlobjecterror()
184  {
185  Assert.assertFalse(
186  new CXMLObject().execute(
187  false, IContext.EMPTYPLAN,
188  Stream.of( "xmlobject", "testxml" )
189  .map( CRawTerm::from )
190  .collect( Collectors.toList() ),
191  Collections.emptyList()
192  ).value()
193  );
194  }
195 
196 
200  @Test
201  public final void xmlobjectsingle()
202  {
203  final List<ITerm> l_return = new ArrayList<>();
204 
205  new CXMLObject().execute(
206  false, IContext.EMPTYPLAN,
207  Stream.of( "https://en.wikipedia.org/wiki/Special:Export/Normalized_compression_distance", "testxml" )
208  .map( CRawTerm::from )
209  .collect( Collectors.toList() ),
210  l_return
211  );
212 
213  Assert.assertEquals( l_return.size(), 1 );
214  Assert.assertTrue( l_return.get( 0 ).raw() instanceof ILiteral );
215  Assert.assertEquals( l_return.get( 0 ).<ILiteral>raw().functor(), "testxml" );
216  }
217 
218 
222  @Test
223  public final void xmlobjectmultiple()
224  {
225  final List<ITerm> l_return = new ArrayList<>();
226 
227  new CXMLObject().execute(
228  false, IContext.EMPTYPLAN,
229  Stream.of( "https://en.wikipedia.org/wiki/Special:Export/Normalized_compression_distance", "testxml", "ncd" )
230  .map( CRawTerm::from )
231  .collect( Collectors.toList() ),
232  l_return
233  );
234 
235  Assert.assertEquals( l_return.size(), 1 );
236  Assert.assertTrue( l_return.get( 0 ).raw() instanceof ILiteral );
237  Assert.assertEquals( l_return.get( 0 ).<ILiteral>raw().functor(), "testxml" );
238  Assert.assertTrue( l_return.get( 0 ).<ILiteral>raw().values().findFirst().isPresent() );
239  Assert.assertEquals(
240  l_return.get( 0 ).<ILiteral>raw().values().findFirst().map( ITerm::functor ).orElseThrow( IllegalArgumentException::new ),
241  "ncd"
242  );
243  }
244 
245 }
final void jsonobjectsingle()
test json object with single argument
final void jsonlistmultiple()
test json list with multiple argument
base test class with helpers
Definition: IBaseTest.java:33
action for calling a restful webservice with a JSON object list.
Definition: CJsonList.java:49
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
final void jsonlistsingle()
test json list with single argument
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: CJsonList.java:67
Stream< ITerm > values(final IPath... p_path)
returns a stream over value items
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: CXMLObject.java:65
action to call a restful webservice with XML data.
Definition: CXMLObject.java:48
execution context with local data
Definition: IContext.java:42
final void xmlobjectsingle()
test xml object with single argument
String functor()
returns the functor without path
< T > T raw()
cast to any raw value type
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
final void xmlobjectmultiple()
test xml object with single argument
final void jsonobjectmultiple()
test json object with multiple arguments
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
Definition: CRawTerm.java:45
action for calling a restful webservice with a JSON object.