2017-10-13 6 views
1

J'écris des tests unitaires et je veux utiliser TimeTree avec des répertoires Spring, pour attacher automatiquement des nœuds d'événements à un arbre temporel. Quelque chose comme problème this, mais j'utilise boot 2.0 et SDN5. Je pense que mon principal problème est que je ne sais pas comment configurer la configuration de sorte que mes dépôts et mon TimeTree utilisent le même GraphDatabaseService. Mon @Confuration est comme ceci:Comment configurer mes propres GraphDatabaseService et GraphAwareRuntime dans un test unitaire avec boot 2.0 et Neo4j SDN5

@Configuration 
    public class SpringConfig { 

     @Bean 
     public SessionFactory sessionFactory() { 
      // with domain entity base package(s) 
      return new SessionFactory(configuration(), "org.neo4j.boot.test.domain"); 
     } 

     @Bean 
     public org.neo4j.ogm.config.Configuration configuration() { 
      return new org.neo4j.ogm.config.Configuration.Builder() 
       .uri("bolt://localhost") 
       .build(); 
     } 

     @Bean 
     public Session getSession() { 
      return sessionFactory().openSession(); 
     } 

     @Bean 
     public GraphDatabaseService graphDatabaseService() { 
      return new GraphDatabaseFactory() 
       .newEmbeddedDatabase(new File("/tmp/graphDb")); 
     } 

     @Bean 
     public GraphAwareRuntime graphAwareRuntime() { 
      GraphDatabaseService graphDatabaseService = graphDatabaseService(); 
      GraphAwareRuntime runtime = GraphAwareRuntimeFactory 
       .createRuntime(graphDatabaseService); 

      runtime.registerModule(new TimeTreeModule("timetree", 
       TimeTreeConfiguration 
        .defaultConfiguration() 
        .withAutoAttach(true) 
        .with(new NodeInclusionPolicy() { 
         @Override 
         public Iterable<Node> getAll(GraphDatabaseService graphDatabaseService) { 
          return null; 
         } 

         @Override 
         public boolean include(Node node) { 
          return node.hasLabel(Label.label("User")); 
         } 
        }) 
        .withRelationshipType(RelationshipType.withName("CREATED_ON")) 
        .withTimeZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+1"))) 
        .withTimestampProperty("createdOn") 
        .withResolution(Resolution.DAY) 
    //      .withCustomTimeTreeRootProperty("timeTreeName") 
        .withResolution(Resolution.HOUR), graphDatabaseService)); 
      runtime.start(); 
      return runtime; 
     } 
    } 

Et mon test ressemble à ceci:

User user = new User("Michal"); 
    user.setCreatedOn(1431937636995l); 
    userRepository.save(user); 

    GraphUnit.assertSameGraph(graphDb, "CREATE (u:User {name:'Michal', createdOn:1431937636995})," + 
      "(root:TimeTreeRoot)," + 
      "(root)-[:FIRST]->(year:Year {value:2015})," + 
      "(root)-[:CHILD]->(year)," + 
      "(root)-[:LAST]->(year)," + 
      "(year)-[:FIRST]->(month:Month {value:5})," + 
      "(year)-[:CHILD]->(month)," + 
      "(year)-[:LAST]->(month)," + 
      "(month)-[:FIRST]->(day:Day {value:18})," + 
      "(month)-[:CHILD]->(day)," + 
      "(month)-[:LAST]->(day)," + 
      "(day)<-[:CREATED_ON]-(u)" 
    ); 

    GraphUnit.printGraph(graphDb); 
    graphDb.shutdown(); 

Il y a une foule d'erreurs, mais je pense qu'ils proviennent tous de celui-ci:

Bean instantiation via factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to 
instantiate [org.springframework.data.repository.support.Repositories]: 
Factory method 'repositories' threw exception; nested exception is 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'userRepository': Unsatisfied dependency 
expressed through method 'setSession' parameter 0; nested exception is 
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No 
qualifying bean of type 'org.neo4j.ogm.session.Session' available: 
expected single matching bean but found 2: getSession, 
org.springframework.data.neo4j.transaction.SharedSessionCreator#0 

Répondre

1

C'est parce que la classe de configuration redéfinit certains beans déjà configurés automatiquement par Spring boot (ici le Session). L'injection de ressort ne sait pas comment choisir entre les deux.

Une deuxième chose est que votre SessionFactory doit utiliser la configuration de base de données intégrée dans la méthode graphDatabaseService(). Pour cela, configurez un pilote intégré avec la base de données existante.

Résumé config qui devrait fonctionner très bien pour vous:

@Bean 
public SessionFactory sessionFactory() { 
    EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService()); 
    return new SessionFactory(driver, "org.neo4j.boot.test.domain"); 
} 

@Bean 
public PlatformTransactionManager transactionManager() { 
    return new Neo4jTransactionManager(sessionFactory()); 
} 

@Bean 
public GraphDatabaseService graphDatabaseService() { 
    return new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase(); 
} 

@Bean 
public GraphAwareRuntime graphAwareRuntime() { 
    ... 
+1

Un grand merci, on dirait que je suis de retour dans les affaires. Des bières sur moi si jamais nous nous rencontrons! –