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.formats;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.neo4j.server.rest.repr.BadInputException;
25 import org.neo4j.server.rest.repr.DefaultFormat;
26 import org.neo4j.server.rest.repr.MediaTypeNotSupportedException;
27
28 import javax.ws.rs.core.MediaType;
29 import java.util.ArrayList;
30 import java.util.Map;
31
32 import static org.hamcrest.Matchers.*;
33 import static org.junit.Assert.*;
34
35 public class DefaultFormatTest
36 {
37 private DefaultFormat input;
38
39 @Before
40 public void setUp() throws Exception
41 {
42 JsonFormat inner = new JsonFormat();
43 ArrayList<MediaType> supported = new ArrayList<MediaType>();
44 MediaType requested = MediaType.APPLICATION_JSON_TYPE;
45 input = new DefaultFormat( inner, supported, requested );
46 }
47
48 @Test
49 public void canReadEmptyMap() throws Exception
50 {
51 Map<String, Object> map = input.readMap( "{}" );
52 assertNotNull( map );
53 assertTrue( "map is not empty", map.isEmpty() );
54 }
55
56 @Test
57 public void canReadMapWithTwoValues() throws Exception
58 {
59 Map<String, Object> map = input.readMap( "{\"key1\":\"value1\", \"key2\":\"value11\"}" );
60 assertNotNull( map );
61 assertThat( map, hasEntry( "key1", (Object) "value1" ) );
62 assertThat( map, hasEntry( "key2", (Object) "value11" ) );
63 assertTrue( "map contained extra values", map.size() == 2 );
64 }
65
66 @Test
67 public void canReadMapWithNestedMap() throws Exception
68 {
69 Map<String, Object> map = input.readMap( "{\"nested\": {\"key\": \"valuable\"}}" );
70 assertNotNull( map );
71 assertThat( map, hasKey( "nested" ) );
72 assertTrue( "map contained extra values", map.size() == 1 );
73 Object nested = map.get( "nested" );
74 assertThat( nested, instanceOf( Map.class ) );
75 @SuppressWarnings( "unchecked" ) Map<String, String> nestedMap = (Map<String, String>) nested;
76 assertThat( nestedMap, hasEntry( "key", "valuable" ) );
77 }
78
79 @Test(expected = MediaTypeNotSupportedException.class)
80 public void failsWithTheCorrectExceptionWhenGettingTheWrongInput() throws BadInputException
81 {
82 input.readValue( "<xml />" );
83 }
84
85
86 @Test(expected = MediaTypeNotSupportedException.class)
87 public void failsWithTheCorrectExceptionWhenGettingTheWrongInput2() throws BadInputException
88 {
89 input.readMap( "<xml />" );
90 }
91
92
93 @Test(expected = MediaTypeNotSupportedException.class)
94 public void failsWithTheCorrectExceptionWhenGettingTheWrongInput3() throws BadInputException
95 {
96 input.readUri( "<xml />" );
97 }
98 }