Skip to content

Commit

Permalink
Completes a previous change on InputEntityDecorators
Browse files Browse the repository at this point in the history
where everything should be Decorator, not Function there
  • Loading branch information
tinwelint committed Sep 14, 2016
1 parent a2605cf commit 2812e38
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 22 deletions.
Expand Up @@ -19,7 +19,7 @@
*/ */
package org.neo4j.unsafe.impl.batchimport.input; package org.neo4j.unsafe.impl.batchimport.input;


import java.util.function.Function; import java.util.stream.Stream;


import org.neo4j.helpers.ArrayUtil; import org.neo4j.helpers.ArrayUtil;
import org.neo4j.unsafe.impl.batchimport.input.csv.Decorator; import org.neo4j.unsafe.impl.batchimport.input.csv.Decorator;
Expand Down Expand Up @@ -75,15 +75,26 @@ public static Decorator<InputRelationship> defaultRelationshipType( final String
}; };
} }


public static <ENTITY extends InputEntity> Function<ENTITY,ENTITY> decorators( public static <ENTITY extends InputEntity> Decorator<ENTITY> decorators(
final Function<ENTITY,ENTITY>... decorators ) final Decorator<ENTITY>... decorators )
{ {
return from -> { return new Decorator<ENTITY>()
for ( Function<ENTITY,ENTITY> decorator : decorators ) {
@Override
public ENTITY apply( ENTITY from )
{
for ( Decorator<ENTITY> decorator : decorators )
{
from = decorator.apply( from );
}
return from;
}

@Override
public boolean isMutable()
{ {
from = decorator.apply( from ); return Stream.of( decorators ).anyMatch( Decorator::isMutable );
} }
return from;
}; };
} }


Expand Down
Expand Up @@ -22,18 +22,22 @@
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;


import java.util.function.Function;

import org.neo4j.helpers.ArrayUtil; import org.neo4j.helpers.ArrayUtil;
import org.neo4j.unsafe.impl.batchimport.input.csv.Decorator;


import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.neo4j.helpers.collection.Iterators.asSet; import static org.neo4j.helpers.collection.Iterators.asSet;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.additiveLabels;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.decorators;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.defaultRelationshipType;


