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.junit.Assert.assertEquals;
    23
    24  import java.net.URI;
    25  import java.util.Arrays;
    26  import java.util.Collections;
    27
    28  import org.junit.Before;
    29  import org.junit.Test;
    30  import org.neo4j.server.rest.domain.JsonHelper;
    31  import org.neo4j.server.rest.repr.ListRepresentation;
    32  import org.neo4j.server.rest.repr.MappingRepresentation;
    33  import org.neo4j.server.rest.repr.MappingSerializer;
    34  import org.neo4j.server.rest.repr.OutputFormat;
    35  import org.neo4j.server.rest.repr.ValueRepresentation;
    36
    37  public class JsonFormatTest
    38  {
    39      private OutputFormat json;
    40
    41      @Before
    42      public void createOutputFormat() throws Exception
    43      {
    44          json = new OutputFormat( new JsonFormat(), new URI( "http://localhost/" ), null );
    45      }
    46
    47      @Test
    48      public void canFormatString() throws Exception
    49      {
    50          String entity = json.format( ValueRepresentation.string( "expected value" ) );
    51          assertEquals( entity, "\"expected value\"" );
    52      }
    53
    54      @Test
    55      public void canFormatListOfStrings() throws Exception
    56      {
    57          String entity = json.format( ListRepresentation.strings( "hello", "world" ) );
    58          String expectedString = JsonHelper.createJsonFrom( Arrays.asList( "hello", "world" ) );
    59          assertEquals( expectedString, entity );
    60      }
    61
    62      @Test
    63      public void canFormatInteger() throws Exception
    64      {
    65          String entity = json.format( ValueRepresentation.number( 10 ) );
    66          assertEquals( "10", entity );
    67      }
    68
    69      @Test
    70      public void canFormatEmptyObject() throws Exception
    71      {
    72          String entity = json.format( new MappingRepresentation( "empty" )
    73          {
    74              @Override
    75              protected void serialize( MappingSerializer serializer )
    76              {
    77              }
    78          } );
    79          assertEquals( JsonHelper.createJsonFrom( Collections.emptyMap() ), entity );
    80      }
    81
    82      @Test
    83      public void canFormatObjectWithStringField() throws Exception
    84      {
    85          String entity = json.format( new MappingRepresentation( "string" )
    86          {
    87              @Override
    88              protected void serialize( MappingSerializer serializer )
    89              {
    90                  serializer.putString( "key", "expected string" );
    91              }
    92          } );
    93          assertEquals(
    94                  JsonHelper.createJsonFrom( Collections.singletonMap( "key", "expected string" ) ),
    95                  entity );
    96      }
    97
    98      @Test
    99      public void canFormatObjectWithUriField() throws Exception
    100     {
    101         String entity = json.format( new MappingRepresentation( "uri" )
    102         {
    103             @Override
    104             protected void serialize( MappingSerializer serializer )
    105             {
    106                 serializer.putUri( "URL", "subpath" );
    107             }
    108         } );
    109
    110         assertEquals( JsonHelper.createJsonFrom( Collections.singletonMap( "URL",
    111                 "http://localhost/subpath" ) ), entity );
    112     }
    113
    114     @Test
    115     public void canFormatObjectWithNestedObject() throws Exception
    116     {
    117         String entity = json.format( new MappingRepresentation( "nesting" )
    118         {
    119             @Override
    120             protected void serialize( MappingSerializer serializer )
    121             {
    122                 serializer.putMapping( "nested", new MappingRepresentation( "data" )
    123                 {
    124                     @Override
    125                     protected void serialize( MappingSerializer nested )
    126                     {
    127                         nested.putString( "data", "expected data" );
    128                     }
    129                 } );
    130             }
    131         } );
    132         assertEquals( JsonHelper.createJsonFrom( Collections.singletonMap( "nested",
    133                 Collections.singletonMap( "data", "expected data" ) ) ), entity );
    134     }
    135 }