Skip to content

Commit

Permalink
Better name for value writer used for INIT metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
lutovich committed Jul 3, 2018
1 parent 7132fbc commit 233cfcd
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 67 deletions.
4 changes: 4 additions & 0 deletions community/bolt/pom.xml
Expand Up @@ -104,6 +104,10 @@
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId> <artifactId>junit-jupiter-engine</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId> <artifactId>hamcrest-core</artifactId>
Expand Down
Expand Up @@ -66,7 +66,7 @@ public RequestMessage decode( Neo4jPack.Unpacker unpacker ) throws IOException
private static Map<String,Object> readAuthToken( Neo4jPack.Unpacker unpacker ) throws IOException private static Map<String,Object> readAuthToken( Neo4jPack.Unpacker unpacker ) throws IOException
{ {
MapValue authTokenValue = unpacker.unpackMap(); MapValue authTokenValue = unpacker.unpackMap();
AuthTokenValuesWriter writer = new AuthTokenValuesWriter(); PrimitiveOnlyValueWriter writer = new PrimitiveOnlyValueWriter();
Map<String,Object> tokenMap = new HashMap<>( authTokenValue.size() ); Map<String,Object> tokenMap = new HashMap<>( authTokenValue.size() );
authTokenValue.foreach( ( key, value ) -> tokenMap.put( key, writer.valueAsObject( value ) ) ); authTokenValue.foreach( ( key, value ) -> tokenMap.put( key, writer.valueAsObject( value ) ) );
return tokenMap; return tokenMap;
Expand Down
Expand Up @@ -19,6 +19,12 @@
*/ */
package org.neo4j.bolt.v1.messaging.decoder; package org.neo4j.bolt.v1.messaging.decoder;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;

import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.spatial.Point; import org.neo4j.graphdb.spatial.Point;
Expand All @@ -29,9 +35,9 @@


/** /**
* {@link AnyValueWriter Writer} that allows to convert {@link AnyValue} to any primitive Java type. It explicitly * {@link AnyValueWriter Writer} that allows to convert {@link AnyValue} to any primitive Java type. It explicitly
* prohibits conversion of nodes, relationships and points. They are not expected in auth token map. * prohibits conversion of nodes, relationships, spatial and temporal types. They are not expected in auth token map.
*/ */
class AuthTokenValuesWriter extends BaseToObjectValueWriter<RuntimeException> class PrimitiveOnlyValueWriter extends BaseToObjectValueWriter<RuntimeException>
{ {
Object valueAsObject( AnyValue value ) Object valueAsObject( AnyValue value )
{ {
Expand All @@ -56,4 +62,46 @@ protected Point newPoint( CoordinateReferenceSystem crs, double[] coordinate )
{ {
throw new UnsupportedOperationException( "INIT message metadata should not contain points" ); throw new UnsupportedOperationException( "INIT message metadata should not contain points" );
} }

@Override
public void writeByteArray( byte[] value )
{
throw new UnsupportedOperationException( "INIT message metadata should not contain byte arrays" );
}

@Override
public void writeDuration( long months, long days, long seconds, int nanos )
{
throw new UnsupportedOperationException( "INIT message metadata should not contain durations" );
}

@Override
public void writeDate( LocalDate localDate )
{
throw new UnsupportedOperationException( "INIT message metadata should not contain dates" );
}

@Override
public void writeLocalTime( LocalTime localTime )
{
throw new UnsupportedOperationException( "INIT message metadata should not contain local dates" );
}

@Override
public void writeTime( OffsetTime offsetTime )
{
throw new UnsupportedOperationException( "INIT message metadata should not contain time values" );
}

@Override
public void writeLocalDateTime( LocalDateTime localDateTime )
{
throw new UnsupportedOperationException( "INIT message metadata should not contain local date-time values" );
}

@Override
public void writeDateTime( ZonedDateTime zonedDateTime )
{
throw new UnsupportedOperationException( "INIT message metadata should not contain date-time values" );
}
} }
Expand Up @@ -19,17 +19,29 @@
*/ */
package org.neo4j.bolt.v1.messaging.decoder; package org.neo4j.bolt.v1.messaging.decoder;


import org.junit.Test; import org.junit.jupiter.api.Test;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.util.stream.Stream;

import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.CoordinateReferenceSystem; import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.storable.LongValue; import org.neo4j.values.storable.LongValue;
import org.neo4j.values.storable.PointValue;
import org.neo4j.values.storable.TextValue; import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Values;
import org.neo4j.values.virtual.NodeValue; import org.neo4j.values.virtual.NodeValue;
import org.neo4j.values.virtual.RelationshipValue; import org.neo4j.values.virtual.RelationshipValue;


import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.neo4j.values.storable.Values.byteArray;
import static org.neo4j.values.storable.Values.longValue; import static org.neo4j.values.storable.Values.longValue;
import static org.neo4j.values.storable.Values.pointValue; import static org.neo4j.values.storable.Values.pointValue;
import static org.neo4j.values.storable.Values.stringArray; import static org.neo4j.values.storable.Values.stringArray;
Expand All @@ -38,82 +50,30 @@
import static org.neo4j.values.virtual.VirtualValues.nodeValue; import static org.neo4j.values.virtual.VirtualValues.nodeValue;
import static org.neo4j.values.virtual.VirtualValues.relationshipValue; import static org.neo4j.values.virtual.VirtualValues.relationshipValue;


public class AuthTokenValuesWriterTest class PrimitiveOnlyValueWriterTest
{ {
@Test @Test
public void shouldFailToWriteNode() void shouldConvertStringValueToString()
{
AuthTokenValuesWriter writer = new AuthTokenValuesWriter();

NodeValue value = nodeValue( 42, stringArray( "Person" ), EMPTY_MAP );

try
{
writer.valueAsObject( value );
fail( "Exception expected" );
}
catch ( UnsupportedOperationException ignore )
{
}
}

@Test
public void shouldFailToWriteRelationship()
{
AuthTokenValuesWriter writer = new AuthTokenValuesWriter();

NodeValue startNode = nodeValue( 24, stringArray( "Person" ), EMPTY_MAP );
NodeValue endNode = nodeValue( 42, stringArray( "Person" ), EMPTY_MAP );
RelationshipValue value = relationshipValue( 42, startNode, endNode, stringValue( "KNOWS" ), EMPTY_MAP );

try
{
writer.valueAsObject( value );
fail( "Exception expected" );
}
catch ( UnsupportedOperationException ignore )
{
}
}

@Test
public void shouldFailToWritePoint()
{ {
AuthTokenValuesWriter writer = new AuthTokenValuesWriter(); PrimitiveOnlyValueWriter writer = new PrimitiveOnlyValueWriter();
PointValue value = pointValue( CoordinateReferenceSystem.WGS84, new double[2] );

try
{
writer.valueAsObject( value );
fail( "Exception expected" );
}
catch ( UnsupportedOperationException ignore )
{
}
}

@Test
public void shouldConvertStringValueToString()
{
AuthTokenValuesWriter writer = new AuthTokenValuesWriter();
TextValue value = stringValue( "Hello" ); TextValue value = stringValue( "Hello" );


assertEquals( "Hello", writer.valueAsObject( value ) ); assertEquals( "Hello", writer.valueAsObject( value ) );
} }


@Test @Test
public void shouldConvertLongValueToLong() void shouldConvertLongValueToLong()
{ {
AuthTokenValuesWriter writer = new AuthTokenValuesWriter(); PrimitiveOnlyValueWriter writer = new PrimitiveOnlyValueWriter();
LongValue value = longValue( 42 ); LongValue value = longValue( 42 );


assertEquals( 42L, writer.valueAsObject( value ) ); assertEquals( 42L, writer.valueAsObject( value ) );
} }


@Test @Test
public void shouldConvertMultipleValues() void shouldConvertMultipleValues()
{ {
AuthTokenValuesWriter writer = new AuthTokenValuesWriter(); PrimitiveOnlyValueWriter writer = new PrimitiveOnlyValueWriter();


TextValue value1 = stringValue( "Hello" ); TextValue value1 = stringValue( "Hello" );
TextValue value2 = stringValue( " " ); TextValue value2 = stringValue( " " );
Expand All @@ -125,4 +85,35 @@ public void shouldConvertMultipleValues()
assertEquals( "World!", writer.valueAsObject( value3 ) ); assertEquals( "World!", writer.valueAsObject( value3 ) );
assertEquals( 42L, writer.valueAsObject( value4 ) ); assertEquals( 42L, writer.valueAsObject( value4 ) );
} }

@ParameterizedTest
@MethodSource( "unsupportedValues" )
void shouldFailToWriteComplexValue( AnyValue value )
{
PrimitiveOnlyValueWriter writer = new PrimitiveOnlyValueWriter();
assertThrows( UnsupportedOperationException.class, () -> writer.valueAsObject( value ) );
}

private static Stream<AnyValue> unsupportedValues()
{
return Stream.of(
nodeValue( 42, stringArray( "Person" ), EMPTY_MAP ),
newRelationshipValue(),
pointValue( CoordinateReferenceSystem.WGS84, new double[2] ),
byteArray( new byte[]{1, 2, 3} ),
Values.of( Duration.ofHours( 1 ) ),
Values.of( LocalDate.now() ),
Values.of( LocalTime.now() ),
Values.of( OffsetTime.now() ),
Values.of( LocalDateTime.now() ),
Values.of( ZonedDateTime.now() )
);
}

private static RelationshipValue newRelationshipValue()
{
NodeValue startNode = nodeValue( 24, stringArray( "Person" ), EMPTY_MAP );
NodeValue endNode = nodeValue( 42, stringArray( "Person" ), EMPTY_MAP );
return relationshipValue( 42, startNode, endNode, stringValue( "KNOWS" ), EMPTY_MAP );
}
} }

0 comments on commit 233cfcd

Please sign in to comment.