Skip to content

Commit

Permalink
A fix for #276 that doesn break everything
Browse files Browse the repository at this point in the history
  • Loading branch information
systay authored and Peter Neubauer committed Mar 9, 2012
1 parent 1671aed commit d96e717
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 111 deletions.
Expand Up @@ -19,19 +19,11 @@
*/
package org.neo4j.server.rest;

import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.containsString;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.ws.rs.core.Response.Status;

import org.junit.Ignore;
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.Pair;
import org.neo4j.kernel.impl.annotations.Documented;
import org.neo4j.server.rest.domain.JsonHelper;
Expand All @@ -42,6 +34,16 @@
import org.neo4j.test.GraphDescription.REL;
import org.neo4j.test.TestData.Title;

import javax.ws.rs.core.Response.Status;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.containsString;

public class CypherFunctionalTest extends AbstractRestFunctionalTestBase {


Expand Down Expand Up @@ -148,13 +150,35 @@ public void send_queries_with_syntax_errors() throws Exception {
@Graph( value = { "I know you" }, autoIndexNodes = true )
public void nested_results() throws Exception {
data.get();
String script = "start n = node(%I%,%you%) return collect(n.name), collect(n)";
String response = cypherRestCall( script, Status.OK);

String script = "start n = node(%I%,%you%) return collect(n.name)";
String response = cypherRestCall(script, Status.OK);

Map<String, Object> resultMap = JsonHelper.jsonToMap( response );
assertEquals( 2, resultMap.size() );
assertTrue( response.contains( "[ [ [ \"I\"" ) );
assertEquals(2, resultMap.size());
assertThat(response, containsString("\"I\", \"you\""));
}

@Test
@Graph( value = { "I know you" }, autoIndexNodes = false )
public void array_property() throws Exception {
setProperty("I", "array1", new int[] { 1, 2, 3 } );
setProperty("I", "array2", new String[] { "a", "b", "c" } );

String script = "start n = node(%I%) return n.array1, n.array2";
String response = cypherRestCall( script, Status.OK );

assertThat(response, containsString("[ 1, 2, 3 ]"));
assertThat(response, containsString("[ \"a\", \"b\", \"c\" ]"));
}

void setProperty(String nodeName, String propertyName, Object propertyValue) {
Node i = this.getNode(nodeName);
GraphDatabaseService db = i.getGraphDatabase();

Transaction tx = db.beginTx();
i.setProperty(propertyName, propertyValue);
tx.success();
tx.finish();
}

@Test
Expand All @@ -167,17 +191,14 @@ public void send_queries_with_errors() throws Exception {
".name = {name} return TYPE(r)";
String response = cypherRestCall( script, Status.BAD_REQUEST, Pair.of( "startName", "I" ), Pair.of( "name", "you" ) );


assertEquals( 3, ( JsonHelper.jsonToMap( response ) ).size() );
assertTrue( response.contains( "message" ) );
assertThat( response, containsString( "message" ) );
}

private String cypherRestCall( String script, Status status,
Pair<String, String> ...params )
private String cypherRestCall( String script, Status status, Pair<String, String> ...params )
{
return super.doCypherRestCall( cypherUri(), script, status, params );
}


private String cypherUri()
{
Expand All @@ -188,6 +209,4 @@ private String cypherAltUri()
{
return getDataUri() + "cypher_alt";
}


}
}
Expand Up @@ -19,18 +19,15 @@
*/
package org.neo4j.server.rest.repr;

import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.server.webadmin.rest.representations.JmxAttributeRepresentationDispatcher;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;

public class CypherResultRepresentation extends ObjectRepresentation
{

private final ExecutionResult queryResult;

public CypherResultRepresentation( ExecutionResult result )
Expand All @@ -42,7 +39,6 @@ public CypherResultRepresentation( ExecutionResult result )
@Mapping( "columns" )
public Representation columns()
{

return ListRepresentation.string( queryResult.columns() );
}

Expand All @@ -59,71 +55,34 @@ public Representation data()
{
Representation rowRep = getRepresentation( row.get( column ) );
fields.add( rowRep );

}
rows.add( new ListRepresentation( "row", fields ) );
}
return new ListRepresentation( "data", rows );
}

private Representation getRepresentation( Object r )
Representation getRepresentation( Object r )
{
if(r == null ) {
if( r == null ) {
return ValueRepresentation.string( null );
} else if(r instanceof Iterable) {
return handleIterable( (Iterable) r );
}
if ( r instanceof Node )
{
return new NodeRepresentation( (Node) r );
}
if ( r instanceof Relationship )
{
return new RelationshipRepresentation( (Relationship) r );
}
else if ( r instanceof Double || r instanceof Float )
{
return ValueRepresentation.number( ( (Number) r ).doubleValue() );
}
else if ( r instanceof Long || r instanceof Integer )
{
return ValueRepresentation.number( ( (Number) r ).longValue() );
}
else if ( r instanceof Path )
{
return new PathRepresentation<Path>((Path) r );
}
else if ( r instanceof Iterable )
{
return getListRepresentation( (Iterable) r );
}
else
{
return ValueRepresentation.string( r.toString() );
}
}

Representation getListRepresentation( Iterable data )
{
final List<Representation> results = convertValuesToRepresentations( data );
return new ListRepresentation( getType( results ), results );
JmxAttributeRepresentationDispatcher representationDispatcher = new JmxAttributeRepresentationDispatcher();
return representationDispatcher.dispatch( r, "" );
}

List<Representation> convertValuesToRepresentations( Iterable data )
{
Representation handleIterable( Iterable data ) {
final List<Representation> results = new ArrayList<Representation>();
for ( final Object value : data )
{
if ( value instanceof Iterable )
{
List<Representation> nested = new ArrayList<Representation>();
nested.addAll( convertValuesToRepresentations( (Iterable) value ) );
results.add( new ListRepresentation( getType( nested ), nested ) );
}
else
{
results.add( getSingleRepresentation( value ) );
}
Representation rep = getRepresentation(value);
results.add(rep);
}
return results;

RepresentationType representationType = getType(results);
return new ListRepresentation( representationType, results );
}

RepresentationType getType( List<Representation> representations )
Expand All @@ -132,34 +91,4 @@ RepresentationType getType( List<Representation> representations )
return RepresentationType.STRING;
return representations.get( 0 ).getRepresentationType();
}

Representation getSingleRepresentation( Object result )
{
if ( result == null )
return ValueRepresentation.string( "null" );
else if ( result instanceof Node )
{
return new NodeRepresentation( (Node) result );
}
else if ( result instanceof Relationship )
{
return new RelationshipRepresentation( (Relationship) result );
}
else if ( result instanceof Double || result instanceof Float )
{
return ValueRepresentation.number( ( (Number) result ).doubleValue() );
}
else if ( result instanceof Long )
{
return ValueRepresentation.number( ( (Long) result ).longValue() );
}
else if ( result instanceof Integer )
{
return ValueRepresentation.number( ( (Integer) result ).intValue() );
}
else
{
return ValueRepresentation.string( result.toString() );
}
}
}
}

0 comments on commit d96e717

Please sign in to comment.