View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 }