-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ResourceInjectionTest.java
512 lines (438 loc) · 15.5 KB
/
ResourceInjectionTest.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
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.proc;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.proc.BasicContext;
import org.neo4j.kernel.api.proc.CallableProcedure;
import org.neo4j.kernel.api.proc.CallableUserAggregationFunction;
import org.neo4j.kernel.api.proc.CallableUserFunction;
import org.neo4j.logging.Log;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Procedure;
import org.neo4j.procedure.UserAggregationFunction;
import org.neo4j.procedure.UserAggregationResult;
import org.neo4j.procedure.UserAggregationUpdate;
import org.neo4j.procedure.UserFunction;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@SuppressWarnings( "WeakerAccess" )
public class ResourceInjectionTest
{
@Rule
public ExpectedException exception = ExpectedException.none();
private ReflectiveProcedureCompiler compiler;
private Log log = mock(Log.class);
@Before
public void setUp()
{
ComponentRegistry safeComponents = new ComponentRegistry();
ComponentRegistry allComponents = new ComponentRegistry();
safeComponents.register( MyAwesomeAPI.class, ( ctx ) -> new MyAwesomeAPI() );
allComponents.register( MyAwesomeAPI.class, ( ctx ) -> new MyAwesomeAPI() );
allComponents.register( MyUnsafeAPI.class, ( ctx ) -> new MyUnsafeAPI() );
compiler = new ReflectiveProcedureCompiler( new TypeMappers(), safeComponents, allComponents, log,
ProcedureConfig.DEFAULT );
}
@Test
public void shouldCompileAndRunProcedure() throws Throwable
{
// Given
CallableProcedure proc =
compiler.compileProcedure( ProcedureWithInjectedAPI.class, Optional.empty(), true ).get( 0 );
// Then
List<Object[]> out = Iterators.asList( proc.apply( new BasicContext(), new Object[0] ) );
// Then
assertThat( out.get( 0 ), equalTo( (new Object[]{"Bonnie"}) ) );
assertThat( out.get( 1 ), equalTo( (new Object[]{"Clyde"}) ) );
}
@Test
public void shouldFailNicelyWhenUnknownAPI() throws Throwable
{
//When
exception.expect( ProcedureException.class );
exception.expectMessage( "Unable to set up injection for procedure `ProcedureWithUnknownAPI`, " +
"the field `api` has type `class org.neo4j.kernel.impl.proc.ResourceInjectionTest$UnknownAPI` " +
"which is not a known injectable component." );
// Then
compiler.compileProcedure( ProcedureWithUnknownAPI.class, Optional.empty(), true );
}
@Test
public void shouldCompileAndRunUnsafeProcedureUnsafeMode() throws Throwable
{
// Given
CallableProcedure proc =
compiler.compileProcedure( ProcedureWithUnsafeAPI.class, Optional.empty(), true ).get( 0 );
// Then
List<Object[]> out = Iterators.asList( proc.apply( new BasicContext(), new Object[0] ) );
// Then
assertThat( out.get( 0 ), equalTo( (new Object[]{"Morpheus"}) ) );
assertThat( out.get( 1 ), equalTo( (new Object[]{"Trinity"}) ) );
assertThat( out.get( 2 ), equalTo( (new Object[]{"Neo"}) ) );
assertThat( out.get( 3 ), equalTo( (new Object[]{"Emil"}) ) );
}
@Test
public void shouldFailNicelyWhenUnsafeAPISafeMode() throws Throwable
{
//When
List<CallableProcedure> procList =
compiler.compileProcedure( ProcedureWithUnsafeAPI.class, Optional.empty(), false );
verify( log )
.warn( "org.neo4j.kernel.impl.proc.listCoolPeople is not " +
"available due to not having unrestricted access rights, check configuration." );
assertThat( procList.size(), equalTo( 1 ) );
try
{
procList.get( 0 ).apply( new BasicContext(), new Object[0] );
fail();
}
catch ( ProcedureException e )
{
assertThat( e.getMessage(), containsString(
"org.neo4j.kernel.impl.proc.listCoolPeople is not " +
"available due to not having unrestricted access rights, check configuration." ) );
}
}
@Test
public void shouldCompileAndRunUserFunctions() throws Throwable
{
// Given
CallableUserFunction proc =
compiler.compileFunction( FunctionWithInjectedAPI.class).get( 0 );
// When
Object out = proc.apply( new BasicContext(), new Object[0] );
// Then
assertThat( out, equalTo( "[Bonnie, Clyde]" ) );
}
@Test
public void shouldFailNicelyWhenFunctionUsesUnknownAPI() throws Throwable
{
//When
exception.expect( ProcedureException.class );
exception.expectMessage( "Unable to set up injection for procedure `FunctionWithUnknownAPI`, " +
"the field `api` has type `class org.neo4j.kernel.impl.proc.ResourceInjectionTest$UnknownAPI` " +
"which is not a known injectable component." );
// Then
compiler.compileFunction( FunctionWithUnknownAPI.class );
}
@Test
public void shouldFailNicelyWhenUnsafeAPISafeModeFunction() throws Throwable
{
//When
List<CallableUserFunction> procList =
compiler.compileFunction( FunctionWithUnsafeAPI.class);
verify( log )
.warn( "org.neo4j.kernel.impl.proc.listCoolPeople is not " +
"available due to not having unrestricted access rights, check configuration." );
assertThat( procList.size(), equalTo( 1 ) );
try
{
procList.get( 0 ).apply( new BasicContext(), new Object[0] );
fail();
}
catch ( ProcedureException e )
{
assertThat( e.getMessage(), containsString(
"org.neo4j.kernel.impl.proc.listCoolPeople is not " +
"available due to not having unrestricted access rights, check configuration." ) );
}
}
@Test
public void shouldCompileAndRunUserAggregationFunctions() throws Throwable
{
// Given
CallableUserAggregationFunction proc =
compiler.compileAggregationFunction( AggregationFunctionWithInjectedAPI.class).get( 0 );
// When
proc.create( new BasicContext() ).update( new Object[]{} );
Object out = proc.create( new BasicContext() ).result();
// Then
assertThat( out, equalTo( "[Bonnie, Clyde]" ) );
}
@Test
public void shouldFailNicelyWhenAggregationFunctionUsesUnknownAPI() throws Throwable
{
//When
exception.expect( ProcedureException.class );
exception.expectMessage( "Unable to set up injection for procedure `AggregationFunctionWithUnknownAPI`, " +
"the field `api` has type `class org.neo4j.kernel.impl.proc.ResourceInjectionTest$UnknownAPI` " +
"which is not a known injectable component." );
// Then
compiler.compileAggregationFunction( AggregationFunctionWithUnknownAPI.class );
}
@Test
public void shouldFailNicelyWhenUnsafeAPISafeModeAggregationFunction() throws Throwable
{
//When
List<CallableUserAggregationFunction> procList =
compiler.compileAggregationFunction( AggregationFunctionWithUnsafeAPI.class);
verify( log )
.warn( "org.neo4j.kernel.impl.proc.listCoolPeople is not " +
"available due to not having unrestricted access rights, check configuration." );
assertThat( procList.size(), equalTo( 1 ) );
try
{
procList.get(0).create( new BasicContext() ).update( new Object[]{} );
Object out = procList.get(0).create( new BasicContext() ).result();
fail();
}
catch ( ProcedureException e )
{
assertThat( e.getMessage(), containsString(
"org.neo4j.kernel.impl.proc.listCoolPeople is not " +
"available due to not having unrestricted access rights, check configuration." ) );
}
}
@Test
public void shouldFailNicelyWhenAllUsesUnsafeAPI() throws Throwable
{
//When
compiler.compileFunction( FunctionsAndProcedureUnsafe.class );
compiler.compileProcedure( FunctionsAndProcedureUnsafe.class, Optional.empty(), false );
compiler.compileAggregationFunction( FunctionsAndProcedureUnsafe.class );
// Then
verify( log )
.warn( "org.neo4j.kernel.impl.proc.listCoolPeople is not " +
"available due to not having unrestricted access rights, check configuration." );
verify( log )
.warn( "org.neo4j.kernel.impl.proc.safeUserFunctionInUnsafeAPIClass is not " +
"available due to not having unrestricted access rights, check configuration." );
verify( log )
.warn( "org.neo4j.kernel.impl.proc.listCoolPeopleProcedure is not " +
"available due to not having unrestricted access rights, check configuration." );
}
public static class MyOutputRecord
{
public String name;
public MyOutputRecord( String name )
{
this.name = name;
}
}
public static class MyAwesomeAPI
{
List<String> listCoolPeople()
{
return asList( "Bonnie", "Clyde" );
}
}
public static class UnknownAPI
{
List<String> listCoolPeople()
{
return singletonList( "booh!" );
}
}
public static class MyUnsafeAPI
{
List<String> listCoolPeople()
{
return asList( "Morpheus", "Trinity", "Neo", "Emil" );
}
}
public static class ProcedureWithInjectedAPI
{
@Context
public MyAwesomeAPI api;
@Procedure
public Stream<MyOutputRecord> listCoolPeople()
{
return api.listCoolPeople().stream().map( MyOutputRecord::new );
}
}
public static class FunctionWithInjectedAPI
{
@Context
public MyAwesomeAPI api;
@UserFunction
public String listCoolPeople()
{
return api.listCoolPeople().toString();
}
}
public static class AggregationFunctionWithInjectedAPI
{
@Context
public MyAwesomeAPI api;
@UserAggregationFunction
public VoidOutput listCoolPeople()
{
return new VoidOutput(api);
}
public static class VoidOutput
{
private MyAwesomeAPI api;
public VoidOutput(MyAwesomeAPI api){
this.api=api;
}
@UserAggregationUpdate
public void update()
{
}
@UserAggregationResult
public String result()
{
return api.listCoolPeople().toString();
}
}
}
public static class ProcedureWithUnknownAPI
{
@Context
public UnknownAPI api;
@Procedure
public Stream<MyOutputRecord> listCoolPeople()
{
return api.listCoolPeople().stream().map( MyOutputRecord::new );
}
}
public static class FunctionWithUnknownAPI
{
@Context
public UnknownAPI api;
@UserFunction
public String listCoolPeople()
{
return api.listCoolPeople().toString();
}
}
public static class AggregationFunctionWithUnknownAPI
{
@Context
public UnknownAPI api;
@UserAggregationFunction
public VoidOutput listCoolPeople()
{
return new VoidOutput(api);
}
public static class VoidOutput
{
private UnknownAPI api;
public VoidOutput(UnknownAPI api){
this.api=api;
}
@UserAggregationUpdate
public void update()
{
}
@UserAggregationResult
public String result()
{
return api.listCoolPeople().toString();
}
}
}
public static class ProcedureWithUnsafeAPI
{
@Context
public MyUnsafeAPI api;
@Procedure
public Stream<MyOutputRecord> listCoolPeople()
{
return api.listCoolPeople().stream().map( MyOutputRecord::new );
}
}
public static class FunctionWithUnsafeAPI
{
@Context
public MyUnsafeAPI api;
@UserFunction
public String listCoolPeople()
{
return api.listCoolPeople().toString();
}
}
public static class AggregationFunctionWithUnsafeAPI
{
@Context
public MyUnsafeAPI api;
@UserAggregationFunction
public VoidOutput listCoolPeople()
{
return new VoidOutput(api);
}
public static class VoidOutput
{
private MyUnsafeAPI api;
public VoidOutput(MyUnsafeAPI api){
this.api=api;
}
@UserAggregationUpdate
public void update()
{
}
@UserAggregationResult
public String result()
{
return api.listCoolPeople().toString();
}
}
}
public static class FunctionsAndProcedureUnsafe
{
@Context
public MyUnsafeAPI api;
@UserAggregationFunction
public VoidOutput listCoolPeople()
{
return new VoidOutput( api );
}
public static class VoidOutput
{
private MyUnsafeAPI api;
public VoidOutput( MyUnsafeAPI api )
{
this.api = api;
}
@UserAggregationUpdate
public void update()
{
}
@UserAggregationResult
public String result()
{
return api.listCoolPeople().toString();
}
}
@Procedure
public Stream<MyOutputRecord> listCoolPeopleProcedure()
{
return api.listCoolPeople().stream().map( MyOutputRecord::new );
}
@UserFunction
public String safeUserFunctionInUnsafeAPIClass()
{
return "a safe function";
}
}
}