-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
InterceptorServiceTest.java
716 lines (570 loc) · 22.6 KB
/
InterceptorServiceTest.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
package ca.uhn.fhir.interceptor.executor;
import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.HookParams;
import ca.uhn.fhir.interceptor.api.IAnonymousInterceptor;
import ca.uhn.fhir.interceptor.api.IPointcut;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.rest.server.exceptions.AuthenticationException;
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class InterceptorServiceTest {
private final List<String> myInvocations = new ArrayList<>();
@Test
public void testInterceptorWithAnnotationDefinedOnInterface() {
InterceptorService svc = new InterceptorService();
TestInterceptorWithAnnotationDefinedOnInterface_Class interceptor = new TestInterceptorWithAnnotationDefinedOnInterface_Class();
svc.registerInterceptor(interceptor);
assertEquals(1, interceptor.getRegisterCount());
}
@Test
public void testInterceptorThrowsException() {
class InterceptorThrowingException {
@Hook(Pointcut.TEST_RB)
public void test(String theValue) {
throw new AuthenticationException(theValue);
}
}
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(new InterceptorThrowingException());
try {
svc.callHooks(Pointcut.TEST_RB, new HookParams("A MESSAGE", "B"));
fail();
} catch (AuthenticationException e) {
assertEquals("A MESSAGE", e.getMessage());
}
}
@Test
public void testInterceptorReturnsClass() {
class InterceptorReturningClass {
private BaseServerResponseException myNextResponse;
@Hook(Pointcut.TEST_RO)
public BaseServerResponseException hook() {
return myNextResponse;
}
}
InterceptorReturningClass interceptor0 = new InterceptorReturningClass();
InterceptorReturningClass interceptor1 = new InterceptorReturningClass();
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(interceptor0);
svc.registerInterceptor(interceptor1);
interceptor0.myNextResponse = new InvalidRequestException("0");
interceptor1.myNextResponse = new InvalidRequestException("1");
Object response = svc.callHooksAndReturnObject(Pointcut.TEST_RO, new HookParams("", ""));
assertEquals("0", ((InvalidRequestException) response).getMessage());
interceptor0.myNextResponse = null;
response = svc.callHooksAndReturnObject(Pointcut.TEST_RO, new HookParams("", ""));
assertEquals("1", ((InvalidRequestException) response).getMessage());
}
/**
* Hook methods with private access are ignored
*/
@Test
public void testInterceptorWithPrivateAccessHookMethod() {
class InterceptorThrowingException {
@Hook(Pointcut.TEST_RB)
private void test(String theValue) {
throw new AuthenticationException(theValue);
}
}
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(new InterceptorThrowingException());
// Should not fail
svc.callHooks(Pointcut.TEST_RB, new HookParams("A MESSAGE", "B"));
}
@Test
public void testInterceptorWithDefaultAccessHookMethod() {
class InterceptorThrowingException {
@Hook(Pointcut.TEST_RB)
void test(String theValue) {
throw new AuthenticationException(theValue);
}
}
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(new InterceptorThrowingException());
try {
svc.callHooks(Pointcut.TEST_RB, new HookParams("A MESSAGE", "B"));
fail();
} catch (AuthenticationException e) {
assertEquals("A MESSAGE", e.getMessage());
}
}
@Test
public void testInterceptorWithInheritedHookMethod() {
class InterceptorThrowingException {
@Hook(Pointcut.TEST_RB)
void test(String theValue) {
throw new AuthenticationException(theValue);
}
}
class InterceptorThrowingException2 extends InterceptorThrowingException {
// nothing
}
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(new InterceptorThrowingException2());
try {
svc.callHooks(Pointcut.TEST_RB, new HookParams("A MESSAGE", "B"));
fail();
} catch (AuthenticationException e) {
assertEquals("A MESSAGE", e.getMessage());
}
}
@Test
public void testInterceptorWithNoHooks() {
class InterceptorWithNoHooks {
// nothing
}
InterceptorService svc = new InterceptorService();
svc.setWarnOnInterceptorWithNoHooks(false);
boolean outcome = svc.registerInterceptor(new InterceptorWithNoHooks());
assertFalse(outcome);
}
@Test
public void testRegisterHookFails() {
InterceptorService svc = new InterceptorService();
int initialSize = svc.getGlobalInterceptorsForUnitTest().size();
try {
svc.registerInterceptor(new InterceptorThatFailsOnRegister());
fail();
} catch (InternalErrorException e) {
// good
}
assertThat(svc.getGlobalInterceptorsForUnitTest()).hasSize(initialSize);
}
@Test
public void testManuallyRegisterInterceptor() {
InterceptorService svc = new InterceptorService();
// Registered in opposite order to verify that the order on the annotation is used
MyTestInterceptorTwo interceptor1 = new MyTestInterceptorTwo();
MyTestInterceptorOne interceptor0 = new MyTestInterceptorOne();
assertFalse(svc.hasHooks(Pointcut.TEST_RB));
svc.registerInterceptor(interceptor1);
assertTrue(svc.hasHooks(Pointcut.TEST_RB));
svc.registerInterceptor(interceptor0);
assertTrue(svc.hasHooks(Pointcut.TEST_RB));
// Register the manual interceptor (has Order right in the middle)
MyTestInterceptorManual myInterceptorManual = new MyTestInterceptorManual();
svc.registerInterceptor(myInterceptorManual);
List<Object> globalInterceptors = svc.getGlobalInterceptorsForUnitTest();
assertThat(globalInterceptors).hasSize(3);
assertInstanceOf(MyTestInterceptorOne.class, globalInterceptors.get(0), globalInterceptors.get(0).getClass().toString());
assertInstanceOf(MyTestInterceptorManual.class, globalInterceptors.get(1), globalInterceptors.get(1).getClass().toString());
assertInstanceOf(MyTestInterceptorTwo.class, globalInterceptors.get(2), globalInterceptors.get(2).getClass().toString());
// Try to register again (should have no effect
svc.registerInterceptor(myInterceptorManual);
globalInterceptors = svc.getGlobalInterceptorsForUnitTest();
assertThat(globalInterceptors).hasSize(3);
assertInstanceOf(MyTestInterceptorOne.class, globalInterceptors.get(0), globalInterceptors.get(0).getClass().toString());
assertInstanceOf(MyTestInterceptorManual.class, globalInterceptors.get(1), globalInterceptors.get(1).getClass().toString());
assertInstanceOf(MyTestInterceptorTwo.class, globalInterceptors.get(2), globalInterceptors.get(2).getClass().toString());
// Make sure we have the right invokers in the right order
List<Object> invokers = svc.getInterceptorsWithInvokersForPointcut(Pointcut.TEST_RB);
assertThat(invokers.get(0)).isSameAs(interceptor0);
assertThat(invokers.get(1)).isSameAs(myInterceptorManual);
assertThat(invokers.get(2)).isSameAs(interceptor1);
// Finally, unregister it
svc.unregisterInterceptor(myInterceptorManual);
globalInterceptors = svc.getGlobalInterceptorsForUnitTest();
assertThat(globalInterceptors).hasSize(2);
assertInstanceOf(MyTestInterceptorOne.class, globalInterceptors.get(0), globalInterceptors.get(0).getClass().toString());
assertInstanceOf(MyTestInterceptorTwo.class, globalInterceptors.get(1), globalInterceptors.get(1).getClass().toString());
// Unregister the two others
assertTrue(svc.hasHooks(Pointcut.TEST_RB));
svc.unregisterInterceptor(interceptor1);
assertTrue(svc.hasHooks(Pointcut.TEST_RB));
svc.unregisterInterceptor(interceptor0);
assertFalse(svc.hasHooks(Pointcut.TEST_RB));
}
@Test
public void testInvokeGlobalInterceptorMethods() {
InterceptorService svc = new InterceptorService();
// Registered in opposite order to verify that the order on the annotation is used
MyTestInterceptorTwo interceptor1 = new MyTestInterceptorTwo();
MyTestInterceptorOne interceptor0 = new MyTestInterceptorOne();
svc.registerInterceptor(interceptor1);
svc.registerInterceptor(interceptor0);
if (svc.hasHooks(Pointcut.TEST_RB)) {
boolean outcome = svc.callHooks(Pointcut.TEST_RB, new HookParams("A", "B"));
assertTrue(outcome);
}
assertThat(myInvocations).containsExactly("MyTestInterceptorOne.testRb", "MyTestInterceptorTwo.testRb");
assertThat(interceptor0.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString1).isSameAs("B");
}
@Test
public void testInvokeAnonymousInterceptorMethods() {
InterceptorService svc = new InterceptorService();
MyTestAnonymousInterceptorOne interceptor0 = new MyTestAnonymousInterceptorOne();
MyTestAnonymousInterceptorTwo interceptor1 = new MyTestAnonymousInterceptorTwo();
svc.registerAnonymousInterceptor(Pointcut.TEST_RB, interceptor0);
svc.registerAnonymousInterceptor(Pointcut.TEST_RB, interceptor1);
if (svc.hasHooks(Pointcut.TEST_RB)) {
boolean outcome = svc.callHooks(Pointcut.TEST_RB, new HookParams("A", "B"));
assertTrue(outcome);
}
assertThat(myInvocations).containsExactly("MyTestAnonymousInterceptorOne.testRb", "MyTestAnonymousInterceptorTwo.testRb");
assertThat(interceptor0.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString1).isSameAs("B");
}
@Test
public void testInvokeUsingSupplierArg() {
InterceptorService svc = new InterceptorService();
MyTestInterceptorOne interceptor0 = new MyTestInterceptorOne();
MyTestInterceptorTwo interceptor1 = new MyTestInterceptorTwo();
svc.registerInterceptor(interceptor0);
svc.registerInterceptor(interceptor1);
boolean outcome = svc.callHooks(Pointcut.TEST_RB, new HookParams("A", "B"));
assertTrue(outcome);
assertThat(myInvocations).containsExactly("MyTestInterceptorOne.testRb", "MyTestInterceptorTwo.testRb");
assertThat(interceptor0.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString1).isSameAs("B");
}
@Test
public void testInvokeGlobalInterceptorMethods_MethodAbortsProcessing() {
InterceptorService svc = new InterceptorService();
MyTestInterceptorOne interceptor0 = new MyTestInterceptorOne();
MyTestInterceptorTwo interceptor1 = new MyTestInterceptorTwo();
svc.registerInterceptor(interceptor0);
svc.registerInterceptor(interceptor1);
interceptor0.myNextReturn = false;
boolean outcome = svc.callHooks(Pointcut.TEST_RB, new HookParams("A", "B"));
assertFalse(outcome);
assertThat(myInvocations).containsExactly("MyTestInterceptorOne.testRb");
assertThat(interceptor0.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString0).isSameAs(null);
assertThat(interceptor1.myLastString1).isSameAs(null);
}
@Test
public void testCallHooksInvokedWithNullParameters() {
InterceptorService svc = new InterceptorService();
class NullParameterInterceptor {
private String myValue0 = "";
private String myValue1 = "";
@Hook(Pointcut.TEST_RB)
public void hook(String theValue0, String theValue1) {
myValue0 = theValue0;
myValue1 = theValue1;
}
}
NullParameterInterceptor interceptor;
HookParams params;
// Both null
interceptor = new NullParameterInterceptor();
svc.registerInterceptor(interceptor);
params = new HookParams()
.add(String.class, null)
.add(String.class, null);
svc.callHooks(Pointcut.TEST_RB, params);
assertNull(interceptor.myValue0);
assertNull(interceptor.myValue1);
svc.unregisterAllInterceptors();
// First null
interceptor = new NullParameterInterceptor();
svc.registerInterceptor(interceptor);
params = new HookParams()
.add(String.class, null)
.add(String.class, "A");
svc.callHooks(Pointcut.TEST_RB, params);
assertNull(interceptor.myValue0);
assertEquals("A", interceptor.myValue1);
svc.unregisterAllInterceptors();
// Second null
interceptor = new NullParameterInterceptor();
svc.registerInterceptor(interceptor);
params = new HookParams()
.add(String.class, "A")
.add(String.class, null);
svc.callHooks(Pointcut.TEST_RB, params);
assertEquals("A", interceptor.myValue0);
assertNull(interceptor.myValue1);
svc.unregisterAllInterceptors();
}
@Test
public void testCallHooksLogAndSwallowException() {
InterceptorService svc = new InterceptorService();
class LogAndSwallowInterceptor0 {
private boolean myHit;
@Hook(Pointcut.TEST_RB)
public void hook(String theValue0, String theValue1) {
myHit = true;
throw new IllegalStateException();
}
}
LogAndSwallowInterceptor0 interceptor0 = new LogAndSwallowInterceptor0();
svc.registerInterceptor(interceptor0);
class LogAndSwallowInterceptor1 {
private boolean myHit;
@Hook(Pointcut.TEST_RB)
public void hook(String theValue0, String theValue1) {
myHit = true;
throw new IllegalStateException();
}
}
LogAndSwallowInterceptor1 interceptor1 = new LogAndSwallowInterceptor1();
svc.registerInterceptor(interceptor1);
class LogAndSwallowInterceptor2 {
private boolean myHit;
@Hook(Pointcut.TEST_RB)
public void hook(String theValue0, String theValue1) {
myHit = true;
throw new NullPointerException("AAA");
}
}
LogAndSwallowInterceptor2 interceptor2 = new LogAndSwallowInterceptor2();
svc.registerInterceptor(interceptor2);
HookParams params = new HookParams()
.add(String.class, null)
.add(String.class, null);
try {
svc.callHooks(Pointcut.TEST_RB, params);
fail();
} catch (NullPointerException e) {
assertEquals("AAA", e.getMessage());
}
assertTrue(interceptor0.myHit);
assertTrue(interceptor1.myHit);
assertTrue(interceptor2.myHit);
}
@Test
public void testCallHooksInvokedWithWrongParameters() {
InterceptorService svc = new InterceptorService();
Integer msg = 123;
CanonicalSubscription subs = new CanonicalSubscription();
HookParams params = new HookParams(msg, subs);
try {
svc.callHooks(Pointcut.TEST_RB, params);
fail();
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("Invalid params for pointcut " + Pointcut.TEST_RB + " - Wanted java.lang.String,java.lang.String but found ");
}
}
@Test
public void testValidateParamTypes() {
InterceptorService svc = new InterceptorService();
HookParams params = new HookParams();
params.add(String.class, "A");
params.add(String.class, "B");
boolean validated = svc.haveAppropriateParams(Pointcut.TEST_RB, params);
assertTrue(validated);
}
@Test
public void testValidateParamTypesMissingParam() {
InterceptorService svc = new InterceptorService();
HookParams params = new HookParams();
params.add(String.class, "A");
try {
svc.haveAppropriateParams(Pointcut.TEST_RB, params);
fail();
} catch (IllegalArgumentException e) {
assertEquals(Msg.code(1909) + "Wrong number of params for pointcut " + Pointcut.TEST_RB + " - Wanted java.lang.String,java.lang.String but found [String]", e.getMessage());
}
}
@Test
public void testValidateParamTypesExtraParam() {
InterceptorService svc = new InterceptorService();
HookParams params = new HookParams();
params.add(String.class, "A");
params.add(String.class, "B");
params.add(String.class, "C");
params.add(String.class, "D");
params.add(String.class, "E");
params.add(String.class, "F");
params.add(String.class, "G");
try {
svc.haveAppropriateParams(Pointcut.STORAGE_PRECOMMIT_RESOURCE_UPDATED, params);
fail();
} catch (IllegalArgumentException e) {
assertEquals(Msg.code(1909) + "Wrong number of params for pointcut STORAGE_PRECOMMIT_RESOURCE_UPDATED - Wanted ca.uhn.fhir.rest.api.InterceptorInvocationTimingEnum,ca.uhn.fhir.rest.api.server.RequestDetails,ca.uhn.fhir.rest.api.server.storage.TransactionDetails,ca.uhn.fhir.rest.server.servlet.ServletRequestDetails,org.hl7.fhir.instance.model.api.IBaseResource,org.hl7.fhir.instance.model.api.IBaseResource but found [String, String, String, String, String, String, String]", e.getMessage());
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testValidateParamTypesWrongParam() {
InterceptorService svc = new InterceptorService();
HookParams params = new HookParams();
params.add((Class) String.class, 1);
params.add((Class) String.class, 2);
params.add((Class) String.class, 3);
params.add((Class) String.class, 4);
params.add((Class) String.class, 5);
params.add((Class) String.class, 6);
try {
svc.haveAppropriateParams(Pointcut.STORAGE_PRECOMMIT_RESOURCE_UPDATED, params);
fail();
} catch (IllegalArgumentException e) {
assertEquals("Invalid params for pointcut " + Pointcut.STORAGE_PRECOMMIT_RESOURCE_UPDATED + " - class java.lang.Integer is not of type class java.lang.String", e.getMessage());
}
}
/**
* Verify the ifPresent methods match the base behaviour.
*/
@Nested
class SupplierDefaultMethods {
HookParams params = new HookParams("1", "2");
@Test
void testBooleanWithNoHooks_returnsTrue() {
InterceptorService svc = new InterceptorService();
assertTrue(svc.callHooks(Pointcut.TEST_RB, params));
assertTrue(svc.ifHasCallHooks(Pointcut.TEST_RB, () -> params));
}
@Test
void testBooleanWithAllHooksReturnTrue_returnsTrue() {
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(new BooleanHook(true));
svc.registerInterceptor(new BooleanHook(true));
assertTrue(svc.callHooks(Pointcut.TEST_RB, params));
assertTrue(svc.ifHasCallHooks(Pointcut.TEST_RB, () -> params));
}
@Test
void testBooleanWithAHookReturnFalse_returnsFalse() {
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(new BooleanHook(true));
svc.registerInterceptor(new BooleanHook(false));
svc.registerInterceptor(new BooleanHook(true));
assertFalse(svc.callHooks(Pointcut.TEST_RB, params));
assertFalse(svc.ifHasCallHooks(Pointcut.TEST_RB, () -> params));
}
@Test
void testObjectWithNoHooks_returnsNull() {
InterceptorService svc = new InterceptorService();
assertNull(svc.callHooksAndReturnObject(Pointcut.TEST_RO, params));
assertNull(svc.ifHasCallHooksAndReturnObject(Pointcut.TEST_RO, () -> params));
}
@Test
void testObjectWithAllHooksReturnNull_returnsNull() {
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(new ObjectHook<>(null));
svc.registerInterceptor(new ObjectHook<>(null));
assertNull(svc.callHooksAndReturnObject(Pointcut.TEST_RO, params));
assertNull(svc.ifHasCallHooksAndReturnObject(Pointcut.TEST_RO, () -> params));
}
@Test
void testObjectWithAHookReturnValue_returnsFirstValue() {
InterceptorService svc = new InterceptorService();
svc.registerInterceptor(new ObjectHook<>(null));
svc.registerInterceptor(new ObjectHook<>(new ResourceNotFoundException("first")));
svc.registerInterceptor(new ObjectHook<>(new ResourceNotFoundException("second")));
assertEquals("first", ((BaseServerResponseException) svc.callHooksAndReturnObject(Pointcut.TEST_RO, params)).getMessage());
assertEquals("first", ((BaseServerResponseException) svc.ifHasCallHooksAndReturnObject(Pointcut.TEST_RO, () -> params)).getMessage());
}
static class BooleanHook {
final boolean myResult;
BooleanHook(boolean theResult) {
myResult = theResult;
}
@Hook(Pointcut.TEST_RB)
boolean doIt() {
return myResult;
}
}
static class ObjectHook<T extends BaseServerResponseException> {
final T myResult;
ObjectHook(T theResult) {
myResult = theResult;
}
@Hook(Pointcut.TEST_RO)
T doIt() {
return myResult;
}
}
}
@BeforeEach
public void before() {
myInvocations.clear();
}
interface TestInterceptorWithAnnotationDefinedOnInterface_Interface {
@Hook(Pointcut.INTERCEPTOR_REGISTERED)
void registered();
}
@Interceptor(order = 100)
public class MyTestInterceptorOne {
private String myLastString0;
private boolean myNextReturn = true;
public MyTestInterceptorOne() {
super();
}
@Hook(Pointcut.TEST_RB)
public boolean testRb(String theString0) {
myLastString0 = theString0;
myInvocations.add("MyTestInterceptorOne.testRb");
return myNextReturn;
}
}
@Interceptor(order = 300)
public class MyTestInterceptorTwo {
private String myLastString0;
private String myLastString1;
@Hook(Pointcut.TEST_RB)
public boolean testRb(String theString0, String theString1) {
myLastString0 = theString0;
myLastString1 = theString1;
myInvocations.add("MyTestInterceptorTwo.testRb");
return true;
}
}
public class MyTestAnonymousInterceptorOne implements IAnonymousInterceptor {
private String myLastString0;
@Override
public void invoke(IPointcut thePointcut, HookParams theArgs) {
myLastString0 = theArgs.get(String.class, 0);
myInvocations.add("MyTestAnonymousInterceptorOne.testRb");
}
}
public class MyTestAnonymousInterceptorTwo implements IAnonymousInterceptor {
private String myLastString0;
private String myLastString1;
@Override
public void invoke(IPointcut thePointcut, HookParams theArgs) {
myLastString0 = theArgs.get(String.class, 0);
myLastString1 = theArgs.get(String.class, 1);
myInvocations.add("MyTestAnonymousInterceptorTwo.testRb");
}
}
@Interceptor(order = 200)
public class MyTestInterceptorManual {
@Hook(Pointcut.TEST_RB)
public void testRb() {
myInvocations.add("MyTestInterceptorManual.testRb");
}
}
public static class TestInterceptorWithAnnotationDefinedOnInterface_Class implements TestInterceptorWithAnnotationDefinedOnInterface_Interface {
private int myRegisterCount = 0;
public int getRegisterCount() {
return myRegisterCount;
}
@Override
public void registered() {
myRegisterCount++;
}
}
/**
* Just a make-believe version of this class for the unit test
*/
private static class CanonicalSubscription {
}
@Interceptor()
public static class InterceptorThatFailsOnRegister {
@Hook(Pointcut.INTERCEPTOR_REGISTERED)
public void start() throws Exception {
throw new Exception("InterceptorThatFailsOnRegister FAILED!");
}
}
}