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 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  }