Search in sources :

Example 1 with SessionBoundMongoTemplate

use of org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateTests method setUp.

@Before
public void setUp() {
    MongoClient client = new MongoClient();
    MongoDbFactory factory = new SimpleMongoDbFactory(client, "session-bound-mongo-template-tests") {

        @Override
        public MongoDatabase getDb() throws DataAccessException {
            MongoDatabase spiedDatabse = Mockito.spy(super.getDb());
            spiedDatabases.add(spiedDatabse);
            return spiedDatabse;
        }
    };
    session = client.startSession(ClientSessionOptions.builder().build());
    this.template = new MongoTemplate(factory);
    this.sessionBoundTemplate = new SessionBoundMongoTemplate(session, new MongoTemplate(factory, getDefaultMongoConverter(factory))) {

        @Override
        protected MongoCollection<Document> prepareCollection(MongoCollection<Document> collection) {
            injectCollectionSpy(collection);
            return super.prepareCollection(collection);
        }

        @SuppressWarnings({ "ConstantConditions", "unchecked" })
        private void injectCollectionSpy(MongoCollection<Document> collection) {
            InvocationHandler handler = Proxy.getInvocationHandler(collection);
            Advised advised = (Advised) ReflectionTestUtils.getField(handler, "advised");
            for (Advisor advisor : advised.getAdvisors()) {
                Advice advice = advisor.getAdvice();
                if (advice instanceof SessionAwareMethodInterceptor) {
                    MongoCollection<Document> spiedCollection = Mockito.spy((MongoCollection<Document>) ReflectionTestUtils.getField(advice, "target"));
                    spiedCollections.add(spiedCollection);
                    ReflectionTestUtils.setField(advice, "target", spiedCollection);
                }
            }
        }
    };
}
Also used : SessionAwareMethodInterceptor(org.springframework.data.mongodb.SessionAwareMethodInterceptor) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) Advisor(org.springframework.aop.Advisor) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Document(org.bson.Document) InvocationHandler(java.lang.reflect.InvocationHandler) MongoClient(com.mongodb.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) Advised(org.springframework.aop.framework.Advised) Advice(org.aopalliance.aop.Advice) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) MongoDatabase(com.mongodb.client.MongoDatabase) Before(org.junit.Before)

Example 2 with SessionBoundMongoTemplate

use of org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateUnitTests method setUp.

@Before
public void setUp() {
    when(client.getDatabase(anyString())).thenReturn(database);
    when(codecRegistry.get(any(Class.class))).thenReturn(new BsonValueCodec());
    when(database.getCodecRegistry()).thenReturn(codecRegistry);
    when(database.getCollection(anyString(), any())).thenReturn(collection);
    when(database.listCollectionNames(any(ClientSession.class))).thenReturn(mongoIterable);
    when(collection.find(any(ClientSession.class), any(), any())).thenReturn(findIterable);
    when(collection.aggregate(any(ClientSession.class), anyList(), any())).thenReturn(aggregateIterable);
    when(collection.distinct(any(ClientSession.class), any(), any(), any())).thenReturn(distinctIterable);
    when(collection.mapReduce(any(ClientSession.class), any(), any(), any())).thenReturn(mapReduceIterable);
    when(findIterable.iterator()).thenReturn(cursor);
    when(aggregateIterable.collation(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.allowDiskUse(anyBoolean())).thenReturn(aggregateIterable);
    when(aggregateIterable.batchSize(anyInt())).thenReturn(aggregateIterable);
    when(aggregateIterable.map(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.useCursor(anyBoolean())).thenReturn(aggregateIterable);
    when(aggregateIterable.into(any())).thenReturn(Collections.emptyList());
    when(mongoIterable.iterator()).thenReturn(cursor);
    when(distinctIterable.map(any())).thenReturn(distinctIterable);
    when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
    when(mapReduceIterable.sort(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.filter(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.map(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(findIterable.projection(any())).thenReturn(findIterable);
    factory = new SimpleMongoDbFactory(client, "foo");
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext);
    this.template = new SessionBoundMongoTemplate(clientSession, new MongoTemplate(factory, converter));
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) ClientSession(com.mongodb.session.ClientSession) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Before(org.junit.Before)

Aggregations

Before (org.junit.Before)2 SessionBoundMongoTemplate (org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate)2 MongoClient (com.mongodb.MongoClient)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 ClientSession (com.mongodb.session.ClientSession)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Advice (org.aopalliance.aop.Advice)1 Document (org.bson.Document)1 BsonValueCodec (org.bson.codecs.BsonValueCodec)1 Advisor (org.springframework.aop.Advisor)1 Advised (org.springframework.aop.framework.Advised)1 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)1 SessionAwareMethodInterceptor (org.springframework.data.mongodb.SessionAwareMethodInterceptor)1 DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)1 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)1 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)1