Package org.hibernate.ogm.datastore.mongodb.utils.MockMongoClientBuilder

Examples of org.hibernate.ogm.datastore.mongodb.utils.MockMongoClientBuilder.MockMongoClient


  }

  @Test
  public void shouldApplyConfiguredReadPreferenceForGettingTuple() {
    // given an empty database
    MockMongoClient mockClient = mockClient().build();
    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

    final Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when getting a golf player
    session.get( GolfPlayer.class, 1L );

    transaction.commit();
    session.close();

    // then expect a findOne() call with the configured read preference
    verify( mockClient.getCollection( "GolfPlayer" ) ).findOne( any( DBObject.class ), any( DBObject.class ), eq( ReadPreference.secondaryPreferred() ) );
  }
View Full Code Here


  public void shouldApplyConfiguredReadPreferenceForGettingEmbeddedAssociation() {
    // given a persisted player with one associated golf course
    BasicDBObject player = getPlayer();
    player.put( "playedCourses", getPlayedCoursesAssociationEmbedded() );

    MockMongoClient mockClient = mockClient()
        .insert( "GolfPlayer", player )
        .insert( "GolfCourse", getGolfCourse() )
        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

    Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when getting the golf player
    GolfPlayer ben = (GolfPlayer) session.get( GolfPlayer.class, 1L );
    List<GolfCourse> playedCourses = ben.getPlayedCourses();
    assertThat( playedCourses ).onProperty( "id" ).containsExactly( 1L );

    transaction.commit();
    session.close();

    // then expect a findOne() call for the entity and the embedded association with the configured read preference
    verify( mockClient.getCollection( "GolfPlayer" ) ).findOne( any( DBObject.class ), any( DBObject.class ), eq( ReadPreference.secondaryPreferred() ) );
  }
View Full Code Here

  }

  @Test
  public void shouldApplyConfiguredReadPreferenceForGettingAssociationStoredAsAssociation() {
    // given a persisted player with one associated golf course
    MockMongoClient mockClient = mockClient()
        .insert( "GolfPlayer", getPlayer() )
        .insert( "GolfCourse", getGolfCourse() )
        .insert( "Associations", getPlayedCoursesAssociationAsDocument() )
        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ), AssociationStorageType.ASSOCIATION_DOCUMENT );

    Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when getting the golf player
    GolfPlayer ben = (GolfPlayer) session.get( GolfPlayer.class, 1L );
    List<GolfCourse> playedCourses = ben.getPlayedCourses();
    assertThat( playedCourses ).onProperty( "id" ).containsExactly( 1L );

    transaction.commit();
    session.close();

    // then expect a findOne() call for the entity and one for the association  with the configured read preference
    verify( mockClient.getCollection( "GolfPlayer" ) ).findOne( any( DBObject.class ), any( DBObject.class ), eq( ReadPreference.secondaryPreferred() ) );
    verify( mockClient.getCollection( "Associations" ) ).findOne( any( DBObject.class ), any( DBObject.class ), eq( ReadPreference.primaryPreferred() ) );
    verifyNoMoreInteractions( mockClient.getCollection( "GolfPlayer" ) );
  }
