1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.rest.repr;
21
22 import static org.junit.Assert.assertNotNull;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.util.Collections;
27 import java.util.Map;
28
29 import org.junit.Test;
30 import org.neo4j.graphdb.Node;
31
32 public class NodeRepresentationTest extends RepresentationTestBase
33 {
34 @Test
35 public void shouldHaveSelfLink() throws BadInputException
36 {
37 assertUriMatches( uriPattern( "" ), noderep( 1234 ).selfUri() );
38 }
39
40 @Test
41 public void shouldHaveAllRelationshipsLink() throws BadInputException
42 {
43 assertUriMatches( uriPattern( "/relationships/all" ), noderep( 1234 ).allRelationshipsUri() );
44 }
45
46 @Test
47 public void shouldHaveIncomingRelationshipsLink() throws BadInputException
48 {
49 assertUriMatches( uriPattern( "/relationships/in" ),
50 noderep( 1234 ).incomingRelationshipsUri() );
51 }
52
53 @Test
54 public void shouldHaveOutgoingRelationshipsLink() throws BadInputException
55 {
56 assertUriMatches( uriPattern( "/relationships/out" ),
57 noderep( 1234 ).outgoingRelationshipsUri() );
58 }
59
60 @Test
61 public void shouldHaveAllTypedRelationshipsLinkTemplate() throws BadInputException
62 {
63 assertUriMatches( uriPattern( "/relationships/all/\\{-list\\|&\\|types\\}" ),
64 noderep( 1234 ).allTypedRelationshipsUriTemplate() );
65 }
66
67 @Test
68 public void shouldHaveIncomingTypedRelationshipsLinkTemplate() throws BadInputException
69 {
70 assertUriMatches( uriPattern( "/relationships/in/\\{-list\\|&\\|types\\}" ),
71 noderep( 1234 ).incomingTypedRelationshipsUriTemplate() );
72 }
73
74 @Test
75 public void shouldHaveOutgoingTypedRelationshipsLinkTemplate() throws BadInputException
76 {
77 assertUriMatches( uriPattern( "/relationships/out/\\{-list\\|&\\|types\\}" ),
78 noderep( 1234 ).outgoingTypedRelationshipsUriTemplate() );
79 }
80
81 @Test
82 public void shouldHaveRelationshipCreationLink() throws BadInputException
83 {
84 assertUriMatches( uriPattern( "/relationships" ), noderep( 1234 ).relationshipCreationUri() );
85 }
86
87 @Test
88 public void shouldHavePropertiesLink() throws BadInputException
89 {
90 assertUriMatches( uriPattern( "/properties" ), noderep( 1234 ).propertiesUri() );
91 }
92
93 @Test
94 public void shouldHavePropertyLinkTemplate() throws BadInputException
95 {
96 assertUriMatches( uriPattern( "/properties/\\{key\\}" ),
97 noderep( 1234 ).propertyUriTemplate() );
98 }
99
100 @Test
101 public void shouldHaveTraverseLinkTemplate() throws BadInputException
102 {
103 assertUriMatches( uriPattern( "/traverse/\\{returnType\\}" ),
104 noderep( 1234 ).traverseUriTemplate() );
105 }
106
107 @Test
108 public void shouldSerialiseToMap()
109 {
110 Map<String, Object> repr = serialize( noderep( 1234 ) );
111 assertNotNull( repr );
112 verifySerialisation( repr );
113 }
114
115 private NodeRepresentation noderep( long id )
116 {
117 return new NodeRepresentation( node( id ) );
118 }
119
120 private Node node( long id )
121 {
122 Node node = mock( Node.class );
123 when( node.getId() ).thenReturn( id );
124 when( node.getPropertyKeys() ).thenReturn( Collections.<String>emptySet() );
125 return node;
126 }
127
128 @SuppressWarnings( "unchecked" )
129 public static void verifySerialisation( Map<String, Object> noderep )
130 {
131 assertUriMatches( uriPattern( "" ), noderep.get( "self" ).toString() );
132 assertUriMatches( uriPattern( "/relationships" ),
133 noderep.get( "create_relationship" ).toString() );
134 assertUriMatches( uriPattern( "/relationships/all" ),
135 noderep.get( "all_relationships" ).toString() );
136 assertUriMatches( uriPattern( "/relationships/in" ),
137 noderep.get( "incoming_relationships" ).toString() );
138 assertUriMatches( uriPattern( "/relationships/out" ),
139 noderep.get( "outgoing_relationships" ).toString() );
140 assertUriMatches( uriPattern( "/relationships/all/\\{-list\\|&\\|types\\}" ),
141 (String) noderep.get( "all_typed_relationships" ) );
142 assertUriMatches( uriPattern( "/relationships/in/\\{-list\\|&\\|types\\}" ),
143 (String) noderep.get( "incoming_typed_relationships" ) );
144 assertUriMatches( uriPattern( "/relationships/out/\\{-list\\|&\\|types\\}" ),
145 (String) noderep.get( "outgoing_typed_relationships" ) );
146 assertUriMatches( uriPattern( "/properties" ), noderep.get( "properties" ).toString() );
147 assertUriMatches( uriPattern( "/properties/\\{key\\}" ), (String) noderep.get( "property" ) );
148 assertUriMatches( uriPattern( "/traverse/\\{returnType\\}" ),
149 (String) noderep.get( "traverse" ) );
150 assertNotNull( (Map<String, Object>) noderep.get( "data" ) );
151 }
152 }