1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.krms.test;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
23 import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
24 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
25 import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
26 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
27
28 import java.util.Arrays;
29 import java.util.List;
30
31 import static org.junit.Assert.*;
32 import static org.junit.Assert.assertEquals;
33
34
35
36
37
38
39
40 public class RuleManagementNaturalLanguageUsageTest extends RuleManagementBaseTest {
41 @Override
42 @Before
43 public void setClassDiscriminator() {
44
45 CLASS_DISCRIMINATOR = "RMNLUT";
46 }
47
48
49
50
51
52
53
54 @Test
55 public void testCreateNaturalLanguageUsage() {
56
57 RuleManagementBaseTestObjectNames t0 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t0");
58
59
60 NaturalLanguageUsage usage = buildTestNaturalLanguageUsage(t0.namespaceName, t0.object0 );
61
62
63 usage = ruleManagementService.getNaturalLanguageUsage(t0.nlUsage0_Id);
64 assertEquals("Unexpected Name returned",t0.nlUsage0_Name,usage.getName());
65 assertEquals("Unexpected Namespace returned ", t0.namespaceName, usage.getNamespace());
66 assertEquals("Unexpected Description returned", t0.nlUsage0_Descr, usage.getDescription());
67 assertTrue("Unexpected Active value returned", usage.isActive());
68
69
70 try {
71 ruleManagementService.createNaturalLanguageUsage(null);
72 fail("Should have thrown IllegalArgumentException: naturalLanguageUsage was null");
73 } catch (IllegalArgumentException e) {
74
75 }
76 }
77
78
79
80
81
82
83
84 @Test
85 public void testGetNaturalLanguageUsage() {
86
87 RuleManagementBaseTestObjectNames t1 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t1");
88
89
90 NaturalLanguageUsage usage = buildTestNaturalLanguageUsage(t1.namespaceName, t1.object0 );
91
92
93 assertEquals("Unexpected Description returned",
94 t1.nlUsage0_Descr, ruleManagementService.getNaturalLanguageUsage(t1.nlUsage0_Id).getDescription());
95
96
97 try {
98 ruleManagementService.getNaturalLanguageUsage(null);
99 fail("Should have thrown IllegalArgumentException: naturalLanguageUsageId was null");
100 } catch (IllegalArgumentException e) {
101
102 }
103
104
105 try {
106 ruleManagementService.getNaturalLanguageUsage(" ");
107 fail("Should have thrown IllegalArgumentException: naturalLanguageUsageId was blank");
108 } catch (IllegalArgumentException e) {
109
110 }
111
112
113 assertNull("Should not have return object", ruleManagementService.getNaturalLanguageUsage("badValue"));
114 }
115
116
117
118
119
120
121
122
123 @Test
124 public void testUpdateNaturalLanguageUsage() {
125
126 RuleManagementBaseTestObjectNames t2 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t2");
127
128
129 NaturalLanguageUsage usage = buildTestNaturalLanguageUsage(t2.namespaceName, t2.object0 );
130
131
132 usage = ruleManagementService.getNaturalLanguageUsage(t2.nlUsage0_Id);
133 assertEquals("Unexpected Name returned",t2.nlUsage0_Name,usage.getName());
134 assertEquals("Unexpected Namespace returned ", t2.namespaceName, usage.getNamespace());
135 assertEquals("Unexpected Description returned", t2.nlUsage0_Descr, usage.getDescription());
136 assertTrue("Unexpected Active value returned", usage.isActive());
137
138
139 NaturalLanguageUsage.Builder usageBuilder = NaturalLanguageUsage.Builder.create(usage);
140 usageBuilder.setNamespace(t2.namespaceName + "Changed");
141 usageBuilder.setName(t2.nlUsage0_Name + "Changed");
142 usageBuilder.setDescription(t2.nlUsage0_Descr + "Changed");
143 usageBuilder.setActive(false);
144 ruleManagementService.updateNaturalLanguageUsage(usageBuilder.build());
145
146
147 usage = ruleManagementService.getNaturalLanguageUsage(t2.nlUsage0_Id);
148 assertEquals("Unexpected Name returned",t2.nlUsage0_Name + "Changed",usage.getName());
149 assertEquals("Unexpected Namespace returned ", t2.namespaceName + "Changed", usage.getNamespace());
150 assertEquals("Unexpected Description returned", t2.nlUsage0_Descr + "Changed", usage.getDescription());
151 assertFalse("Unexpected Active value returned", usage.isActive());
152
153
154 try {
155 ruleManagementService.updateNaturalLanguageUsage(null);
156 fail("Should have thrown IllegalArgumentException: naturalLanguageUsage was null");
157 } catch (IllegalArgumentException e) {
158
159 }
160 }
161
162
163
164
165
166
167
168
169 @Test
170 public void testDeleteNaturalLanguageUsage() {
171
172 RuleManagementBaseTestObjectNames t3 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t3");
173
174
175 NaturalLanguageUsage usage = buildTestNaturalLanguageUsage(t3.namespaceName, t3.object0 );
176
177
178 assertEquals("Unexpected Description returned",
179 t3.nlUsage0_Descr, ruleManagementService.getNaturalLanguageUsage(t3.nlUsage0_Id).getDescription());
180
181
182 ruleManagementService.deleteNaturalLanguageUsage(t3.nlUsage0_Id);
183
184
185 assertNull("Should not have returned deleted entry", ruleManagementService.getNaturalLanguageUsage(t3.nlUsage0_Id));
186
187
188 try {
189 ruleManagementService.deleteNaturalLanguageUsage(null);
190 fail("Should have thrown IllegalArgumentException: naturalLanguageUsageId was null");
191 } catch (IllegalArgumentException e) {
192
193 }
194
195
196 try {
197 ruleManagementService.deleteNaturalLanguageUsage(" ");
198 fail("Should have thrown IllegalArgumentException: naturalLanguageUsageId was blank");
199 } catch (IllegalArgumentException e) {
200
201 }
202
203
204 try {
205 ruleManagementService.deleteNaturalLanguageUsage("badValue");
206 fail("Should have thrown IllegalStateException: the NaturalLanguageUsage to delete does not exists: badValue");
207 } catch (IllegalStateException e) {
208
209 }
210 }
211
212
213
214
215
216
217
218
219 @Test
220 public void testGetNaturalLanguageUsagesByNamespace() {
221
222 RuleManagementBaseTestObjectNames t4 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t4");
223
224
225 buildTestNaturalLanguageUsage(t4.namespaceName, t4.object0 );
226 buildTestNaturalLanguageUsage(t4.namespaceName, t4.object1 );
227 buildTestNaturalLanguageUsage(t4.namespaceName, t4.object2 );
228 List<String> usageIds = Arrays.asList(t4.nlUsage0_Id, t4.nlUsage1_Id, t4.nlUsage2_Id);
229
230
231 List<NaturalLanguageUsage> usages = ruleManagementService.getNaturalLanguageUsagesByNamespace(t4.namespaceName);
232
233 assertEquals("Should have returned 3 entries",3,usages.size());
234 for (NaturalLanguageUsage usage : usages) {
235 assertTrue("Unexpected entry id returned", usageIds.contains(usage.getId()));
236 }
237
238
239 try {
240 ruleManagementService.getNaturalLanguageUsagesByNamespace(null);
241 fail("Should have thrown IllegalArgumentException: namespace is null or blank");
242 } catch (IllegalArgumentException e) {
243
244 }
245
246
247 try {
248 ruleManagementService.getNaturalLanguageUsagesByNamespace(" ");
249 fail("Should have thrown IllegalArgumentException: namespace is null or blank");
250 } catch (IllegalArgumentException e) {
251
252 }
253
254
255 assertEquals("Should have returned 0 entries", 0, ruleManagementService.getNaturalLanguageUsagesByNamespace(
256 "badValue").size());
257 }
258
259
260
261
262
263
264
265
266 @Test
267 public void testGetNaturalLanguageUsageByNameAndNamespace() {
268
269 RuleManagementBaseTestObjectNames t5 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t5");
270
271
272 buildTestNaturalLanguageUsage(t5.namespaceName, t5.object0 );
273 buildTestNaturalLanguageUsage(t5.namespaceName, t5.object1 );
274
275 assertEquals("Unexpected Description on entry returned",
276 t5.nlUsage0_Descr, ruleManagementService.getNaturalLanguageUsageByNameAndNamespace(
277 t5.nlUsage0_Name,t5.namespaceName).getDescription());
278
279
280 try {
281 ruleManagementService.getNaturalLanguageUsageByNameAndNamespace(null, t5.namespaceName);
282 fail("Should have thrown RiceIllegalArgumentException: name was a null or blank value");
283 } catch (RiceIllegalArgumentException e) {
284
285 }
286
287
288 try {
289 ruleManagementService.getNaturalLanguageUsageByNameAndNamespace(" ",t5.namespaceName);
290 fail("Should have thrown RiceIllegalArgumentException: name was a null or blank value");
291 } catch (RiceIllegalArgumentException e) {
292
293 }
294
295
296 assertNull("Should not have return object",
297 ruleManagementService.getNaturalLanguageUsageByNameAndNamespace("badValue", t5.namespaceName));
298
299
300 try {
301 ruleManagementService.getNaturalLanguageUsageByNameAndNamespace(t5.nlUsage0_Name,null);
302 fail("Should have thrown RiceIllegalArgumentException: namespace was a null or blank value");
303 } catch (RiceIllegalArgumentException e) {
304
305 }
306
307
308 try {
309 ruleManagementService.getNaturalLanguageUsageByNameAndNamespace(t5.nlUsage0_Name," ");
310 fail("Should have thrown RiceIllegalArgumentException: namespace was a null or blank value");
311 } catch (RiceIllegalArgumentException e) {
312
313 }
314
315
316 assertNull("Should not have return object",
317 ruleManagementService.getNaturalLanguageUsageByNameAndNamespace(t5.nlUsage0_Name,"badValue"));
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 @Test
334 public void testTranslateNaturalLanguageForObject() {
335
336 RuleManagementBaseTestObjectNames t6 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t6");
337
338
339 PropositionDefinition propositionDefinition = createTestPropositionForTranslation(t6.object0, t6.namespaceName,
340 "proposition");
341 NaturalLanguageTemplate template = createTestNaturalLanguageTemplate(t6.namespaceName, "sw", "proposition",
342 "Detta ändamål får inte vara inaktiv", true);
343
344
345 String translation = ruleManagementService.translateNaturalLanguageForObject(
346 template.getNaturalLanguageUsageId(),"proposition",propositionDefinition.getId(),"sw");
347
348
349 assertEquals("Unexpected translation returned","Detta ändamål får inte vara inaktiv. ",translation);
350
351
352 assertEquals("Should have returned '. ' String", ". ",
353 ruleManagementService.translateNaturalLanguageForObject(null, "proposition",
354 propositionDefinition.getId(), "sw"));
355
356
357 try {
358 ruleManagementService.translateNaturalLanguageForObject(
359 template.getNaturalLanguageUsageId(),null,propositionDefinition.getId(),"sw");
360 fail("Should have thrown NullPointerException");
361 } catch (NullPointerException e) {
362
363 }
364
365
366 try {
367 ruleManagementService.translateNaturalLanguageForObject(
368 template.getNaturalLanguageUsageId(),"proposition",null,"sw");
369 fail("Should have thrown RiceIllegalArgumentException: Proposition id must not be null or blank");
370 } catch (RiceIllegalArgumentException e) {
371
372 }
373
374
375 try {
376 ruleManagementService.translateNaturalLanguageForObject(
377 template.getNaturalLanguageUsageId(),"proposition",propositionDefinition.getId(),null);
378 fail("Should have thrown IllegalArgumentException: languageCode is null or blank");
379 } catch (IllegalArgumentException e) {
380
381 }
382
383
384 assertEquals("Should have returned '. ' String", ". ", ruleManagementService.translateNaturalLanguageForObject(
385 " ","proposition",propositionDefinition.getId(),"sw"));
386
387
388
389 assertEquals("Should have returned empty String", StringUtils.EMPTY,
390 ruleManagementService.translateNaturalLanguageForObject(template.getNaturalLanguageUsageId(),
391 " ", propositionDefinition.getId(), "sw"));
392
393
394
395 try {
396 ruleManagementService.translateNaturalLanguageForObject(
397 template.getNaturalLanguageUsageId(),"proposition"," ","sw");
398 fail("Should have thrown RiceIllegalArgumentException: Proposition id must not be null or blank");
399 } catch (RiceIllegalArgumentException e) {
400
401 }
402
403
404 try {
405 ruleManagementService.translateNaturalLanguageForObject(
406 template.getNaturalLanguageUsageId(),"proposition",propositionDefinition.getId()," ");
407 fail("Should have thrown IllegalArgumentException: languageCode is null or blank");
408 } catch (IllegalArgumentException e) {
409
410 }
411
412
413 assertEquals("Should have returned '. ' String", ". ",
414 ruleManagementService.translateNaturalLanguageForObject("badValue", "proposition",
415 propositionDefinition.getId(), "sw"));
416
417
418 assertEquals("Should have returned empty String", StringUtils.EMPTY,
419 ruleManagementService.translateNaturalLanguageForObject(template.getNaturalLanguageUsageId(),
420 "badValue", propositionDefinition.getId(), "sw"));
421
422
423 try {
424 ruleManagementService.translateNaturalLanguageForObject(
425 template.getNaturalLanguageUsageId(),"proposition","badValue","sw");
426 fail("Should have thrown RiceIllegalArgumentException: badValue is not an Id for a proposition");
427 } catch (RiceIllegalArgumentException e) {
428
429 }
430
431
432 assertEquals("Should have returned '. ' String", ". ", ruleManagementService.translateNaturalLanguageForObject(
433 template.getNaturalLanguageUsageId(),"proposition",propositionDefinition.getId(),"xx"));
434 }
435
436
437
438
439
440
441
442
443
444
445 @Test
446 public void testTranslateNaturalLanguageForProposition() {
447
448 RuleManagementBaseTestObjectNames t7 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t7");
449
450 PropositionDefinition propositionDefinition = createTestPropositionForTranslation(t7.object0, t7.namespaceName, "proposition" );
451 NaturalLanguageTemplate template = createTestNaturalLanguageTemplate(t7.namespaceName, "tr", "proposition",
452 "Bu nesne inaktif olmamalidir", true);
453
454 String translation = ruleManagementService.translateNaturalLanguageForProposition(template.getNaturalLanguageUsageId(),propositionDefinition,"tr");
455
456 assertEquals("Unexpected translation returned","Bu nesne inaktif olmamalidir. ",translation);
457
458
459 assertEquals("Should have returned '. ' String",". ", ruleManagementService.translateNaturalLanguageForProposition(null, propositionDefinition, "tr"));
460
461
462 try {
463 ruleManagementService.translateNaturalLanguageForProposition(
464 template.getNaturalLanguageUsageId(), null, "tr");
465 fail("Should have thrown NullPointerException");
466 } catch (NullPointerException e) {
467
468 }
469
470
471 try {
472 ruleManagementService.translateNaturalLanguageForProposition(template.getNaturalLanguageUsageId(),
473 propositionDefinition, null);
474 fail("Should have thrown IllegalArgumentException: languageCode is null or blank");
475 } catch (IllegalArgumentException e) {
476
477 }
478
479
480 assertEquals("Should have returned '. ' String", ". ",
481 ruleManagementService.translateNaturalLanguageForProposition(" ", propositionDefinition, "tr"));
482
483
484 try {
485 ruleManagementService.translateNaturalLanguageForProposition(template.getNaturalLanguageUsageId(),propositionDefinition," ");
486 fail("Should have thrown IllegalArgumentException: languageCode is null or blank");
487 } catch (IllegalArgumentException e) {
488
489 }
490
491
492 assertEquals("Should have returned '. ' String",". ",
493 ruleManagementService.translateNaturalLanguageForProposition("badValue",propositionDefinition,"tr"));
494
495
496 assertEquals("Should have returned '. ' String",". ",
497 ruleManagementService.translateNaturalLanguageForProposition(
498 template.getNaturalLanguageUsageId(),propositionDefinition,"badValue"));
499 }
500
501
502
503
504
505
506
507
508
509
510 @Test
511 public void testTranslateNaturalLanguageTreeForProposition() {
512
513 RuleManagementBaseTestObjectNames t8 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t8");
514
515
516 PropositionDefinition propositionDefinition = createTestPropositionForTranslation(t8.object0, t8.namespaceName, "proposition" );
517 NaturalLanguageTemplate template = createTestNaturalLanguageTemplate(t8.namespaceName, "cy", "proposition",
518 "Ni ddylai hyn fod yn segur, Gwrthrych", true);
519
520 NaturalLanguageTree naturalLanguageTree = ruleManagementService.translateNaturalLanguageTreeForProposition(
521 template.getNaturalLanguageUsageId(), propositionDefinition, "cy");
522
523 String translation = naturalLanguageTree.getNaturalLanguage();
524 assertEquals("Unexpected translation returned","Ni ddylai hyn fod yn segur, Gwrthrych",translation);
525
526
527 assertNull("Should have returned null",naturalLanguageTree.getChildren());
528 }
529
530
531
532
533
534
535
536
537
538
539 @Test
540 public void testCompoundTranslateNaturalLanguageTreeForProposition() {
541
542 RuleManagementBaseTestObjectNames t9 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t9");
543
544
545 PropositionDefinition propositionDefinition = createTestCompoundProposition(t9);
546
547
548 NaturalLanguageTemplate template = createTestNaturalLanguageTemplate(t9.namespaceName, "ms",
549 krmsTypeRepository.getTypeById(propositionDefinition.getTypeId()).getName(),
550 "Objek ini tidak boleh aktif", true);
551 PropositionDefinition child1 = propositionDefinition.getCompoundComponents().get(0);
552 createTestNaturalLanguageTemplate(t9.namespaceName, "ms", "Account", "Objek ini tidak boleh aktif",
553 "krms.nl.proposition", true);
554 PropositionDefinition child2 = propositionDefinition.getCompoundComponents().get(1);
555 createTestNaturalLanguageTemplate(t9.namespaceName, "ms", "Occasion", "Objek ini tidak boleh aktif",
556 "krms.nl.proposition", true);
557
558
559 NaturalLanguageTree naturalLanguageTree = ruleManagementService.translateNaturalLanguageTreeForProposition(
560 template.getNaturalLanguageUsageId(), propositionDefinition, "ms");
561 List<NaturalLanguageTree> naturalLanguageTrees = naturalLanguageTree.getChildren();
562 assertEquals("Should have found 2 child entries",2,naturalLanguageTrees.size());
563
564
565 try {
566 ruleManagementService.translateNaturalLanguageTreeForProposition(
567 template.getNaturalLanguageUsageId(), null, "ms");
568 fail("Should have thrown NullPointerException");
569 } catch (NullPointerException e) {
570
571 }
572
573
574 try {
575 ruleManagementService.translateNaturalLanguageTreeForProposition(
576 template.getNaturalLanguageUsageId(), propositionDefinition, null);
577 fail("Should have thrown IllegalArgumentException: languageCode is null or blank");
578 } catch (IllegalArgumentException e) {
579
580 }
581 }
582
583
584
585
586
587
588
589
590
591
592
593 @Test
594 public void testNullTranslateNaturalLanguageTreeForProposition() {
595
596 RuleManagementBaseTestObjectNames t9 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t9");
597
598
599 PropositionDefinition propositionDefinition = createTestPropositionForTranslation(t9.object0, t9.namespaceName, "proposition" );
600
601 NaturalLanguageTree naturalLanguageTree = ruleManagementService.translateNaturalLanguageTreeForProposition(
602 null, propositionDefinition, "cy2");
603
604 String translation = naturalLanguageTree.getNaturalLanguage();
605 assertEquals("Non-empty translation returned",StringUtils.EMPTY,translation);
606
607
608 assertNull("Should have returned null",naturalLanguageTree.getChildren());
609 }
610
611
612
613
614
615
616
617
618
619
620 @Test
621 public void testEmptyTranslateNaturalLanguageTreeForProposition() {
622
623 RuleManagementBaseTestObjectNames t10 = new RuleManagementBaseTestObjectNames( CLASS_DISCRIMINATOR, "t10");
624
625
626 PropositionDefinition propositionDefinition = createTestPropositionForTranslation(t10.object0, t10.namespaceName, "proposition2" );
627 NaturalLanguageTemplate template = createTestNaturalLanguageTemplate(t10.namespaceName, "cy", "proposition2",
628 "Ddylai hyn fod yn segur, Gwrthrych", true);
629
630 NaturalLanguageTree naturalLanguageTree = ruleManagementService.translateNaturalLanguageTreeForProposition(
631 template.getNaturalLanguageUsageId(), propositionDefinition, "en");
632
633 String translation = naturalLanguageTree.getNaturalLanguage();
634 assertEquals("Non-empty translation returned",StringUtils.EMPTY,translation);
635
636
637 assertNull("Should have returned null",naturalLanguageTree.getChildren());
638 }
639 }