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.assertTrue;
23
24 import java.net.URI;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MediaType;
31
32 import org.neo4j.server.rest.repr.formats.ListWrappingWriter;
33 import org.neo4j.server.rest.repr.formats.MapWrappingWriter;
34
35 public abstract class RepresentationTestBase
36 {
37 public static final URI BASE_URI = URI.create( "http://neo4j.org/" );
38 static final String NODE_URI_PATTERN = "http://.*/node/[0-9]+";
39 static final String RELATIONSHIP_URI_PATTERN = "http://.*/relationship/[0-9]+";
40
41 static void assertUriMatches( String expectedRegex, ValueRepresentation uriRepr ) throws BadInputException
42 {
43 assertUriMatches( expectedRegex, serialize( uriRepr ) );
44 }
45
46 static void assertUriMatches( String expectedRegex, URI actualUri )
47 {
48 assertUriMatches( expectedRegex, actualUri.toString() );
49 }
50
51 public static Object serialize( Representation repr ) throws BadInputException
52 {
53 if ( repr instanceof ValueRepresentation )
54 {
55 return serialize( (ValueRepresentation) repr );
56 }
57 else if ( repr instanceof MappingRepresentation )
58 {
59 return serialize( (MappingRepresentation) repr );
60 }
61 else if ( repr instanceof ListRepresentation )
62 {
63 return serialize( (ListRepresentation) repr );
64 }
65 else
66 {
67 throw new IllegalArgumentException( repr.getClass().toString() );
68 }
69 }
70
71 public static String serialize( ValueRepresentation repr ) throws BadInputException
72 {
73 return repr.serialize( new StringFormat(), BASE_URI, null );
74 }
75
76 static void assertUriMatches( String expectedRegex, String actualUri )
77 {
78 assertTrue( "expected <" + expectedRegex + "> got <" + actualUri + ">",
79 actualUri.matches( expectedRegex ) );
80 }
81
82 static String uriPattern( String subPath )
83 {
84 return "http://.*/[0-9]+" + subPath;
85 }
86
87 public static Map<String, Object> serialize( MappingRepresentation repr )
88 {
89 Map<String, Object> result = new HashMap<String, Object>();
90 repr.serialize( new MappingSerializer( new MapWrappingWriter( result ), BASE_URI, null ) );
91 return result;
92 }
93
94 public static List<Object> serialize( ListRepresentation repr )
95 {
96 List<Object> result = new ArrayList<Object>();
97 repr.serialize( new ListSerializer( new ListWrappingWriter( result ), BASE_URI, null ) );
98 return result;
99 }
100
101 private static class StringFormat extends RepresentationFormat
102 {
103 StringFormat()
104 {
105 super( MediaType.WILDCARD_TYPE );
106 }
107
108 @Override
109 protected String serializeValue( String type, Object value )
110 {
111 return value.toString();
112 }
113
114 @Override
115 protected String complete( ListWriter serializer )
116 {
117 throw new UnsupportedOperationException( "StringFormat.complete(ListWriter)" );
118 }
119
120 @Override
121 protected String complete( MappingWriter serializer )
122 {
123 throw new UnsupportedOperationException( "StringFormat.complete(MappingWriter)" );
124 }
125
126 @Override
127 protected ListWriter serializeList( String type )
128 {
129 throw new UnsupportedOperationException( "StringFormat.serializeList()" );
130 }
131
132 @Override
133 protected MappingWriter serializeMapping( String type )
134 {
135 throw new UnsupportedOperationException( "StringFormat.serializeMapping()" );
136 }
137
138 @Override
139 public List<Object> readList( String input )
140 {
141 throw new UnsupportedOperationException( "StringFormat.readList()" );
142 }
143
144 @Override
145 public Map<String, Object> readMap( String input )
146 {
147 throw new UnsupportedOperationException( "StringFormat.readMap()" );
148 }
149
150 @Override
151 public Object readValue( String input )
152 {
153 throw new UnsupportedOperationException( "StringFormat.readValue()" );
154 }
155
156 @Override
157 public URI readUri( String input )
158 {
159 throw new UnsupportedOperationException( "StringFormat.readUri()" );
160 }
161 }
162 }