-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ProcedureJarLoaderTest.java
292 lines (246 loc) · 10.1 KB
/
ProcedureJarLoaderTest.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
/*
* 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.hamcrest.core.IsEqual;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
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.ProcedureSignature;
import org.neo4j.logging.NullLog;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
import static org.neo4j.helpers.collection.Iterators.asList;
import static org.neo4j.kernel.api.proc.Neo4jTypes.NTInteger;
import static org.neo4j.kernel.api.proc.ProcedureSignature.procedureSignature;
public class ProcedureJarLoaderTest
{
@Rule
public TemporaryFolder tmpdir = new TemporaryFolder();
@Rule
public ExpectedException exception = ExpectedException.none();
private final ProcedureJarLoader jarloader =
new ProcedureJarLoader( new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(),
new ComponentRegistry(), NullLog.getInstance(), ProcedureAllowedConfig.DEFAULT ), NullLog.getInstance() );
@Test
public void shouldLoadProcedureFromJar() throws Throwable
{
// Given
URL jar = createJarFor( ClassWithOneProcedure.class );
// When
List<CallableProcedure> procedures = jarloader.loadProcedures( jar ).procedures();
// Then
List<ProcedureSignature> signatures = procedures.stream().map( CallableProcedure::signature ).collect( toList() );
assertThat( signatures, contains(
procedureSignature( "org","neo4j", "kernel", "impl", "proc", "myProcedure" ).out( "someNumber", NTInteger ).build() ));
assertThat( asList( procedures.get( 0 ).apply( new BasicContext(), new Object[0] ) ),
contains( IsEqual.equalTo( new Object[]{1337L} )) );
}
@Test
public void shouldLoadProcedureWithArgumentFromJar() throws Throwable
{
// Given
URL jar = createJarFor( ClassWithProcedureWithArgument.class );
// When
List<CallableProcedure> procedures = jarloader.loadProcedures( jar ).procedures();
// Then
List<ProcedureSignature> signatures = procedures.stream().map( CallableProcedure::signature ).collect( toList() );
assertThat( signatures, contains(
procedureSignature( "org","neo4j", "kernel", "impl", "proc", "myProcedure" )
.in( "value", NTInteger )
.out( "someNumber", NTInteger )
.build() ));
assertThat( asList(procedures.get( 0 ).apply( new BasicContext(), new Object[]{42L} ) ),
contains( IsEqual.equalTo( new Object[]{42L} )) );
}
@Test
public void shouldLoadProcedureFromJarWithMultipleProcedureClasses() throws Throwable
{
// Given
URL jar = createJarFor( ClassWithOneProcedure.class, ClassWithAnotherProcedure.class, ClassWithNoProcedureAtAll.class );
// When
List<CallableProcedure> procedures = jarloader.loadProcedures( jar ).procedures();
// Then
List<ProcedureSignature> signatures = procedures.stream().map( CallableProcedure::signature ).collect( toList() );
assertThat( signatures, containsInAnyOrder(
procedureSignature( "org","neo4j", "kernel", "impl", "proc", "myOtherProcedure" ).out( "someNumber", NTInteger ).build(),
procedureSignature( "org","neo4j", "kernel", "impl", "proc", "myProcedure" ).out( "someNumber", NTInteger ).build() ));
}
@Test
public void shouldGiveHelpfulErrorOnInvalidProcedure() throws Throwable
{
// Given
URL jar = createJarFor( ClassWithOneProcedure.class, ClassWithInvalidProcedure.class );
// Expect
exception.expect( ProcedureException.class );
exception.expectMessage( String.format("Procedures must return a Stream of records, where a record is a concrete class%n" +
"that you define, with public non-final fields defining the fields in the record.%n" +
"If you''d like your procedure to return `boolean`, you could define a record class " +
"like:%n" +
"public class Output '{'%n" +
" public boolean out;%n" +
"'}'%n" +
"%n" +
"And then define your procedure as returning `Stream<Output>`." ));
// When
jarloader.loadProcedures( jar );
}
@Test
public void shouldLoadProceduresFromDirectory() throws Throwable
{
// Given
createJarFor( ClassWithOneProcedure.class );
createJarFor( ClassWithAnotherProcedure.class );
// When
List<CallableProcedure> procedures = jarloader.loadProceduresFromDir( tmpdir.getRoot() ).procedures();
// Then
List<ProcedureSignature> signatures = procedures.stream().map( CallableProcedure::signature ).collect( toList() );
assertThat( signatures, containsInAnyOrder(
procedureSignature( "org","neo4j", "kernel", "impl", "proc", "myOtherProcedure" ).out( "someNumber", NTInteger ).build(),
procedureSignature( "org","neo4j", "kernel", "impl", "proc", "myProcedure" ).out( "someNumber", NTInteger ).build() ));
}
@Test
public void shouldGiveHelpfulErrorOnWildCardProcedure() throws Throwable
{
// Given
URL jar = createJarFor( ClassWithWildCardStream.class );
// Expect
exception.expect( ProcedureException.class );
exception.expectMessage( String.format("Procedures must return a Stream of records, where a record is a concrete class%n" +
"that you define and not a Stream<?>." ));
// When
jarloader.loadProcedures( jar );
}
@Test
public void shouldGiveHelpfulErrorOnRawStreamProcedure() throws Throwable
{
// Given
URL jar = createJarFor( ClassWithRawStream.class );
// Expect
exception.expect( ProcedureException.class );
exception.expectMessage( String.format("Procedures must return a Stream of records, where a record is a concrete class%n" +
"that you define and not a raw Stream." ));
// When
jarloader.loadProcedures( jar );
}
@Test
public void shouldGiveHelpfulErrorOnGenericStreamProcedure() throws Throwable
{
// Given
URL jar = createJarFor( ClassWithGenericStream.class );
// Expect
exception.expect( ProcedureException.class );
exception.expectMessage( String.format("Procedures must return a Stream of records, where a record is a concrete class%n" +
"that you define and not a parameterized type such as java.util.List<org.neo4j" +
".kernel.impl.proc.ProcedureJarLoaderTest$Output>."));
// When
jarloader.loadProcedures( jar );
}
public URL createJarFor( Class<?> ... targets ) throws IOException
{
return new JarBuilder().createJarFor( tmpdir.newFile( new Random().nextInt() + ".jar" ), targets );
}
public static class Output
{
public long someNumber = 1337;
public Output()
{
}
public Output( long anotherNumber )
{
this.someNumber = anotherNumber;
}
}
public static class ClassWithInvalidProcedure
{
@Procedure
public boolean booleansAreNotAcceptableReturnTypes()
{
return false;
}
}
public static class ClassWithOneProcedure
{
@Procedure
public Stream<Output> myProcedure()
{
return Stream.of( new Output() );
}
}
public static class ClassWithNoProcedureAtAll
{
void thisMethodIsEntirelyUnrelatedToAllThisExcitement()
{
}
}
public static class ClassWithAnotherProcedure
{
@Procedure
public Stream<Output> myOtherProcedure()
{
return Stream.of( new Output() );
}
}
public static class ClassWithProcedureWithArgument
{
@Procedure
public Stream<Output> myProcedure(@Name( "value" ) long value)
{
return Stream.of( new Output(value) );
}
}
public static class ClassWithWildCardStream
{
@Procedure
public Stream<?> wildCardProc()
{
return Stream.of( new Output() );
}
}
public static class ClassWithRawStream
{
@Procedure
public Stream rawStreamProc()
{
return Stream.of( new Output() );
}
}
public static class ClassWithGenericStream
{
@Procedure
public Stream<List<Output>> genericStream()
{
return Stream.of( Collections.singletonList( new Output() ));
}
}
}