PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-dataset/src/main/java/org/apache/camel/component/dataset/DataSetTestEndpoint.java

https://github.com/gnodet/camel
Java | 161 lines | 97 code | 20 blank | 44 comment | 6 complexity | 24e3531f5d84c5193aac108b1240e172 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.dataset;
  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import org.apache.camel.Component;
  22. import org.apache.camel.Endpoint;
  23. import org.apache.camel.Exchange;
  24. import org.apache.camel.Processor;
  25. import org.apache.camel.WrappedFile;
  26. import org.apache.camel.component.mock.MockEndpoint;
  27. import org.apache.camel.spi.Metadata;
  28. import org.apache.camel.spi.UriEndpoint;
  29. import org.apache.camel.spi.UriParam;
  30. import org.apache.camel.spi.UriPath;
  31. import org.apache.camel.support.EndpointHelper;
  32. import org.apache.camel.support.ObjectHelper;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. /**
  36. * Extends the mock component by pulling messages from another endpoint on startup to set the expected message bodies.
  37. *
  38. * That is, you use the test endpoint in a route and messages arriving on it will be implicitly compared to some
  39. * expected messages extracted from some other location. So you can use, for example, an expected set of message bodies
  40. * as files. This will then set up a properly configured Mock endpoint, which is only valid if the received messages
  41. * match the number of expected messages and their message payloads are equal.
  42. */
  43. @UriEndpoint(firstVersion = "1.3.0", scheme = "dataset-test", title = "DataSet Test", syntax = "dataset-test:name",
  44. producerOnly = true, label = "core,testing", lenientProperties = true)
  45. public class DataSetTestEndpoint extends MockEndpoint {
  46. private static final Logger LOG = LoggerFactory.getLogger(DataSetTestEndpoint.class);
  47. private Endpoint expectedMessageEndpoint;
  48. @UriPath(description = "Name of endpoint to lookup in the registry to use for polling messages used for testing")
  49. @Metadata(required = true)
  50. private String name;
  51. @UriParam
  52. private boolean anyOrder;
  53. @UriParam(defaultValue = "2000", javaType = "java.time.Duration")
  54. private long timeout = 2000L;
  55. @UriParam
  56. private boolean split;
  57. @UriParam
  58. private String delimiter = "\\n|\\r";
  59. public DataSetTestEndpoint(String endpointUri, Component component) {
  60. super(endpointUri, component);
  61. }
  62. public void setExpectedMessageEndpoint(Endpoint expectedMessageEndpoint) {
  63. this.expectedMessageEndpoint = expectedMessageEndpoint;
  64. }
  65. @Override
  66. protected void doStart() throws Exception {
  67. LOG.debug("Consuming expected messages from: {}", expectedMessageEndpoint);
  68. final List<Object> expectedBodies = new ArrayList<>();
  69. EndpointHelper.pollEndpoint(expectedMessageEndpoint, new Processor() {
  70. public void process(Exchange exchange) throws Exception {
  71. // if file based we need to load the file into memory as the file may be deleted/moved afterwards
  72. Object body = getInBody(exchange);
  73. if (body instanceof WrappedFile) {
  74. body = exchange.getIn().getBody(String.class);
  75. }
  76. if (split) {
  77. // use new lines in both styles
  78. Iterator<?> it = ObjectHelper.createIterator(body, delimiter, false, true);
  79. while (it.hasNext()) {
  80. Object line = it.next();
  81. LOG.trace("Received message body {}", line);
  82. expectedBodies.add(line);
  83. }
  84. } else {
  85. expectedBodies.add(body);
  86. }
  87. }
  88. }, timeout);
  89. LOG.info("Received: {} expected message(s) from: {}", expectedBodies.size(), expectedMessageEndpoint);
  90. if (anyOrder) {
  91. expectedBodiesReceivedInAnyOrder(expectedBodies);
  92. } else {
  93. expectedBodiesReceived(expectedBodies);
  94. }
  95. }
  96. /**
  97. * This method allows us to convert or coerce the expected message body into some other type
  98. */
  99. protected Object getInBody(Exchange exchange) {
  100. return exchange.getIn().getBody();
  101. }
  102. public long getTimeout() {
  103. return timeout;
  104. }
  105. /**
  106. * The timeout to use when polling for message bodies from the URI
  107. */
  108. public void setTimeout(long timeout) {
  109. this.timeout = timeout;
  110. }
  111. public boolean isAnyOrder() {
  112. return anyOrder;
  113. }
  114. /**
  115. * Whether the expected messages should arrive in the same order or can be in any order.
  116. */
  117. public void setAnyOrder(boolean anyOrder) {
  118. this.anyOrder = anyOrder;
  119. }
  120. public boolean isSplit() {
  121. return split;
  122. }
  123. /**
  124. * If enabled the messages loaded from the test endpoint will be split using new line delimiters so each line is an
  125. * expected message. <br/>
  126. * For example to use a file endpoint to load a file where each line is an expected message.
  127. */
  128. public void setSplit(boolean split) {
  129. this.split = split;
  130. }
  131. public String getDelimiter() {
  132. return delimiter;
  133. }
  134. /**
  135. * The split delimiter to use when split is enabled. By default the delimiter is new line based. The delimiter can
  136. * be a regular expression.
  137. */
  138. public void setDelimiter(String delimiter) {
  139. this.delimiter = delimiter;
  140. }
  141. }