All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lordofthejars.nosqlunit.neo4j.SpringGraphDatabaseServiceNeo4jRule Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.lordofthejars.nosqlunit.neo4j;

import static ch.lambdaj.collection.LambdaCollections.with;
import static org.hamcrest.CoreMatchers.anything;

import java.util.Map;

import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.neo4j.graphdb.GraphDatabaseService;
import org.springframework.context.ApplicationContext;

import com.lordofthejars.nosqlunit.core.PropertyGetter;

class SpringGraphDatabaseServiceNeo4jRule extends Neo4jRule {

	private PropertyGetter propertyGetter = new PropertyGetter();

	public SpringGraphDatabaseServiceNeo4jRule(Neo4jConfiguration neo4jConfiguration) {
		super(neo4jConfiguration);
	}

	public SpringGraphDatabaseServiceNeo4jRule(Neo4jConfiguration neo4jConfiguration, Object object) {
		super(neo4jConfiguration, object);
	}

	@Override
	public Statement apply(Statement base, FrameworkMethod method, Object testObject) {
		this.databaseOperation = new Neo4jOperation(definedGraphDatabaseService(testObject));
		return super.apply(base, method, testObject);
	}

	private GraphDatabaseService definedGraphDatabaseService(Object testObject) {
		ApplicationContext applicationContext = propertyGetter.propertyByType(testObject, ApplicationContext.class);

		Map beansOfType = applicationContext.getBeansOfType(GraphDatabaseService.class);
		
		if(beansOfType == null) {
			throw new IllegalArgumentException(
					"At least one GraphDatabaseService instance should be defined into Spring Application Context.");
		}
		
		GraphDatabaseService graphDatabaseService = with(beansOfType).values().first(anything());

		if (graphDatabaseService == null) {
			throw new IllegalArgumentException(
					"At least one GraphDatabaseService instance should be defined into Spring Application Context.");
		}

		return graphDatabaseService;

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy