-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AutoIndexIT.java
434 lines (391 loc) · 14.8 KB
/
AutoIndexIT.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.server.rest;
import org.junit.Test;
import java.util.List;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.ResourceIterable;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
import org.neo4j.server.rest.domain.JsonHelper;
import org.neo4j.server.rest.domain.JsonParseException;
import org.neo4j.server.rest.web.RestfulGraphDatabase;
import org.neo4j.test.GraphDescription.Graph;
import org.neo4j.test.GraphDescription.NODE;
import org.neo4j.test.GraphDescription.PROP;
import org.neo4j.test.GraphDescription.REL;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class AutoIndexIT extends AbstractRestFunctionalTestBase
{
/**
* Find node by query from an automatic index.
* <p/>
* See Find node by query for the actual query syntax.
*/
@Test
@Graph( nodes = {@NODE( name = "I", setNameProperty = true )}, autoIndexNodes = true )
public void shouldRetrieveFromAutoIndexByQuery()
{
data.get();
assertSize( 1, gen.get()
.expectedStatus( 200 )
.get( nodeAutoIndexUri() + "?query=name:I" )
.entity() );
}
private String nodeAutoIndexUri()
{
return getDataUri() + "index/auto/node/";
}
/**
* Automatic index nodes can be found via exact lookups with normal Index
* REST syntax.
*/
@Test
@Graph( nodes = {@NODE( name = "I", setNameProperty = true )}, autoIndexNodes = true )
public void find_node_by_exact_match_from_an_automatic_index()
{
data.get();
assertSize( 1, gen.get()
.expectedStatus( 200 )
.get( nodeAutoIndexUri() + "name/I" )
.entity() );
}
/**
* The automatic relationship index can not be removed.
*/
@Test
@Graph( nodes = {@NODE( name = "I", setNameProperty = true )}, autoIndexNodes = true )
public void Relationship_AutoIndex_is_not_removable()
{
data.get();
gen.get()
.expectedStatus( 405 )
.delete( relationshipAutoIndexUri() )
.entity();
}
/**
* The automatic node index can not be removed.
*/
@Test
@Graph( nodes = {@NODE( name = "I", setNameProperty = true )}, autoIndexNodes = true )
public void AutoIndex_is_not_removable()
{
gen.get()
.expectedStatus( 405 )
.delete( nodeAutoIndexUri() )
.entity();
}
/**
* It is not allowed to add items manually to automatic indexes.
*/
@Test
@Graph( nodes = {@NODE( name = "I", setNameProperty = true )}, autoIndexNodes = true )
public void items_can_not_be_added_manually_to_an_AutoIndex()
{
data.get();
String indexName;
try ( Transaction tx = graphdb().beginTx() )
{
indexName = graphdb().index().getNodeAutoIndexer().getAutoIndex().getName();
tx.success();
}
gen.get()
.expectedStatus( 405 )
.payload( createJsonStringFor( getNodeUri( data.get()
.get( "I" ) ), "name", "I" ) )
.post( postNodeIndexUri( indexName ) )
.entity();
}
private String createJsonStringFor( final String targetUri, final String key, final String value )
{
return "{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"uri\": \"" + targetUri + "\"}";
}
/**
* It is not allowed to add items manually to automatic indexes.
*/
@Test
@Graph( nodes = {@NODE( name = "I" ), @NODE( name = "you" )}, relationships = {@REL( start = "I", end = "you",
type = "know", properties = {@PROP( key = "since", value = "today" )} )}, autoIndexRelationships = true )
public void items_can_not_be_added_manually_to_a_Relationship_AutoIndex()
{
data.get();
String indexName;
try ( Transaction tx = graphdb().beginTx() )
{
indexName = graphdb().index().getRelationshipAutoIndexer().getAutoIndex().getName();
tx.success();
}
try ( Transaction tx = graphdb().beginTx() )
{
ResourceIterable<Relationship> relationships =
(ResourceIterable<Relationship>) data.get().get( "I" ).getRelationships();
try ( ResourceIterator<Relationship> resourceIterator = relationships.iterator() )
{
gen.get().expectedStatus( 405 )
.payload( createJsonStringFor( getRelationshipUri( resourceIterator.next() ), "name", "I" ) )
.post( postRelationshipIndexUri( indexName ) ).entity();
}
}
}
/**
* It is not allowed to remove entries manually from automatic indexes.
*/
@Test
@Graph( nodes = {@NODE( name = "I", setNameProperty = true )}, autoIndexNodes = true )
public void autoindexed_items_cannot_be_removed_manually()
{
long id = data.get()
.get( "I" )
.getId();
String indexName;
try ( Transaction tx = graphdb().beginTx() )
{
indexName = graphdb().index().getNodeAutoIndexer().getAutoIndex().getName();
tx.success();
}
gen.get()
.expectedStatus( 405 )
.delete( getDataUri() + "index/node/" + indexName + "/name/I/" + id )
.entity();
gen.get()
.expectedStatus( 405 )
.delete( getDataUri() + "index/node/" + indexName + "/name/" + id )
.entity();
gen.get()
.expectedStatus( 405 )
.delete( getDataUri() + "index/node/" + indexName + "/" + id )
.entity();
}
/**
* It is not allowed to remove entries manually from automatic indexes.
*/
@Test
@Graph( nodes = {@NODE( name = "I" ), @NODE( name = "you" )}, relationships = {@REL( start = "I", end = "you",
type = "know", properties = {@PROP( key = "since", value = "today" )} )}, autoIndexRelationships = true )
public void autoindexed_relationships_cannot_be_removed_manually()
{
try ( Transaction tx = graphdb().beginTx() )
{
data.get();
tx.success();
}
try ( Transaction tx = graphdb().beginTx() )
{
ResourceIterable<Relationship> relationshipIterable =
(ResourceIterable<Relationship>) data.get().get( "I" ).getRelationships();
long id;
try ( ResourceIterator<Relationship> resourceIterator = relationshipIterable.iterator() )
{
id = resourceIterator.next().getId();
}
String indexName = graphdb().index()
.getRelationshipAutoIndexer()
.getAutoIndex()
.getName();
gen.get()
.expectedStatus( 405 )
.delete( getDataUri() + "index/relationship/" + indexName + "/since/today/" + id )
.entity();
gen.get()
.expectedStatus( 405 )
.delete( getDataUri() + "index/relationship/" + indexName + "/since/" + id )
.entity();
gen.get()
.expectedStatus( 405 )
.delete( getDataUri() + "index/relationship/" + indexName + "/" + id )
.entity();
}
}
/**
* See the example request.
*/
@Test
@Graph( nodes = {@NODE( name = "I" ), @NODE( name = "you" )}, relationships = {@REL( start = "I", end = "you",
type = "know", properties = {@PROP( key = "since", value = "today" )} )}, autoIndexRelationships = true )
public void Find_relationship_by_query_from_an_automatic_index()
{
data.get();
assertSize( 1, gen.get()
.expectedStatus( 200 )
.get( relationshipAutoIndexUri() + "?query=since:today" )
.entity() );
}
/**
* See the example request.
*/
@Test
@Graph( nodes = {@NODE( name = "I" ), @NODE( name = "you" )}, relationships = {@REL( start = "I", end = "you",
type = "know", properties = {@PROP( key = "since", value = "today" )} )}, autoIndexRelationships = true )
public void Find_relationship_by_exact_match_from_an_automatic_index()
{
data.get();
assertSize( 1, gen.get()
.expectedStatus( 200 )
.get( relationshipAutoIndexUri() + "since/today/" )
.entity() );
}
/**
* Get current status for autoindexing on nodes.
*/
@Test
public void getCurrentStatusForNodes()
{
setEnabledAutoIndexingForType( "node", false );
checkAndAssertAutoIndexerIsEnabled( "node", false );
}
/**
* Enable node autoindexing.
*/
@Test
public void enableNodeAutoIndexing()
{
setEnabledAutoIndexingForType( "node", true );
checkAndAssertAutoIndexerIsEnabled( "node", true );
}
/**
* Add a property for autoindexing on nodes.
*/
@Test
public void addAutoIndexingPropertyForNodes()
{
gen.get()
.expectedStatus( 204 )
.payload( "myProperty1" )
.post( autoIndexURI( "node" ) + "/properties" );
}
/**
* Lookup list of properties being autoindexed.
*/
@Test
public void listAutoIndexingPropertiesForNodes() throws JsonParseException
{
int initialPropertiesSize = getAutoIndexedPropertiesForType( "node" ).size();
String propName = "some-property" + System.currentTimeMillis();
GraphDatabaseFacade db = server().getDatabase().getGraph();
try ( Transaction tx = db.beginTx() )
{
db.index().getNodeAutoIndexer().startAutoIndexingProperty( propName );
tx.success();
}
List<String> properties = getAutoIndexedPropertiesForType( "node" );
assertEquals( initialPropertiesSize + 1, properties.size() );
assertThat( properties, hasItem( propName ) );
}
/**
* Remove a property for autoindexing on nodes.
*/
@Test
public void removeAutoIndexingPropertyForNodes()
{
gen.get()
.expectedStatus( 204 )
.delete( autoIndexURI( "node" ) + "/properties/myProperty1" );
}
@Test
public void switchOnOffAutoIndexingForNodes()
{
switchOnOffAutoIndexingForType( "node" );
}
@Test
public void switchOnOffAutoIndexingForRelationships()
{
switchOnOffAutoIndexingForType( "relationship" );
}
@Test
public void addRemoveAutoIndexedPropertyForNodes() throws JsonParseException
{
addRemoveAutoIndexedPropertyForType( "node" );
}
@Test
public void addRemoveAutoIndexedPropertyForRelationships() throws JsonParseException
{
addRemoveAutoIndexedPropertyForType( "relationship" );
}
private String relationshipAutoIndexUri()
{
return getDataUri() + "index/auto/relationship/";
}
private void addRemoveAutoIndexedPropertyForType( String uriPartForType ) throws JsonParseException
{
int initialPropertiesSize = getAutoIndexedPropertiesForType( uriPartForType ).size();
long millis = System.currentTimeMillis();
String myProperty1 = uriPartForType + "-myProperty1-" + millis;
String myProperty2 = uriPartForType + "-myProperty2-" + millis;
gen.get()
.expectedStatus( 204 )
.payload( myProperty1 )
.post( autoIndexURI( uriPartForType ) + "/properties" );
gen.get()
.expectedStatus( 204 )
.payload( myProperty2 )
.post( autoIndexURI( uriPartForType ) + "/properties" );
List<String> properties = getAutoIndexedPropertiesForType( uriPartForType );
assertEquals( initialPropertiesSize + 2, properties.size() );
assertTrue( properties.contains( myProperty1 ) );
assertTrue( properties.contains( myProperty2 ) );
gen.get()
.expectedStatus( 204 )
.payload( null )
.delete( autoIndexURI( uriPartForType )
+ "/properties/" + myProperty2 );
properties = getAutoIndexedPropertiesForType( uriPartForType );
assertEquals( initialPropertiesSize + 1, properties.size() );
assertTrue( properties.contains( myProperty1 ) );
}
@SuppressWarnings( "unchecked" )
private List<String> getAutoIndexedPropertiesForType( String uriPartForType )
throws JsonParseException
{
String result = gen.get()
.expectedStatus( 200 )
.get( autoIndexURI( uriPartForType ) + "/properties" ).entity();
return (List<String>) JsonHelper.readJson( result );
}
private void switchOnOffAutoIndexingForType( String uriPartForType )
{
setEnabledAutoIndexingForType( uriPartForType, true );
checkAndAssertAutoIndexerIsEnabled( uriPartForType, true );
setEnabledAutoIndexingForType( uriPartForType, false );
checkAndAssertAutoIndexerIsEnabled( uriPartForType, false );
}
private void setEnabledAutoIndexingForType( String uriPartForType, boolean enabled )
{
gen.get()
.expectedStatus( 204 )
.payload( Boolean.toString( enabled ) )
.put( autoIndexURI( uriPartForType ) + "/status" );
}
private void checkAndAssertAutoIndexerIsEnabled( String uriPartForType, boolean enabled )
{
String result = gen.get()
.expectedStatus( 200 )
.get( autoIndexURI( uriPartForType ) + "/status" ).entity();
assertEquals( enabled, Boolean.parseBoolean( result ) );
}
private String autoIndexURI( String type )
{
return getDataUri()
+ RestfulGraphDatabase.PATH_AUTO_INDEX.replace( "{type}", type );
}
}