Add MockMessageHandler to the Testing Framework

Fix PayloadMatcherTests for generics

Address PR comments and other improvements

* Revert `rawtypes` mode for the `PayloadMatcher`
* Make `HeaderMatcher` as `rawtypes` as well
* Make `MockMessageHandler` expect `rawtypes` for `Matcher`s.
This way we can just support `Matcher`s like `notNullValue(Message.class)`
* Rename `expect()` to `assertNext()`
* Rename `andReply()` to `thenReply()`
* Track replies are supplied in the `MockMessageHandler`
* Distinguish simple `MH` from the `MP` types in the
`MockIntegrationContext#instead()` do not let to replace simple `MH`
with fully configured `MockMessageHandler` or any other `MP` implementation.
Fail replace if types mismatch; wrap `MockMessageHandler` to simple `MH`
if it doesn't have replies when we are going to replace simple `MH`
* Wrap `MockMessageHandler` to the `Mockito.spy()` in the
`MockIntegration#mockMessageHandler()` to allow to `verify()` interaction
in the test-case

Remove wrapping `MockMH` to raw `MH` when no reply supported.
If `MockMH` isn't supplied with replies ti's safe to use it as is - no harm to target endpoint
which supposed to be last one in the flow

Some polishing and JavaDocs

More JavaDocs

Add docs for the `MockMessageHandler` and fix some JavaDocs

Make the `MockMessageHandler` with an API like:
```
MockIntegration.mockMessageHandler()
             .handleNext(Consumer<Message<?>>)
             .handleNext(Consumer<Message<?>>)
             .handleNextAndReply(Function<Message<?>, Object>)
             .handleNext(Consumer<Message<?>>)
             .handleNextAndReply(Function<Message<?>, Object>)
             .handleNextAndReply(Function<Message<?>, Object>);
```

