-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
VirtualValueTestUtil.java
148 lines (133 loc) · 5.17 KB
/
VirtualValueTestUtil.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
/*
* 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.values.virtual;
import java.util.Arrays;
import org.neo4j.values.AnyValue;
import org.neo4j.values.VirtualValue;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.Values;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.neo4j.values.storable.Values.stringArray;
import static org.neo4j.values.storable.Values.stringValue;
import static org.neo4j.values.virtual.VirtualValues.edgeValue;
import static org.neo4j.values.virtual.VirtualValues.emptyMap;
import static org.neo4j.values.virtual.VirtualValues.nodeValue;
@SuppressWarnings( "WeakerAccess" )
public class VirtualValueTestUtil
{
public static AnyValue toAnyValue( Object o )
{
if ( o instanceof AnyValue )
{
return (AnyValue) o;
}
else
{
return Values.of( o );
}
}
public static NodeValue node( long id, String... labels )
{
TextValue[] labelValues = new TextValue[labels.length];
for ( int i = 0; i < labels.length; i++ )
{
labelValues[i] = stringValue( labels[i] );
}
return nodeValue( id, stringArray( labels ), emptyMap() );
}
public static VirtualValue path( VirtualValue... pathElements )
{
assert pathElements.length % 2 == 1;
NodeValue[] nodes = new NodeValue[pathElements.length / 2 + 1];
EdgeValue[] edges = new EdgeValue[pathElements.length / 2];
nodes[0] = (NodeValue) pathElements[0];
for ( int i = 1; i < pathElements.length; i += 2 )
{
edges[i / 2] = (EdgeValue) pathElements[i];
nodes[i / 2 + 1] = (NodeValue) pathElements[i + 1];
}
return VirtualValues.path( nodes, edges );
}
public static EdgeValue edge( long id, long start, long end )
{
return edgeValue( id, node( start ), node( end ), stringValue( "T" ), emptyMap() );
}
public static ListValue list( Object... objects )
{
AnyValue[] values = new AnyValue[objects.length];
for ( int i = 0; i < objects.length; i++ )
{
values[i] = toAnyValue( objects[i] );
}
return VirtualValues.list( values );
}
public static VirtualValue map( Object... keyOrVal )
{
assert keyOrVal.length % 2 == 0;
String[] keys = new String[keyOrVal.length / 2];
AnyValue[] values = new AnyValue[keyOrVal.length / 2];
for ( int i = 0; i < keyOrVal.length; i += 2 )
{
keys[i / 2] = (String) keyOrVal[i];
values[i / 2] = toAnyValue( keyOrVal[i + 1] );
}
return VirtualValues.map( keys, values );
}
public static void assertEqual( VirtualValue a, VirtualValue b )
{
assertTrue(
String.format( "%s should be equal %s", a.getClass().getSimpleName(), b.getClass().getSimpleName() ),
a.equals( b ) );
assertTrue(
String.format( "%s should be equal %s", a.getClass().getSimpleName(), b.getClass().getSimpleName() ),
b.equals( a ) );
assertTrue( String.format( "%s should have same hashcode as %s", a.getClass().getSimpleName(),
b.getClass().getSimpleName() ), a.hashCode() == b.hashCode() );
}
public static void assertNotEqual( VirtualValue a, VirtualValue b )
{
assertFalse( a + " should not equal " + b, a.equals( b ) );
assertFalse( b + "should not equal " + a, b.equals( a ) );
}
public static void assertEqualValues( VirtualValue a, Value b )
{
assertTrue( "should be equal values", a.equals( b ) );
assertTrue( "should be equal values", b.equals( a ) );
}
public static void assertNotEqualValues( VirtualValue a, Value b )
{
assertFalse( "should not equal values", a.equals( b ) );
assertFalse( "should not equal values", b.equals( a ) );
}
public static NodeValue[] nodes( long... ids )
{
return Arrays.stream( ids )
.mapToObj( id -> nodeValue( id, stringArray( "L" ), emptyMap() ) )
.toArray( NodeValue[]::new );
}
public static EdgeValue[] edges( long... ids )
{
return Arrays.stream( ids )
.mapToObj( id -> edgeValue( id, node( 0L ), node( 1L ), stringValue( "T" ), emptyMap() ) )
.toArray( EdgeValue[]::new );
}
}