@Test public void testCreateRelationship() { Node n1 = restAPI.createNode(map("name", "newnode1")); final Transaction tx = restAPI.beginTx(); Node n2 = restAPI.createNode(map("name", "newnode2")); RestRelationship rel = restAPI.createRelationship(n1, n2, Type.TEST, map("name", "rel")); Iterable<Relationship> allRelationships = n1.getRelationships(); tx.success(); tx.finish(); Relationship foundRelationship = TestHelper.firstRelationshipBetween( n1.getRelationships(Type.TEST, Direction.OUTGOING), n1, n2); Assert.assertNotNull("found relationship", foundRelationship); assertEquals("same relationship", rel, foundRelationship); assertEquals("rel", rel.getProperty("name")); assertThat( n1.getRelationships(Type.TEST, Direction.OUTGOING), new IsRelationshipToNodeMatcher(n1, n2)); assertThat(n1.getRelationships(Direction.OUTGOING), new IsRelationshipToNodeMatcher(n1, n2)); assertThat(n1.getRelationships(Direction.BOTH), new IsRelationshipToNodeMatcher(n1, n2)); assertThat(n1.getRelationships(Type.TEST), new IsRelationshipToNodeMatcher(n1, n2)); assertThat(allRelationships, new IsRelationshipToNodeMatcher(n1, n2)); }
@Test public void testAddToIndex() { final MatrixDataGraph matrixDataGraph = new MatrixDataGraph(getGraphDatabase()); matrixDataGraph.createNodespace(); final RestNode neoNode = restAPI.getNodeById(matrixDataGraph.getNeoNode().getId()); final Transaction tx = restAPI.beginTx(); restAPI.index().forNodes("heroes").add(neoNode, "indexname", "Neo2"); Node n1 = restAPI.createNode(map("name", "Apoc")); final Index<Node> index = restAPI.index().forNodes("heroes"); index.add(n1, "indexname", "Apoc"); final Node indexResult = getGraphDatabase().index().forNodes("heroes").get("indexname", "Neo2").getSingle(); assertNull(indexResult); final IndexHits<Node> heroes = index.query("indexname:Apoc"); tx.success(); tx.finish(); assertEquals("1 hero", 1, heroes.size()); IndexManager realIndex = getGraphDatabase().index(); Index<Node> goodGuys = realIndex.forNodes("heroes"); IndexHits<Node> hits = goodGuys.get("indexname", "Apoc"); Node apoc = hits.getSingle(); assertEquals("Apoc indexed", apoc, heroes.iterator().next()); }
/** * Gets the online data to write it. * * @param doc * @param type */ public void getDataToWrite(Document doc, String type, String name) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); String loc = null; try { // 2 Documents: doc to get the data online, and docu where we will // write the data. DocumentBuilder builder = factory.newDocumentBuilder(); Document docu = builder.newDocument(); Element root = docu.createElement(name.toLowerCase()); docu.appendChild(root); NodeList movieList = doc.getElementsByTagName("movie"); Node p = movieList.item(0); if (p == null) { nodecheck = false; } // Sets the attributes of the new data from the ones gathered from // the document doc. if (p.getNodeType() == Node.ELEMENT_NODE) { Element movie = (Element) p; root.setAttribute("title", movie.getAttribute("title")); root.setAttribute("type", movie.getAttribute("type")); root.setAttribute("year", movie.getAttribute("year")); root.setAttribute("runtime", movie.getAttribute("runtime")); root.setAttribute("genre", movie.getAttribute("genre")); root.setAttribute("plot", movie.getAttribute("plot")); root.setAttribute("language", movie.getAttribute("language")); root.setAttribute("actors", movie.getAttribute("actors")); root.setAttribute("director", movie.getAttribute("director")); } DOMSource source = new DOMSource(docu); // The program understands where yo want to do the action. if (type.equals("series")) { loc = "resources/series.xml"; } else if (type.equals("movies")) { loc = "resources/movies.xml"; } Result result = new StreamResult(loc); TransformerFactory transf = TransformerFactory.newInstance(); Transformer transformer = transf.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); System.out.println("Archivo escrito."); } catch (Exception ex) { System.out.println("Please, write a correct database name."); // If the database name is incorrect, the method will launch again. getDataToWrite(api.getDocu(), api.enterType(), api.enterName()); } }
@Test(expected = org.neo4j.graphdb.NotFoundException.class) public void testDeleteNode() { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map("name", "node1")); n1.delete(); Node n2 = restAPI.createNode(map("name", "node2")); tx.success(); tx.finish(); loadRealNode(n1); }
@Test public void testDeleteIndex() { final MatrixDataGraph matrixDataGraph = new MatrixDataGraph(getGraphDatabase()); matrixDataGraph.createNodespace(); final Transaction tx = restAPI.beginTx(); final Index<Node> heroes = restAPI.index().forNodes("heroes"); heroes.delete(); tx.success(); tx.finish(); Assert.assertFalse(getGraphDatabase().index().existsForNodes("heroes")); }
@Test public void testRemoveEntryFromIndexWithGivenNode() { Node n1 = restAPI.createNode(map("name", "node1")); final Index<Node> index = restAPI.index().forNodes("testIndex"); index.add(n1, "indexname", "Node1"); final Transaction tx = restAPI.beginTx(); index.remove(n1); tx.success(); tx.finish(); assertNull(index.get("indexname", "Node1").getSingle()); }
@Test(expected = UnsupportedOperationException.class) public void testRemoveEntryFromIndexWithGivenNodeAndKeyAndValue() { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map("name", "node1")); final Index<Node> index = restAPI.index().forNodes("testIndex"); index.add(n1, "indexname", "Node1"); index.remove(n1, "indexname", "Node1"); tx.success(); tx.finish(); assertNull(index.get("indexname", "Node1").getSingle()); }
@Test(expected = RestResultException.class) public void testFailingDoubleDelete() throws Exception { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map()); n1.delete(); n1.delete(); tx.success(); tx.finish(); }
@Test public void testQueryIndex() { final MatrixDataGraph matrixDataGraph = new MatrixDataGraph(getGraphDatabase()); matrixDataGraph.createNodespace(); final Transaction tx = restAPI.beginTx(); final Index<Node> index = restAPI.index().forNodes("heroes"); final IndexHits<Node> heroes = index.query("name:Neo"); tx.success(); tx.finish(); assertEquals("1 hero", 1, heroes.size()); assertEquals("Neo indexed", matrixDataGraph.getNeoNode(), heroes.iterator().next()); }
@Test public void testCreateNode() { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map("name", "node1")); Node n2 = restAPI.createNode(map("name", "node2")); final int count = countExistingNodes(); assertEquals("only reference node", 1, count); tx.success(); tx.finish(); assertEquals("node1", n1.getProperty("name")); assertEquals("node1", loadRealNode(n1).getProperty("name")); assertEquals("node2", n2.getProperty("name")); }
@Test public void testDeleteRelationship() { Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map("name", "newnode1")); Node n2 = restAPI.createNode(map("name", "newnode2")); Relationship rel = restAPI.createRelationship(n1, n2, Type.TEST, map("name", "rel")); rel.delete(); tx.success(); tx.finish(); Relationship foundRelationship = TestHelper.firstRelationshipBetween( n1.getRelationships(Type.TEST, Direction.OUTGOING), n1, n2); Assert.assertNull("found relationship", foundRelationship); }
@Test public void testCreateNodeAndAddToIndex() throws Exception { RestIndex<Node> index = restAPI.createIndex(Node.class, "index", LuceneIndexImplementation.FULLTEXT_CONFIG); final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map()); index.add(n1, "key", "value"); tx.success(); tx.finish(); Node node = index.get("key", "value").getSingle(); assertEquals("created node found in index", n1, node); }
@Test public void testSetNodeProperties() { final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map("name", "node1")); n1.setProperty("test", "true"); n1.setProperty("test2", "stilltrue"); tx.success(); tx.finish(); assertEquals("node1", n1.getProperty("name")); assertEquals("true", n1.getProperty("test")); assertEquals("stilltrue", n1.getProperty("test2")); assertEquals("true", loadRealNode(n1).getProperty("test")); assertEquals("stilltrue", loadRealNode(n1).getProperty("test2")); }
@Test public void testCreateRelationshipToNodeOutsideofBatch() throws Exception { final Node node1 = restAPI.createNode(map()); final Transaction tx = restAPI.beginTx(); Node node2 = restAPI.createNode(map()); final Relationship relationship = node1.createRelationshipTo(node2, DynamicRelationshipType.withName("foo")); tx.success(); tx.finish(); assertEquals("foo", relationship.getType().name()); assertEquals( "foo", getGraphDatabase().getRelationshipById(relationship.getId()).getType().name()); }
@Test public void testEnableBatchTransactions() throws Exception { System.setProperty(Config.CONFIG_BATCH_TRANSACTION, "true"); Transaction tx = restAPI.beginTx(); tx.failure(); tx.finish(); assertTrue(tx instanceof BatchTransaction); }
@Test(expected = RestResultException.class) public void testFailingCreateNodeAndAddToIndex() throws Exception { RestIndex<Node> index = restAPI.createIndex( Node.class, "index", MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "fulltext_ _")); final Transaction tx = restAPI.beginTx(); Node n1 = restAPI.createNode(map()); index.add(n1, "key", "value"); tx.success(); tx.finish(); Node node = index.get("key", "value").getSingle(); assertEquals("created node found in index", n1, node); }
/** * Reads the written data from a specific database and a specific item. * * @param type * @param name * @throws IOException */ public Visual readWrittenData(String type, String name) throws IOException { String id; Document doc = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); // Decides from where you want to read. if (type.equals("series")) { doc = builder.parse("resources/series.xml"); } else if (type.equals("movies")) { doc = builder.parse("resources/movies.xml"); } NodeList personList = doc.getElementsByTagName(name); for (int i = 0; i < personList.getLength(); i++) { Node p = personList.item(i); if (p == null) { nodecheck = false; } if (p.getNodeType() == Node.ELEMENT_NODE) { Element movie = (Element) p; // Gets the attribute from the specified element. visual.setTitle(movie.getAttribute("title")); visual.setType(movie.getAttribute("type")); visual.setDate(movie.getAttribute("year")); visual.setLenght(movie.getAttribute("runtime")); visual.setGenre(movie.getAttribute("genre")); visual.setSynopsis(movie.getAttribute("plot")); visual.setLanguage(movie.getAttribute("language")); visual.setDirector(movie.getAttribute("director")); visual.setActors(movie.getAttribute("actors")); System.out.println("Archivo leido."); } } } catch (Exception ex) { System.out.println("Please, write a correct database name."); // If the database´s name isn´t correct, the method will launch again. readWrittenData(api.lookForType(), api.lookForName()); } return visual; }
@Override protected Boolean doInBackground(String... params) { RestAPI api = new RestAPI(); boolean userAuth = false; try { JSONObject jsonObj = api.UserAuthentication(params[0], params[1]); JSONParser parser = new JSONParser(); String is_Auth = jsonObj.getString("is_Auth"); String token = jsonObj.getString("token"); userAuth = Boolean.parseBoolean(is_Auth); userName = params[0]; } catch (Exception e) { Log.d("AsyncLogin", e.getMessage()); } return userAuth; }
/** Here is the main menu, where the user will interact. */ public void menu() { System.out.println("Insert the option number to execute it:"); System.out.println("-------------------------"); System.out.println("1 -Get data online and read it:"); System.out.println("2 -Get online data and write it into the local database:"); System.out.println("3 -Look for a specific record in the local database:"); while (check) { getOption(); } switch (str) { case "1": { Visual visual; visual = getDataToSee(api.transformXML()); if (checkNode()) { System.out.println(visual.toString()); System.out.println("-------------------------"); System.out.println("Want to save it? Y/N :"); str = sc.next(); if (str.equals("Y") || str.equals("y")) { System.out.println("Info saved."); System.out.println(""); submenu(); } else if (str.equals("N") || str.equals("n")) { submenu(); } } else { submenu(); } submenu(); } case "2": { getDataToSee(api.transformXML()); if (checkNode()) { System.out.println( "Received data: Now choose in what database you want it, and the name of the register:"); getDataToWrite(api.getDocu(), api.enterType(), api.enterName()); submenu(); } else { submenu(); } } case "3": { Visual visual = null; try { visual = readWrittenData(api.lookForType(), api.lookForName()); if (checkNode()) { System.out.println(visual.toString()); } else { System.out.println("Sorry, couldn´t find the data."); } } catch (IOException e) { System.out.println("Error. The file that you were trying to read doesn´t exist."); submenu(); } submenu(); } } }
@Override @After public void tearDown() throws Exception { restAPI.close(); }