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  import org.neo4j.graphdb.Relationship;
32  import org.neo4j.graphdb.RelationshipType;
33  
34  public class RelationshipRepresentationTest extends RepresentationTestBase
35  {
36      @Test
37      public void shouldHaveSelfLink() throws BadInputException
38      {
39          assertUriMatches( RELATIONSHIP_URI_PATTERN, relrep( 1234 ).selfUri() );
40      }
41  
42      @Test
43      public void shouldHaveType()
44      {
45          assertNotNull( relrep( 1234 ).getType() );
46      }
47  
48      @Test
49      public void shouldHaveStartNodeLink() throws BadInputException
50      {
51          assertUriMatches( NODE_URI_PATTERN, relrep( 1234 ).startNodeUri() );
52      }
53  
54      @Test
55      public void shouldHaveEndNodeLink() throws BadInputException
56      {
57          assertUriMatches( NODE_URI_PATTERN, relrep( 1234 ).endNodeUri() );
58      }
59  
60      @Test
61      public void shouldHavePropertiesLink() throws BadInputException
62      {
63          assertUriMatches( RELATIONSHIP_URI_PATTERN + "/properties", relrep( 1234 ).propertiesUri() );
64      }
65  
66      @Test
67      public void shouldHavePropertyLinkTemplate() throws BadInputException
68      {
69          assertUriMatches( RELATIONSHIP_URI_PATTERN + "/properties/\\{key\\}",
70                  relrep( 1234 ).propertyUriTemplate() );
71      }
72  
73      @Test
74      public void shouldSerialiseToMap()
75      {
76          Map<String, Object> repr = serialize( relrep( 1234 ) );
77          assertNotNull( repr );
78          verifySerialisation( repr );
79      }
80  
81      private RelationshipRepresentation relrep( long id )
82      {
83          return new RelationshipRepresentation( relationship( id ) );
84      }
85  
86      static Relationship relationship( long id )
87      {
88          Node startNode = mock( Node.class );
89          when( startNode.getId() ).thenReturn( 0L );
90  
91          Node endNode = mock( Node.class );
92          when( endNode.getId() ).thenReturn( 1L );
93  
94          RelationshipType type = mock( RelationshipType.class );
95          when( type.name() ).thenReturn( "LOVES" );
96  
97          Relationship relationship = mock( Relationship.class );
98          when( relationship.getId() ).thenReturn( id );
99          when( relationship.getPropertyKeys() ).thenReturn( Collections.<String>emptySet() );
100         when( relationship.getStartNode() ).thenReturn( startNode );
101         when( relationship.getEndNode() ).thenReturn( endNode );
102         when( relationship.getType() ).thenReturn( type );
103 
104         return relationship;
105     }
106 
107     @SuppressWarnings( "unchecked" )
108     public static void verifySerialisation( Map<String, Object> relrep )
109     {
110         assertUriMatches( RELATIONSHIP_URI_PATTERN, relrep.get( "self" ).toString() );
111         assertUriMatches( NODE_URI_PATTERN, relrep.get( "start" ).toString() );
112         assertUriMatches( NODE_URI_PATTERN, relrep.get( "end" ).toString() );
113         assertNotNull( (String) relrep.get( "type" ) );
114         assertUriMatches( RELATIONSHIP_URI_PATTERN + "/properties",
115                 relrep.get( "properties" ).toString() );
116         assertUriMatches( RELATIONSHIP_URI_PATTERN + "/properties/\\{key\\}",
117                 (String) relrep.get( "property" ) );
118         assertNotNull( (Map<String, Object>) relrep.get( "data" ) );
119     }
120 }