Neo4jProducer extends DefaultProducer { @Override public void process(Exchange exchange) throws Exception { Object body = exchange.getIn().getBody(); Neo4jOperation op = (Neo4jOperation)exchange.getIn().getHeader(Neo4jEndpoint.HEADER_OPERATION); if (op == null) { throw new Neo4jException("No operation specified for exchange " + exchange); } switch (op) { case CREATE_NODE: Node node = createNode(body); LOGGER.debug("Node created [{}]", node); exchange.getIn().setHeader(Neo4jEndpoint.HEADER_NODE_ID, node.getId()); break; case CREATE_RELATIONSHIP: Relationship r = createRelationship(body); LOGGER.debug("Relationship created [{}]", r); exchange.getIn().setHeader(Neo4jEndpoint.HEADER_RELATIONSHIP_ID, r.getId()); break; case REMOVE_NODE: removeNode(body); break; case REMOVE_RELATIONSHIP: removeRelationship(body); break; default: } } Neo4jProducer(Neo4jEndpoint endpoint, GraphDatabase graphDatabase);  Neo4jProducer(Neo4jEndpoint endpoint, GraphDatabase graphDatabase, Neo4jTemplate template); @Override void process(Exchange exchange);  }
@Test(expected = Neo4jException.class) public void testUnsupportedBodyForCreateRelationship() throws Exception { when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.CREATE_RELATIONSHIP); when(msg.getBody()).thenReturn(new Object()); producer.process(exchange); }
@Test(expected = Neo4jException.class) public void testUnsupportedBodyForDeleteNode() throws Exception { when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.REMOVE_NODE); when(msg.getBody()).thenReturn(new Object()); producer.process(exchange); }
@Test(expected = Neo4jException.class) public void testUnsupportedBodyForDeleteRelationship() throws Exception { when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.REMOVE_RELATIONSHIP); when(msg.getBody()).thenReturn(new Object()); producer.process(exchange); }
@Test public void testCreateRelationshipWithBasicBody() throws Exception { when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.CREATE_RELATIONSHIP); Node start = mock(Node.class); Node end = mock(Node.class); String type = "friendswith"; BasicRelationship br = new BasicRelationship(start, end, type); when(msg.getBody()).thenReturn(br); Relationship r = mock(Relationship.class); when(r.getId()).thenReturn(99L); when(template.createRelationshipBetween(start, end, type, null)).thenReturn(r); producer.process(exchange); verify(template).createRelationshipBetween(start, end, type, null); verify(msg).setHeader(Neo4jEndpoint.HEADER_RELATIONSHIP_ID, 99L); }
@Test public void testCreateRelationshipWithSpringBody() throws Exception { when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.CREATE_RELATIONSHIP); Object start = new Object(); Object end = new Object(); Class<Relationship> entityClass = Relationship.class; String type = "friendswith"; SpringDataRelationship<Relationship> spring = new SpringDataRelationship<Relationship>(start, end, entityClass, type, true); when(msg.getBody()).thenReturn(spring); Relationship r = mock(Relationship.class); when(r.getId()).thenReturn(55L); when(template.createRelationshipBetween(start, end, entityClass, type, true)).thenReturn(r); producer.process(exchange); verify(template).createRelationshipBetween(start, end, entityClass, type, true); verify(msg).setHeader(Neo4jEndpoint.HEADER_RELATIONSHIP_ID, 55L); }
@Test(expected = Neo4jException.class) public void testNullOperationFails() throws Exception { producer.process(exchange); }
@Test public void testRemoveNodeBasicBody() throws Exception { Node node = mock(Node.class); when(node.getId()).thenReturn(14L); when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.REMOVE_NODE); when(msg.getBody()).thenReturn(node); producer.process(exchange); verify(template).delete(node); }
@Test public void testRemoveNodeById() throws Exception { Node node = mock(Node.class); when(template.getNode(44L)).thenReturn(node); when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.REMOVE_NODE); when(msg.getBody()).thenReturn(44L); producer.process(exchange); verify(template).delete(node); }
@Test public void testRemoveRelationshipByBasic() throws Exception { Relationship r = mock(Relationship.class); when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.REMOVE_RELATIONSHIP); when(msg.getBody()).thenReturn(r); producer.process(exchange); verify(template).delete(r); }
@Test public void testRemoveRelationshipById() throws Exception { Relationship r = mock(Relationship.class); when(template.getRelationship(51L)).thenReturn(r); when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.REMOVE_RELATIONSHIP); when(msg.getBody()).thenReturn(51L); producer.process(exchange); verify(template).delete(r); }
@Test public void testRemoveRelationshipBySpringData() throws Exception { Object start = new Object(); Object end = new Object(); Class<String> entityClass = String.class; String type = "friendswith"; SpringDataRelationship<String> spring = new SpringDataRelationship<String>(start, end, entityClass, type, true); when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.REMOVE_RELATIONSHIP); when(msg.getBody()).thenReturn(spring); producer.process(exchange); verify(template).deleteRelationshipBetween(start, end, type); }
@Test(expected = Neo4jException.class) public void testUnsupportedBodyForCreateNode() throws Exception { when(msg.getHeader(Neo4jEndpoint.HEADER_OPERATION)).thenReturn(Neo4jOperation.CREATE_NODE); when(msg.getBody()).thenReturn(new Object()); producer.process(exchange); }