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;
    21
    22  import static org.junit.Assert.assertEquals;
    23  import static org.junit.Assert.assertFalse;
    24  import static org.junit.Assert.assertTrue;
    25  import static org.mockito.Mockito.mock;
    26  import static org.mockito.Mockito.when;
    27
    28  import java.util.Arrays;
    29  import java.util.HashMap;
    30  import java.util.Iterator;
    31  import java.util.List;
    32  import java.util.Map;
    33
    34  import org.junit.Test;
    35  import org.neo4j.graphdb.PropertyContainer;
    36
    37  public class PropertiesRepresentationTest extends RepresentationTestBase
    38  {
    39      @Test
    40      public void shouldContainAddedPropertiesWhenCreatedFromPropertyContainer()
    41      {
    42          Map<String, Object> values = new HashMap<String, Object>();
    43          values.put( "foo", "bar" );
    44          Map<String, Object> serialized = serialize( new PropertiesRepresentation(
    45                  container( values ) ) );
    46          assertEquals( "bar", serialized.get( "foo" ) );
    47      }
    48
    49      @Test
    50      public void shouldSerializeToMapWithSamePropertiesWhenCreatedFromPropertyContainer()
    51      {
    52          Map<String, Object> values = new HashMap<String, Object>();
    53          values.put( "foo", "bar" );
    54          PropertiesRepresentation properties = new PropertiesRepresentation( container( values ) );
    55          Map<String, Object> map = serialize( properties );
    56          assertEquals( values, map );
    57      }
    58
    59      @Test
    60      public void shouldSerializeToMap()
    61      {
    62          Map<String, Object> values = new HashMap<String, Object>();
    63          values.put( "string", "value" );
    64          values.put( "int", 5 );
    65          values.put( "long", 17L );
    66          values.put( "double", 3.14 );
    67          values.put( "float", 42.0f );
    68          values.put( "string array", new String[] { "one", "two" } );
    69          values.put( "long array", new long[] { 5L, 17L } );
    70          values.put( "double array", new double[] { 3.14, 42.0 } );
    71
    72          PropertiesRepresentation properties = new PropertiesRepresentation( container( values ) );
    73          Map<String, Object> map = serialize( properties );
    74
    75          assertEquals( "value", map.get( "string" ) );
    76          assertEquals( 5, ( (Number) map.get( "int" ) ).longValue() );
    77          assertEquals( 17, ( (Number) map.get( "long" ) ).longValue() );
    78          assertEquals( 3.14, ( (Number) map.get( "double" ) ).doubleValue(), 0.0 );
    79          assertEquals( 42.0, ( (Number) map.get( "float" ) ).doubleValue(), 0.0 );
    80          assertEqualContent( Arrays.asList( "one", "two" ), (List) map.get( "string array" ) );
    81          assertEqualContent( Arrays.asList( 5L, 17L ), (List) map.get( "long array" ) );
    82          assertEqualContent( Arrays.asList( 3.14, 42.0 ), (List) map.get( "double array" ) );
    83      }
    84
    85      @Test
    86      public void shouldBeAbleToSignalEmptiness()
    87      {
    88          PropertiesRepresentation properties = new PropertiesRepresentation(
    89                  container( new HashMap<String, Object>() ) );
    90          Map<String, Object> values = new HashMap<String, Object>();
    91          values.put( "key", "value" );
    92          assertTrue( properties.isEmpty() );
    93          properties = new PropertiesRepresentation( container( values ) );
    94          assertFalse( properties.isEmpty() );
    95      }
    96
    97      private void assertEqualContent( List<?> expected, List<?> actual )
    98      {
    99          assertEquals( expected.size(), actual.size() );
    100         for ( Iterator<?> ex = expected.iterator(), ac = actual.iterator(); ex.hasNext()
    101                                                                             && ac.hasNext(); )
    102         {
    103             assertEquals( ex.next(), ac.next() );
    104         }
    105     }
    106
    107     static PropertyContainer container( Map<String, Object> values )
    108     {
    109         PropertyContainer container = mock( PropertyContainer.class );
    110         when( container.getPropertyKeys() ).thenReturn( values.keySet() );
    111         for ( Map.Entry<String, Object> entry : values.entrySet() )
    112         {
    113             when( container.getProperty( entry.getKey(), null ) ).thenReturn( entry.getValue() );
    114         }
    115         return container;
    116     }
    117 }