Doc Polishing
This commit is contained in:
Artem Bilan
2017-05-08 18:19:49 -04:00
committed by Gary Russell
parent ea6cf0f4ef
commit 079ccb84e2
13 changed files with 577 additions and 126 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,10 @@
package org.springframework.integration.test.matcher;
import static org.hamcrest.CoreMatchers.is;
import java.util.Date;
import java.util.Map;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
@@ -68,6 +67,7 @@ import org.springframework.messaging.MessageHeaders;
*
* @author Alex Peters
* @author Iwein Fuld
* @author Artem Bilan
*
*/
public class HeaderMatcher extends TypeSafeMatcher<Message<?>> {
@@ -75,27 +75,22 @@ public class HeaderMatcher extends TypeSafeMatcher<Message<?>> {
private final Matcher<?> matcher;
/**
* @param matcher
* @param matcher the target matcher to delegate
*/
HeaderMatcher(Matcher<?> matcher) {
private HeaderMatcher(Matcher<?> matcher) {
super();
this.matcher = matcher;
}
/**
* {@inheritDoc}
*/
@Override
public boolean matchesSafely(Message<?> item) {
return matcher.matches(item.getHeaders());
return this.matcher.matches(item.getHeaders());
}
/**
* {@inheritDoc}
*/
@Override
public void describeTo(Description description) {
description.appendText("a Message with Headers containing ").appendDescriptionOf(matcher);
description.appendText("a Message with Headers containing ")
.appendDescriptionOf(this.matcher);
}
@Factory
@@ -104,12 +99,12 @@ public class HeaderMatcher extends TypeSafeMatcher<Message<?>> {
}
@Factory
public static <T> Matcher<Message<?>> hasHeader(String key, Matcher<?> valueMatcher) {
public static <T> Matcher<Message<?>> hasHeader(String key, Matcher<T> valueMatcher) {
return new HeaderMatcher(MapContentMatchers.hasEntry(key, valueMatcher));
}
@Factory
public static <T> Matcher<Message<?>> hasHeaderKey(String key) {
public static Matcher<Message<?>> hasHeaderKey(String key) {
return new HeaderMatcher(MapContentMatchers.hasKey(key));
}
@@ -130,7 +125,7 @@ public class HeaderMatcher extends TypeSafeMatcher<Message<?>> {
@Factory
public static Matcher<Message<?>> hasSequenceNumber(Integer value) {
return hasSequenceNumber(is(value));
return hasSequenceNumber(CoreMatchers.is(value));
}
@Factory
@@ -140,7 +135,7 @@ public class HeaderMatcher extends TypeSafeMatcher<Message<?>> {
@Factory
public static Matcher<Message<?>> hasSequenceSize(Integer value) {
return hasSequenceSize(is(value));
return hasSequenceSize(CoreMatchers.is(value));
}
@Factory
@@ -150,7 +145,7 @@ public class HeaderMatcher extends TypeSafeMatcher<Message<?>> {
@Factory
public static Matcher<Message<?>> hasExpirationDate(Date value) {
return hasExpirationDate(is(value.getTime()));
return hasExpirationDate(CoreMatchers.is(value.getTime()));
}
@Factory
@@ -160,7 +155,7 @@ public class HeaderMatcher extends TypeSafeMatcher<Message<?>> {
@Factory
public static Matcher<Message<?>> hasTimestamp(Date value) {
return hasTimestamp(is(value.getTime()));
return hasTimestamp(CoreMatchers.is(value.getTime()));
}
@Factory

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
package org.springframework.integration.test.matcher;
import static org.hamcrest.CoreMatchers.anything;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -25,8 +23,8 @@ import java.util.Map;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.IsEqual;
/**
* Matchers that examine the contents of a {@link Map}.
@@ -62,6 +60,7 @@ import org.hamcrest.core.IsEqual;
* @author Alex Peters
* @author Iwein Fuld
* @author Gunnar Hillert
* @author Artem Bilan
*
*/
public class MapContentMatchers<T, V> extends
@@ -71,67 +70,48 @@ public class MapContentMatchers<T, V> extends
private final Matcher<V> valueMatcher;
/**
* @param key
* @param value
*/
MapContentMatchers(T key, V value) {
this(key, IsEqual.equalTo(value));
private MapContentMatchers(T key, V value) {
this(key, Matchers.equalTo(value));
}
/**
* @param key
* @param valueMatcher
*/
MapContentMatchers(T key, Matcher<V> valueMatcher) {
super();
private MapContentMatchers(T key, Matcher<V> valueMatcher) {
this.key = key;
this.valueMatcher = valueMatcher;
}
/**
* {@inheritDoc}
*/
@Override
public boolean matchesSafely(Map<? super T, ? super V> item) {
return item.containsKey(key) && valueMatcher.matches(item.get(key));
}
/**
* {@inheritDoc}
*/
@Override
public void describeTo(Description description) {
description.appendText("an entry with key ").appendValue(key)
.appendText(" and value matching ").appendDescriptionOf(
valueMatcher);
valueMatcher);
}
@Factory
public static <T, V> Matcher<Map<? super T, ? super V>> hasEntry(T key,
V value) {
return new MapContentMatchers<T, V>(key, value);
public static <T, V> Matcher<Map<? super T, ? super V>> hasEntry(T key, V value) {
return new MapContentMatchers<>(key, value);
}
@Factory
public static <T, V> Matcher<Map<? super T, ? super V>> hasEntry(T key,
Matcher<V> valueMatcher) {
return new MapContentMatchers<T, V>(key, valueMatcher);
public static <T, V> Matcher<Map<? super T, ? super V>> hasEntry(T key, Matcher<V> valueMatcher) {
return new MapContentMatchers<>(key, valueMatcher);
}
@Factory
@SuppressWarnings("unchecked")
public static <T, V> Matcher<Map<? super T, ? super V>> hasKey(T key) {
return new MapContentMatchers<T, V>(key, (Matcher<V>) anything("any Value"));
return new MapContentMatchers<>(key, (Matcher<V>) Matchers.anything());
}
@Factory
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T, V> Matcher<Map<? super T, ? super V>> hasAllEntries(
Map<T, V> entries) {
List<Matcher<? extends Map<? super T, ? super V>>> matchers = new ArrayList<Matcher<? extends Map<? super T, ? super V>>>(
entries.size());
public static <T, V> Matcher<Map<? super T, ? super V>> hasAllEntries(Map<T, V> entries) {
List<Matcher<Map<? super T, ? super V>>> matchers = new ArrayList<>(entries.size());
for (Map.Entry<T, V> entry : entries.entrySet()) {
final V value = entry.getValue();
if (value instanceof Matcher<?>) {
@@ -144,4 +124,5 @@ public class MapContentMatchers<T, V> extends
//return AllOf.allOf(matchers); //Does not work with Hamcrest 1.3
return new AllOf(matchers);
}
}

View File

@@ -16,14 +16,11 @@
package org.springframework.integration.test.matcher;
import static org.mockito.ArgumentMatchers.argThat;
import static org.springframework.integration.test.matcher.HeaderMatcher.hasHeader;
import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload;
import java.util.Map;
import org.hamcrest.Matcher;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.mockito.internal.hamcrest.HamcrestArgumentMatcher;
@@ -75,30 +72,28 @@ public class MockitoMessageMatchers {
super();
}
@SuppressWarnings("unchecked")
public static <T> Message<T> messageWithPayload(Matcher<T> payloadMatcher) {
return argThat(new HamcrestArgumentMatcher<>(hasPayload(payloadMatcher)));
public static <T> Message<?> messageWithPayload(Matcher<? super T> payloadMatcher) {
return ArgumentMatchers.argThat(new HamcrestArgumentMatcher<>(PayloadMatcher.hasPayload(payloadMatcher)));
}
@SuppressWarnings("unchecked")
public static <T> Message<T> messageWithPayload(T payload) {
return argThat(new HamcrestArgumentMatcher<>(hasPayload(payload)));
public static <T> Message<?> messageWithPayload(T payload) {
return ArgumentMatchers.argThat(new HamcrestArgumentMatcher<>(PayloadMatcher.hasPayload(payload)));
}
public static Message<?> messageWithHeaderEntry(String key, Object value) {
return argThat(new HamcrestArgumentMatcher<>(hasHeader(key, value)));
return ArgumentMatchers.argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.hasHeader(key, value)));
}
public static Message<?> messageWithHeaderKey(String key) {
return argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.hasHeaderKey(key)));
return ArgumentMatchers.argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.hasHeaderKey(key)));
}
public static <T> Message<?> messageWithHeaderEntry(String key, Matcher<T> valueMatcher) {
return argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.<T>hasHeader(key, valueMatcher)));
return ArgumentMatchers.argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.<T>hasHeader(key, valueMatcher)));
}
public static Message<?> messageWithHeaderEntries(Map<String, ?> entries) {
return argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.hasAllHeaders(entries)));
return ArgumentMatchers.argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.hasAllHeaders(entries)));
}
}

View File

@@ -47,6 +47,7 @@ import org.springframework.messaging.MessageHeaders;
* </pre>
*
* @author Dave Syer
* @author Artem Bilan
*
*/
public class PayloadAndHeaderMatcher extends BaseMatcher<Message<?>> {
@@ -65,28 +66,32 @@ public class PayloadAndHeaderMatcher extends BaseMatcher<Message<?>> {
private PayloadAndHeaderMatcher(Message<?> expected, String... ignoreKeys) {
this.ignoreKeys = ignoreKeys;
this.payload = expected.getPayload();
this.headers = getHeaders(expected);
this.headers = extractHeadersToAssert(expected);
}
private Map<String, Object> getHeaders(Message<?> operand) {
HashMap<String, Object> headers = new HashMap<String, Object>(operand.getHeaders());
private Map<String, Object> extractHeadersToAssert(Message<?> operand) {
HashMap<String, Object> headers = new HashMap<>(operand.getHeaders());
headers.remove(MessageHeaders.ID);
headers.remove(MessageHeaders.TIMESTAMP);
for (String key : ignoreKeys) {
headers.remove(key);
if (this.ignoreKeys != null) {
for (String key : this.ignoreKeys) {
headers.remove(key);
}
}
return headers;
}
public boolean matches(Object arg) {
Message<?> input = (Message<?>) arg;
Map<String, Object> inputHeaders = getHeaders(input);
return input.getPayload().equals(payload) && inputHeaders.equals(headers);
Map<String, Object> inputHeaders = extractHeadersToAssert(input);
return input.getPayload().equals(this.payload) && inputHeaders.equals(this.headers);
}
public void describeTo(Description description) {
description.appendText("a Message with Headers that match except ID and timestamp for payload: ").appendValue(
payload).appendText(" and headers: ").appendValue(headers);
description.appendText("a Message with Headers that match except ID and timestamp for payload: ")
.appendValue(this.payload)
.appendText(" and headers: ")
.appendValue(this.headers);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,44 +56,38 @@ import org.springframework.messaging.Message;
* @author Iwein Fuld
*
*/
@SuppressWarnings("rawtypes")
public class PayloadMatcher extends TypeSafeMatcher<Message> {
public class PayloadMatcher extends TypeSafeMatcher<Message<?>> {
private final Matcher matcher;
private final Matcher<?> matcher;
/**
* Create a PayloadMatcher that matches the payload of messages against the given matcher
*/
PayloadMatcher(Matcher matcher) {
private PayloadMatcher(Matcher<?> matcher) {
super();
this.matcher = matcher;
}
/**
* {@inheritDoc}
*/
@Override
public boolean matchesSafely(Message message) {
return matcher.matches(message.getPayload());
public boolean matchesSafely(Message<?> message) {
return this.matcher.matches(message.getPayload());
}
/**
* {@inheritDoc}
*/
//@Override
@Override
public void describeTo(Description description) {
description.appendText("a Message with payload: ").appendDescriptionOf(matcher);
description.appendText("a Message with payload: ")
.appendDescriptionOf(this.matcher);
}
@Factory
public static <T> Matcher<Message> hasPayload(T payload) {
public static <T> Matcher<Message<?>> hasPayload(T payload) {
return new PayloadMatcher(IsEqual.equalTo(payload));
}
@Factory
public static <T> Matcher<Message> hasPayload(Matcher<? super T> payloadMatcher) {
public static <T> Matcher<Message<?>> hasPayload(Matcher<? super T> payloadMatcher) {
return new PayloadMatcher(payloadMatcher);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,9 @@
package org.springframework.integration.test.support;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,8 +41,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* <li>A payload or message to send as a request message on the inputChannel</li>
* <li>A handler to validate the response received on the outputChannel</li>
* </ul>
*
* @author David Turanski
* @author Gary Russell
* @author Artem Bilan
*/
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class AbstractRequestResponseScenarioTests {
@@ -78,16 +78,16 @@ public abstract class AbstractRequestResponseScenarioTests {
((SubscribableChannel) outputChannel).subscribe(scenario.getResponseValidator());
}
assertTrue(name + ": message not sent on " + scenario.getInputChannelName(),
Assert.assertTrue(name + ": message not sent on " + scenario.getInputChannelName(),
inputChannel.send(scenario.getMessage()));
if (outputChannel instanceof PollableChannel) {
Message<?> response = ((PollableChannel) outputChannel).receive(10000);
assertNotNull(name + ": receive timeout on " + scenario.getOutputChannelName(), response);
Assert.assertNotNull(name + ": receive timeout on " + scenario.getOutputChannelName(), response);
scenario.getResponseValidator().handleMessage(response);
}
assertNotNull("message was not handled on " + outputChannel + " for scenario '" + name + "'.",
Assert.assertNotNull("message was not handled on " + outputChannel + " for scenario '" + name + "'.",
scenario.getResponseValidator().getLastMessage());
if (outputChannel instanceof SubscribableChannel) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,51 +35,43 @@ import org.springframework.messaging.support.MessageBuilder;
* @author Alex Peters
* @author Iwein Fuld
* @author Gunnar Hillert
* @author Artem Bilan
*/
public class PayloadMatcherTests {
static final BigDecimal ANY_PAYLOAD = new BigDecimal("1.123");
private static final BigDecimal ANY_PAYLOAD = new BigDecimal("1.123");
Message<BigDecimal> message = MessageBuilder.withPayload(ANY_PAYLOAD).build();
private final Message<BigDecimal> message = MessageBuilder.withPayload(ANY_PAYLOAD).build();
@Test
public void hasPayload_withEqualValue_matches() throws Exception {
assertThat(message, hasPayload(new BigDecimal("1.123")));
assertThat(this.message, hasPayload(new BigDecimal("1.123")));
}
@Test
public void hasPayload_withNotEqualValue_notMatching() throws Exception {
assertThat(message, not(hasPayload(new BigDecimal("456"))));
assertThat(this.message, not(hasPayload(new BigDecimal("456"))));
}
@Test
public void hasPayload_withMatcher_matches() throws Exception {
assertThat(message,
hasPayload(is(instanceOf(BigDecimal.class))));
assertThat(message, hasPayload(notNullValue()));
assertThat(this.message, hasPayload(is(instanceOf(BigDecimal.class))));
assertThat(this.message, hasPayload(notNullValue()));
}
@Test
public void hasPayload_withNotMatchingMatcher_notMatching()
throws Exception {
assertThat(message, not((hasPayload(is(instanceOf(String.class))))));
public void hasPayload_withNotMatchingMatcher_notMatching() throws Exception {
assertThat(this.message, not((hasPayload(is(instanceOf(String.class))))));
}
@Test
public void readableException() throws Exception {
try {
assertThat(message, hasPayload("woot"));
assertThat(this.message, hasPayload("woot"));
}
catch (AssertionError ae) {
assertTrue(ae.getMessage().contains("Expected: a Message with payload: "));
}
}
@SuppressWarnings("rawtypes")
@Test
public void shouldMatchNonParametrizedMessage() throws Exception {
Message message = this.message;
assertThat(message, hasPayload(new BigDecimal("1.123")));
}
}