/
BehaviorsSkillTest.java
349 lines (302 loc) · 12.5 KB
/
BehaviorsSkillTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* $Id$
*
* Janus platform is an open-source multiagent platform.
* More details on http://www.janusproject.io
*
* Copyright (C) 2014-2015 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.janusproject.tests.kernel.bic;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.UUID;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import io.janusproject.kernel.bic.BehaviorsSkill;
import io.janusproject.kernel.bic.InternalEventBusCapacity;
import io.janusproject.kernel.bic.SchedulesSkill;
import io.janusproject.tests.testutils.AbstractJanusTest;
import io.sarl.core.InnerContextAccess;
import io.sarl.core.Schedules;
import io.sarl.lang.core.Address;
import io.sarl.lang.core.Agent;
import io.sarl.lang.core.AgentContext;
import io.sarl.lang.core.AgentTrait;
import io.sarl.lang.core.Behavior;
import io.sarl.lang.core.BuiltinCapacitiesProvider;
import io.sarl.lang.core.Capacities;
import io.sarl.lang.core.Capacity;
import io.sarl.lang.core.Event;
import io.sarl.lang.core.EventListener;
import io.sarl.lang.core.EventSpace;
import io.sarl.lang.core.EventSpaceSpecification;
import io.sarl.lang.core.Scope;
import io.sarl.lang.core.Skill;
import io.sarl.lang.core.SpaceID;
import io.sarl.lang.util.ClearableReference;
import io.sarl.tests.api.Nullable;
import io.sarl.util.Scopes;
/**
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@SuppressWarnings("all")
public class BehaviorsSkillTest extends AbstractJanusTest {
@Nullable
private EventListener eventListener;
@Nullable
private InternalEventBusCapacity busCapacity;
@Nullable
private InnerContextAccess innerCapacity;
@Nullable
private SchedulesSkill schedulesCapacity;
@Nullable
private EventSpace innerSpace;
@Nullable
private Address address;
@Nullable
private BehaviorsSkill skill;
@Nullable
private Skill1 specificCapacity;
@Nullable
private TestAgent agent;
@Before
public void setUp() throws Exception {
this.eventListener = Mockito.mock(EventListener.class);
this.address = new Address(new SpaceID(UUID.randomUUID(), UUID.randomUUID(), EventSpaceSpecification.class),
UUID.randomUUID());
this.busCapacity = Mockito.mock(InternalEventBusCapacity.class);
Mockito.when(this.busCapacity.asEventListener()).thenReturn(this.eventListener);
this.innerSpace = Mockito.mock(EventSpace.class);
Mockito.when(this.innerSpace.getAddress(ArgumentMatchers.any(UUID.class))).thenReturn(this.address);
AgentContext innerContext = Mockito.mock(AgentContext.class);
Mockito.when(innerContext.getDefaultSpace()).thenReturn(this.innerSpace);
this.innerCapacity = Mockito.mock(InnerContextAccess.class);
Mockito.when(this.innerCapacity.getInnerContext()).thenReturn(innerContext);
this.schedulesCapacity = Mockito.mock(SchedulesSkill.class);
this.agent = new TestAgent(this);
this.skill = this.reflect.newInstance(BehaviorsSkill.class, this.agent, this.address);
this.specificCapacity = new Skill1(this.agent);
}
@Test
public void asEventListener() {
assertSame(this.eventListener, this.skill.asEventListener());
}
@Test
public void registerBehavior() {
Behavior b = new TestBehavior(this.agent);
b = spy(b);
assertSame(b, this.skill.registerBehavior(b));
ArgumentCaptor<Behavior> argument1 = ArgumentCaptor.forClass(Behavior.class);
ArgumentCaptor<Boolean> argument2 = ArgumentCaptor.forClass(Boolean.class);
ArgumentCaptor<Function1<? super Event, ? extends Boolean>> argument3 = ArgumentCaptor.forClass(Function1.class);
Mockito.verify(this.busCapacity).registerEventListener(argument1.capture(), argument2.capture(), argument3.capture());
assertSame(b, argument1.getValue());
assertTrue(argument2.getValue());
assertNull(argument3.getValue());
}
@Test
public void registerBehavior_null() {
Behavior b = new TestBehavior(this.agent);
b = spy(b);
assertSame(b, this.skill.registerBehavior(b, null));
ArgumentCaptor<Behavior> argument1 = ArgumentCaptor.forClass(Behavior.class);
ArgumentCaptor<Boolean> argument2 = ArgumentCaptor.forClass(Boolean.class);
ArgumentCaptor<Function1<? super Event, ? extends Boolean>> argument3 = ArgumentCaptor.forClass(Function1.class);
Mockito.verify(this.busCapacity).registerEventListener(argument1.capture(), argument2.capture(), argument3.capture());
assertSame(b, argument1.getValue());
assertTrue(argument2.getValue());
assertNull(argument3.getValue());
}
@Test
public void registerBehavior_validFilter() {
Behavior b = new TestBehavior(this.agent);
b = spy(b);
Function1<? super Event, ? extends Boolean> filter = (event) -> true;
assertSame(b, this.skill.registerBehavior(b, filter));
ArgumentCaptor<Behavior> argument1 = ArgumentCaptor.forClass(Behavior.class);
ArgumentCaptor<Boolean> argument2 = ArgumentCaptor.forClass(Boolean.class);
ArgumentCaptor<Function1<? super Event, ? extends Boolean>> argument3 = ArgumentCaptor.forClass(Function1.class);
Mockito.verify(this.busCapacity).registerEventListener(argument1.capture(), argument2.capture(), argument3.capture());
assertSame(b, argument1.getValue());
assertTrue(argument2.getValue());
assertSame(filter, argument3.getValue());
}
@Test
public void registerBehavior_invalidFilter() {
Behavior b = new TestBehavior(this.agent);
b = spy(b);
Function1<? super Event, ? extends Boolean> filter = (event) -> false;
assertSame(b, this.skill.registerBehavior(b, filter));
ArgumentCaptor<Behavior> argument1 = ArgumentCaptor.forClass(Behavior.class);
ArgumentCaptor<Boolean> argument2 = ArgumentCaptor.forClass(Boolean.class);
ArgumentCaptor<Function1<? super Event, ? extends Boolean>> argument3 = ArgumentCaptor.forClass(Function1.class);
Mockito.verify(this.busCapacity).registerEventListener(argument1.capture(), argument2.capture(), argument3.capture());
assertSame(b, argument1.getValue());
assertTrue(argument2.getValue());
assertSame(filter, argument3.getValue());
}
@Test
public void unregisterBehavior() {
Behavior b = new TestBehavior(this.agent);
b = spy(b);
this.skill.registerBehavior(b);
//
assertSame(b, this.skill.unregisterBehavior(b));
ArgumentCaptor<Behavior> argument1 = ArgumentCaptor.forClass(Behavior.class);
ArgumentCaptor<Boolean> argument2 = ArgumentCaptor.forClass(Boolean.class);
Mockito.verify(this.busCapacity, Mockito.times(1)).unregisterEventListener(argument1.capture(), argument2.capture());
assertSame(b, argument1.getValue());
assertTrue(argument2.getValue());
}
@Test
public void hasRegisteredBehavior() {
Mockito.doReturn(false).when(this.busCapacity).hasRegisteredEventListener(ArgumentMatchers.any());
assertFalse(this.skill.hasRegisteredBehavior());
//
Mockito.doReturn(true).when(this.busCapacity).hasRegisteredEventListener(ArgumentMatchers.any());
assertTrue(this.skill.hasRegisteredBehavior());
}
@Test
public void getRegisteredBehaviors() {
Mockito.doReturn(0).when(this.busCapacity).getRegisteredEventListeners(ArgumentMatchers.any(), ArgumentMatchers.any());
assertContains(this.skill.getRegisteredBehaviors());
//
Object behaviorListener = new TestBehavior(this.agent);
Mockito.doAnswer((it) -> {
((Collection) it.getArgument(1)).add(behaviorListener);
return 1;
}).when(this.busCapacity).getRegisteredEventListeners(ArgumentMatchers.any(), ArgumentMatchers.any());
assertContains(this.skill.getRegisteredBehaviors(), behaviorListener);
}
@Test
public void wake_noScope() {
Event event = mock(Event.class);
this.skill.wake(event);
ArgumentCaptor<UUID> argument1 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Event> argument2 = ArgumentCaptor.forClass(Event.class);
ArgumentCaptor<Scope<Address>> argument3 = ArgumentCaptor.forClass(Scope.class);
Mockito.verify(this.innerSpace).emit(argument1.capture(), argument2.capture(), argument3.capture());
assertEquals(this.agent.getID(), argument1.getValue());
assertSame(event, argument2.getValue());
assertNull(argument3.getValue());
}
@Test
public void wake_scope_all() {
Event event = mock(Event.class);
this.skill.wake(event, Scopes.allParticipants());
ArgumentCaptor<UUID> argument1 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Event> argument2 = ArgumentCaptor.forClass(Event.class);
ArgumentCaptor<Scope<Address>> argument3 = ArgumentCaptor.forClass(Scope.class);
Mockito.verify(this.innerSpace).emit(argument1.capture(), argument2.capture(), argument3.capture());
assertEquals(this.agent.getID(), argument1.getValue());
assertSame(event, argument2.getValue());
assertNotNull(argument3.getValue());
assertSame(Scopes.allParticipants(), argument3.getValue());
}
@Test
public void wake_scope_any() {
Event event = mock(Event.class);
SpaceID spaceID = new SpaceID(UUID.randomUUID(), UUID.randomUUID(), null);
Scope<Address> scope = Scopes.addresses(new Address(spaceID, UUID.randomUUID()));
this.skill.wake(event, scope);
ArgumentCaptor<UUID> argument1 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Event> argument2 = ArgumentCaptor.forClass(Event.class);
ArgumentCaptor<Scope<Address>> argument3 = ArgumentCaptor.forClass(Scope.class);
Mockito.verify(this.innerSpace).emit(argument1.capture(), argument2.capture(), argument3.capture());
assertEquals(this.agent.getID(), argument1.getValue());
assertSame(event, argument2.getValue());
assertNotNull(argument3.getValue());
assertSame(scope, argument3.getValue());
}
@Test
public void contextAwareCapacityCall() throws Exception {
TestBehavior b = new TestBehavior(this.agent);
//
b.runContextAwareCapacityCallTest();
//
assertNotNull(this.specificCapacity.caller);
assertSame(b, this.specificCapacity.caller);
}
public static interface Capacity1 extends Capacity {
void callCapacity();
public static class ContextAwareCapacityWrapper<C extends Capacity1> extends Capacity.ContextAwareCapacityWrapper<C> implements Capacity1 {
public ContextAwareCapacityWrapper(C capacity, AgentTrait caller) {
super(capacity, caller);
}
public void callCapacity() {
try {
ensureCallerInLocalThread();
this.capacity.callCapacity();
} finally {
resetCallerInLocalThread();
}
}
}
}
public static class Skill1 extends Skill implements Capacity1 {
public Object caller;
public Skill1(Agent agent) {
super(agent);
}
@Override
public void callCapacity() {
Object caller = Capacities.getCaller();
this.caller = caller;
}
}
public static class TestAgent extends Agent {
private final BehaviorsSkillTest test;
public TestAgent(BehaviorsSkillTest test) {
super(Mockito.mock(BuiltinCapacitiesProvider.class), UUID.randomUUID(), null);
this.test = test;
}
@Override
protected ClearableReference<Skill> $getSkill(Class<? extends Capacity> capacity) {
if (Capacity1.class.equals(capacity))
return new ClearableReference(this.test.specificCapacity);
if (InternalEventBusCapacity.class.equals(capacity))
return new ClearableReference(this.test.busCapacity);
if (InnerContextAccess.class.equals(capacity))
return new ClearableReference(this.test.innerCapacity);
if (Schedules.class.equals(capacity))
return new ClearableReference(this.test.schedulesCapacity);
return new ClearableReference<>(null);
}
}
public static class TestBehavior extends Behavior {
public TestBehavior(Agent owner) {
super(owner);
}
public void runContextAwareCapacityCallTest() {
Capacity1 skill = getSkill(Capacity1.class);
assertNotNull(skill);
assertInstanceOf(Capacity1.ContextAwareCapacityWrapper.class, skill);
Capacity1 original = ((Capacity1.ContextAwareCapacityWrapper<?>) skill).getDelegate();
assertInstanceOf(Skill1.class, original);
Skill1 skill1 = (Skill1) original;
skill.callCapacity();
assertSame(this, skill1.caller);
}
}
}