/** * Create a unique node or return fail (create). * * <p>Here, in case of an already existing node, an error should be returned. In this example, no * existing indexed node is found and a new node is created. */ @Documented @Test public void create_a_unique_node_or_fail_create() throws Exception { final String index = "people", key = "name", value = "Tobias"; helper.createNodeIndex(index); ResponseEntity response = gen.get() .noGraph() .expectedStatus(201 /* created */) .payloadType(MediaType.APPLICATION_JSON_TYPE) .payload( "{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"sequence\": 1}}") .post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=create_or_fail"); MultivaluedMap<String, String> headers = response.response().getHeaders(); Map<String, Object> result = JsonHelper.jsonToMap(response.entity()); assertEquals(result.get("indexed"), headers.getFirst("Location")); Map<String, Object> data = assertCast(Map.class, result.get("data")); assertEquals(value, data.get(key)); assertEquals(1, data.get("sequence")); }
/** Remove all entries with a given node, key and value from an index. */ @Documented @Test public void shouldBeAbleToRemoveIndexingByIdAndKeyAndValue() { String key1 = "kvkey1"; String key2 = "kvkey2"; String value1 = "value1"; String value2 = "value2"; String indexName = "kvnode"; long node = helper.createNode(MapUtil.map(key1, value1, key1, value2, key2, value1, key2, value2)); helper.addNodeToIndex(indexName, key1, value1, node); helper.addNodeToIndex(indexName, key1, value2, node); helper.addNodeToIndex(indexName, key2, value1, node); helper.addNodeToIndex(indexName, key2, value2, node); gen() .noGraph() .expectedStatus(204) .delete( functionalTestHelper.nodeIndexUri() + indexName + "/" + key1 + "/" + value1 + "/" + node); assertEquals(0, helper.getIndexedNodes(indexName, key1, value1).size()); assertEquals(1, helper.getIndexedNodes(indexName, key1, value2).size()); assertEquals(1, helper.getIndexedNodes(indexName, key2, value1).size()); assertEquals(1, helper.getIndexedNodes(indexName, key2, value2).size()); }
@Test public void get_or_create_node_with_array_properties() throws Exception { final String index = "people", key = "name", value = "Tobias"; helper.createNodeIndex(index); ResponseEntity response = gen() .expectedStatus(201 /* created */) .payloadType(MediaType.APPLICATION_JSON_TYPE) .payload( "{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"array\": [1,2,3]}}") .post(functionalTestHelper.nodeIndexUri() + index + "?unique"); MultivaluedMap<String, String> headers = response.response().getHeaders(); Map<String, Object> result = JsonHelper.jsonToMap(response.entity()); String location = headers.getFirst("Location"); assertEquals(result.get("indexed"), location); Map<String, Object> data = assertCast(Map.class, result.get("data")); assertEquals(value, data.get(key)); assertEquals(Arrays.asList(1, 2, 3), data.get("array")); Node node; try (Transaction tx = graphdb().beginTx()) { node = graphdb().index().forNodes(index).get(key, value).getSingle(); } assertThat(node, inTx(graphdb(), hasProperty(key).withValue(value))); assertThat(node, inTx(graphdb(), hasProperty("array").withValue(new int[] {1, 2, 3}))); }
@Test public void shouldGet404WhenDeletingNonExtistentIndex() { String indexName = "nosuchindex"; String indexUri = functionalTestHelper.nodeIndexUri() + indexName; JaxRsResponse response = RestRequest.req().delete(indexUri); assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); }
@Test public void shouldGetNodeIndexRoot() { JaxRsResponse response = RestRequest.req().get(functionalTestHelper.nodeIndexUri(), MediaType.TEXT_HTML_TYPE); assertEquals(Status.OK.getStatusCode(), response.getStatus()); assertValidHtml(response.getEntity()); response.close(); }
@Test public void shouldGet404WhenRequestingIndexUriWhichDoesntExist() { String key = "key3"; String value = "value"; String indexName = "nosuchindex"; String indexUri = functionalTestHelper.nodeIndexUri() + indexName + "/" + key + "/" + value; JaxRsResponse response = RestRequest.req().get(indexUri); assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); }
/** List node indexes. */ @Documented @Test public void shouldGetListOfNodeIndexesWhenOneExist() throws PropertyValueException { String indexName = "favorites"; helper.createNodeIndex(indexName); String entity = gen().noGraph().expectedStatus(200).get(functionalTestHelper.nodeIndexUri()).entity(); Map<String, Object> map = JsonHelper.jsonToMap(entity); assertNotNull(map.get(indexName)); assertEquals( "Was: " + map + ", no-auto-index:" + functionalTestHelper.removeAnyAutoIndex(map), 1, functionalTestHelper.removeAnyAutoIndex(map).size()); }
/** * Create node index with configuration. This request is only necessary if you want to customize * the index settings. If you are happy with the defaults, you can just start indexing * nodes/relationships, as non-existent indexes will automatically be created as you do. See * <<indexing-create-advanced>> for more information on index configuration. */ @Documented @Test public void shouldCreateANamedNodeIndexWithConfiguration() throws Exception { int expectedIndexes = helper.getNodeIndexes().length + 1; gen() .noGraph() .payload( "{\"name\":\"fulltext\", \"config\":{\"type\":\"fulltext\",\"provider\":\"lucene\"}}") .expectedStatus(201) .expectedHeader("Location") .post(functionalTestHelper.nodeIndexUri()); assertEquals(expectedIndexes, helper.getNodeIndexes().length); assertThat(helper.getNodeIndexes(), FunctionalTestHelper.arrayContains("fulltext")); }
@Test public void shouldCreateANamedNodeIndexWithSpaces() { String indexName = "favorites with spaces"; int expectedIndexes = helper.getNodeIndexes().length + 1; Map<String, String> indexSpecification = new HashMap<>(); indexSpecification.put("name", indexName); gen() .payload(JsonHelper.createJsonFrom(indexSpecification)) .expectedStatus(201) .expectedHeader("Location") .post(functionalTestHelper.nodeIndexUri()); assertEquals(expectedIndexes, helper.getNodeIndexes().length); assertThat(helper.getNodeIndexes(), FunctionalTestHelper.arrayContains(indexName)); }
/** * Backward Compatibility Test (using old syntax ?unique) Put node if absent - Create. * * <p>Add a node to an index unless a node already exists for the given index data. In this case, * a new node is created since nothing existing is found in the index. */ @Documented @Test public void put_node_if_absent___create() throws Exception { final String index = "people", key = "name", value = "Mattias"; helper.createNodeIndex(index); String uri = functionalTestHelper.nodeIndexUri() + index + "?unique"; gen() .expectedStatus(201 /* created */) .payloadType(MediaType.APPLICATION_JSON_TYPE) .payload( "{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"uri\":\"" + functionalTestHelper.nodeUri(helper.createNode()) + "\"}") .post(uri); }
/** * Create a unique node or return fail (fail). * * <p>Here, in case of an already existing node, an error should be returned. In this example, an * existing node indexed with the same data is found and an error is returned. */ @Documented @Test public void create_a_unique_node_or_return_fail___fail() throws Exception { final String index = "people", key = "name", value = "Peter"; GraphDatabaseService graphdb = graphdb(); helper.createNodeIndex(index); try (Transaction tx = graphdb.beginTx()) { Node peter = graphdb.createNode(); peter.setProperty(key, value); peter.setProperty("sequence", 1); graphdb.index().forNodes(index).add(peter, key, value); tx.success(); } RestRequest.req(); ResponseEntity response = gen.get() .noGraph() .expectedStatus(409 /* conflict */) .payloadType(MediaType.APPLICATION_JSON_TYPE) .payload( "{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"sequence\": 2}}") .post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=create_or_fail"); Map<String, Object> result = JsonHelper.jsonToMap(response.entity()); Map<String, Object> data = assertCast(Map.class, result.get("data")); assertEquals(value, data.get(key)); assertEquals(1, data.get("sequence")); }