/** * Method that will try to convert value of this node to a Java <b>boolean</b>. * JSON booleans map naturally; integer numbers other than 0 map to true, and * 0 maps to false * and Strings 'true' and 'false' map to corresponding values. *<p> * If representation can not be converted to a boolean value (including structured types * like Objects and Arrays), * default value of <b>false</b> will be returned; no exceptions are thrown. * * @since 1.9 (replaces <code>getValueAsBoolean</code>) */ public boolean asBoolean() { return asBoolean(false); }
/** * Method that will try to convert value of this node to a Java <b>boolean</b>. * JSON booleans map naturally; integer numbers other than 0 map to true, and * 0 maps to false * and Strings 'true' and 'false' map to corresponding values. *<p> * If representation can not be converted to a boolean value (including structured types * like Objects and Arrays), * specified <b>defaultValue</b> will be returned; no exceptions are thrown. * * @since 1.7 * * @deprecated Since 1.9, use {@link #asBoolean} instead */ @Deprecated public boolean getValueAsBoolean(boolean defaultValue) { return asBoolean(defaultValue); }
/** * Method that will try to convert value of this node to a Java <b>boolean</b>. * JSON booleans map naturally; integer numbers other than 0 map to true, and * 0 maps to false * and Strings 'true' and 'false' map to corresponding values. *<p> * If representation can not be converted to a boolean value (including structured types * like Objects and Arrays), * default value of <b>false</b> will be returned; no exceptions are thrown. * * @since 1.7 * * @deprecated Since 1.9, use {@link #asBoolean} instead */ @Deprecated public boolean getValueAsBoolean() { return asBoolean(false); }
@Override protected boolean matchesSafely( HTTP.Response response ) { try { JsonNode list = TransactionMatchers.getJsonNodeWithName( response, "rest" ).iterator().next(); assertThat( list.get( 0 ).get( "metadata" ).get( "deleted" ).asBoolean(), equalTo( Boolean.TRUE ) ); assertThat( list.get( 1 ).get( "someKey" ).get( "metadata" ).get( "deleted" ).asBoolean(), equalTo( Boolean.TRUE ) ); return true; } catch ( JsonParseException e ) { return false; } }
private boolean isAddressableValue(Schema value) { if (value.getType().equals(Type.RECORD)) { if (value.getJsonProp(ADDRESSABLE_NAME) != null && !value.getFullName().equals(rootSchemaName)) { return value.getJsonProp(ADDRESSABLE_NAME).asBoolean(); } return true; } return false; }
@Test public void shouldWriteNestedMaps() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator json = new JsonFactory( new Neo4jJsonCodec() ).createJsonGenerator( out ); JsonNode rest = serialize( out, json, new RestRepresentationWriter( URI.create( "localhost" ) ) ); MatcherAssert.assertThat( rest.size(), equalTo( 1 ) ); JsonNode firstCell = rest.get( 0 ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); }
@Test public void shouldWriteNestedMaps() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator json = new JsonFactory( new Neo4jJsonCodec() ).createJsonGenerator( out ); JsonNode row = serialize( out, json, new RowWriter( ) ); MatcherAssert.assertThat( row.size(), equalTo( 1 ) ); JsonNode firstCell = row.get( 0 ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); MatcherAssert.assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); }
@Override protected boolean matchesSafely( HTTP.Response response ) { try { for ( JsonNode node : getJsonNodeWithName( response, "meta" ) ) { assertFalse( node.get( "deleted" ).asBoolean() ); } return true; } catch ( JsonParseException e ) { return false; } }
@Test public void shouldSerializeMapsCorrectlyInRowsFormat() throws Exception { Response response = http.POST( "db/data/transaction/commit", quotedJson( "{ 'statements': [ { 'statement': 'RETURN {one:{two:[true, {three: 42}]}}' } ] }" ) ); // then assertThat( response.status(), equalTo( 200 ) ); JsonNode data = response.get( "results" ).get( 0 ); JsonNode row = data.get( "data" ).get( 0 ).get( "row" ); assertThat( row.size(), equalTo( 1 ) ); JsonNode firstCell = row.get( 0 ); assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); }
@Override protected boolean matchesSafely( HTTP.Response response ) { try { Iterator<JsonNode> relationships = getJsonNodeWithName( response, "graph" ).get( "relationships" ).iterator(); for ( int i = 0; i < amount; ++i ) { assertTrue( relationships.hasNext() ); JsonNode node = relationships.next(); assertThat( node.get( "deleted" ).asBoolean(), equalTo( Boolean.TRUE ) ); } if ( relationships.hasNext() ) { JsonNode node = relationships.next(); fail( "Expected no more nodes, but got a node with id " + node.get( "id" ) ); } return true; } catch ( JsonParseException e ) { return false; } }
@Test public void shouldSerializeMapsCorrectlyInRestFormat() throws Exception { Response response = http.POST( "db/data/transaction/commit", quotedJson( "{ 'statements': [ { 'statement': " + "'RETURN {one:{two:[true, {three: " + "42}]}}', " + "'resultDataContents':['rest'] } ] " + "}" ) ); // then assertThat( response.status(), equalTo( 200 ) ); JsonNode data = response.get( "results" ).get( 0 ); JsonNode rest = data.get( "data" ).get( 0 ).get( "rest" ); assertThat( rest.size(), equalTo( 1 ) ); JsonNode firstCell = rest.get( 0 ); assertThat( firstCell.get( "one" ).get( "two" ).size(), is( 2 ) ); assertThat( firstCell.get( "one" ).get( "two" ).get( 0 ).asBoolean(), is( true ) ); assertThat( firstCell.get( "one" ).get( "two" ).get( 1 ).get( "three" ).asInt(), is( 42 ) ); assertThat( response.get( "errors" ).size(), equalTo( 0 ) ); }
@Override protected void customizeType(Record record, Schema fieldTypeSchema) { if (record != null && record.getSchema().getName().equals(RECORD_FIELD_TYPE)) { JsonNode addressableNode = fieldTypeSchema.getJsonProp(ADDRESSABLE); if (addressableNode != null && addressableNode.isBoolean()) { record.put(ADDRESSABLE, addressableNode.asBoolean()); } else { record.put(ADDRESSABLE, true); } } }
@Override protected boolean matchesSafely( HTTP.Response response ) { try { Iterator<JsonNode> entities = getJsonNodeWithName( response, "rest" ).iterator(); for ( int i = 0; i < amount; ++i ) { assertTrue( entities.hasNext() ); JsonNode node = entities.next(); assertThat( node.get( "metadata" ).get( "deleted" ).asBoolean(), equalTo( Boolean.TRUE ) ); } if ( entities.hasNext() ) { fail( "Expected no more entities" ); } return true; } catch ( JsonParseException e ) { return false; } }
@Override protected boolean matchesSafely( HTTP.Response response ) { try { Iterator<JsonNode> nodes = getJsonNodeWithName( response, "graph" ).get( "nodes" ).iterator(); int deleted = 0; while ( nodes.hasNext() ) { JsonNode node = nodes.next(); if ( node.get( "deleted" ) != null ) { assertTrue( node.get( "deleted" ).asBoolean() ); deleted++; } } assertEquals( format( "Expected to see %d deleted elements but %d was encountered.", amount, deleted ), amount, deleted ); return true; } catch ( JsonParseException e ) { return false; } }
return node.asBoolean(); } else { return null;
@Test @Documented( "User status\n" + "\n" + "Given that you know the current password, you can ask the server for the user status." ) public void user_status() throws JsonParseException, IOException { // Given startServerWithConfiguredUser(); // Document RESTRequestGenerator.ResponseEntity response = gen.get() .expectedStatus( 200 ) .withHeader( HttpHeaders.AUTHORIZATION, HTTP.basicAuthHeader( "neo4j", "secret" ) ) .get( userURL( "neo4j" ) ); // Then JsonNode data = JsonHelper.jsonNode( response.entity() ); assertThat( data.get( "username" ).asText(), equalTo( "neo4j" ) ); assertThat( data.get( "password_change_required" ).asBoolean(), equalTo( false ) ); assertThat( data.get( "password_change" ).asText(), equalTo( passwordURL( "neo4j" ) ) ); }
@Test @Documented( "User status on first access\n" + "\n" + "On first access, and using the default password, the user status will indicate " + "that the users password requires changing." ) public void user_status_first_access() throws JsonParseException, IOException { // Given startServer( true ); // Document RESTRequestGenerator.ResponseEntity response = gen.get() .expectedStatus( 200 ) .withHeader( HttpHeaders.AUTHORIZATION, HTTP.basicAuthHeader( "neo4j", "neo4j" ) ) .get( userURL( "neo4j" ) ); // Then JsonNode data = JsonHelper.jsonNode( response.entity() ); assertThat( data.get( "username" ).asText(), equalTo( "neo4j" ) ); assertThat( data.get( "password_change_required" ).asBoolean(), equalTo( true ) ); assertThat( data.get( "password_change" ).asText(), equalTo( passwordURL( "neo4j" ) ) ); }
@Test @Documented( "Authenticate to access the server\n" + "\n" + "Authenticate by sending a username and a password to Neo4j using HTTP Basic Auth.\n" + "Requests should include an +Authorization+ header, with a value of +Basic <payload>+,\n" + "where \"payload\" is a base64 encoded string of \"username:password\"." ) public void successful_authentication() throws JsonParseException, IOException { // Given startServerWithConfiguredUser(); // Document RESTRequestGenerator.ResponseEntity response = gen.get() .expectedStatus( 200 ) .withHeader( HttpHeaders.AUTHORIZATION, HTTP.basicAuthHeader( "neo4j", "secret" ) ) .get( userURL( "neo4j" ) ); // Then JsonNode data = JsonHelper.jsonNode( response.entity() ); assertThat( data.get( "username" ).asText(), equalTo( "neo4j" ) ); assertThat( data.get( "password_change_required" ).asBoolean(), equalTo( false ) ); assertThat( data.get( "password_change" ).asText(), equalTo( passwordURL( "neo4j" ) ) ); }