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.formats;
21  
22  import static org.hamcrest.Matchers.hasEntry;
23  import static org.hamcrest.Matchers.hasKey;
24  import static org.hamcrest.Matchers.instanceOf;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertThat;
27  import static org.junit.Assert.assertTrue;
28  
29  import java.util.Map;
30  
31  import org.junit.Test;
32  
33  public class JsonInputTest
34  {
35      private final JsonFormat input = new JsonFormat();
36  
37      @Test
38      public void canReadEmptyMap() throws Exception
39      {
40          Map<String, Object> map = input.readMap( "{}" );
41          assertNotNull( map );
42          assertTrue( "map is not empty", map.isEmpty() );
43      }
44  
45      @Test
46      public void canReadMapWithTwoValues() throws Exception
47      {
48          Map<String, Object> map = input.readMap( "{\"key1\":\"value1\",     \"key2\":\"value11\"}" );
49          assertNotNull( map );
50          assertThat( map, hasEntry( "key1", (Object) "value1" ) );
51          assertThat( map, hasEntry( "key2", (Object) "value11" ) );
52          assertTrue( "map contained extra values", map.size() == 2 );
53      }
54  
55      @Test
56      public void canReadMapWithNestedMap() throws Exception
57      {
58          Map<String, Object> map = input.readMap( "{\"nested\": {\"key\": \"valuable\"}}" );
59          assertNotNull( map );
60          assertThat( map, hasKey( "nested" ) );
61          assertTrue( "map contained extra values", map.size() == 1 );
62          Object nested = map.get( "nested" );
63          assertThat( nested, instanceOf( Map.class ) );
64          @SuppressWarnings( "unchecked" ) Map<String, String> nestedMap = (Map<String, String>) nested;
65          assertThat( nestedMap, hasEntry( "key", "valuable" ) );
66      }
67  }