Search in sources :

Example 56 with MongoDbAvailable

use of org.springframework.integration.mongodb.rules.MongoDbAvailable in project spring-integration by spring-projects.

the class MongoDbMessageSourceTests method validateSuccessfulSubObjectQueryWithSingleElementIfOneInList.

@Test
@MongoDbAvailable
public void validateSuccessfulSubObjectQueryWithSingleElementIfOneInList() 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.setBeanFactory(mock(BeanFactory.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());
}
Also used : MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 57 with MongoDbAvailable

use of org.springframework.integration.mongodb.rules.MongoDbAvailable in project spring-integration by spring-projects.

the class MongoDbOutboundGatewayTests method testNoFactorySpecified.

@SuppressWarnings("ConstantConditions")
@Test
@MongoDbAvailable
public void testNoFactorySpecified() {
    MongoDbFactory nullFactory = null;
    try {
        new MongoDbOutboundGateway(nullFactory);
        Assert.fail("Expected the test case to throw an IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertEquals("MongoDbFactory translator must not be null!", e.getMessage());
    }
}
Also used : MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 58 with MongoDbAvailable

use of org.springframework.integration.mongodb.rules.MongoDbAvailable in project spring-integration by spring-projects.

the class MongoDbOutboundGatewayTests method testWithNullCollectionNameExpression.

@Test
@MongoDbAvailable
public void testWithNullCollectionNameExpression() throws Exception {
    MongoDbOutboundGateway gateway = new MongoDbOutboundGateway(mongoDbFactory);
    gateway.setBeanFactory(beanFactory);
    gateway.setQueryExpression(new LiteralExpression("{name : 'Xavi'}"));
    gateway.setExpectSingleResult(true);
    try {
        gateway.afterPropertiesSet();
        Assert.fail("Expected the test case to throw an IllegalArgumentException");
    } catch (IllegalStateException e) {
        assertEquals("no collection name specified", e.getMessage());
    }
}
Also used : LiteralExpression(org.springframework.expression.common.LiteralExpression) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 59 with MongoDbAvailable

use of org.springframework.integration.mongodb.rules.MongoDbAvailable in project spring-integration by spring-projects.

the class MongoDbOutboundGatewayTests method testWithCollectionCallbackCount.

@Test
@MongoDbAvailable
public void testWithCollectionCallbackCount() throws Exception {
    Message<String> message = MessageBuilder.withPayload("").build();
    MongoDbOutboundGateway gateway = createGateway();
    gateway.setEntityClass(Person.class);
    gateway.setCollectionNameExpression(new LiteralExpression("data"));
    gateway.setCollectionCallback(MongoCollection::count);
    gateway.afterPropertiesSet();
    long result = (long) gateway.handleRequestMessage(message);
    assertEquals(4, result);
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) LiteralExpression(org.springframework.expression.common.LiteralExpression) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 60 with MongoDbAvailable

use of org.springframework.integration.mongodb.rules.MongoDbAvailable in project spring-integration by spring-projects.

the class AbstractMongoDbMessageStoreTests method testAddGetWithObjectDefaultConstructorPayload.

@Test
@MongoDbAvailable
public void testAddGetWithObjectDefaultConstructorPayload() throws Exception {
    cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageStore store = getMessageStore();
    Person p = new Person();
    p.setFname("John");
    p.setLname("Doe");
    Message<?> messageToStore = MessageBuilder.withPayload(p).build();
    store.addMessage(messageToStore);
    Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
    assertNotNull(retrievedMessage);
    assertEquals(messageToStore.getPayload(), retrievedMessage.getPayload());
    assertEquals(messageToStore.getHeaders(), retrievedMessage.getHeaders());
    assertEquals(messageToStore, retrievedMessage);
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) MongoClient(com.mongodb.MongoClient) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Aggregations

Test (org.junit.Test)70 MongoDbAvailable (org.springframework.integration.mongodb.rules.MongoDbAvailable)70 MongoClient (com.mongodb.MongoClient)28 SimpleMongoDbFactory (org.springframework.data.mongodb.core.SimpleMongoDbFactory)28 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)27 GenericMessage (org.springframework.messaging.support.GenericMessage)20 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)19 LiteralExpression (org.springframework.expression.common.LiteralExpression)17 AbstractBatchingMessageGroupStore (org.springframework.integration.store.AbstractBatchingMessageGroupStore)17 MessageGroupStore (org.springframework.integration.store.MessageGroupStore)17 MessageStore (org.springframework.integration.store.MessageStore)14 BeanFactory (org.springframework.beans.factory.BeanFactory)13 MessageGroup (org.springframework.integration.store.MessageGroup)13 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)11 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 Expression (org.springframework.expression.Expression)8 Matchers.containsString (org.hamcrest.Matchers.containsString)7 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)6 MessageChannel (org.springframework.messaging.MessageChannel)6 PollableChannel (org.springframework.messaging.PollableChannel)6