/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/MongoDbMessageStoreClaimCheckIntegrationTests.java

https://github.com/artembilan/spring-integration · Java · 202 lines · 156 code · 27 blank · 19 comment · 16 complexity · 0653ab5b9a45f9b1e3914f9a9bb3ce50 MD5 · raw file

  1. /*
  2. * Copyright 2002-2019 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.store;
  17. import static org.assertj.core.api.Assertions.assertThat;
  18. import java.io.Serializable;
  19. import org.junit.After;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import org.springframework.context.support.GenericApplicationContext;
  23. import org.springframework.integration.mongodb.rules.MongoDbAvailable;
  24. import org.springframework.integration.mongodb.rules.MongoDbAvailableTests;
  25. import org.springframework.integration.support.MessageBuilder;
  26. import org.springframework.integration.test.util.TestUtils;
  27. import org.springframework.integration.transformer.ClaimCheckInTransformer;
  28. import org.springframework.integration.transformer.ClaimCheckOutTransformer;
  29. import org.springframework.messaging.Message;
  30. /**
  31. * @author Mark Fisher
  32. * @author Artem Bilan
  33. */
  34. public class MongoDbMessageStoreClaimCheckIntegrationTests extends MongoDbAvailableTests {
  35. private final GenericApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
  36. @Before
  37. public void setup() {
  38. this.testApplicationContext.refresh();
  39. }
  40. @After
  41. public void tearDown() {
  42. this.testApplicationContext.close();
  43. }
  44. @Test
  45. @MongoDbAvailable
  46. public void stringPayload() {
  47. MongoDbMessageStore messageStore = new MongoDbMessageStore(MONGO_DATABASE_FACTORY);
  48. messageStore.afterPropertiesSet();
  49. ClaimCheckInTransformer checkin = new ClaimCheckInTransformer(messageStore);
  50. ClaimCheckOutTransformer checkout = new ClaimCheckOutTransformer(messageStore);
  51. Message<?> originalMessage = MessageBuilder.withPayload("test1").build();
  52. Message<?> claimCheckMessage = checkin.transform(originalMessage);
  53. assertThat(claimCheckMessage.getPayload()).isEqualTo(originalMessage.getHeaders().getId());
  54. Message<?> checkedOutMessage = checkout.transform(claimCheckMessage);
  55. assertThat(checkedOutMessage.getHeaders().getId()).isEqualTo(claimCheckMessage.getPayload());
  56. assertThat(checkedOutMessage.getPayload()).isEqualTo(originalMessage.getPayload());
  57. assertThat(checkedOutMessage).isEqualTo(originalMessage);
  58. }
  59. @Test
  60. @MongoDbAvailable
  61. public void objectPayload() {
  62. MongoDbMessageStore messageStore = new MongoDbMessageStore(MONGO_DATABASE_FACTORY);
  63. messageStore.afterPropertiesSet();
  64. ClaimCheckInTransformer checkin = new ClaimCheckInTransformer(messageStore);
  65. ClaimCheckOutTransformer checkout = new ClaimCheckOutTransformer(messageStore);
  66. Beverage payload = new Beverage();
  67. payload.setName("latte");
  68. payload.setShots(3);
  69. payload.setIced(false);
  70. Message<?> originalMessage = MessageBuilder.withPayload(payload).build();
  71. Message<?> claimCheckMessage = checkin.transform(originalMessage);
  72. assertThat(claimCheckMessage.getPayload()).isEqualTo(originalMessage.getHeaders().getId());
  73. Message<?> checkedOutMessage = checkout.transform(claimCheckMessage);
  74. assertThat(checkedOutMessage.getPayload()).isEqualTo(originalMessage.getPayload());
  75. assertThat(checkedOutMessage.getHeaders().getId()).isEqualTo(claimCheckMessage.getPayload());
  76. assertThat(checkedOutMessage).isEqualTo(originalMessage);
  77. }
  78. @Test
  79. @MongoDbAvailable
  80. public void stringPayloadConfigurable() {
  81. ConfigurableMongoDbMessageStore messageStore = new ConfigurableMongoDbMessageStore(MONGO_DATABASE_FACTORY);
  82. messageStore.setApplicationContext(this.testApplicationContext);
  83. messageStore.afterPropertiesSet();
  84. ClaimCheckInTransformer checkin = new ClaimCheckInTransformer(messageStore);
  85. ClaimCheckOutTransformer checkout = new ClaimCheckOutTransformer(messageStore);
  86. Message<?> originalMessage = MessageBuilder.withPayload("test1").build();
  87. Message<?> claimCheckMessage = checkin.transform(originalMessage);
  88. assertThat(claimCheckMessage.getPayload()).isEqualTo(originalMessage.getHeaders().getId());
  89. Message<?> checkedOutMessage = checkout.transform(claimCheckMessage);
  90. assertThat(checkedOutMessage.getHeaders().getId()).isEqualTo(claimCheckMessage.getPayload());
  91. assertThat(checkedOutMessage.getPayload()).isEqualTo(originalMessage.getPayload());
  92. assertThat(checkedOutMessage).isEqualTo(originalMessage);
  93. }
  94. @Test
  95. @MongoDbAvailable
  96. public void objectPayloadConfigurable() {
  97. ConfigurableMongoDbMessageStore messageStore = new ConfigurableMongoDbMessageStore(MONGO_DATABASE_FACTORY);
  98. messageStore.setApplicationContext(this.testApplicationContext);
  99. messageStore.afterPropertiesSet();
  100. ClaimCheckInTransformer checkin = new ClaimCheckInTransformer(messageStore);
  101. ClaimCheckOutTransformer checkout = new ClaimCheckOutTransformer(messageStore);
  102. Beverage payload = new Beverage();
  103. payload.setName("latte");
  104. payload.setShots(3);
  105. payload.setIced(false);
  106. Message<?> originalMessage = MessageBuilder.withPayload(payload).build();
  107. Message<?> claimCheckMessage = checkin.transform(originalMessage);
  108. assertThat(claimCheckMessage.getPayload()).isEqualTo(originalMessage.getHeaders().getId());
  109. Message<?> checkedOutMessage = checkout.transform(claimCheckMessage);
  110. assertThat(checkedOutMessage.getPayload()).isEqualTo(originalMessage.getPayload());
  111. assertThat(checkedOutMessage.getHeaders().getId()).isEqualTo(claimCheckMessage.getPayload());
  112. assertThat(checkedOutMessage).isEqualTo(originalMessage);
  113. }
  114. @SuppressWarnings("serial")
  115. static class Beverage implements Serializable {
  116. private String name;
  117. private int shots;
  118. private boolean iced;
  119. @SuppressWarnings("unused")
  120. public String getName() {
  121. return name;
  122. }
  123. public void setName(String name) {
  124. this.name = name;
  125. }
  126. @SuppressWarnings("unused")
  127. public int getShots() {
  128. return shots;
  129. }
  130. public void setShots(int shots) {
  131. this.shots = shots;
  132. }
  133. @SuppressWarnings("unused")
  134. public boolean isIced() {
  135. return iced;
  136. }
  137. public void setIced(boolean iced) {
  138. this.iced = iced;
  139. }
  140. @Override
  141. public int hashCode() {
  142. final int prime = 31;
  143. int result = 1;
  144. result = prime * result + (iced ? 1231 : 1237);
  145. result = prime * result + ((name == null) ? 0 : name.hashCode());
  146. result = prime * result + shots;
  147. return result;
  148. }
  149. @Override
  150. public boolean equals(Object obj) {
  151. if (this == obj) {
  152. return true;
  153. }
  154. if (obj == null) {
  155. return false;
  156. }
  157. if (getClass() != obj.getClass()) {
  158. return false;
  159. }
  160. Beverage other = (Beverage) obj;
  161. if (iced != other.iced) {
  162. return false;
  163. }
  164. if (name == null) {
  165. if (other.name != null) {
  166. return false;
  167. }
  168. }
  169. else if (!name.equals(other.name)) {
  170. return false;
  171. }
  172. return shots == other.shots;
  173. }
  174. }
  175. }