private void checkContentTypeCharsetUtf8( Response response ) { assertTrue( response.getMetadata() .getFirst( HttpHeaders.CONTENT_TYPE ).toString().contains( "UTF-8" )); }
@Test public void testFullAppsGetGzipJsonHeaderType() throws Exception { Response response = applicationsResource.getContainers( Version.V2.name(), MediaType.APPLICATION_JSON, "gzip", // encoding EurekaAccept.full.name(), null, // uriInfo null // remote regions ); assertThat(response.getMetadata().getFirst("Content-Encoding").toString(), is("gzip")); assertThat(response.getMetadata().getFirst("Content-Type").toString(), is(MediaType.APPLICATION_JSON)); }
@Test public void testFullAppsGetGzipXmlHeaderType() throws Exception { Response response = applicationsResource.getContainers( Version.V2.name(), MediaType.APPLICATION_XML, "gzip", // encoding EurekaAccept.full.name(), null, // uriInfo null // remote regions ); assertThat(response.getMetadata().getFirst("Content-Encoding").toString(), is("gzip")); assertThat(response.getMetadata().getFirst("Content-Type").toString(), is(MediaType.APPLICATION_XML)); }
@Test public void shouldBeAbleToIndexNode() { Response response = service.createNode( null ); URI nodeUri = (URI) response.getMetadata() .getFirst( "Location" ); Map<String, String> postBody = new HashMap<>(); postBody.put( "key", "mykey" ); postBody.put( "value", "my/key" ); postBody.put( "uri", nodeUri.toString() ); response = service.addToNodeIndex( "node", null, null, JsonHelper.createJsonFrom( postBody ) ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata() .getFirst( "Location" ) ); }
@Test public void shouldNotBeAbleToIndexRelationshipUniquelyWithRequiredParameterMissing() { URI start = (URI) service.createNode( null ).getMetadata().getFirst( "Location" ); URI end = (URI) service.createNode( null ).getMetadata().getFirst( "Location" ); Map<String, Object> body = new HashMap<>(); body.put( "key", "mykey" ); body.put( "value", "my/key" ); body.put( "start", start.toString() ); body.put( "end", end.toString() ); body.put( "type", "knows" ); for ( String key : body.keySet() ) { Map<String, Object> postBody = new HashMap<>( body ); postBody.remove( key ); Response response = service.addToRelationshipIndex( "unique-relationships", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( "unexpected response code with \"" + key + "\" missing.", 400, response.getStatus() ); } }
@Test public void shouldBeAbleToIndexRelationshipUniquely() { URI start = (URI) service.createNode( null ).getMetadata().getFirst( "Location" ); URI end = (URI) service.createNode( null ).getMetadata().getFirst( "Location" ); Map<String, String> postBody = new HashMap<>(); postBody.put( "key", "mykey" ); postBody.put( "value", "my/key" ); postBody.put( "start", start.toString() ); postBody.put( "end", end.toString() ); postBody.put( "type", "knows" ); for ( int i = 0; i < 2; i++ ) { Response response = service.addToNodeIndex( "unique-relationships", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 201 - i, response.getStatus() ); if ( i == 0 ) { assertNotNull( response.getMetadata().getFirst( "Location" ) ); } } }
@Test public void shouldNotBeAbleToIndexNodeUniquelyWithRequiredParameterMissing() { service.createNode( null ).getMetadata().getFirst( "Location" ); Map<String, Object> body = new HashMap<>(); body.put( "key", "mykey" ); body.put( "value", "my/key" ); for ( String key : body.keySet() ) { Map<String, Object> postBody = new HashMap<>( body ); postBody.remove( key ); Response response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( "unexpected response code with \"" + key + "\" missing.", 400, response.getStatus() ); } }
@Test public void shouldReturnAbsoluteURIForSeeOther() throws Exception { URI relativeURI = new URI( "/test/path" ); OutputFormat outputFormat = new OutputFormat( new JsonFormat(), new URI( "http://base.local:8765/" ), null ); Response response = outputFormat.seeOther( relativeURI ); assertEquals( 303, response.getStatus() ); assertEquals( new URI( "http://base.local:8765/test/path" ), response.getMetadata().getFirst( "Location" ) ); } }
@Test public void specifiedPropertiesOverrideKeyAndValueForUniquelyIndexedNodes() { final String key = "a_key"; String value = "a value"; Map<String, Object> postBody = new HashMap<>(); postBody.put( "key", key ); postBody.put( "value", value ); Map<String, Object> properties = new HashMap<>(); properties.put( "name", "Jürgen" ); properties.put( "age", "42" ); properties.put( "occupation", "crazy" ); postBody.put( "properties", properties ); Response response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 201, response.getStatus() ); Object node = response.getMetadata().getFirst( "Location" ); assertNotNull( node ); String uri = node.toString(); assertEquals( properties, helper.getNodeProperties( parseLong( uri.substring( uri.lastIndexOf( '/' ) + 1 ) ) ) ); }
@Test public void uniquelyIndexedNodeGetsTheSpecifiedKeyAndValueAsPropertiesIfNoPropertiesAreSpecified() { final String key = "somekey"; String value = "somevalue"; Map<String, Object> postBody = new HashMap<>(); postBody.put( "key", key ); postBody.put( "value", value ); Response response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 201, response.getStatus() ); Object node = response.getMetadata().getFirst( "Location" ); assertNotNull( node ); String uri = node.toString(); Map<String, Object> properties = helper.getNodeProperties( parseLong( uri.substring( uri.lastIndexOf( '/' ) + 1 ) ) ); assertEquals( 1, properties.size() ); assertEquals( value, properties.get( key ) ); }
@Test public void shouldNotBeAbleToIndexNodeUniquelyWithBothUriAndPropertiesInPayload() throws Exception { URI node = (URI) service.createNode( null ).getMetadata().getFirst( "Location" ); Map<String, Object> postBody = new HashMap<>(); postBody.put( "key", "mykey" ); postBody.put( "value", "my/key" ); postBody.put( "uri", node.toString() ); postBody.put( "properties", new HashMap<String,Object>() ); Response response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 400, response.getStatus() ); assertEquals( Statement.ArgumentError.code().serialize(), singleErrorCode( response ) ); }
@Test public void shouldRespondWith201AndLocationWhenRelationshipIsCreatedWithoutProperties() { long startNode = helper.createNode(); long endNode = helper.createNode(); Response response = service.createRelationship( startNode, "{\"to\" : \"" + BASE_URI + endNode + "\", \"type\" : \"LOVES\"}" ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata() .get( "Location" ) .get( 0 ) ); }
@Test public void shouldBeAbleToIndexNodeUniquely() { Map<String, String> postBody = new HashMap<>(); postBody.put( "key", "mykey" ); postBody.put( "value", "my/key" ); Response response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata().getFirst( "Location" ) ); response = service.addToNodeIndex( "unique-nodes", "", "", JsonHelper.createJsonFrom( postBody ) ); assertEquals( 200, response.getStatus() ); }
@Test public void shouldRespondWith201AndLocationWhenRelationshipIsCreatedWithProperties() { long startNode = helper.createNode(); long endNode = helper.createNode(); Response response = service.createRelationship( startNode, "{\"to\" : \"" + BASE_URI + endNode + "\", \"type\" : \"LOVES\", " + "\"properties\" : {\"foo\" : \"bar\"}}" ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata() .get( "Location" ) .get( 0 ) ); }
@Test public void shouldReturnRedirectToAbsoluteAPIUsingOutputFormat() throws Exception { Config config = Config.defaults( ServerSettings.browser_path, "/browser/" ); String baseUri = "http://www.example.com:5435"; DiscoveryService ds = new DiscoveryService( config, new EntityOutputFormat( new JsonFormat(), new URI( baseUri ), null ), communityDiscoverableURIs( config, null ) ); Response response = ds.redirectToBrowser(); assertThat( response.getMetadata().getFirst( "Location" ), is( new URI( "http://www.example" + ".com:5435/browser/" ) ) ); }
@Test @SuppressWarnings( "unchecked" ) public void shouldRespondWith201LocationHeaderAndNodeRepresentationInJSONWhenPopulatedNodeCreatedWithArrays() throws Exception { Response response = service.createNode( "{\"foo\" : [\"bar\", \"baz\"] }" ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata() .get( "Location" ) .get( 0 ) ); String json = entityAsString( response ); Map<String, Object> map = JsonHelper.jsonToMap( json ); assertNotNull( map ); Map<String, Object> data = (Map<String, Object>) map.get( "data" ); List<String> foo = (List<String>) data.get( "foo" ); assertNotNull( foo ); assertEquals( 2, foo.size() ); }
@Test public void shouldRespondWith201LocationHeaderAndNodeRepresentationInJSONWhenEmptyNodeCreated() throws Exception { Response response = service.createNode( null ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata() .get( "Location" ) .get( 0 ) ); checkContentTypeCharsetUtf8( response ); String json = entityAsString( response ); Map<String, Object> map = JsonHelper.jsonToMap( json ); assertNotNull( map ); assertTrue( map.containsKey( "self" ) ); }
@Test public void shouldRespondWith201LocationHeaderAndNodeRepresentationInJSONWhenPopulatedNodeCreated() throws Exception { Response response = service.createNode( "{\"foo\" : \"bar\"}" ); assertEquals( 201, response.getStatus() ); assertNotNull( response.getMetadata() .get( "Location" ) .get( 0 ) ); checkContentTypeCharsetUtf8(response); String json = entityAsString( response ); Map<String, Object> map = JsonHelper.jsonToMap( json ); assertNotNull( map ); assertTrue( map.containsKey( "self" ) ); @SuppressWarnings( "unchecked" ) Map<String, Object> data = (Map<String, Object>) map.get( "data" ); assertEquals( "bar", data.get( "foo" ) ); }
@Test public void shouldBeAbleToGetNodeRepresentationFromIndexUri() throws Exception { String key = "key_get_noderep"; String value = "value"; long nodeId = helper.createNode(); String indexName = "all-the-best-nodes"; helper.addNodeToIndex( indexName, key, value, nodeId ); Response response = service.getNodeFromIndexUri( indexName, key, value, nodeId ); assertEquals( 200, response.getStatus() ); checkContentTypeCharsetUtf8( response ); assertNull( response.getMetadata() .get( "Location" ) ); Map<String, Object> map = JsonHelper.jsonToMap( entityAsString( response ) ); assertNotNull( map ); assertTrue( map.containsKey( "self" ) ); }
@Test public void shouldBeAbleToGetRelationshipRepresentationFromIndexUri() throws Exception { String key = "key_get_noderep"; String value = "value"; long startNodeId = helper.createNode(); long endNodeId = helper.createNode(); String relationshipType = "knows"; long relationshipId = helper.createRelationship( relationshipType, startNodeId, endNodeId ); String indexName = "all-the-best-relationships"; helper.addRelationshipToIndex( indexName, key, value, relationshipId ); Response response = service.getRelationshipFromIndexUri( indexName, key, value, relationshipId ); assertEquals( 200, response.getStatus() ); checkContentTypeCharsetUtf8(response); assertNull( response.getMetadata() .get( "Location" ) ); Map<String, Object> map = JsonHelper.jsonToMap( entityAsString( response ) ); assertNotNull( map ); assertTrue( map.containsKey( "self" ) ); }