Search in sources :

Example 26 with MongoDbAvailable

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

the class MongoDbMessageSourceTests method validatePipelineInModifyOut.

@Test
@MongoDbAvailable
public void validatePipelineInModifyOut() throws Exception {
    MongoDbFactory mongoDbFactory = this.prepareMongoFactory();
    MongoTemplate template = new MongoTemplate(mongoDbFactory);
    template.save(BasicDBObject.parse("{'name' : 'Manny', 'id' : 1}"), "data");
    Expression queryExpression = new LiteralExpression("{'name' : 'Manny'}");
    MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoDbFactory, queryExpression);
    messageSource.setExpectSingleResult(true);
    messageSource.setBeanFactory(mock(BeanFactory.class));
    messageSource.afterPropertiesSet();
    Document result = (Document) messageSource.receive().getPayload();
    Object id = result.get("_id");
    result.put("company", "PepBoys");
    template.save(result, "data");
    result = (Document) messageSource.receive().getPayload();
    assertEquals(id, result.get("_id"));
}
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) BasicDBObject(com.mongodb.BasicDBObject) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Document(org.bson.Document) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 27 with MongoDbAvailable

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

the class MongoDbMessageSourceTests method validateSuccessfulQueryWithSingleElementIfOneInListAndSingleResult.

@Test
@MongoDbAvailable
public void validateSuccessfulQueryWithSingleElementIfOneInListAndSingleResult() 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.setBeanFactory(mock(BeanFactory.class));
    messageSource.afterPropertiesSet();
    Person person = (Person) messageSource.receive().getPayload();
    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 28 with MongoDbAvailable

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

the class MongoDbOutboundGatewayTests method testNoTemplateSpecified.

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

Example 29 with MongoDbAvailable

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

the class MongoDbOutboundGatewayTests method testSingleResultWithQueryExpressionAndNoEntityClass.

@Test
@MongoDbAvailable
public void testSingleResultWithQueryExpressionAndNoEntityClass() {
    Message<String> message = MessageBuilder.withPayload("").build();
    MongoDbOutboundGateway gateway = createGateway();
    gateway.setQueryExpression(new LiteralExpression("{name : 'Xavi'}"));
    gateway.setExpectSingleResult(true);
    gateway.afterPropertiesSet();
    Object result = gateway.handleRequestMessage(message);
    Document person = (Document) result;
    assertEquals("Xavi", person.get("name"));
}
Also used : LiteralExpression(org.springframework.expression.common.LiteralExpression) Document(org.bson.Document) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 30 with MongoDbAvailable

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

the class MongoDbOutboundGatewayTests method testWithCollectionNameExpressionSpecified.

@Test
@MongoDbAvailable
public void testWithCollectionNameExpressionSpecified() throws Exception {
    Message<String> message = MessageBuilder.withPayload("").build();
    MongoDbOutboundGateway gateway = createGateway();
    gateway.setQueryExpression(new LiteralExpression("{name : 'Xavi'}"));
    gateway.setExpectSingleResult(true);
    gateway.setCollectionNameExpression(new LiteralExpression("anotherCollection"));
    gateway.afterPropertiesSet();
    Object result = gateway.handleRequestMessage(message);
    assertNull(result);
    LiteralExpression collectionNameExpression = (LiteralExpression) TestUtils.getPropertyValue(gateway, "collectionNameExpression");
    assertNotNull(collectionNameExpression);
    assertEquals("anotherCollection", collectionNameExpression.getValue());
}
Also used : LiteralExpression(org.springframework.expression.common.LiteralExpression) 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