/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/outbound/MongoDbOutboundGatewayXmlTests.java

https://github.com/artembilan/spring-integration · Java · 187 lines · 128 code · 38 blank · 21 comment · 0 complexity · 49eb6d1b2bbee2348ae825b9bbf2e0f5 MD5 · raw file

  1. /*
  2. * Copyright 2016-2020 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.integration.mongodb.outbound;
  17. import static org.assertj.core.api.Assertions.assertThat;
  18. import java.util.List;
  19. import org.junit.After;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import org.junit.runner.RunWith;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.context.ApplicationContext;
  25. import org.springframework.data.mongodb.MongoDatabaseFactory;
  26. import org.springframework.data.mongodb.core.MongoTemplate;
  27. import org.springframework.integration.endpoint.EventDrivenConsumer;
  28. import org.springframework.integration.mongodb.rules.MongoDbAvailable;
  29. import org.springframework.integration.mongodb.rules.MongoDbAvailableTests;
  30. import org.springframework.integration.support.MessageBuilder;
  31. import org.springframework.messaging.Message;
  32. import org.springframework.messaging.PollableChannel;
  33. import org.springframework.test.annotation.DirtiesContext;
  34. import org.springframework.test.context.junit4.SpringRunner;
  35. /**
  36. * @author Xavier Padro
  37. * @author Artem Bilan
  38. *
  39. * @since 5.0
  40. */
  41. @RunWith(SpringRunner.class)
  42. @DirtiesContext
  43. public class MongoDbOutboundGatewayXmlTests extends MongoDbAvailableTests {
  44. private static final String COLLECTION_NAME = "data";
  45. @Autowired
  46. private ApplicationContext context;
  47. @Before
  48. public void setUp() {
  49. MongoDatabaseFactory mongoDbFactory = this.prepareMongoFactory();
  50. MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
  51. mongoTemplate.save(this.createPerson("Artem"), COLLECTION_NAME);
  52. mongoTemplate.save(this.createPerson("Gary"), COLLECTION_NAME);
  53. mongoTemplate.save(this.createPerson("Oleg"), COLLECTION_NAME);
  54. mongoTemplate.save(this.createPerson("Xavi"), COLLECTION_NAME);
  55. }
  56. @After
  57. public void cleanUp() {
  58. MongoDatabaseFactory mongoDbFactory = this.prepareMongoFactory();
  59. MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
  60. mongoTemplate.dropCollection(COLLECTION_NAME);
  61. }
  62. @Test
  63. @MongoDbAvailable
  64. public void testSingleQuery() {
  65. EventDrivenConsumer consumer = context.getBean("gatewaySingleQuery", EventDrivenConsumer.class);
  66. PollableChannel outChannel = context.getBean("out", PollableChannel.class);
  67. Message<String> message = MessageBuilder.withPayload("").build();
  68. consumer.getHandler().handleMessage(message);
  69. Message<?> result = outChannel.receive(10000);
  70. Person person = getPerson(result);
  71. assertThat(person.getName()).isEqualTo("Xavi");
  72. }
  73. @Test
  74. @MongoDbAvailable
  75. public void testSingleQueryWithTemplate() {
  76. EventDrivenConsumer consumer = context.getBean("gatewayWithTemplate", EventDrivenConsumer.class);
  77. PollableChannel outChannel = context.getBean("out", PollableChannel.class);
  78. Message<String> message = MessageBuilder.withPayload("").build();
  79. consumer.getHandler().handleMessage(message);
  80. Message<?> result = outChannel.receive(10000);
  81. Person person = getPerson(result);
  82. assertThat(person.getName()).isEqualTo("Xavi");
  83. }
  84. @Test
  85. @MongoDbAvailable
  86. public void testSingleQueryExpression() {
  87. EventDrivenConsumer consumer = context.getBean("gatewaySingleQueryExpression", EventDrivenConsumer.class);
  88. PollableChannel outChannel = context.getBean("out", PollableChannel.class);
  89. Message<String> message = MessageBuilder
  90. .withPayload("")
  91. .setHeader("query", "{'name' : 'Gary'}")
  92. .setHeader("collectionName", "data")
  93. .build();
  94. consumer.getHandler().handleMessage(message);
  95. Message<?> result = outChannel.receive(10000);
  96. Person person = getPerson(result);
  97. assertThat(person.getName()).isEqualTo("Gary");
  98. }
  99. @Test
  100. @MongoDbAvailable
  101. public void testQueryExpression() {
  102. EventDrivenConsumer consumer = context.getBean("gatewayQueryExpression", EventDrivenConsumer.class);
  103. PollableChannel outChannel = context.getBean("out", PollableChannel.class);
  104. Message<String> message = MessageBuilder
  105. .withPayload("")
  106. .setHeader("query", "{}")
  107. .setHeader("collectionName", "data")
  108. .build();
  109. consumer.getHandler().handleMessage(message);
  110. Message<?> result = outChannel.receive(10000);
  111. List<Person> persons = getPersons(result);
  112. assertThat(persons.size()).isEqualTo(4);
  113. }
  114. @Test
  115. @MongoDbAvailable
  116. public void testQueryExpressionWithLimit() {
  117. EventDrivenConsumer consumer = context.getBean("gatewayQueryExpressionLimit", EventDrivenConsumer.class);
  118. PollableChannel outChannel = context.getBean("out", PollableChannel.class);
  119. Message<String> message = MessageBuilder
  120. .withPayload("")
  121. .setHeader("collectionName", "data")
  122. .build();
  123. consumer.getHandler().handleMessage(message);
  124. Message<?> result = outChannel.receive(10000);
  125. List<Person> persons = getPersons(result);
  126. assertThat(persons.size()).isEqualTo(2);
  127. }
  128. @Test
  129. @MongoDbAvailable
  130. public void testCollectionCallback() {
  131. EventDrivenConsumer consumer = context.getBean("gatewayCollectionCallback", EventDrivenConsumer.class);
  132. PollableChannel outChannel = context.getBean("out", PollableChannel.class);
  133. Message<String> message = MessageBuilder
  134. .withPayload("")
  135. .setHeader("collectionName", "data")
  136. .build();
  137. consumer.getHandler().handleMessage(message);
  138. Message<?> result = outChannel.receive(10000);
  139. long personsCount = (Long) result.getPayload();
  140. assertThat(personsCount).isEqualTo(4);
  141. }
  142. private Person getPerson(Message<?> message) {
  143. return (Person) message.getPayload();
  144. }
  145. @SuppressWarnings("unchecked")
  146. private List<Person> getPersons(Message<?> message) {
  147. return (List<Person>) message.getPayload();
  148. }
  149. }