Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-ince committed Nov 26, 2018
1 parent ad28dac commit c10d32c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 44 deletions.
Expand Up @@ -23,6 +23,8 @@
import org.neo4j.kernel.configuration.ConnectorPortRegister;
import org.neo4j.server.configuration.ServerSettings;

import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.NORMAL;

public class CommunityDiscoverableURIs
{
/**
Expand All @@ -31,8 +33,8 @@ public class CommunityDiscoverableURIs
public static DiscoverableURIs communityDiscoverableURIs( Config config, ConnectorPortRegister portRegister )
{
return new DiscoverableURIs.Builder()
.add( "data", config.get( ServerSettings.rest_api_path ).getPath() + "/", DiscoverableURIs.NORMAL )
.add( "management", config.get( ServerSettings.management_api_path ).getPath() + "/", DiscoverableURIs.NORMAL )
.add( "data", config.get( ServerSettings.rest_api_path ).getPath() + "/", NORMAL )
.add( "management", config.get( ServerSettings.management_api_path ).getPath() + "/", NORMAL )
.addBoltConnectorFromConfig( "bolt", "bolt", config, ServerSettings.bolt_discoverable_address, portRegister )
.build();
}
Expand Down
Expand Up @@ -25,7 +25,6 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

Expand All @@ -35,16 +34,24 @@
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.ConnectorPortRegister;

import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.HIGH;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.HIGHEST;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.LOW;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.LOWEST;

/**
* Repository of URIs that the REST API publicly advertises at the root endpoint.
*/
public class DiscoverableURIs
{
public static final int HIGHEST = 5;
public static final int HIGH = 4;
public static final int NORMAL = 3;
public static final int LOW = 2;
public static final int LOWEST = 1;
public enum Precedence
{
LOWEST,
LOW,
NORMAL,
HIGH,
HIGHEST
}

private final Collection<URIEntry> entries;

Expand All @@ -64,10 +71,10 @@ public void forEach( BiConsumer<String,URI> consumer )
private static class URIEntry
{
private String key;
private int precedence;
private Precedence precedence;
private URI uri;

private URIEntry( String key, URI uri, int precedence )
private URIEntry( String key, URI uri, Precedence precedence )
{
this.key = key;
this.uri = uri;
Expand All @@ -89,21 +96,19 @@ public Builder( DiscoverableURIs copy )
entries = new ArrayList<>( copy.entries );
}

public Builder add( String key, URI uri, int precedence )
public Builder add( String key, URI uri, Precedence precedence )
{
Optional<URIEntry> entry = entries.stream().filter( e -> e.key.equals( key ) && e.precedence == precedence ).findFirst();

if ( entry.isPresent() )
if ( entries.stream().anyMatch( e -> e.key.equals( key ) && e.precedence == precedence ) )
{
throw new InvalidSettingException(
String.format( "Unable to add two entries with the same precedence using key '%s' and precedence '%d'", key, precedence ) );
String.format( "Unable to add two entries with the same precedence using key '%s' and precedence '%s'", key, precedence ) );
}

entries.add( new URIEntry( key, uri, precedence ) );
return this;
}

public Builder add( String key, String uri, int precedence )
public Builder add( String key, String uri, Precedence precedence )
{
try
{
Expand All @@ -116,7 +121,7 @@ public Builder add( String key, String uri, int precedence )
}
}

public Builder add( String key, String scheme, String hostname, int port, int precedence )
public Builder add( String key, String scheme, String hostname, int port, Precedence precedence )
{
try
{
Expand Down
Expand Up @@ -39,6 +39,11 @@
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.HIGH;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.HIGHEST;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.LOW;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.LOWEST;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.NORMAL;

public class DiscoverableURIsTest
{
Expand All @@ -59,9 +64,9 @@ public void shouldInvokeConsumerForEachKey() throws URISyntaxException
{
DiscoverableURIs discoverables =
new DiscoverableURIs.Builder()
.add( "a", "/test", DiscoverableURIs.NORMAL )
.add( "b", "/data", DiscoverableURIs.NORMAL )
.add( "c", "http://www.example.com", DiscoverableURIs.LOW )
.add( "a", "/test", NORMAL )
.add( "b", "/data", NORMAL )
.add( "c", "http://www.example.com", LOW )
.build();

discoverables.forEach( consumer );
Expand All @@ -78,8 +83,8 @@ public void shouldThrowWhenAddingTwoEntriesWithSamePrecedence()
{
DiscoverableURIs discoverables =
new DiscoverableURIs.Builder()
.add( "a", "/test", DiscoverableURIs.NORMAL )
.add( "a", "/data", DiscoverableURIs.NORMAL )
.add( "a", "/test", NORMAL )
.add( "a", "/data", NORMAL )
.build();

fail( "exception expected" );
Expand All @@ -96,12 +101,12 @@ public void shouldInvokeConsumerForEachKeyWithHighestPrecedence() throws URISynt
{
DiscoverableURIs discoverables =
new DiscoverableURIs.Builder()
.add( "c", "bolt://localhost:7687", DiscoverableURIs.HIGHEST )
.add( "a", "/test", DiscoverableURIs.NORMAL )
.add( "b", "/data", DiscoverableURIs.NORMAL )
.add( "b", "/data2", DiscoverableURIs.LOWEST )
.add( "a", "/test2", DiscoverableURIs.HIGHEST )
.add( "c", "bolt://localhost:7688", DiscoverableURIs.LOW )
.add( "c", "bolt://localhost:7687", HIGHEST )
.add( "a", "/test", NORMAL )
.add( "b", "/data", NORMAL )
.add( "b", "/data2", LOWEST )
.add( "a", "/test2", HIGHEST )
.add( "c", "bolt://localhost:7688", LOW )
.build();

discoverables.forEach( consumer );
Expand All @@ -116,11 +121,11 @@ public void shouldInvokeConsumerForEachKeyWithHighestPrecedenceOnce() throws URI
{
DiscoverableURIs discoverables =
new DiscoverableURIs.Builder()
.add( "a", "/test1", DiscoverableURIs.LOWEST )
.add( "a", "/test2", DiscoverableURIs.LOW )
.add( "a", "/data3", DiscoverableURIs.NORMAL )
.add( "a", "/test4", DiscoverableURIs.HIGH )
.add( "a", "/test5", DiscoverableURIs.HIGHEST )
.add( "a", "/test1", LOWEST )
.add( "a", "/test2", LOW )
.add( "a", "/data3", NORMAL )
.add( "a", "/test4", HIGH )
.add( "a", "/test5", HIGHEST )
.build();

discoverables.forEach( consumer );
Expand All @@ -132,7 +137,7 @@ public void shouldInvokeConsumerForEachKeyWithHighestPrecedenceOnce() throws URI
public void shouldConvertStringIntoURI() throws URISyntaxException
{
DiscoverableURIs empty = new DiscoverableURIs.Builder()
.add( "a", "bolt://localhost:7687", DiscoverableURIs.NORMAL )
.add( "a", "bolt://localhost:7687", NORMAL )
.build();

empty.forEach( consumer );
Expand All @@ -144,7 +149,7 @@ public void shouldConvertStringIntoURI() throws URISyntaxException
public void shouldConvertSchemeHostPortIntoURI() throws URISyntaxException
{
DiscoverableURIs empty = new DiscoverableURIs.Builder()
.add( "a", "bolt", "www.example.com", 8888, DiscoverableURIs.NORMAL )
.add( "a", "bolt", "www.example.com", 8888, NORMAL )
.build();

empty.forEach( consumer );
Expand All @@ -158,7 +163,7 @@ public void shouldUsePassedURI() throws URISyntaxException
URI uri = new URI( "bolt://www.example.com:9999" );

DiscoverableURIs empty = new DiscoverableURIs.Builder()
.add( "a", uri, DiscoverableURIs.NORMAL )
.add( "a", uri, NORMAL )
.build();

empty.forEach( consumer );
Expand All @@ -171,7 +176,7 @@ public void shouldOverrideLowestForAbsolute() throws URISyntaxException
{
URI override = new URI( "http://www.example.com:9999" );
DiscoverableURIs empty = new DiscoverableURIs.Builder()
.add( "a", "bolt://localhost:8989", DiscoverableURIs.LOWEST )
.add( "a", "bolt://localhost:8989", LOWEST )
.overrideAbsolutesFromRequest( override )
.build();

Expand All @@ -185,10 +190,10 @@ public void shouldNotOverrideOtherThanLowestForAbsolute() throws URISyntaxExcept
{
URI override = new URI( "http://www.example.com:9999" );
DiscoverableURIs empty = new DiscoverableURIs.Builder()
.add( "a", "bolt://localhost:8989", DiscoverableURIs.LOW )
.add( "b", "bolt://localhost:8990", DiscoverableURIs.NORMAL )
.add( "c", "bolt://localhost:8991", DiscoverableURIs.HIGH )
.add( "d", "bolt://localhost:8992", DiscoverableURIs.HIGHEST )
.add( "a", "bolt://localhost:8989", LOW )
.add( "b", "bolt://localhost:8990", NORMAL )
.add( "c", "bolt://localhost:8991", HIGH )
.add( "d", "bolt://localhost:8992", HIGHEST )
.overrideAbsolutesFromRequest( override )
.build();

Expand Down
Expand Up @@ -29,6 +29,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.neo4j.server.rest.discovery.DiscoverableURIs.Precedence.NORMAL;

public class DiscoveryRepresentationTest
{
Expand All @@ -39,9 +40,9 @@ public void shouldCreateAMapContainingDataAndManagementURIs() throws URISyntaxEx
String dataUri = "/data";
DiscoveryRepresentation dr = new DiscoveryRepresentation(
new DiscoverableURIs.Builder()
.add( "management", managementUri, DiscoverableURIs.NORMAL )
.add( "data", dataUri, DiscoverableURIs.NORMAL )
.add( "bolt", new URI( "bolt://localhost:7687" ), DiscoverableURIs.NORMAL ).build() );
.add( "management", managementUri, NORMAL )
.add( "data", dataUri, NORMAL )
.add( "bolt", new URI( "bolt://localhost:7687" ), NORMAL ).build() );

Map<String,Object> mapOfUris = RepresentationTestAccess.serialize( dr );

Expand Down

0 comments on commit c10d32c

Please sign in to comment.