private void save(List<? extends T> items) { Session session = this.sessionFactory.openSession(); for (T item : items) { session.save(item); } } }
private void delete(List<? extends T> items) { Session session = this.sessionFactory.openSession(); for(T item : items) { session.delete(item); } }
@SuppressWarnings("unchecked") @Override protected Iterator<T> doPageRead() { Session session = getSessionFactory().openSession(); Iterable<T> queryResults = session.query(getTargetType(), generateLimitCypherQuery(), getParameterValues()); if(queryResults != null) { return queryResults.iterator(); } else { return new ArrayList<T>().iterator(); } } }
@SuppressWarnings("unchecked") @Test public void testNoResultsWithSession() throws Exception { Neo4jItemReader<String> itemReader = buildSessionBasedReader(); ArgumentCaptor<String> query = ArgumentCaptor.forClass(String.class); when(this.sessionFactory.openSession()).thenReturn(this.session); when(this.session.query(eq(String.class), query.capture(), isNull())).thenReturn(result); when(result.iterator()).thenReturn(Collections.emptyIterator()); assertFalse(itemReader.doPageRead().hasNext()); assertEquals("START n=node(*) RETURN * ORDER BY n.age SKIP 0 LIMIT 50", query.getValue()); }
@SuppressWarnings("unchecked") @Test public void testNullResultsWithSession() throws Exception { Neo4jItemReader<String> itemReader = buildSessionBasedReader(); ArgumentCaptor<String> query = ArgumentCaptor.forClass(String.class); when(this.sessionFactory.openSession()).thenReturn(this.session); when(this.session.query(eq(String.class), query.capture(), isNull())).thenReturn(null); assertFalse(itemReader.doPageRead().hasNext()); assertEquals("START n=node(*) RETURN * ORDER BY n.age SKIP 0 LIMIT 50", query.getValue()); }
@Test public void testWriteNoItemsWithSession() throws Exception { writer = new Neo4jItemWriter<>(); writer.setSessionFactory(this.sessionFactory); writer.afterPropertiesSet(); when(this.sessionFactory.openSession()).thenReturn(this.session); writer.write(new ArrayList<>()); verifyZeroInteractions(this.session); }
@Test public void testBasicWriter() throws Exception{ Neo4jItemWriter<String> writer = new Neo4jItemWriterBuilder<String>().sessionFactory(this.sessionFactory).build(); List<String> items = new ArrayList<>(); items.add("foo"); items.add("bar"); when(this.sessionFactory.openSession()).thenReturn(this.session); writer.write(items); verify(this.session).save("foo"); verify(this.session).save("bar"); verify(this.session, never()).delete("foo"); verify(this.session, never()).delete("bar"); }
@Test public void testWriteItemsWithSession() throws Exception { writer = new Neo4jItemWriter<>(); writer.setSessionFactory(this.sessionFactory); writer.afterPropertiesSet(); List<String> items = new ArrayList<>(); items.add("foo"); items.add("bar"); when(this.sessionFactory.openSession()).thenReturn(this.session); writer.write(items); verify(this.session).save("foo"); verify(this.session).save("bar"); }
@SuppressWarnings("serial") @Test public void testResultsWithMatchAndWhereWithSession() throws Exception { Neo4jItemReader<String> itemReader = buildSessionBasedReader(); itemReader.setMatchStatement("n -- m"); itemReader.setWhereStatement("has(n.name)"); itemReader.setReturnStatement("m"); itemReader.afterPropertiesSet(); when(this.sessionFactory.openSession()).thenReturn(this.session); when(this.session.query(String.class, "START n=node(*) MATCH n -- m WHERE has(n.name) RETURN m ORDER BY n.age SKIP 0 LIMIT 50", null)).thenReturn(result); when(result.iterator()).thenReturn(Arrays.asList("foo", "bar", "baz").iterator()); assertTrue(itemReader.doPageRead().hasNext()); }
@Test public void testBasicDelete() throws Exception{ Neo4jItemWriter<String> writer = new Neo4jItemWriterBuilder<String>().delete(true).sessionFactory(this.sessionFactory).build(); List<String> items = new ArrayList<>(); items.add("foo"); items.add("bar"); when(this.sessionFactory.openSession()).thenReturn(this.session); writer.write(items); verify(this.session).delete("foo"); verify(this.session).delete("bar"); verify(this.session, never()).save("foo"); verify(this.session, never()).save("bar"); }
@Test public void testWriteNullWithSession() throws Exception { writer = new Neo4jItemWriter<>(); writer.setSessionFactory(this.sessionFactory); writer.afterPropertiesSet(); when(this.sessionFactory.openSession()).thenReturn(this.session); writer.write(null); verifyZeroInteractions(this.session); }
@SuppressWarnings("serial") @Test public void testResultsWithMatchAndWhereWithParametersWithSession() throws Exception { Neo4jItemReader<String> itemReader = buildSessionBasedReader(); Map<String, Object> params = new HashMap<>(); params.put("foo", "bar"); itemReader.setParameterValues(params); itemReader.setMatchStatement("n -- m"); itemReader.setWhereStatement("has(n.name)"); itemReader.setReturnStatement("m"); itemReader.afterPropertiesSet(); when(this.sessionFactory.openSession()).thenReturn(this.session); when(this.session.query(String.class, "START n=node(*) MATCH n -- m WHERE has(n.name) RETURN m ORDER BY n.age SKIP 0 LIMIT 50", params)).thenReturn(result); when(result.iterator()).thenReturn(Arrays.asList("foo", "bar", "baz").iterator()); assertTrue(itemReader.doPageRead().hasNext()); } }
@Test public void testDeleteItemsWithSession() throws Exception { writer = new Neo4jItemWriter<>(); writer.setSessionFactory(this.sessionFactory); writer.afterPropertiesSet(); List<String> items = new ArrayList<>(); items.add("foo"); items.add("bar"); writer.setDelete(true); when(this.sessionFactory.openSession()).thenReturn(this.session); writer.write(items); verify(this.session).delete("foo"); verify(this.session).delete("bar"); } }
/** * Create a Neo4j OGM Session to be bound to a request. * <p> * Can be overridden in subclasses. * * @param sessionFactory the SessionFactory to use * @see SessionFactory#openSession */ protected Session createSession(SessionFactory sessionFactory) { return sessionFactory.openSession(); }
@Test public void testResultsWithMatchAndWhereWithParametersWithSession() throws Exception { Map<String, Object> params = new HashMap<>(); params.put("foo", "bar"); Neo4jItemReader<String> itemReader = new Neo4jItemReaderBuilder<String>() .sessionFactory(this.sessionFactory) .targetType(String.class) .startStatement("n=node(*)") .returnStatement("*") .orderByStatement("n.age") .pageSize(50) .name("foo") .parameterValues(params) .matchStatement("n -- m") .whereStatement("has(n.name)") .returnStatement("m") .build(); when(this.sessionFactory.openSession()).thenReturn(this.session); when(this.session.query(String.class, "START n=node(*) MATCH n -- m WHERE has(n.name) RETURN m ORDER BY n.age SKIP 0 LIMIT 50", params)) .thenReturn(result); when(result.iterator()).thenReturn(Arrays.asList("foo", "bar", "baz").iterator()); assertEquals("The expected value was not returned by reader.", "foo", itemReader.read()); }
@Test public void testCurrentSize() throws Exception { Neo4jItemReader<String> itemReader = new Neo4jItemReaderBuilder<String>() .sessionFactory(this.sessionFactory) .targetType(String.class) .startStatement("n=node(*)") .orderByStatement("n.age") .pageSize(50).name("bar") .returnStatement("m") .currentItemCount(0) .maxItemCount(1) .build(); when(this.sessionFactory.openSession()).thenReturn(this.session); when(this.session.query(String.class, "START n=node(*) RETURN m ORDER BY n.age SKIP 0 LIMIT 50", null)) .thenReturn(result); when(result.iterator()).thenReturn(Arrays.asList("foo", "bar", "baz").iterator()); assertEquals("The expected value was not returned by reader.", "foo", itemReader.read()); assertNull("The expected value was not should be null.", itemReader.read()); }
@Test public void testFullyQualifiedItemReader() throws Exception { Neo4jItemReader<String> itemReader = new Neo4jItemReaderBuilder<String>() .sessionFactory(this.sessionFactory) .targetType(String.class) .startStatement("n=node(*)") .orderByStatement("n.age") .pageSize(50).name("bar") .matchStatement("n -- m") .whereStatement("has(n.name)") .returnStatement("m").build(); when(this.sessionFactory.openSession()).thenReturn(this.session); when(this.session.query(String.class, "START n=node(*) MATCH n -- m WHERE has(n.name) RETURN m ORDER BY n.age SKIP 0 LIMIT 50", null)) .thenReturn(result); when(result.iterator()).thenReturn(Arrays.asList("foo", "bar", "baz").iterator()); assertEquals("The expected value was not returned by reader.", "foo", itemReader.read()); assertEquals("The expected value was not returned by reader.", "bar", itemReader.read()); assertEquals("The expected value was not returned by reader.", "baz", itemReader.read()); }
@Override protected void doHealthCheck(Health.Builder builder) throws Exception { Session session = this.sessionFactory.openSession(); extractResult(session, builder); }
public static Session getSession(SessionFactory sessionFactory) throws IllegalStateException { Assert.notNull(sessionFactory, "No SessionFactory specified"); SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); if (sessionHolder != null) { if (!sessionHolder.isSynchronizedWithTransaction() && TransactionSynchronizationManager.isSynchronizationActive()) { sessionHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager .registerSynchronization(new SessionSynchronization(sessionHolder, sessionFactory, false)); } return sessionHolder.getSession(); } if (!TransactionSynchronizationManager.isSynchronizationActive()) { return null; } Session session = sessionFactory.openSession(); logger.debug("Registering transaction synchronization for Neo4j Session"); // Use same Session for further Neo4j actions within the transaction. // Thread object will get removed by synchronization at transaction completion. sessionHolder = new SessionHolder(session); sessionHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager .registerSynchronization(new SessionSynchronization(sessionHolder, sessionFactory, true)); TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder); return session; }
@Override public void preHandle(WebRequest request) throws DataAccessException { String participateAttributeName = getParticipateAttributeName(); WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); if (asyncManager.hasConcurrentResult()) { if (applyCallableInterceptor(asyncManager, participateAttributeName)) { return; } } if (TransactionSynchronizationManager.hasResource(getSessionFactory())) { // Do not modify the Session: just mark the request accordingly. Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); int newCount = (count != null ? count + 1 : 1); request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST); } else { logger.debug("Opening Neo4j OGM Session in OpenSessionInViewInterceptor"); Session session = sessionFactory.openSession(); SessionHolder sessionHolder = new SessionHolder(session); TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder); AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getSessionFactory(), sessionHolder); asyncManager.registerCallableInterceptor(participateAttributeName, interceptor); asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor); } }