From 7122c51a494a95ec8f6cdae3f5222747601384b4 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Fri, 12 Oct 2018 18:39:04 +0200 Subject: [PATCH] Fixes gh-691 . Add JUnit5 Extension. --- spring-cloud-contract-dependencies/pom.xml | 11 + spring-cloud-contract-stub-runner/pom.xml | 8 + .../ExceptionThrowingMessageVerifier.java | 52 ++++ .../junit/PortStubRunnerExtensionOptions.java | 26 ++ .../stubrunner/junit/StubRunnerExtension.java | 284 ++++++++++++++++++ .../junit/StubRunnerExtensionOptions.java | 134 +++++++++ .../stubrunner/junit/StubRunnerRule.java | 39 +-- .../junit/StubRunnerRuleOptions.java | 30 +- ...it5ExtensionCustomMessageVerifierTest.java | 96 ++++++ ...ubRunnerJUnit5ExtensionCustomPortTest.java | 85 ++++++ ...rJUnit5ExtensionExceptionThrowingTest.java | 63 ++++ .../junit/StubRunnerJUnit5ExtensionTest.java | 80 +++++ 12 files changed, 868 insertions(+), 40 deletions(-) create mode 100644 spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/ExceptionThrowingMessageVerifier.java create mode 100644 spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/PortStubRunnerExtensionOptions.java create mode 100644 spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerExtension.java create mode 100644 spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerExtensionOptions.java create mode 100644 spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionCustomMessageVerifierTest.java create mode 100644 spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionCustomPortTest.java create mode 100644 spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionExceptionThrowingTest.java create mode 100644 spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionTest.java diff --git a/spring-cloud-contract-dependencies/pom.xml b/spring-cloud-contract-dependencies/pom.xml index 2b67e2b8af..36436502a0 100644 --- a/spring-cloud-contract-dependencies/pom.xml +++ b/spring-cloud-contract-dependencies/pom.xml @@ -18,6 +18,7 @@ 0.4.13 1.0.2.v20150114 3.0.7 + 5.2.0 @@ -155,6 +156,16 @@ maven-settings-builder 3.2.1 + + org.junit.jupiter + junit-jupiter-engine + ${junit5-version} + + + org.junit.jupiter + junit-jupiter-api + ${junit5-version} + diff --git a/spring-cloud-contract-stub-runner/pom.xml b/spring-cloud-contract-stub-runner/pom.xml index bb4af4fa08..7f0f2e2aaa 100644 --- a/spring-cloud-contract-stub-runner/pom.xml +++ b/spring-cloud-contract-stub-runner/pom.xml @@ -153,6 +153,14 @@ spring-cloud-starter-consul-discovery true + + org.junit.jupiter + junit-jupiter-engine + + + org.junit.jupiter + junit-jupiter-api + diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/ExceptionThrowingMessageVerifier.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/ExceptionThrowingMessageVerifier.java new file mode 100644 index 0000000000..3724f595b1 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/ExceptionThrowingMessageVerifier.java @@ -0,0 +1,52 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.junit; + +import org.springframework.cloud.contract.verifier.messaging.MessageVerifier; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * @author Olga Maciaszek-Sharma + * @since 2.1.0 + */ +class ExceptionThrowingMessageVerifier implements MessageVerifier { + + private static final String EXCEPTION_MESSAGE = "Please provide a custom MessageVerifier to use this feature"; + + @Override + public void send(Object message, String destination) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE); + } + + @Override + public Object receive(String destination, long timeout, TimeUnit timeUnit) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE); + } + + @Override + public Object receive(String destination) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE); + } + + @Override + public void send(Object payload, Map headers, String destination) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE); + } + +} diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/PortStubRunnerExtensionOptions.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/PortStubRunnerExtensionOptions.java new file mode 100644 index 0000000000..aece0f3369 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/PortStubRunnerExtensionOptions.java @@ -0,0 +1,26 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.junit; + +/** + * @author Olga Maciaszek-Sharma + * @since 2.1.0 + */ +interface PortStubRunnerExtensionOptions { + + StubRunnerExtension withPort(Integer port); +} diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerExtension.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerExtension.java new file mode 100644 index 0000000000..8a580dc054 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerExtension.java @@ -0,0 +1,284 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.junit; + +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.contract.spec.Contract; +import org.springframework.cloud.contract.stubrunner.BatchStubRunner; +import org.springframework.cloud.contract.stubrunner.BatchStubRunnerFactory; +import org.springframework.cloud.contract.stubrunner.RunningStubs; +import org.springframework.cloud.contract.stubrunner.StubConfiguration; +import org.springframework.cloud.contract.stubrunner.StubFinder; +import org.springframework.cloud.contract.stubrunner.StubNotFoundException; +import org.springframework.cloud.contract.stubrunner.StubRunnerOptions; +import org.springframework.cloud.contract.stubrunner.StubRunnerOptionsBuilder; +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; +import org.springframework.cloud.contract.verifier.messaging.MessageVerifier; + +import java.io.IOException; +import java.net.URL; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * JUnit 5 extension that allows to download and run stubs. + * + * @author Olga Maciaszek-Sharma + * @since 2.1.0 + */ +public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback, StubFinder, StubRunnerExtensionOptions { + + private static final String DELIMITER = ":"; + private static final String LATEST_VERSION = "+"; + + private static org.slf4j.Logger LOG = LoggerFactory.getLogger(StubRunnerExtension.class); + + StubRunnerExtension delegate = this; + private BatchStubRunner stubFinder; + private StubRunnerOptionsBuilder stubRunnerOptionsBuilder = new StubRunnerOptionsBuilder( + StubRunnerOptions.fromSystemProps()); + private MessageVerifier verifier = new ExceptionThrowingMessageVerifier(); + + public StubRunnerExtension() { + } + + StubRunnerExtension(StubRunnerExtension delegate) { + this.delegate = delegate; + } + + @Override + public void beforeAll(ExtensionContext extensionContext) { + stubFinder(new BatchStubRunnerFactory(builder().build(), verifier()).buildBatchStubRunner()); + stubFinder().runStubs(); + } + + @Override + public void afterAll(ExtensionContext extensionContext) { + try { + stubFinder().close(); + } catch (IOException exception) { + LOG.warn(exception.getMessage(), exception); + } + } + + @Override + public URL findStubUrl(String groupId, String artifactId) throws StubNotFoundException { + return stubFinder().findStubUrl(groupId, artifactId); + } + + @Override + public URL findStubUrl(String ivyNotation) throws StubNotFoundException { + return stubFinder().findStubUrl(ivyNotation); + } + + @Override + public RunningStubs findAllRunningStubs() { + return stubFinder().findAllRunningStubs(); + } + + @Override + public Map> getContracts() { + return stubFinder().getContracts(); + } + + @Override + public boolean trigger(String ivyNotation, String labelName) { + boolean result = stubFinder().trigger(ivyNotation, labelName); + if (!result) { + throw new IllegalStateException("Failed to trigger a message with notation [" + + ivyNotation + "] and label [" + labelName + "]"); + } + return result; + } + + @Override + public boolean trigger(String labelName) { + boolean result = stubFinder().trigger(labelName); + if (!result) { + throw new IllegalStateException( + "Failed to trigger a message with label [" + labelName + "]"); + } + return result; + } + + @Override + public boolean trigger() { + boolean result = stubFinder().trigger(); + if (!result) { + throw new IllegalStateException("Failed to trigger a message"); + } + return result; + } + + @Override + public Map> labels() { + return stubFinder().labels(); + } + + @Override + public StubRunnerExtension messageVerifier(MessageVerifier messageVerifier) { + verifier(messageVerifier); + return delegate; + } + + @Override + public StubRunnerExtension options(StubRunnerOptions stubRunnerOptions) { + builder().withOptions(stubRunnerOptions); + return delegate; + } + + @Override + public StubRunnerExtension minPort(int minPort) { + builder().withMinPort(minPort); + return delegate; + } + + @Override + public StubRunnerExtension maxPort(int maxPort) { + builder().withMaxPort(maxPort); + return delegate; + } + + @Override + public StubRunnerExtension repoRoot(String repoRoot) { + builder().withStubRepositoryRoot(repoRoot); + return delegate; + } + + @Override + public StubRunnerExtension stubsMode(StubRunnerProperties.StubsMode stubsMode) { + builder().withStubsMode(stubsMode); + return delegate; + } + + @Override + public PortStubRunnerExtension downloadStub(String groupId, String artifactId, String version, String classifier) { + builder().withStubs(groupId + DELIMITER + artifactId + DELIMITER + version + + DELIMITER + classifier); + return new PortStubRunnerExtension(delegate); + } + + @Override + public PortStubRunnerExtension downloadLatestStub(String groupId, String artifactId, String classifier) { + builder().withStubs(groupId + DELIMITER + artifactId + DELIMITER + LATEST_VERSION + + DELIMITER + classifier); + return new PortStubRunnerExtension(delegate); + } + + @Override + public PortStubRunnerExtension downloadStub(String groupId, String artifactId, String version) { + builder().withStubs(groupId + DELIMITER + artifactId + DELIMITER + version); + return new PortStubRunnerExtension(delegate); + } + + @Override + public PortStubRunnerExtension downloadStub(String groupId, String artifactId) { + builder().withStubs(groupId + DELIMITER + artifactId); + return new PortStubRunnerExtension(delegate); + } + + @Override + public PortStubRunnerExtension downloadStub(String ivyNotation) { + builder().withStubs(ivyNotation); + return new PortStubRunnerExtension(delegate); + } + + @Override + public StubRunnerExtension downloadStubs(String... ivyNotations) { + builder().withStubs(Arrays.asList(ivyNotations)); + return new PortStubRunnerExtension(delegate); + } + + @Override + public StubRunnerExtension downloadStubs(List ivyNotations) { + builder().withStubs(ivyNotations); + return new PortStubRunnerExtension(delegate); + } + + @Override + public StubRunnerExtension withStubPerConsumer(boolean stubPerConsumer) { + builder().withStubPerConsumer(stubPerConsumer); + return new PortStubRunnerExtension(delegate); + } + + @Override + public StubRunnerExtension withConsumerName(String consumerName) { + builder().withConsumerName(consumerName); + return new PortStubRunnerExtension(delegate); + } + + @Override + public StubRunnerExtension withMappingsOutputFolder(String mappingsOutputFolder) { + builder().withMappingsOutputFolder(mappingsOutputFolder); + return new PortStubRunnerExtension(delegate); + } + + @Override + public StubRunnerExtension withDeleteStubsAfterTest(boolean deleteStubsAfterTest) { + builder().withDeleteStubsAfterTest(deleteStubsAfterTest); + return new PortStubRunnerExtension(delegate); + } + + @Override + public StubRunnerExtension withProperties(Map properties) { + builder().withProperties(properties); + return new PortStubRunnerExtension(delegate); + } + + BatchStubRunner stubFinder() { + return this.delegate.stubFinder; + } + + void stubFinder(BatchStubRunner stubFinder) { + this.delegate.stubFinder = stubFinder; + } + + StubRunnerOptionsBuilder builder() { + return delegate.stubRunnerOptionsBuilder; + } + + MessageVerifier verifier() { + return delegate.verifier; + } + + void verifier(MessageVerifier verifier) { + delegate.verifier = verifier; + } + + /** + * Helper class with additional port, related methods once you pick a stub to download + * + * @since 1.2.0 + */ + public static class PortStubRunnerExtension extends StubRunnerExtension implements PortStubRunnerExtensionOptions { + + PortStubRunnerExtension(StubRunnerExtension delegate) { + super(delegate); + } + + @Override + public StubRunnerExtension withPort(Integer port) { + builder().withPort(port); + return delegate; + } + } +} diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerExtensionOptions.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerExtensionOptions.java new file mode 100644 index 0000000000..a9ca98d922 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerExtensionOptions.java @@ -0,0 +1,134 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.junit; + +import org.springframework.cloud.contract.stubrunner.StubRunnerOptions; +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; +import org.springframework.cloud.contract.verifier.messaging.MessageVerifier; + +import java.util.List; +import java.util.Map; + +/** + * @author Olga Maciaszek-Sharma + * @since 2.1.0 + */ +interface StubRunnerExtensionOptions { + + /** + * Pass the {@link MessageVerifier} that this rule should use. If you don't pass + * anything a {@link ExceptionThrowingMessageVerifier} will be used. + * That means that an exception will be thrown whenever you try to do sth messaging + * related. + */ + StubRunnerExtension messageVerifier(MessageVerifier messageVerifier); + + /** + * Override all options + * + * @see StubRunnerOptions + */ + StubRunnerExtension options(StubRunnerOptions stubRunnerOptions); + + /** + * Min value of port for WireMock server + */ + StubRunnerExtension minPort(int minPort); + + /** + * Max value of port for WireMock server + */ + StubRunnerExtension maxPort(int maxPort); + + /** + * String URI of repository containing stubs + */ + StubRunnerExtension repoRoot(String repoRoot); + + /** + * Stubs mode that should be used + */ + StubRunnerExtension stubsMode(StubRunnerProperties.StubsMode stubsMode); + + /** + * Group Id, artifact Id, version and classifier of a single stub to download + */ + PortStubRunnerExtensionOptions downloadStub(String groupId, String artifactId, + String version, String classifier); + + /** + * Group Id, artifact Id and classifier of a single stub to download in the latest + * version + */ + PortStubRunnerExtensionOptions downloadLatestStub(String groupId, String artifactId, + String classifier); + + /** + * Group Id, artifact Id and version of a single stub to download + */ + PortStubRunnerExtensionOptions downloadStub(String groupId, String artifactId, + String version); + + /** + * Group Id, artifact Id of a single stub to download. Default classifier will be + * picked. + */ + PortStubRunnerExtensionOptions downloadStub(String groupId, String artifactId); + + /** + * Ivy notation of a single stub to download. + */ + PortStubRunnerExtensionOptions downloadStub(String ivyNotation); + + /** + * Stubs to download in Ivy notations + */ + StubRunnerExtension downloadStubs(String... ivyNotations); + + /** + * Stubs to download in Ivy notations + */ + StubRunnerExtension downloadStubs(List ivyNotations); + + /** + * Allows stub per consumer + */ + StubRunnerExtension withStubPerConsumer(boolean stubPerConsumer); + + /** + * Allows setting consumer name + */ + StubRunnerExtension withConsumerName(String consumerName); + + /** + * Allows setting the output folder for mappings + */ + StubRunnerExtension withMappingsOutputFolder(String mappingsOutputFolder); + + /** + * If set to {@code false} will NOT delete stubs from a temporary folder after running + * tests + */ + StubRunnerExtension withDeleteStubsAfterTest(boolean deleteStubsAfterTest); + + /** + * Map of properties that can be passed to custom + * {@link org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder} + */ + StubRunnerExtension withProperties(Map properties); + +} diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRule.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRule.java index 024590e02e..b5412e4507 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRule.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRule.java @@ -16,13 +16,6 @@ package org.springframework.cloud.contract.stubrunner.junit; -import java.net.URL; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; - import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -37,6 +30,12 @@ import org.springframework.cloud.contract.stubrunner.StubRunnerOptionsBuilder; import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; import org.springframework.cloud.contract.verifier.messaging.MessageVerifier; +import java.net.URL; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; + /** * JUnit class rule that allows you to download the provided stubs. * @@ -269,32 +268,6 @@ public class StubRunnerRule implements TestRule, StubFinder, StubRunnerRuleOptio return this.delegate.stubRunnerOptionsBuilder; } - static class ExceptionThrowingMessageVerifier implements MessageVerifier { - - private static final String EXCEPTION_MESSAGE = "Please provide a custom MessageVerifier to use this feature"; - - @Override - public void send(Object message, String destination) { - throw new UnsupportedOperationException(EXCEPTION_MESSAGE); - } - - @Override - public Object receive(String destination, long timeout, TimeUnit timeUnit) { - throw new UnsupportedOperationException(EXCEPTION_MESSAGE); - } - - @Override - public Object receive(String destination) { - throw new UnsupportedOperationException(EXCEPTION_MESSAGE); - } - - @Override - public void send(Object payload, Map headers, String destination) { - throw new UnsupportedOperationException(EXCEPTION_MESSAGE); - } - - } - /** * Helper class with additional port, related methods once you pick a stub to download * diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleOptions.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleOptions.java index f5441e0da9..3eeda394e6 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleOptions.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleOptions.java @@ -1,17 +1,33 @@ -package org.springframework.cloud.contract.stubrunner.junit; +/* + * Copyright 2013-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import java.util.List; -import java.util.Map; +package org.springframework.cloud.contract.stubrunner.junit; import org.springframework.cloud.contract.stubrunner.StubRunnerOptions; import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; import org.springframework.cloud.contract.verifier.messaging.MessageVerifier; +import java.util.List; +import java.util.Map; + interface StubRunnerRuleOptions { /** * Pass the {@link MessageVerifier} that this rule should use. If you don't pass - * anything a {@link StubRunnerRule.ExceptionThrowingMessageVerifier} will be used. + * anything a {@link ExceptionThrowingMessageVerifier} will be used. * That means that an exception will be thrown whenever you try to do sth messaging * related. */ @@ -48,20 +64,20 @@ interface StubRunnerRuleOptions { * Group Id, artifact Id, version and classifier of a single stub to download */ PortStubRunnerRuleOptions downloadStub(String groupId, String artifactId, - String version, String classifier); + String version, String classifier); /** * Group Id, artifact Id and classifier of a single stub to download in the latest * version */ PortStubRunnerRuleOptions downloadLatestStub(String groupId, String artifactId, - String classifier); + String classifier); /** * Group Id, artifact Id and version of a single stub to download */ PortStubRunnerRuleOptions downloadStub(String groupId, String artifactId, - String version); + String version); /** * Group Id, artifact Id of a single stub to download. Default classifier will be diff --git a/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionCustomMessageVerifierTest.java b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionCustomMessageVerifierTest.java new file mode 100644 index 0000000000..042cf0a574 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionCustomMessageVerifierTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.junit; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; +import org.springframework.cloud.contract.verifier.messaging.MessageVerifier; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * @author Olga Maciaszek-Sharma + * @since 2.1.0 + */ +class StubRunnerJUnit5ExtensionCustomMessageVerifierTest { + + @BeforeAll + @AfterAll + static void setupProps() { + System.clearProperty("stubrunner.repository.root"); + System.clearProperty("stubrunner.classifier"); + } + + // Visible for testing + @RegisterExtension + static StubRunnerExtension stubRunnerExtension = new StubRunnerExtension() + .stubsMode(StubRunnerProperties.StubsMode.REMOTE) + .repoRoot(repoRoot()) + .downloadStub("org.springframework.cloud.contract.verifier.stubs", "bootService") + .messageVerifier(new MyMessageVerifier()); + + @Test + void should_use_provided_message_verifier_in_junit5_extension() { + IllegalStateException emptyTriggerException = assertThrows(IllegalStateException.class, + () -> stubRunnerExtension.trigger()); + assertThat(emptyTriggerException.getMessage()).contains("Failed to send a message with headers"); + IllegalStateException wrongLabelException = assertThrows(IllegalStateException.class, + () -> stubRunnerExtension.trigger("return_book_1")); + assertThat(wrongLabelException.getMessage()).contains("Failed to send a message with headers"); + IllegalStateException wrongLabelWithIvyNotation = assertThrows(IllegalStateException.class, + () -> stubRunnerExtension.trigger("bootService", "return_book_1")); + assertThat(wrongLabelWithIvyNotation.getMessage()).contains("Failed to send a message with headers"); + } + + static class MyMessageVerifier implements MessageVerifier { + + @Override + public void send(Object message, String destination) { + throw new IllegalStateException("Failed to send a message"); + } + + @Override + public Object receive(String destination, long timeout, TimeUnit timeUnit) { + throw new IllegalStateException("Failed to receive a message with timeout"); + } + + @Override + public Object receive(String destination) { + throw new IllegalStateException("Failed to receive a message"); + } + + @Override + public void send(Object payload, Map headers, String destination) { + throw new IllegalStateException("Failed to send a message with headers"); + } + } + + private static String repoRoot() { + try { + return StubRunnerRuleCustomPortJUnitTest.class.getResource("/m2repo/repository/").toURI().toString(); + } catch (Exception e) { + return ""; + } + } +} \ No newline at end of file diff --git a/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionCustomPortTest.java b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionCustomPortTest.java new file mode 100644 index 0000000000..7378f0d875 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionCustomPortTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.junit; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; +import org.springframework.util.StreamUtils; + +import java.io.InputStream; +import java.net.URI; +import java.nio.charset.Charset; + +import static org.assertj.core.api.BDDAssertions.then; + +/** + * @author Olga Maciaszek-Sharma + * @since 2.1.0 + */ +class StubRunnerJUnit5ExtensionCustomPortTest { + + @BeforeAll + @AfterAll + static void setupProps() { + System.clearProperty("stubrunner.repository.root"); + System.clearProperty("stubrunner.classifier"); + } + + @RegisterExtension + static StubRunnerExtension stubRunnerExtension = new StubRunnerExtension() + .repoRoot(repoRoot()) + .stubsMode(StubRunnerProperties.StubsMode.REMOTE) + .downloadStub("org.springframework.cloud.contract.verifier.stubs", "loanIssuance") + .withPort(12345) + .downloadStub("org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer:12346"); + + @Test + void should_start_wiremock_servers() throws Exception { + then(stubRunnerExtension.findStubUrl("org.springframework.cloud.contract.verifier.stubs", "loanIssuance")).isNotNull(); + then(stubRunnerExtension.findStubUrl("loanIssuance")).isNotNull(); + then(stubRunnerExtension.findStubUrl("loanIssuance")) + .isEqualTo(stubRunnerExtension.findStubUrl("org.springframework.cloud.contract.verifier.stubs", "loanIssuance")); + then(stubRunnerExtension.findStubUrl("org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer")).isNotNull(); + then(stubRunnerExtension.findAllRunningStubs().isPresent("loanIssuance")).isTrue(); + then(stubRunnerExtension.findAllRunningStubs() + .isPresent("org.springframework.cloud.contract.verifier.stubs", "fraudDetectionServer")).isTrue(); + then(stubRunnerExtension.findAllRunningStubs() + .isPresent("org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer")).isTrue(); + then(httpGet(stubRunnerExtension.findStubUrl("loanIssuance").toString() + "/name")).isEqualTo("loanIssuance"); + then(httpGet(stubRunnerExtension.findStubUrl("fraudDetectionServer").toString() + "/name")).isEqualTo("fraudDetectionServer"); + then(stubRunnerExtension.findStubUrl("loanIssuance")).isEqualTo(URI.create("http://localhost:12345").toURL()); + then(stubRunnerExtension.findStubUrl("fraudDetectionServer")).isEqualTo(URI.create("http://localhost:12346").toURL()); + } + + private static String repoRoot() { + try { + return StubRunnerJUnit5ExtensionCustomPortTest.class.getResource("/m2repo/repository/") + .toURI().toString(); + } catch (Exception e) { + return ""; + } + } + + private String httpGet(String url) throws Exception { + try (InputStream stream = URI.create(url).toURL().openStream()) { + return StreamUtils.copyToString(stream, Charset.forName("UTF-8")); + } + } +} diff --git a/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionExceptionThrowingTest.java b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionExceptionThrowingTest.java new file mode 100644 index 0000000000..34d24a2807 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionExceptionThrowingTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.junit; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * @author Olga Maciaszek-Sharma + * @since 2.1.0 + */ +public class StubRunnerJUnit5ExtensionExceptionThrowingTest { + + @RegisterExtension + static StubRunnerExtension stubRunnerExtension = new StubRunnerExtension() + .stubsMode(StubRunnerProperties.StubsMode.REMOTE) + .repoRoot(repoRoot()) + .downloadStub("org.springframework.cloud.contract.verifier.stubs", "bootService"); + + @BeforeAll + @AfterAll + static void setupProps() { + System.clearProperty("stubrunner.repository.root"); + System.clearProperty("stubrunner.classifier"); + } + + @Test + void should_throw_exception_when_no_message_verifier_was_passed_and_message_related_method_was_triggered() { + UnsupportedOperationException emptyTriggerException = assertThrows(UnsupportedOperationException.class, + () -> stubRunnerExtension.trigger()); + UnsupportedOperationException wrongLabelException = assertThrows(UnsupportedOperationException.class, + () -> stubRunnerExtension.trigger("return_book_1")); + UnsupportedOperationException wrongLabelWithIvyNotation = assertThrows(UnsupportedOperationException.class, + () -> stubRunnerExtension.trigger("bootService", "return_book_1")); + } + + private static String repoRoot() { + try { + return StubRunnerRuleCustomPortJUnitTest.class.getResource("/m2repo/repository/").toURI().toString(); + } catch (Exception e) { + return ""; + } + } +} diff --git a/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionTest.java b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionTest.java new file mode 100644 index 0000000000..1cf47ac939 --- /dev/null +++ b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.stubrunner.junit; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; + +import java.io.File; +import java.net.URL; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @since 2.1.0 + * @author Olga Maciaszek-Sharma + */ + +class StubRunnerJUnit5ExtensionTest { + + @BeforeAll + @AfterAll + static void setupProps() { + System.clearProperty("stubrunner.repository.root"); + System.clearProperty("stubrunner.classifier"); + } + + // Visible for Junit + @RegisterExtension + static StubRunnerExtension stubRunnerExtension = new StubRunnerExtension() + .repoRoot(repoRoot()) + .stubsMode(StubRunnerProperties.StubsMode.REMOTE) + .downloadStub("org.springframework.cloud.contract.verifier.stubs", "loanIssuance") + .downloadStub("org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer") + .withMappingsOutputFolder("target/outputmappingsforrule"); + + @Test + void should_start_WireMock_servers() { + assertThat(stubRunnerExtension.findStubUrl("org.springframework.cloud.contract.verifier.stubs", + "loanIssuance")).isNotNull(); + assertThat(stubRunnerExtension.findStubUrl("loanIssuance")).isNotNull(); + assertThat(stubRunnerExtension.findStubUrl("loanIssuance")).isEqualTo(stubRunnerExtension + .findStubUrl("org.springframework.cloud.contract.verifier.stubs", "loanIssuance")); + assertThat(stubRunnerExtension + .findStubUrl("org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer")).isNotNull(); + } + + @Test + void should_output_mappings_to_output_folder() { + // when + URL url = stubRunnerExtension.findStubUrl("fraudDetectionServer"); + + //then + assertThat(new File("target/outputmappingsforrule", "fraudDetectionServer_" + url.getPort())).exists(); + } + + private static String repoRoot() { + try { + return StubRunnerRuleJUnitTest.class.getResource("/m2repo/repository/").toURI().toString(); + } catch (Exception e) { + return ""; + } + } +}