LightJason - AgentSpeak(L++)
TestCActionCollectionMultimap.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 
27 import com.google.common.collect.HashMultimap;
28 import com.google.common.collect.Multimap;
29 import com.google.common.collect.Multimaps;
30 import org.apache.commons.lang.RandomStringUtils;
31 import org.junit.Assert;
32 import org.junit.Test;
46 
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.List;
50 import java.util.Map;
51 import java.util.Random;
52 import java.util.stream.Collectors;
53 import java.util.stream.IntStream;
54 import java.util.stream.Stream;
55 
56 
60 public final class TestCActionCollectionMultimap extends IBaseTest
61 {
62 
66  @Test
67  public final void create()
68  {
69  final List<ITerm> l_return = new ArrayList<>();
70 
71  new CCreate().execute(
72  false, IContext.EMPTYPLAN,
73  Collections.emptyList(),
74  l_return
75  );
76 
77  Assert.assertEquals( l_return.size(), 1 );
78  Assert.assertTrue( l_return.get( 0 ).raw() instanceof Multimap<?, ?> );
79  Assert.assertTrue( l_return.get( 0 ).<Multimap<?, ?>>raw().isEmpty() );
80  }
81 
85  @Test
86  public final void createsynchronized()
87  {
88  final List<ITerm> l_return = new ArrayList<>();
89 
90  new CCreate().execute(
91  true, IContext.EMPTYPLAN,
92  Collections.emptyList(),
93  l_return
94  );
95 
96  Assert.assertEquals( l_return.size(), 1 );
97  Assert.assertEquals( l_return.get( 0 ).raw().getClass(), Multimaps.synchronizedSetMultimap( HashMultimap.create() ).getClass() );
98  }
99 
100 
104  @Test
105  public final void keysvalues()
106  {
107  final Multimap<Object, Object> l_map = HashMultimap.create();
108  l_map.put( "a", 1 );
109  l_map.put( "a", 2 );
110  l_map.put( "a", 3 );
111  l_map.put( "b", 1 );
112  l_map.put( "c", 10 );
113 
114  final List<ITerm> l_return = new ArrayList<>();
115 
116 
117  new CKeys().execute(
118  false, IContext.EMPTYPLAN,
119  Stream.of( l_map ).map( CRawTerm::from ).collect( Collectors.toList() ),
120  l_return
121  );
122 
123  new CValues().execute(
124  false, IContext.EMPTYPLAN,
125  Stream.of( l_map ).map( CRawTerm::from ).collect( Collectors.toList() ),
126  l_return
127  );
128 
129 
130  Assert.assertEquals( l_return.size(), 2 );
131  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( "a", "b", "c" ).toArray() );
132  Assert.assertArrayEquals( l_return.get( 1 ).<List<?>>raw().toArray(), Stream.of( 1, 2, 3, 1, 10 ).toArray() );
133  }
134 
138  @Test
139  public final void asmap()
140  {
141  final Multimap<Integer, String> l_map = HashMultimap.create();
142  final List<ITerm> l_return = new ArrayList<>();
143 
144  final Random l_random = new Random();
145  IntStream.range( 0, 5 ).map( i -> l_random.nextInt( 8 ) )
146  .forEach( i -> l_map.put( i, RandomStringUtils.random( 10, "abcdefghijklmnop" ) ) );
147 
148  new CAsMap().execute(
149  false, IContext.EMPTYPLAN,
150  Stream.of( l_map ).map( CRawTerm::from ).collect( Collectors.toList() ),
151  l_return
152  );
153 
154  Assert.assertEquals( l_return.size(), 1 );
155  Assert.assertTrue( l_return.get( 0 ).raw() instanceof Map<?, ?> );
156  Assert.assertArrayEquals( l_return.get( 0 ).<Map<?, ?>>raw().keySet().toArray(), l_map.keySet().toArray() );
157 
158  Assert.assertArrayEquals(
159  CCommon.flatten( l_return.get( 0 ).<Map<?, ?>>raw().values().stream().map( CRawTerm::from ) ).map( ITerm::raw ).toArray(),
160  l_map.values().toArray()
161  );
162  }
163 
164 
168  @Test
169  public final void putsingle()
170  {
171  final Multimap<Integer, String> l_map1 = HashMultimap.create();
172  final Multimap<Integer, String> l_map2 = HashMultimap.create();
173 
174  new CPutSingle().execute(
175  false, IContext.EMPTYPLAN,
176  Stream.of( 1, "foo", l_map1, l_map2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
177  Collections.emptyList()
178  );
179 
180  Assert.assertArrayEquals( l_map1.keys().toArray(), Stream.of( 1 ).toArray() );
181  Assert.assertArrayEquals( l_map2.keys().toArray(), Stream.of( 1 ).toArray() );
182 
183  Assert.assertArrayEquals( l_map1.values().toArray(), Stream.of( "foo" ).toArray() );
184  Assert.assertArrayEquals( l_map1.values().toArray(), Stream.of( "foo" ).toArray() );
185  }
186 
187 
191  @Test
192  public final void putmultiple()
193  {
194  final Multimap<Integer, String> l_map = HashMultimap.create();
195 
196  new CPutMultiple().execute(
197  false, IContext.EMPTYPLAN,
198  Stream.of( l_map, 1, "xxx", 2, "blub", 3, "xxx", 3, "yyy" ).map( CRawTerm::from ).collect( Collectors.toList() ),
199  Collections.emptyList()
200  );
201 
202  Assert.assertEquals( l_map.size(), 4 );
203  Assert.assertArrayEquals( l_map.keySet().toArray(), Stream.of( 1, 2, 3 ).toArray() );
204  Assert.assertArrayEquals( l_map.values().toArray(), Stream.of( "xxx", "blub", "yyy", "xxx" ).toArray() );
205  }
206 
207 
211  @Test
212  public final void getmultiple()
213  {
214  final Multimap<Integer, String> l_map = HashMultimap.create();
215  final List<ITerm> l_return = new ArrayList<>();
216 
217  l_map.put( 1, "yyy" );
218  l_map.put( 1, "bar" );
219  l_map.put( 2, "test string" );
220  l_map.put( 3, "blub" );
221 
222  new CGetMultiple().execute(
223  false, IContext.EMPTYPLAN,
224  Stream.of( l_map, 1, 2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
225  l_return
226  );
227 
228  Assert.assertEquals( l_return.size(), 2 );
229  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), l_map.asMap().get( 1 ).toArray() );
230  Assert.assertArrayEquals( l_return.get( 1 ).<List<?>>raw().toArray(), l_map.asMap().get( 2 ).toArray() );
231  }
232 
233 
237  @Test
238  public final void getsingle()
239  {
240  final Multimap<Integer, String> l_map1 = HashMultimap.create();
241  final Multimap<Integer, String> l_map2 = HashMultimap.create();
242  final List<ITerm> l_return = new ArrayList<>();
243 
244  l_map1.put( 1, "foo" );
245  l_map1.put( 2, "bar" );
246  l_map2.put( 1, "foobar" );
247 
248  new CGetSingle().execute(
249  false, IContext.EMPTYPLAN,
250  Stream.of( 1, l_map1, l_map2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
251  l_return
252  );
253 
254  Assert.assertEquals( l_return.size(), 2 );
255  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( "foo" ).toArray() );
256  Assert.assertArrayEquals( l_return.get( 1 ).<List<?>>raw().toArray(), Stream.of( "foobar" ).toArray() );
257  }
258 
259 }
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: CAsMap.java:74
adds an single element to multiple map 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
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
common structure for execution definition
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
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
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
execution context with local data
Definition: IContext.java:42
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
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
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: map/CKeys.java:72
< 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
adds an single element to multiple multimap arguments.
term structure for simple datatypes
Definition: CRawTerm.java:45