INT-2612 Add MongoDb Adapters
Initial support for MongoDb adapters based on initial work done by Amol Nayak INT-2612b Add namespace support Add entityClass and expectSingleResult attribiutes to MongoDbMessageSource INT-2612 Polishing Mostly JavaDocs and a few asserts; plus renamed mongodb-template to mongo-template. INT-2612 Remove 'store-' Prefix from Elements INT-2612 Polish Tests Add tests for mongo-converter pass and fail when template present INT-2612 Add missing files INT-2612 Polishing INT-2612 Add @MongoDbAvailable to parser tests INT-2612b Add dedicated parser tests
This commit is contained in:
committed by
Gary Russell
parent
a333d33112
commit
bd51b3ee7a
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.mongodb.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.integration.mongodb.rules.MongoDbAvailable;
|
||||
import org.springframework.integration.mongodb.rules.MongoDbAvailableTests;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.util.JSON;
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.2
|
||||
*/
|
||||
public class MongoDbInboundChannelAdapterIntegrationTests extends MongoDbAvailableTests {
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithDefaultMongoFactory() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Bob"), "data");
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("inbound-adapter-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("mongoInboundAdapter", SourcePollingChannelAdapter.class);
|
||||
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
|
||||
spca.start();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<List<Person>> message = (Message<List<Person>>) replyChannel.receive(1000);
|
||||
assertNotNull(message);
|
||||
assertEquals("Bob", message.getPayload().get(0).getName());
|
||||
assertNotNull(replyChannel.receive(1000));
|
||||
spca.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithNamedMongoFactory() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Bob"), "data");
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("inbound-adapter-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("mongoInboundAdapterNamedFactory", SourcePollingChannelAdapter.class);
|
||||
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
|
||||
spca.start();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<List<DBObject>> message = (Message<List<DBObject>>) replyChannel.receive(1000);
|
||||
assertNotNull(message);
|
||||
assertEquals("Bob", message.getPayload().get(0).get("name"));
|
||||
spca.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithMongoTemplate() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Bob"), "data");
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("inbound-adapter-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("mongoInboundAdapterWithTemplate", SourcePollingChannelAdapter.class);
|
||||
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
|
||||
spca.start();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<Person> message = (Message<Person>) replyChannel.receive(1000);
|
||||
assertNotNull(message);
|
||||
assertEquals("Bob", message.getPayload().getName());
|
||||
spca.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithNamedCollection() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Bob"), "foo");
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("inbound-adapter-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("mongoInboundAdapterWithNamedCollection", SourcePollingChannelAdapter.class);
|
||||
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
|
||||
spca.start();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<List<Person>> message = (Message<List<Person>>) replyChannel.receive(1000);
|
||||
assertNotNull(message);
|
||||
assertEquals("Bob", message.getPayload().get(0).getName());
|
||||
spca.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithNamedCollectionExpression() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Bob"), "foo");
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("inbound-adapter-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("mongoInboundAdapterWithNamedCollectionExpression", SourcePollingChannelAdapter.class);
|
||||
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
|
||||
spca.start();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<List<Person>> message = (Message<List<Person>>) replyChannel.receive(1000);
|
||||
assertNotNull(message);
|
||||
assertEquals("Bob", message.getPayload().get(0).getName());
|
||||
spca.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithOnSuccessDisposition() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Bob"), "data");
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("inbound-adapter-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("inboundAdapterWithOnSuccessDisposition", SourcePollingChannelAdapter.class);
|
||||
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
|
||||
spca.start();
|
||||
|
||||
assertNotNull(replyChannel.receive(1000));
|
||||
Thread.sleep(300);
|
||||
assertNull(replyChannel.receive(1000));
|
||||
spca.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithMongoConverter() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Bob"), "data");
|
||||
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("inbound-adapter-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("mongoInboundAdapterWithConverter", SourcePollingChannelAdapter.class);
|
||||
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
|
||||
spca.start();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<List<Person>> message = (Message<List<Person>>) replyChannel.receive(1000);
|
||||
assertNotNull(message);
|
||||
assertEquals("Bob", message.getPayload().get(0).getName());
|
||||
assertNotNull(replyChannel.receive(1000));
|
||||
spca.stop();
|
||||
}
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
@MongoDbAvailable
|
||||
public void testFailureWithQueryAndQueryExpression() throws Exception{
|
||||
new ClassPathXmlApplicationContext("inbound-fail-q-qex.xml", this.getClass());
|
||||
}
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
@MongoDbAvailable
|
||||
public void testFailureWithFactoryAndTemplate() throws Exception{
|
||||
new ClassPathXmlApplicationContext("inbound-fail-factory-template.xml", this.getClass());
|
||||
}
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
@MongoDbAvailable
|
||||
public void testFailureWithCollectionAndCollectioinExpression() throws Exception{
|
||||
new ClassPathXmlApplicationContext("inbound-fail-c-cex.xml", this.getClass());
|
||||
}
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
@MongoDbAvailable
|
||||
public void testFailureWithTemplateAndConverter() throws Exception{
|
||||
new ClassPathXmlApplicationContext("inbound-fail-converter-template.xml", this.getClass());
|
||||
}
|
||||
|
||||
public static class DocumentCleaner {
|
||||
public void remove(MongoOperations mongoOperations, Object target, String collectionName) {
|
||||
if (target instanceof List<?>){
|
||||
List<?> documents = (List<?>) target;
|
||||
for (Object document : documents) {
|
||||
mongoOperations.remove(new BasicQuery(JSON.serialize(document)), collectionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.mongodb.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.expression.spel.standard.SpelExpression;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.integration.mongodb.inbound.MongoDbMessageSource;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class MongoDbInboundChannelAdapterParserTests {
|
||||
|
||||
@Test
|
||||
public void minimalConfig(){
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("inbound-adapter-parser-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("minimalConfig.adapter", SourcePollingChannelAdapter.class);
|
||||
MongoDbMessageSource source = TestUtils.getPropertyValue(spca, "source", MongoDbMessageSource.class);
|
||||
|
||||
assertEquals(false, TestUtils.getPropertyValue(spca, "shouldTrack"));
|
||||
assertNotNull(TestUtils.getPropertyValue(source, "mongoTemplate"));
|
||||
assertEquals(context.getBean("mongoDbFactory"), TestUtils.getPropertyValue(source, "mongoDbFactory"));
|
||||
assertNotNull(TestUtils.getPropertyValue(source, "evaluationContext"));
|
||||
assertTrue(TestUtils.getPropertyValue(source, "collectionNameExpression") instanceof LiteralExpression);
|
||||
assertEquals("data", TestUtils.getPropertyValue(source, "collectionNameExpression.literalValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullConfigWithCollectionExpression(){
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("inbound-adapter-parser-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("fullConfigWithCollectionExpression.adapter", SourcePollingChannelAdapter.class);
|
||||
MongoDbMessageSource source = TestUtils.getPropertyValue(spca, "source", MongoDbMessageSource.class);
|
||||
|
||||
assertEquals(false, TestUtils.getPropertyValue(spca, "shouldTrack"));
|
||||
assertNotNull(TestUtils.getPropertyValue(source, "mongoTemplate"));
|
||||
assertEquals(context.getBean("mongoDbFactory"), TestUtils.getPropertyValue(source, "mongoDbFactory"));
|
||||
assertEquals(context.getBean("mongoConverter"), TestUtils.getPropertyValue(source, "mongoConverter"));
|
||||
assertNotNull(TestUtils.getPropertyValue(source, "evaluationContext"));
|
||||
assertTrue(TestUtils.getPropertyValue(source, "collectionNameExpression") instanceof SpelExpression);
|
||||
assertEquals("'foo'", TestUtils.getPropertyValue(source, "collectionNameExpression.expression"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullConfigWithCollectionName(){
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("inbound-adapter-parser-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("fullConfigWithCollectionName.adapter", SourcePollingChannelAdapter.class);
|
||||
MongoDbMessageSource source = TestUtils.getPropertyValue(spca, "source", MongoDbMessageSource.class);
|
||||
|
||||
assertEquals(false, TestUtils.getPropertyValue(spca, "shouldTrack"));
|
||||
assertNotNull(TestUtils.getPropertyValue(source, "mongoTemplate"));
|
||||
assertEquals(context.getBean("mongoDbFactory"), TestUtils.getPropertyValue(source, "mongoDbFactory"));
|
||||
assertEquals(context.getBean("mongoConverter"), TestUtils.getPropertyValue(source, "mongoConverter"));
|
||||
assertNotNull(TestUtils.getPropertyValue(source, "evaluationContext"));
|
||||
assertTrue(TestUtils.getPropertyValue(source, "collectionNameExpression") instanceof LiteralExpression);
|
||||
assertEquals("foo", TestUtils.getPropertyValue(source, "collectionNameExpression.literalValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullConfigWithMongoTemplate(){
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("inbound-adapter-parser-config.xml", this.getClass());
|
||||
SourcePollingChannelAdapter spca = context.getBean("fullConfigWithMongoTemplate.adapter", SourcePollingChannelAdapter.class);
|
||||
MongoDbMessageSource source = TestUtils.getPropertyValue(spca, "source", MongoDbMessageSource.class);
|
||||
|
||||
assertEquals(false, TestUtils.getPropertyValue(spca, "shouldTrack"));
|
||||
assertNotNull(TestUtils.getPropertyValue(source, "mongoTemplate"));
|
||||
assertEquals(context.getBean("mongoDbTemplate"), TestUtils.getPropertyValue(source, "mongoTemplate"));
|
||||
assertNotNull(TestUtils.getPropertyValue(source, "evaluationContext"));
|
||||
assertTrue(TestUtils.getPropertyValue(source, "collectionNameExpression") instanceof LiteralExpression);
|
||||
assertEquals("foo", TestUtils.getPropertyValue(source, "collectionNameExpression.literalValue"));
|
||||
}
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void templateAndFactoryFail(){
|
||||
new ClassPathXmlApplicationContext("inbound-adapter-parser-fail-template-factory-config.xml", this.getClass());
|
||||
}
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void templateAndConverterFail(){
|
||||
new ClassPathXmlApplicationContext("inbound-adapter-parser-fail-template-converter-config.xml", this.getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.mongodb.config;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.mongodb.rules.MongoDbAvailable;
|
||||
import org.springframework.integration.mongodb.rules.MongoDbAvailableTests;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.util.JSON;
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class MongoDbOutboundChannelAdapterIntegrationTests extends MongoDbAvailableTests {
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithDefaultMongoFactory() throws Exception{
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("outbound-adapter-config.xml", this.getClass());
|
||||
|
||||
MessageChannel channel = context.getBean("simpleAdapter", MessageChannel.class);
|
||||
Message<Person> message = new GenericMessage<MongoDbAvailableTests.Person>(this.createPerson("Bob"));
|
||||
channel.send(message);
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
assertNotNull(template.find(new BasicQuery("{'name' : 'Bob'}"), Person.class, "data"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithNamedCollection() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory("foo");
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("outbound-adapter-config.xml", this.getClass());
|
||||
|
||||
MessageChannel channel = context.getBean("simpleAdapterWithNamedCollection", MessageChannel.class);
|
||||
Message<Person> message = MessageBuilder.withPayload(this.createPerson("Bob")).setHeader("collectionName", "foo").build();
|
||||
channel.send(message);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
assertNotNull(template.find(new BasicQuery("{'name' : 'Bob'}"), Person.class, "foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithTemplate() throws Exception{
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory("foo");
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("outbound-adapter-config.xml", this.getClass());
|
||||
|
||||
MessageChannel channel = context.getBean("simpleAdapterWithTemplate", MessageChannel.class);
|
||||
Message<Person> message = MessageBuilder.withPayload(this.createPerson("Bob")).setHeader("collectionName", "foo").build();
|
||||
channel.send(message);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
assertNotNull(template.find(new BasicQuery("{'name' : 'Bob'}"), Person.class, "foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testSavingDbObject() throws Exception{
|
||||
|
||||
BasicDBObject dbObject = (BasicDBObject) JSON.parse("{'foo' : 'bar'}");
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory("foo");
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("outbound-adapter-config.xml", this.getClass());
|
||||
|
||||
MessageChannel channel = context.getBean("simpleAdapterWithTemplate", MessageChannel.class);
|
||||
Message<BasicDBObject> message = MessageBuilder.withPayload(dbObject).setHeader("collectionName", "foo").build();
|
||||
channel.send(message);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
assertNotNull(template.find(new BasicQuery("{'foo' : 'bar'}"), BasicDBObject.class, "foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void testWithMongoConverter() throws Exception{
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("outbound-adapter-config.xml", this.getClass());
|
||||
|
||||
MessageChannel channel = context.getBean("simpleAdapterWithConverter", MessageChannel.class);
|
||||
Message<Person> message = new GenericMessage<MongoDbAvailableTests.Person>(this.createPerson("Bob"));
|
||||
channel.send(message);
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
assertNotNull(template.find(new BasicQuery("{'name' : 'Bob'}"), Person.class, "data"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.mongodb.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.expression.spel.standard.SpelExpression;
|
||||
import org.springframework.integration.mongodb.outbound.MongoDbStoringMessageHandler;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class MongoDbOutboundChannelAdapterParserTests {
|
||||
|
||||
@Test
|
||||
public void minimalConfig(){
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("outbound-adapter-parser-config.xml", this.getClass());
|
||||
MongoDbStoringMessageHandler handler =
|
||||
TestUtils.getPropertyValue(context.getBean("minimalConfig.adapter"), "handler", MongoDbStoringMessageHandler.class);
|
||||
assertEquals("minimalConfig.adapter", TestUtils.getPropertyValue(handler, "componentName"));
|
||||
assertEquals(false, TestUtils.getPropertyValue(handler, "shouldTrack"));
|
||||
assertNotNull(TestUtils.getPropertyValue(handler, "mongoTemplate"));
|
||||
assertEquals(context.getBean("mongoDbFactory"), TestUtils.getPropertyValue(handler, "mongoDbFactory"));
|
||||
assertNotNull(TestUtils.getPropertyValue(handler, "evaluationContext"));
|
||||
assertTrue(TestUtils.getPropertyValue(handler, "collectionNameExpression") instanceof LiteralExpression);
|
||||
assertEquals("data", TestUtils.getPropertyValue(handler, "collectionNameExpression.literalValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullConfigWithCollectionExpression(){
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("outbound-adapter-parser-config.xml", this.getClass());
|
||||
MongoDbStoringMessageHandler handler =
|
||||
TestUtils.getPropertyValue(context.getBean("fullConfigWithCollectionExpression.adapter"), "handler", MongoDbStoringMessageHandler.class);
|
||||
assertEquals("fullConfigWithCollectionExpression.adapter", TestUtils.getPropertyValue(handler, "componentName"));
|
||||
assertEquals(false, TestUtils.getPropertyValue(handler, "shouldTrack"));
|
||||
assertNotNull(TestUtils.getPropertyValue(handler, "mongoTemplate"));
|
||||
assertEquals(context.getBean("mongoDbFactory"), TestUtils.getPropertyValue(handler, "mongoDbFactory"));
|
||||
assertNotNull(TestUtils.getPropertyValue(handler, "evaluationContext"));
|
||||
assertTrue(TestUtils.getPropertyValue(handler, "collectionNameExpression") instanceof SpelExpression);
|
||||
assertEquals("headers.collectionName", TestUtils.getPropertyValue(handler, "collectionNameExpression.expression"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullConfigWithCollection(){
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("outbound-adapter-parser-config.xml", this.getClass());
|
||||
MongoDbStoringMessageHandler handler =
|
||||
TestUtils.getPropertyValue(context.getBean("fullConfigWithCollection.adapter"), "handler", MongoDbStoringMessageHandler.class);
|
||||
assertEquals("fullConfigWithCollection.adapter", TestUtils.getPropertyValue(handler, "componentName"));
|
||||
assertEquals(false, TestUtils.getPropertyValue(handler, "shouldTrack"));
|
||||
assertNotNull(TestUtils.getPropertyValue(handler, "mongoTemplate"));
|
||||
assertEquals(context.getBean("mongoDbFactory"), TestUtils.getPropertyValue(handler, "mongoDbFactory"));
|
||||
assertNotNull(TestUtils.getPropertyValue(handler, "evaluationContext"));
|
||||
assertTrue(TestUtils.getPropertyValue(handler, "collectionNameExpression") instanceof LiteralExpression);
|
||||
assertEquals("foo", TestUtils.getPropertyValue(handler, "collectionNameExpression.literalValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullConfigWithMongoTemplate(){
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("outbound-adapter-parser-config.xml", this.getClass());
|
||||
MongoDbStoringMessageHandler handler =
|
||||
TestUtils.getPropertyValue(context.getBean("fullConfigWithMongoTemplate.adapter"), "handler", MongoDbStoringMessageHandler.class);
|
||||
assertEquals("fullConfigWithMongoTemplate.adapter", TestUtils.getPropertyValue(handler, "componentName"));
|
||||
assertEquals(false, TestUtils.getPropertyValue(handler, "shouldTrack"));
|
||||
assertNotNull(TestUtils.getPropertyValue(handler, "mongoTemplate"));
|
||||
assertNotNull(TestUtils.getPropertyValue(handler, "evaluationContext"));
|
||||
assertTrue(TestUtils.getPropertyValue(handler, "collectionNameExpression") instanceof LiteralExpression);
|
||||
assertEquals("foo", TestUtils.getPropertyValue(handler, "collectionNameExpression.literalValue"));
|
||||
}
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void templateAndFactoryFail(){
|
||||
new ClassPathXmlApplicationContext("outbound-adapter-parser-fail-template-factory-config.xml", this.getClass());
|
||||
}
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void templateAndConverterFail(){
|
||||
new ClassPathXmlApplicationContext("outbound-adapter-parser-fail-template-converter-config.xml", this.getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<mongo:db-factory id="mongoDbFactory" dbname="test"/>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapter"
|
||||
channel="replyChannel"
|
||||
query="{'name' : 'Bob'}"
|
||||
entity-class="java.lang.Object"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="100"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapterNamedFactory"
|
||||
mongodb-factory="mongoDbFactory"
|
||||
channel="replyChannel"
|
||||
query="{'name' : 'Bob'}"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="5000"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapterWithTemplate"
|
||||
channel="replyChannel"
|
||||
mongo-template="mongoDbTemplate"
|
||||
query="{'name' : 'Bob'}"
|
||||
expect-single-result="true"
|
||||
entity-class="org.springframework.integration.mongodb.rules.MongoDbAvailableTests.Person"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="5000"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapterWithNamedCollection"
|
||||
channel="replyChannel"
|
||||
collection-name="foo"
|
||||
mongo-template="mongoDbTemplate"
|
||||
query="{'name' : 'Bob'}"
|
||||
entity-class="java.lang.Object"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="5000"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapterWithNamedCollectionExpression"
|
||||
channel="replyChannel"
|
||||
collection-name-expression="'foo'"
|
||||
mongo-template="mongoDbTemplate"
|
||||
query="{'name' : 'Bob'}"
|
||||
entity-class="java.lang.Object"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="5000"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="inboundAdapterWithOnSuccessDisposition"
|
||||
channel="replyChannel"
|
||||
query="{'name' : 'Bob'}"
|
||||
auto-startup="false">
|
||||
|
||||
<int:poller fixed-rate="200" max-messages-per-poll="1">
|
||||
<int:pseudo-transactional on-success-expression="@documentCleaner.remove(#resource, payload, headers.mongo_collectionName)" on-success-result-channel="nullChannel"/>
|
||||
</int:poller>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapterWithConverter"
|
||||
channel="replyChannel"
|
||||
query="{'name' : 'Bob'}"
|
||||
entity-class="java.lang.Object"
|
||||
mongo-converter="mongoConverter"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="100"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<bean id="documentCleaner" class="org.springframework.integration.mongodb.config.MongoDbInboundChannelAdapterIntegrationTests.DocumentCleaner"/>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoConverter" class="org.springframework.integration.mongodb.rules.MongoDbAvailableTests.TestMongoConverter">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<int:channel id="replyChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<bean id="mongoDbFactory" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.data.mongodb.MongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="minimalConfig"
|
||||
query="bar"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="100"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="fullConfigWithCollectionExpression"
|
||||
collection-name-expression="'foo'"
|
||||
query="bar"
|
||||
mongo-converter="mongoConverter"
|
||||
mongodb-factory="mongoDbFactory"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="100"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="fullConfigWithCollectionName"
|
||||
collection-name="foo"
|
||||
query="bar"
|
||||
mongo-converter="mongoConverter"
|
||||
mongodb-factory="mongoDbFactory"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="100"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="fullConfigWithMongoTemplate"
|
||||
collection-name="foo"
|
||||
query="bar"
|
||||
mongo-template="mongoDbTemplate"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="100"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<bean id="mongoConverter" class="org.springframework.integration.mongodb.rules.MongoDbAvailableTests.TestMongoConverter">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<bean id="mongoDbFactory" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.data.mongodb.MongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="minimalConfig"
|
||||
query="bar"
|
||||
mongo-template="mongoDbTemplate"
|
||||
mongo-converter="mongoConverter"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="100"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<bean id="mongoConverter" class="org.springframework.integration.mongodb.rules.MongoDbAvailableTests.TestMongoConverter">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<bean id="mongoDbFactory" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.data.mongodb.MongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="minimalConfig"
|
||||
query="bar"
|
||||
mongo-template="mongoDbTemplate"
|
||||
mongodb-factory="mongoDbFactory"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="100"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<mongo:db-factory id="mongoDbFactory" dbname="test"/>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapter"
|
||||
channel="replyChannel"
|
||||
collection-name="bar"
|
||||
collection-name-expression="''"
|
||||
query-expression="''"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="5000"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int:channel id="replyChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<mongo:db-factory id="mongoDbFactory" dbname="test"/>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapter"
|
||||
channel="replyChannel"
|
||||
query="{'name' : 'Bob'}"
|
||||
mongo-template="mongoDbTemplate"
|
||||
mongo-converter="mongoConverter"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="5000"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoConverter" class="org.springframework.integration.mongodb.rules.MongoDbAvailableTests.TestMongoConverter">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<int:channel id="replyChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<mongo:db-factory id="mongoDbFactory" dbname="test"/>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapter"
|
||||
channel="replyChannel"
|
||||
query="{'name' : 'Bob'}"
|
||||
mongodb-factory="mongoDbFactory"
|
||||
mongo-template="mongoDbTemplate"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="5000"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<int:channel id="replyChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<mongo:db-factory id="mongoDbFactory" dbname="test"/>
|
||||
|
||||
<int-mongodb:inbound-channel-adapter id="mongoInboundAdapter"
|
||||
channel="replyChannel"
|
||||
query="{'name' : 'Bob'}"
|
||||
query-expression="''"
|
||||
auto-startup="false">
|
||||
<int:poller fixed-rate="5000"/>
|
||||
</int-mongodb:inbound-channel-adapter>
|
||||
|
||||
<int:channel id="replyChannel">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<mongo:db-factory id="mongoDbFactory" dbname="test"/>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="simpleAdapter"/>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="simpleAdapterWithNamedCollection"
|
||||
collection-name-expression="headers.collectionName"/>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="simpleAdapterWithTemplate"
|
||||
collection-name-expression="headers.collectionName"
|
||||
mongo-template="mongoDbTemplate"/>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="simpleAdapterWithConverter"
|
||||
mongo-converter="mongoConverter"/>
|
||||
|
||||
<bean id="mongoConverter" class="org.springframework.integration.mongodb.rules.MongoDbAvailableTests.TestMongoConverter">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<bean id="mongoDbFactory" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.data.mongodb.MongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="minimalConfig"/>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="fullConfigWithCollectionExpression"
|
||||
collection-name-expression="headers.collectionName"
|
||||
mongo-converter="mongoConverter"
|
||||
mongodb-factory="mongoDbFactory"/>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="fullConfigWithCollection"
|
||||
collection-name="foo"
|
||||
mongo-converter="mongoConverter"
|
||||
mongodb-factory="mongoDbFactory"/>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="fullConfigWithMongoTemplate"
|
||||
collection-name="foo"
|
||||
mongo-template="mongoDbTemplate"/>
|
||||
|
||||
<bean id="mongoConverter" class="org.springframework.integration.mongodb.rules.MongoDbAvailableTests.TestMongoConverter">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<bean id="mongoDbFactory" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.data.mongodb.MongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="fullConfigWithCollectionExpression"
|
||||
collection-name-expression="headers.collectionName"
|
||||
mongo-template="mongoDbTemplate"
|
||||
mongo-converter="mongoConverter"/>
|
||||
|
||||
<bean id="mongoConverter" class="org.springframework.integration.mongodb.rules.MongoDbAvailableTests.TestMongoConverter">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mongodb="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/mongodb http://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd">
|
||||
|
||||
<bean id="mongoDbFactory" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.data.mongodb.MongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
<int-mongodb:outbound-channel-adapter id="fullConfigWithCollectionExpression"
|
||||
collection-name-expression="headers.collectionName"
|
||||
mongo-template="mongoDbTemplate"
|
||||
mongodb-factory="mongoDbFactory"/>
|
||||
|
||||
<bean id="mongoDbTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
|
||||
<constructor-arg ref="mongoDbFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,261 @@
|
||||
/*
|
||||
* Copyright 2007-2012 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.mongodb.inbound;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.integration.mongodb.rules.MongoDbAvailable;
|
||||
import org.springframework.integration.mongodb.rules.MongoDbAvailableTests;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.util.JSON;
|
||||
|
||||
/**
|
||||
* @author Amol Nayak
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public class MongoDbMessageSourceTests extends MongoDbAvailableTests {
|
||||
/**
|
||||
* Tests by providing a null MongoDB Factory
|
||||
*
|
||||
*/
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void withNullMongoDBFactory() {
|
||||
Expression expression = mock(Expression.class);
|
||||
new MongoDbMessageSource((MongoDbFactory)null, expression);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void withNullMongoTemplate() {
|
||||
Expression expression = mock(Expression.class);
|
||||
new MongoDbMessageSource((MongoOperations)null, expression);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void withNullQueryExpression() {
|
||||
MongoDbFactory mongoDbFactory = mock(MongoDbFactory.class);
|
||||
new MongoDbMessageSource(mongoDbFactory, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateSuccessfullQueryWithSinigleElementIfOneInListAsDbObject() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson(), "data");
|
||||
|
||||
Expression queryExpression = new LiteralExpression("{'name' : 'Oleg'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
|
||||
messageSource.afterPropertiesSet();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<DBObject> results = ((List<DBObject>)messageSource.receive().getPayload());
|
||||
assertEquals(1, results.size());
|
||||
DBObject resultObject = results.get(0);
|
||||
|
||||
assertEquals("Oleg", resultObject.get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateSuccessfullQueryWithSinigleElementIfOneInList() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson(), "data");
|
||||
|
||||
Expression queryExpression = new LiteralExpression("{'name' : 'Oleg'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
|
||||
messageSource.setEntityClass(Object.class);
|
||||
messageSource.afterPropertiesSet();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Person> results = ((List<Person>)messageSource.receive().getPayload());
|
||||
assertEquals(1, results.size());
|
||||
Person person = results.get(0);
|
||||
assertEquals("Oleg", person.getName());
|
||||
assertEquals("PA", person.getAddress().getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateSuccessfullQueryWithSinigleElementIfOneInListAndSingleResult() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson(), "data");
|
||||
|
||||
Expression queryExpression = new LiteralExpression("{'name' : 'Oleg'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
|
||||
messageSource.setEntityClass(Object.class);
|
||||
messageSource.setExpectSingleResult(true);
|
||||
messageSource.afterPropertiesSet();
|
||||
Person person = (Person)messageSource.receive().getPayload();
|
||||
|
||||
assertEquals("Oleg", person.getName());
|
||||
assertEquals("PA", person.getAddress().getState());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateSuccessfullSubObjectQueryWithSinigleElementIfOneInList() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson(), "data");
|
||||
|
||||
Expression queryExpression = new LiteralExpression("{'address.state' : 'PA'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
|
||||
messageSource.setEntityClass(Object.class);
|
||||
messageSource.afterPropertiesSet();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Person> results = ((List<Person>)messageSource.receive().getPayload());
|
||||
Person person = results.get(0);
|
||||
assertEquals("Oleg", person.getName());
|
||||
assertEquals("PA", person.getAddress().getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateSuccessfullQueryWithMultipleElements() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Manny"), "data");
|
||||
template.save(this.createPerson("Moe"), "data");
|
||||
template.save(this.createPerson("Jack"), "data");
|
||||
|
||||
Expression queryExpression = new LiteralExpression("{'address.state' : 'PA'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
|
||||
messageSource.afterPropertiesSet();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Person> persons = (List<Person>) messageSource.receive().getPayload();
|
||||
assertEquals(3, persons.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateSuccessfullQueryWithNullReturn() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Manny"), "data");
|
||||
template.save(this.createPerson("Moe"), "data");
|
||||
template.save(this.createPerson("Jack"), "data");
|
||||
|
||||
Expression queryExpression = new LiteralExpression("{'address.state' : 'NJ'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
|
||||
messageSource.afterPropertiesSet();
|
||||
assertNull(messageSource.receive());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateSuccessfullQueryWithCustomConverter() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(this.createPerson("Manny"), "data");
|
||||
template.save(this.createPerson("Moe"), "data");
|
||||
template.save(this.createPerson("Jack"), "data");
|
||||
|
||||
Expression queryExpression = new LiteralExpression("{'address.state' : 'PA'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
|
||||
MappingMongoConverter converter = new TestMongoConverter(mongoDbFactory, new MongoMappingContext());
|
||||
converter.afterPropertiesSet();
|
||||
converter = spy(converter);
|
||||
messageSource.setMongoConverter(converter);
|
||||
messageSource.afterPropertiesSet();
|
||||
|
||||
List<Person> persons = (List<Person>) messageSource.receive().getPayload();
|
||||
assertEquals(3, persons.size());
|
||||
verify(converter, times(3)).read((Class<Person>) Mockito.any(), Mockito.any(DBObject.class));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateSuccessfullQueryWithMongoTemplate() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MappingMongoConverter converter = new TestMongoConverter(mongoDbFactory, new MongoMappingContext());
|
||||
converter.afterPropertiesSet();
|
||||
converter = spy(converter);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory, converter);
|
||||
Expression queryExpression = new LiteralExpression("{'address.state' : 'PA'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(template, queryExpression);
|
||||
messageSource.afterPropertiesSet();
|
||||
|
||||
MongoTemplate writingTemplate = new MongoTemplate(mongoDbFactory, converter);
|
||||
writingTemplate.save(this.createPerson("Manny"), "data");
|
||||
writingTemplate.save(this.createPerson("Moe"), "data");
|
||||
writingTemplate.save(this.createPerson("Jack"), "data");
|
||||
|
||||
List<Person> persons = (List<Person>) messageSource.receive().getPayload();
|
||||
assertEquals(3, persons.size());
|
||||
verify(converter, times(3)).read((Class<Person>) Mockito.any(), Mockito.any(DBObject.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validatePipelineInModifyOut() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.save(JSON.parse("{'name' : 'Manny', 'id' : 1}"), "data");
|
||||
|
||||
Expression queryExpression = new LiteralExpression("{'name' : 'Manny'}");
|
||||
MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
|
||||
messageSource.setExpectSingleResult(true);
|
||||
messageSource.afterPropertiesSet();
|
||||
DBObject result = (DBObject) messageSource.receive().getPayload();
|
||||
Object id = result.get("_id");
|
||||
result.put("company","PepBoys");
|
||||
template.save(result, "data");
|
||||
result = (DBObject) messageSource.receive().getPayload();
|
||||
assertEquals(id, result.get("_id"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright 2007-2012 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.mongodb.outbound;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.mongodb.rules.MongoDbAvailable;
|
||||
import org.springframework.integration.mongodb.rules.MongoDbAvailableTests;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* @author Amol Nayak
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public class MongoDbStoringMessageHandlerTests extends MongoDbAvailableTests {
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void withNullMongoDBFactory() {
|
||||
new MongoDbStoringMessageHandler((MongoDbFactory)null);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void withNullMongoTemplate() {
|
||||
new MongoDbStoringMessageHandler((MongoOperations)null);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateMessageHandlingWithDefaultCollection() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoDbStoringMessageHandler handler = new MongoDbStoringMessageHandler(mongoDbFactory);
|
||||
handler.afterPropertiesSet();
|
||||
Message<Person> message = MessageBuilder.withPayload(this.createPerson("Bob")).build();
|
||||
handler.handleMessage(message);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
Query query = new BasicQuery("{'name' : 'Bob'}");
|
||||
Person person = template.findOne(query, Person.class, "data");
|
||||
|
||||
assertEquals("Bob", person.getName());
|
||||
assertEquals("PA", person.getAddress().getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateMessageHandlingWithNamedCollection() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoDbStoringMessageHandler handler = new MongoDbStoringMessageHandler(mongoDbFactory);
|
||||
handler.setCollectionNameExpression(new LiteralExpression("foo"));
|
||||
handler.afterPropertiesSet();
|
||||
Message<Person> message = MessageBuilder.withPayload(this.createPerson("Bob")).build();
|
||||
handler.handleMessage(message);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
Query query = new BasicQuery("{'name' : 'Bob'}");
|
||||
Person person = template.findOne(query, Person.class, "foo");
|
||||
|
||||
assertEquals("Bob", person.getName());
|
||||
assertEquals("PA", person.getAddress().getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateMessageHandlingWithMongoConverter() throws Exception {
|
||||
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MongoDbStoringMessageHandler handler = new MongoDbStoringMessageHandler(mongoDbFactory);
|
||||
handler.setCollectionNameExpression(new LiteralExpression("foo"));
|
||||
MappingMongoConverter converter = new TestMongoConverter(mongoDbFactory, new MongoMappingContext());
|
||||
converter.afterPropertiesSet();
|
||||
converter = spy(converter);
|
||||
handler.setMongoConverter(converter);
|
||||
handler.afterPropertiesSet();
|
||||
Message<Person> message = MessageBuilder.withPayload(this.createPerson("Bob")).build();
|
||||
handler.handleMessage(message);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
Query query = new BasicQuery("{'name' : 'Bob'}");
|
||||
Person person = template.findOne(query, Person.class, "foo");
|
||||
|
||||
assertEquals("Bob", person.getName());
|
||||
assertEquals("PA", person.getAddress().getState());
|
||||
verify(converter, times(1)).write(Mockito.any(), Mockito.any(DBObject.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@MongoDbAvailable
|
||||
public void validateMessageHandlingWithMongoTemplate() throws Exception {
|
||||
MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
|
||||
MappingMongoConverter converter = new TestMongoConverter(mongoDbFactory, new MongoMappingContext());
|
||||
converter.afterPropertiesSet();
|
||||
converter = spy(converter);
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory, converter);
|
||||
|
||||
|
||||
MongoDbStoringMessageHandler handler = new MongoDbStoringMessageHandler(template);
|
||||
handler.setCollectionNameExpression(new LiteralExpression("foo"));
|
||||
handler.afterPropertiesSet();
|
||||
Message<Person> message = MessageBuilder.withPayload(this.createPerson("Bob")).build();
|
||||
handler.handleMessage(message);
|
||||
|
||||
MongoTemplate readingTemplate = new MongoTemplate(mongoDbFactory);
|
||||
Query query = new BasicQuery("{'name' : 'Bob'}");
|
||||
Person person = readingTemplate.findOne(query, Person.class, "foo");
|
||||
|
||||
assertEquals("Bob", person.getName());
|
||||
assertEquals("PA", person.getAddress().getState());
|
||||
verify(converter, times(1)).write(Mockito.any(), Mockito.any(DBObject.class));
|
||||
}
|
||||
}
|
||||
@@ -17,15 +17,21 @@
|
||||
package org.springframework.integration.mongodb.rules;
|
||||
|
||||
import org.junit.Rule;
|
||||
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.Mongo;
|
||||
|
||||
/**
|
||||
* Convenience base class that enables unit test methods to rely upon the {@link MongoDbAvailable} annotation.
|
||||
*
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.1
|
||||
*/
|
||||
@@ -33,13 +39,101 @@ public abstract class MongoDbAvailableTests {
|
||||
|
||||
@Rule
|
||||
public MongoDbAvailableRule redisAvailableRule = new MongoDbAvailableRule();
|
||||
|
||||
|
||||
protected MongoDbFactory prepareMongoFactory() throws Exception{
|
||||
|
||||
|
||||
protected MongoDbFactory prepareMongoFactory(String... additionalCollectionToDrop) throws Exception{
|
||||
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new Mongo(), "test");
|
||||
MongoTemplate template = new MongoTemplate(mongoDbFactory);
|
||||
template.dropCollection("messages");
|
||||
template.dropCollection("data");
|
||||
for (String additionalCollection : additionalCollectionToDrop) {
|
||||
template.dropCollection(additionalCollection);
|
||||
}
|
||||
return mongoDbFactory;
|
||||
}
|
||||
|
||||
public Person createPerson(){
|
||||
Address address = new Address();
|
||||
address.setCity("Philadelphia");
|
||||
address.setStreet("2121 Rawn street");
|
||||
address.setState("PA");
|
||||
|
||||
Person person = new Person();
|
||||
person.setAddress(address);
|
||||
person.setName("Oleg");
|
||||
return person;
|
||||
}
|
||||
|
||||
public Person createPerson(String name){
|
||||
Address address = new Address();
|
||||
address.setCity("Philadelphia");
|
||||
address.setStreet("2121 Rawn street");
|
||||
address.setState("PA");
|
||||
|
||||
Person person = new Person();
|
||||
person.setAddress(address);
|
||||
person.setName(name);
|
||||
return person;
|
||||
}
|
||||
|
||||
public static class Person {
|
||||
private Address address;
|
||||
private String name;
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Address {
|
||||
private String street;
|
||||
private String city;
|
||||
private String state;
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestMongoConverter extends MappingMongoConverter {
|
||||
|
||||
public TestMongoConverter(
|
||||
MongoDbFactory mongoDbFactory,
|
||||
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
|
||||
super(mongoDbFactory, mappingContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object source, DBObject target) {
|
||||
super.write(source, target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> S read(Class<S> clazz, DBObject source) {
|
||||
return super.read(clazz, source);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user