Search in sources :

Example 31 with MongoDbAvailable

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

the class MongoDbOutboundGatewayTests method testWithCollectionCallbackFindOne.

@Test
@MongoDbAvailable
public void testWithCollectionCallbackFindOne() throws Exception {
    Message<String> message = MessageBuilder.withPayload("").build();
    MongoDbOutboundGateway gateway = createGateway();
    gateway.setEntityClass(Person.class);
    gateway.setCollectionNameExpression(new LiteralExpression("data"));
    gateway.setRequiresReply(false);
    gateway.setCollectionCallback(collection -> {
        collection.insertOne(new Document("name", "Mike"));
        return null;
    });
    gateway.afterPropertiesSet();
    gateway.handleRequestMessage(message);
    List<Person> persons = this.mongoTemplate.find(new Query(), Person.class, COLLECTION_NAME);
    assertEquals(5, persons.size());
    assertTrue(persons.stream().anyMatch(p -> p.getName().equals("Mike")));
}
Also used : Document(org.bson.Document) DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) MongoCollection(com.mongodb.client.MongoCollection) BulkOperations(org.springframework.data.mongodb.core.BulkOperations) RunWith(org.junit.runner.RunWith) LiteralExpression(org.springframework.expression.common.LiteralExpression) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) TestUtils(org.springframework.integration.test.util.TestUtils) Function(java.util.function.Function) MessageBuilder(org.springframework.integration.support.MessageBuilder) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) After(org.junit.After) FunctionExpression(org.springframework.integration.expression.FunctionExpression) Message(org.springframework.messaging.Message) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Query(org.springframework.data.mongodb.core.query.Query) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable) MongoDbAvailableTests(org.springframework.integration.mongodb.rules.MongoDbAvailableTests) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) MongoOperations(org.springframework.data.mongodb.core.MongoOperations) ContextConfiguration(org.springframework.test.context.ContextConfiguration) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) LiteralExpression(org.springframework.expression.common.LiteralExpression) Document(org.bson.Document) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 32 with MongoDbAvailable

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

the class AbstractMongoDbMessageStoreTests method testMutableMessageAsPayload.

@Test
@MongoDbAvailable
public void testMutableMessageAsPayload() throws Exception {
    MessageStore store = this.getMessageStore();
    Person p = new Person();
    p.setFname("John");
    p.setLname("Doe");
    Message<?> messageToStore = new GenericMessage<Message<?>>(MutableMessageBuilder.withPayload(p).build());
    store.addMessage(messageToStore);
    Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
    assertNotNull(retrievedMessage);
    assertThat(retrievedMessage.getPayload(), instanceOf(MutableMessage.class));
    assertEquals(messageToStore.getPayload(), retrievedMessage.getPayload());
    assertEquals(messageToStore.getHeaders(), retrievedMessage.getHeaders());
    assertEquals(((Message<?>) messageToStore.getPayload()).getPayload(), p);
    assertEquals(messageToStore, retrievedMessage);
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 33 with MongoDbAvailable

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

the class AbstractMongoDbMessageStoreTests method testInt3153SequenceDetails.

@Test
@MongoDbAvailable
public void testInt3153SequenceDetails() throws Exception {
    cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageStore store = getMessageStore();
    Message<?> messageToStore = MessageBuilder.withPayload("test").pushSequenceDetails(UUID.randomUUID(), 1, 1).pushSequenceDetails(UUID.randomUUID(), 1, 1).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)

Example 34 with MongoDbAvailable

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

the class AbstractMongoDbMessageStoreTests method testAddThenRemoveWithStringPayload.

@Test
@MongoDbAvailable
public void testAddThenRemoveWithStringPayload() throws Exception {
    cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageStore store = getMessageStore();
    Message<?> messageToStore = MessageBuilder.withPayload("Hello").build();
    store.addMessage(messageToStore);
    Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
    assertNotNull(retrievedMessage);
    store.removeMessage(retrievedMessage.getHeaders().getId());
    retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
    assertNull(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)

Example 35 with MongoDbAvailable

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

the class AbstractMongoDbMessageStoreTests method testAdviceMessageAsPayload.

@Test
@MongoDbAvailable
public void testAdviceMessageAsPayload() throws Exception {
    MessageStore store = this.getMessageStore();
    Person p = new Person();
    p.setFname("John");
    p.setLname("Doe");
    Message<Person> inputMessage = MessageBuilder.withPayload(p).build();
    Message<?> messageToStore = new GenericMessage<Message<?>>(new AdviceMessage<String>("foo", inputMessage));
    store.addMessage(messageToStore);
    Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
    assertNotNull(retrievedMessage);
    assertTrue(retrievedMessage.getPayload() instanceof AdviceMessage);
    AdviceMessage<?> adviceMessage = (AdviceMessage<?>) retrievedMessage.getPayload();
    assertEquals("foo", adviceMessage.getPayload());
    assertEquals(messageToStore.getHeaders(), retrievedMessage.getHeaders());
    assertEquals(inputMessage, adviceMessage.getInputMessage());
    assertEquals(messageToStore, retrievedMessage);
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) AdviceMessage(org.springframework.integration.message.AdviceMessage) 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