public class InputEntityDecoratorsTest public class InputEntityDecoratorsTest
{ {
Expand All @@ -42,8 +46,7 @@ public void shouldProvideDefaultRelationshipType() throws Exception
{ {
// GIVEN // GIVEN
String defaultType = "TYPE"; String defaultType = "TYPE";
Function<InputRelationship,InputRelationship> decorator = Decorator<InputRelationship> decorator = defaultRelationshipType( defaultType );
InputEntityDecorators.defaultRelationshipType( defaultType );


// WHEN // WHEN
InputRelationship relationship = new InputRelationship( "source", 1, 0, InputEntity.NO_PROPERTIES, null, InputRelationship relationship = new InputRelationship( "source", 1, 0, InputEntity.NO_PROPERTIES, null,
Expand All @@ -59,8 +62,7 @@ public void shouldNotOverrideAlreadySetRelationshipType() throws Exception
{ {
// GIVEN // GIVEN
String defaultType = "TYPE"; String defaultType = "TYPE";
Function<InputRelationship,InputRelationship> decorator = Decorator<InputRelationship> decorator = defaultRelationshipType( defaultType );
InputEntityDecorators.defaultRelationshipType( defaultType );


// WHEN // WHEN
String customType = "CUSTOM_TYPE"; String customType = "CUSTOM_TYPE";
Expand All @@ -77,8 +79,7 @@ public void shouldNotOverrideAlreadySetRelationshipTypeId() throws Exception
{ {
// GIVEN // GIVEN
String defaultType = "TYPE"; String defaultType = "TYPE";
Function<InputRelationship,InputRelationship> decorator = Decorator<InputRelationship> decorator = defaultRelationshipType( defaultType );
InputEntityDecorators.defaultRelationshipType( defaultType );


// WHEN // WHEN
Integer typeId = 5; Integer typeId = 5;
Expand All @@ -96,7 +97,7 @@ public void shouldAddLabelsToNodeWithoutLabels() throws Exception
{ {
// GIVEN // GIVEN
String[] toAdd = new String[] {"Add1", "Add2"}; String[] toAdd = new String[] {"Add1", "Add2"};
Function<InputNode,InputNode> decorator = InputEntityDecorators.additiveLabels( toAdd ); Decorator<InputNode> decorator = additiveLabels( toAdd );


// WHEN // WHEN
InputNode node = new InputNode( "source", 1, 0, "id", InputEntity.NO_PROPERTIES, null, null, null ); InputNode node = new InputNode( "source", 1, 0, "id", InputEntity.NO_PROPERTIES, null, null, null );
Expand All @@ -111,7 +112,7 @@ public void shouldAddMissingLabels() throws Exception
{ {
// GIVEN // GIVEN
String[] toAdd = new String[] {"Add1", "Add2"}; String[] toAdd = new String[] {"Add1", "Add2"};
Function<InputNode,InputNode> decorator = InputEntityDecorators.additiveLabels( toAdd ); Decorator<InputNode> decorator = additiveLabels( toAdd );


// WHEN // WHEN
String[] nodeLabels = new String[] {"SomeOther"}; String[] nodeLabels = new String[] {"SomeOther"};
Expand All @@ -127,7 +128,7 @@ public void shouldNotTouchLabelsIfNodeHasLabelFieldSet() throws Exception
{ {
// GIVEN // GIVEN
String[] toAdd = new String[] {"Add1", "Add2"}; String[] toAdd = new String[] {"Add1", "Add2"};
Function<InputNode,InputNode> decorator = InputEntityDecorators.additiveLabels( toAdd ); Decorator<InputNode> decorator = additiveLabels( toAdd );


// WHEN // WHEN
long labelField = 123L; long labelField = 123L;
Expand All @@ -143,9 +144,9 @@ public void shouldNotTouchLabelsIfNodeHasLabelFieldSet() throws Exception
public void shouldCramMultipleDecoratorsIntoOne() throws Exception public void shouldCramMultipleDecoratorsIntoOne() throws Exception
{ {
// GIVEN // GIVEN
Function<InputNode,InputNode> decorator1 = spy( new IdentityDecorator() ); Decorator<InputNode> decorator1 = spy( new IdentityDecorator() );
Function<InputNode,InputNode> decorator2 = spy( new IdentityDecorator() ); Decorator<InputNode> decorator2 = spy( new IdentityDecorator() );
Function<InputNode,InputNode> multi = InputEntityDecorators.decorators( decorator1, decorator2 ); Decorator<InputNode> multi = decorators( decorator1, decorator2 );


// WHEN // WHEN
InputNode node = mock( InputNode.class ); InputNode node = mock( InputNode.class );
Expand All @@ -158,12 +159,60 @@ public void shouldCramMultipleDecoratorsIntoOne() throws Exception
order.verifyNoMoreInteractions(); order.verifyNoMoreInteractions();
} }


private static class IdentityDecorator implements Function<InputNode,InputNode> @Test
public void shouldThinkMultiDecoratorIsntMutableIfNooneIs() throws Exception
{
// GIVEN
Decorator<InputNode> decorator1 = spy( new IdentityDecorator() );
Decorator<InputNode> decorator2 = spy( new IdentityDecorator() );
Decorator<InputNode> multi = decorators( decorator1, decorator2 );

// WHEN
boolean mutable = multi.isMutable();

// THEN
assertFalse( mutable );
}

@Test
public void shouldThinkMultiDecoratorIsMutableIfAnyIs() throws Exception
{
// GIVEN
Decorator<InputNode> decorator1 = spy( new IdentityDecorator() );
Decorator<InputNode> decorator2 = spy( new IdentityDecorator( true ) );
Decorator<InputNode> multi = decorators( decorator1, decorator2 );

// WHEN
boolean mutable = multi.isMutable();

// THEN
assertTrue( mutable );
}

private static class IdentityDecorator implements Decorator<InputNode>
{ {
private final boolean mutable;

public IdentityDecorator()
{
this( false );
}

public IdentityDecorator( boolean mutable )
{
this.mutable = mutable;
}

@Override @Override
public InputNode apply( InputNode from ) throws RuntimeException public InputNode apply( InputNode from ) throws RuntimeException
{ {
return from; return from;
} }

@Override
public boolean isMutable()
{
return mutable;
}
} }
} }

0 comments on commit 2812e38

Please sign in to comment.