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);
}
}
}
};
}
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));
}
Aggregations