View Full Code Here

  @Test
  @SuppressWarnings("unchecked")
  public void shouldApplyConfiguredWriteConcernForInsertOfTuple() {
    // given an empty database
    MockMongoClient mockClient = mockClient().build();
    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

    final Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when inserting a golf player
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1 );
    session.persist( ben );

    transaction.commit();
    session.close();

    // then expect one (batched) insert with the configured write concern
    verify( mockClient.getCollection( "GolfPlayer" ) ).insert( any( List.class ), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

  }

  @Test
  public void shouldApplyConfiguredWriteConcernForUpdateOfTuple() {
    // given a database with a golf player
    MockMongoClient mockClient = mockClient()
        .insert( "GolfPlayer", getPlayer() )
        .build();
    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

    final Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when updating the golf player
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.2 );
    session.merge( ben );

    transaction.commit();
    session.close();

    // then expect one (batched) insert with the configured write concern
    verify( mockClient.getCollection( "GolfPlayer" ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

  }

  @Test
  public void shouldApplyConfiguredWriteConcernForRemoveTuple() {
    // given a database with a golf player
    MockMongoClient mockClient = mockClient()
        .insert( "GolfPlayer", getPlayer() )
        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

    final Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when removing the golf player
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1 );
    session.delete( ben );

    transaction.commit();
    session.close();

    // then expect a call to remove with the configured write concern
    verify( mockClient.getCollection( "GolfPlayer" ) ).remove( any( DBObject.class ), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

  }

  @Test
  public void shouldApplyConfiguredWriteConcernForCreationOfEmbeddedAssociation() {
    // given a persisted player and a golf course
    MockMongoClient mockClient = mockClient()
        .insert( "GolfPlayer", getPlayer() )
        .insert( "GolfCourse", getGolfCourse() )
        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

    Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when associating the golf course to the player
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1, new GolfCourse( 1L, "Bepple Peach" ) );
    session.merge( ben );

    transaction.commit();
    session.close();

    // then expect one update using the configured write concern for adding the row
    verify( mockClient.getCollection( "GolfPlayer" ), times( 1 ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

  }

  @Test
  public void shouldApplyConfiguredWriteConcernForCreationOfAssociationStoredAsDocument() {
    // given an empty database
    MockMongoClient mockClient = mockClient().build();
    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ), AssociationStorageType.ASSOCIATION_DOCUMENT );

    Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when inserting a player with an associated course
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1, new GolfCourse( 1L, "Bepple Peach" ) );
    session.persist( ben );

    transaction.commit();
    session.close();

    // then expect association operations using the configured write concern
    verify( mockClient.getCollection( "Associations" ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

  @SuppressWarnings("unchecked")
  @Test
  public void shouldApplyWriteConcernConfiguredOnPropertyLevelForCreationOfAssociationStoredAsDocument() {
    // given an empty database
    MockMongoClient mockClient = mockClient().build();

    OgmConfiguration configuration = TestHelper.getDefaultTestConfiguration( getAnnotatedClasses() );
    configuration.getProperties().put( OgmProperties.DATASTORE_PROVIDER, new MongoDBDatastoreProvider( mockClient.getClient() ) );
    configuration.getProperties().put( DocumentStoreProperties.ASSOCIATIONS_STORE, AssociationStorageType.ASSOCIATION_DOCUMENT );
    configuration.configureOptionsFor( MongoDB.class )
      .entity( GolfPlayer.class )
        .writeConcern( WriteConcernType.REPLICA_ACKNOWLEDGED )
        .property( "playedCourses", ElementType.FIELD )
          .writeConcern( WriteConcernType.ACKNOWLEDGED );

    sessions = configuration.buildSessionFactory();

    Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when inserting a player with an associated course
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1, new GolfCourse( 1L, "Bepple Peach" ) );
    session.persist( ben );

    transaction.commit();
    session.close();

    // then expect tuple and association operations using the configured write concerns
    verify( mockClient.getCollection( "GolfPlayer" ) ).insert( any( List.class ), eq( WriteConcern.REPLICA_ACKNOWLEDGED ) );
    verify( mockClient.getCollection( "Associations" ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.ACKNOWLEDGED ) );
  }
View Full Code Here

  public void shouldApplyConfiguredWriteConcernForUpdateOfEmbeddedAssociation() {
    // given a persisted player with one associated golf course
    BasicDBObject player = getPlayer();
    player.put( "playedCourses", getPlayedCoursesAssociationEmbedded() );

    MockMongoClient mockClient = mockClient()
        .insert( "GolfPlayer", player )
        .insert( "GolfCourse", getGolfCourse() )
        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

    Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when merging the player with two associated courses
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1, new GolfCourse( 1L, "Bepple Peach" ), new GolfCourse( 2L, "Ant Sandrews" ) );
    session.merge( ben );

    transaction.commit();
    session.close();

    // then expect updates to the player document using the configured write concern
    verify( mockClient.getCollection( "GolfPlayer" ), times( 2 ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

TOP

Related Classes of org.hibernate.ogm.datastore.mongodb.utils.MockMongoClientBuilder.MockMongoClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.