Search in sources :

Example 1 with SessionAwareMethodInterceptor

use of org.springframework.data.mongodb.SessionAwareMethodInterceptor 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)

Aggregations

MongoClient (com.mongodb.MongoClient)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Advice (org.aopalliance.aop.Advice)1 Document (org.bson.Document)1 Before (org.junit.Before)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 SessionBoundMongoTemplate (org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate)1