-
Notifications
You must be signed in to change notification settings - Fork 27
/
DroolsAttributesTestCase.java
397 lines (332 loc) · 14.3 KB
/
DroolsAttributesTestCase.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package com.wordpress.ezegrande.drools.attributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.drools.core.time.SessionPseudoClock;
import org.junit.Assert;
import org.junit.Test;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wordpress.ezegrande.drools.attributes.model.Gift;
import com.wordpress.ezegrande.drools.attributes.model.Person;
import com.wordpress.ezegrande.drools.util.TestUtil;
/**
* Tests cases for the Business Rules included in the project drools-attributes.
* The goal of these rules is to explain by examples the available Rules
* attributes.
*
* @author ezegrande
* @see http://ezegrande.wordpress.com/2015/01/21/drools-rules-attributes
*/
public class DroolsAttributesTestCase {
private static final String FIRST = "FIRST";
private static final String SECOND = "SECOND";
private static final String THIRD = "THIRD";
private static final String ME = "me";
private static final String JOHN = "John";
private static final String CHOCOLATES = "Chocolates";
private Logger logger = LoggerFactory.getLogger(DroolsAttributesTestCase.class);
private static final String DRL_PATH_DIALECT = "dialect.drl";
private static final String DRL_PATH_DATE_EFFECTIVE = "date-effective.drl";
private static final String DRL_PATH_DATE_EXPIRES = "date-expires.drl";
private static final String DRL_PATH_DURATION = "duration.drl";
private static final String DRL_PATH_SALIENCE = "salience.drl";
private static final String DRL_PATH_NO_LOOP_DISABLED = "no-loop-disabled.drl";
private static final String DRL_PATH_NO_LOOP_DISABLED_NO_MODIFY = "no-loop-disabled-no-modify.drl";
private static final String DRL_PATH_NO_LOOP_ENABLED = "no-loop-enabled.drl";
/**
* Tests the rules included in the file dialect.drl.
*
* @see: http://mvel.codehaus.org/MVEL+2.0+Block+WITH+Operator
*/
@Test
public void testDialect() {
List<Person> adults = new ArrayList<Person>();
List<Gift> gifts = new ArrayList<Gift>();
// Create the Stateless Session
StatelessKieSession session = TestUtil.createStatelessKieSession(DRL_PATH_DIALECT);
// Add Global Variables
session.setGlobal("adults", adults);
session.setGlobal("gifts", gifts);
Person child = new Person(JOHN, 4);
Person adult = new Person("Peter", 22);
// Execute the StatelessSession
session.execute(Arrays.asList(child, adult));
// Assert Java rule
Assert.assertEquals(1, adults.size());
Assert.assertEquals(adult, adults.get(0));
// Asert MVEL rule
Assert.assertEquals(1, gifts.size());
Assert.assertEquals(JOHN, gifts.get(0).getRecipient());
}
/**
* Tests the rules included in the file date-effective.drl.<br />
* <b>Note:</b>in Drools the date format dd-mmm-yyyy is supported by
* default. It can be overridden by the system property drools.dateformat
*/
@Test
public void testDateEffective() {
List<Gift> gifts = new ArrayList<Gift>();
// Create the Stateless Session
StatelessKieSession session = TestUtil.createStatelessKieSession(DRL_PATH_DATE_EFFECTIVE);
// Add Global Variables
session.setGlobal("gifts", gifts);
// Insert one Person, so the rules conditions are met
Person john = new Person(JOHN, 30);
// Execute the StatelessSession
session.execute(john);
// Assert that the only gift given are the Chocolates, so only 1 rule
// was triggered
Assert.assertEquals(1, gifts.size());
Gift chocolates = gifts.get(0);
Assert.assertEquals(CHOCOLATES, chocolates.getDescription());
Assert.assertEquals(john.getName(), chocolates.getRecipient());
}
/**
* Tests the rules included in the file date-expires.drl.<br />
* <b>Note:</b>in Drools the date format dd-mmm-yyyy is supported by
* default. It can be overridden by the system property drools.dateformat
*/
@Test
public void testDateExpires() {
List<Gift> gifts = new ArrayList<Gift>();
// Create the Stateless Session
StatelessKieSession session = TestUtil.createStatelessKieSession(DRL_PATH_DATE_EXPIRES);
// Add Global Variables
session.setGlobal("gifts", gifts);
// Insert one Person, so the rules conditions are met
Person john = new Person(JOHN, 30);
// Execute the StatelessSession
session.execute(john);
// Assert that the only gift given are the Fruits
Assert.assertEquals(1, gifts.size());
Gift fruits = gifts.get(0);
Assert.assertEquals("Fruits", fruits.getDescription());
Assert.assertEquals(john.getName(), fruits.getRecipient());
}
/**
* Tests the rules included in the file duration.drl<br />
* A Stateful Session with Pseudo clock is used, in order to emulate the
* flow of time.
*
* @throws InterruptedException
*/
@Test
public void testDuration() throws InterruptedException {
// Create the Stateful Session
KieSession session = TestUtil.createKieSession(true, DRL_PATH_DURATION);
List<Gift> gifts = new ArrayList<Gift>();
try {
// Add Global Variables
session.setGlobal("gifts", gifts);
// Insert one Person, so the rules conditions are met
Person john = new Person(JOHN, 30);
// Retrieve the pseudo clock so we can advance time for testing the
// duration attribute
SessionPseudoClock clock = session.getSessionClock();
// Execute the Stateful Session
session.insert(john);
session.fireAllRules();
// Assert that the rule did NOT get fired
Assert.assertEquals(0, gifts.size());
clock.advanceTime(5, TimeUnit.SECONDS);
session.fireAllRules();
// Assert that the rule DID get fired
Assert.assertEquals(1, gifts.size());
Gift chocolates = gifts.get(0);
Assert.assertEquals(CHOCOLATES, chocolates.getDescription());
Assert.assertEquals(john.getName(), chocolates.getRecipient());
} finally {
if (session != null) {
session.dispose();
}
}
}
/**
* Tests the rules included in the file duration.drl<br />
* A Stateful Session with Pseudo clock is used, in order to emulate the
* flow of time.
*/
@Test
public void testDuration_NotMet() {
List<Gift> gifts = new ArrayList<Gift>();
// Create the Stateful Session
KieSession session = TestUtil.createKieSession(true, DRL_PATH_DURATION);
try {
// Add Global Variables
session.setGlobal("gifts", gifts);
// Insert one Person, so the rules conditions are met
Person john = new Person(JOHN, 30);
// Retrieve the pseudo clock so we can advance time for testing the
// duration attribute
SessionPseudoClock clock = session.getSessionClock();
// Insert the Object and Fire all rules
FactHandle johnFactHandle = session.insert(john);
session.fireAllRules();
clock.advanceTime(4, TimeUnit.SECONDS);
// Remove the object from the memory
session.delete(johnFactHandle);
clock.advanceTime(1, TimeUnit.SECONDS);
// Let's fire rules again after 5s (duration of the rule)
session.fireAllRules();
// Assert that the rule did NOT get fired
Assert.assertEquals(0, gifts.size());
} finally {
if (session != null) {
session.dispose();
}
}
}
/**
* Tests the rules included in the file salience.drl<br />
*/
@Test
public void testSalience() {
List<String> messages = new ArrayList<String>();
// Create the Stateless Session
StatelessKieSession session = TestUtil.createStatelessKieSession(DRL_PATH_SALIENCE);
// Add Global Variables
session.setGlobal("messages", messages);
session.execute("stringFact");
// Assert that the rule did NOT get fired
Assert.assertEquals(3, messages.size());
Assert.assertEquals(FIRST, messages.get(0));
Assert.assertEquals(SECOND, messages.get(1));
Assert.assertEquals(THIRD, messages.get(2));
}
/**
* Tests the rules included in the file noloop-disabled-no-modify.drl, which
* does not use the modify clause. Note that the engine does not get
* notified of the modification and because of this the rule is not
* evaluated again.<br />
*
*/
@Test
public void testNoLoop_Disabled_NoModify() {
// Create the Stateless Session
StatelessKieSession session = TestUtil.createStatelessKieSession(DRL_PATH_NO_LOOP_DISABLED_NO_MODIFY);
LoopCountAgendaEventListener listener = new LoopCountAgendaEventListener();
session.addEventListener(listener);
session.setGlobal("logger", logger);
Gift chocolateForJohn = new Gift(CHOCOLATES, JOHN);
session.execute(chocolateForJohn);
// Assert that the rule ran more than one time
Assert.assertEquals(1, listener.getMatchCount());
// Assert that the Gift object was modified by the rule
Assert.assertEquals(ME, chocolateForJohn.getRecipient());
}
/**
* Tests the rules included in the file noloop-disabled.drl, using a
* Stateless Session<br />
*
* @throws InterruptedException
*/
@Test
public void testNoLoop_Disabled_Stateless() throws InterruptedException {
// Create the Stateless Session
final StatelessKieSession session = TestUtil.createStatelessKieSession(DRL_PATH_NO_LOOP_DISABLED);
LoopCountAgendaEventListener listener = new LoopCountAgendaEventListener();
session.addEventListener(listener);
session.setGlobal("logger", logger);
final Gift chocolateForJohn = new Gift(CHOCOLATES, JOHN);
// Fire the rules in a separate thread, which will be stacked in an
// infinite loop firing itself
Thread executeSessionThread = new Thread() {
@Override
public void run() {
session.execute(chocolateForJohn);
}
};
executeSessionThread.start();
Thread.sleep(2 * 1000); // Give time to the rule to run in an infinite
// loop
// Assert that the rule ran more than one time
Assert.assertTrue(listener.getMatchCount() > 1);
// Assert that the Gift object was modified by the rule
Assert.assertEquals(ME, chocolateForJohn.getRecipient());
}
/**
* Tests the rules included in the file noloop-disabled.drl, using a
* Stateful Session<br />
*
* @throws InterruptedException
*/
@Test
public void testNoLoop_Disabled_Stateful() throws InterruptedException {
// Create the Stateful Session
final KieSession session = TestUtil.createKieSession(DRL_PATH_NO_LOOP_DISABLED);
try {
LoopCountAgendaEventListener listener = new LoopCountAgendaEventListener();
session.addEventListener(listener);
session.setGlobal("logger", logger);
final Gift chocolateForJohn = new Gift(CHOCOLATES, JOHN);
session.insert(chocolateForJohn);
// Fire the rules in a separate thread, which will be stacked in an
// infinite loop firing itself
Thread executeSessionThread = new Thread() {
@Override
public void run() {
session.fireAllRules();
}
};
executeSessionThread.start();
Thread.sleep(2 * 1000); // Give time to the rule to run in an
// infinite loop
// Assert that the rule ran more than one time
Assert.assertTrue(listener.getMatchCount() > 1);
// Assert that the Gift object was modified by the rule
Assert.assertEquals(ME, chocolateForJohn.getRecipient());
} finally {
if (session != null) {
session.dispose();
}
}
}
/**
* Tests the rules included in the file noloop-enabled.drl, using a
* Stateless Session<br />
*/
@Test
public void testNoLoop_Enabled_Stateless() {
// Create the Stateless Session
final StatelessKieSession session = TestUtil.createStatelessKieSession(DRL_PATH_NO_LOOP_ENABLED);
LoopCountAgendaEventListener listener = new LoopCountAgendaEventListener();
session.addEventListener(listener);
session.setGlobal("logger", logger);
final Gift chocolateForJohn = new Gift(CHOCOLATES, JOHN);
session.execute(chocolateForJohn);
// Assert that the rule only fired once
Assert.assertEquals(1, listener.getMatchCount());
// Assert that the Gift object was modified by the rule
Assert.assertEquals(ME, chocolateForJohn.getRecipient());
}
/**
* Tests the rules included in the file noloop-enabled.drl, using a Stateful
* session<br />
*/
@Test
public void testNoLoop_Enabled_Stateful() {
// Create the Stateful Session
final KieSession session = TestUtil.createKieSession(DRL_PATH_NO_LOOP_ENABLED);
try {
LoopCountAgendaEventListener listener = new LoopCountAgendaEventListener();
session.addEventListener(listener);
session.setGlobal("logger", logger);
final Gift chocolateForJohn = new Gift(CHOCOLATES, JOHN);
session.insert(chocolateForJohn);
session.fireAllRules();
// Assert that the rule only fired once
Assert.assertEquals(1, listener.getMatchCount());
// Assert that the Gift object was modified by the rule
Assert.assertEquals(ME, chocolateForJohn.getRecipient());
} finally {
if (session != null) {
session.dispose();
}
}
}
}