Messaging polyglot support (#1472)

added support for AMQP, KAFKA and standalone options
This commit is contained in:
Marcin Grzejszczak
2020-08-21 18:05:32 +02:00
committed by GitHub
parent c5d3456d3a
commit a915cf102b
74 changed files with 3399 additions and 791 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.contract.verifier.wiremock;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -35,17 +34,14 @@ import org.springframework.cloud.contract.spec.Contract;
class DefaultWireMockStubPostProcessor implements WireMockStubPostProcessor {
private static final List<Class> APPLICABLE_CLASSES = Arrays.asList(String.class,
StubMapping.class, Map.class);
public static final String WIREMOCK_METADATA_ENTRY = "wiremock";
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public StubMapping postProcess(StubMapping stubMapping, Contract contract) {
Object wiremock = getWiremockEntry(contract);
StubMapping stubMappingFromMetadata = stubMappingFromMetadata(wiremock);
WireMockMetaData wireMockMetaData = WireMockMetaData
.fromMetadata(contract.getMetadata());
StubMapping stubMappingFromMetadata = stubMappingFromMetadata(
wireMockMetaData.getStubMapping());
stubMapping.setResponse(mergedResponse(stubMapping, stubMappingFromMetadata));
if (stubMappingFromMetadata.getPostServeActions() != null) {
setPostServeActions(stubMapping, stubMappingFromMetadata);
@@ -149,10 +145,6 @@ class DefaultWireMockStubPostProcessor implements WireMockStubPostProcessor {
: stubMapping.getResponse().getFixedDelayMilliseconds();
}
private Object getWiremockEntry(Contract contract) {
return contract.getMetadata().get(WIREMOCK_METADATA_ENTRY);
}
private StubMapping stubMappingFromMetadata(Object wiremock) {
if (wiremock instanceof String) {
return StubMapping.buildFrom((String) wiremock);
@@ -176,13 +168,15 @@ class DefaultWireMockStubPostProcessor implements WireMockStubPostProcessor {
@Override
public boolean isApplicable(Contract contract) {
boolean contains = contract.getMetadata().containsKey(WIREMOCK_METADATA_ENTRY);
boolean contains = contract.getMetadata()
.containsKey(WireMockMetaData.METADATA_KEY);
if (!contains) {
return false;
}
Object wiremock = getWiremockEntry(contract);
return APPLICABLE_CLASSES.stream()
.anyMatch(aClass -> aClass.isAssignableFrom(wiremock.getClass()));
Object stubMapping = WireMockMetaData.fromMetadata(contract.getMetadata())
.getStubMapping();
return WireMockMetaData.APPLICABLE_CLASSES.stream()
.anyMatch(aClass -> aClass.isAssignableFrom(stubMapping.getClass()));
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2013-2020 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
*
* https://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.verifier.wiremock;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.springframework.cloud.contract.verifier.util.MetadataUtil;
import org.springframework.cloud.contract.verifier.util.SpringCloudContractMetadata;
public class WireMockMetaData implements SpringCloudContractMetadata {
/**
* Key under which this metadata entry can be found in contract's metadata.
*/
public static final String METADATA_KEY = "wiremock";
/**
* Applicable classes for Stub Mapping.
*/
static final List<Class> APPLICABLE_CLASSES = Arrays.asList(String.class,
StubMapping.class, Map.class);
/**
* {@link StubMapping} represented by one of the classes in
* {@link #APPLICABLE_CLASSES}.
*/
private Object stubMapping;
public Object getStubMapping() {
return stubMapping;
}
public void setStubMapping(Object stubMapping) {
this.stubMapping = stubMapping;
}
public static WireMockMetaData fromMetadata(Map<String, Object> metadata) {
return MetadataUtil.fromMetadata(metadata, WireMockMetaData.METADATA_KEY,
new WireMockMetaData());
}
@Override
public String key() {
return METADATA_KEY;
}
@Override
public String description() {
return "Metadata for extending WireMock stubs.\n\nStubMapping can be "
+ "one of the following classes "
+ APPLICABLE_CLASSES.stream()
.map(aClass -> "`" + aClass.getSimpleName() + "`")
.collect(Collectors.toList())
+ ". Please check "
+ "the http://wiremock.org/docs/stubbing/ for more information about the StubMapping class properties.";
}
@Override
public List<Class> additionalClassesToLookAt() {
return Collections.singletonList(StubMapping.class);
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.contract.verifier.wiremock;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -66,7 +67,9 @@ class DefaultWireMockStubPostProcessorTests {
@Test
void should_not_be_applicable_for_invalid_metadata_entry() {
Contract contract = new Contract();
contract.getMetadata().put("wiremock", 5);
Map<String, Object> map = new HashMap<>();
map.put("stubMapping", 5);
contract.getMetadata().put("wiremock", map);
then(new DefaultWireMockStubPostProcessor().isApplicable(contract)).isFalse();
}
@@ -74,15 +77,17 @@ class DefaultWireMockStubPostProcessorTests {
@Test
void should_be_applicable_for_valid_metadata_entry() {
Contract contract = new Contract();
contract.getMetadata().put("wiremock", "foo");
Map<String, Object> map = new HashMap<>();
map.put("stubMapping", "foo");
contract.getMetadata().put("wiremock", map);
then(new DefaultWireMockStubPostProcessor().isApplicable(contract)).isTrue();
contract.getMetadata().put("wiremock", new StubMapping());
map.put("stubMapping", new StubMapping());
then(new DefaultWireMockStubPostProcessor().isApplicable(contract)).isTrue();
contract.getMetadata().put("wiremock", new HashMap<>());
map.put("stubMapping", new HashMap<>());
then(new DefaultWireMockStubPostProcessor().isApplicable(contract)).isTrue();
}
@@ -90,7 +95,9 @@ class DefaultWireMockStubPostProcessorTests {
@Test
void should_merge_stub_mappings_when_stub_mapping_is_string() {
Contract contract = new Contract();
contract.getMetadata().put("wiremock", POST_SERVE_ACTION);
Map<String, Object> map = new HashMap<>();
map.put("stubMapping", POST_SERVE_ACTION);
contract.getMetadata().put("wiremock", map);
StubMapping stubMapping = StubMapping.buildFrom(STUB_MAPPING);
StubMapping result = new DefaultWireMockStubPostProcessor()
@@ -102,7 +109,9 @@ class DefaultWireMockStubPostProcessorTests {
@Test
void should_merge_stub_mappings_when_stub_mapping_is_stub_mapping() {
Contract contract = new Contract();
contract.getMetadata().put("wiremock", StubMapping.buildFrom(POST_SERVE_ACTION));
Map<String, Object> map = new HashMap<>();
map.put("stubMapping", StubMapping.buildFrom(POST_SERVE_ACTION));
contract.getMetadata().put("wiremock", map);
StubMapping stubMapping = StubMapping.buildFrom(STUB_MAPPING);
StubMapping result = new DefaultWireMockStubPostProcessor()
@@ -115,8 +124,10 @@ class DefaultWireMockStubPostProcessorTests {
void should_merge_stub_mappings_when_stub_mapping_is_map()
throws JsonProcessingException {
Contract contract = new Contract();
contract.getMetadata().put("wiremock",
Map<String, Object> map = new HashMap<>();
map.put("stubMapping",
new ObjectMapper().readValue(POST_SERVE_ACTION, HashMap.class));
contract.getMetadata().put("wiremock", map);
StubMapping stubMapping = StubMapping.buildFrom(STUB_MAPPING);
StubMapping result = new DefaultWireMockStubPostProcessor()
@@ -128,7 +139,9 @@ class DefaultWireMockStubPostProcessorTests {
@Test
void should_merge_stub_mappings_when_stub_mapping_is_string_and_contains_response() {
Contract contract = new Contract();
contract.getMetadata().put("wiremock", RESPONSE_DELAY);
Map<String, Object> map = new HashMap<>();
map.put("stubMapping", RESPONSE_DELAY);
contract.getMetadata().put("wiremock", map);
StubMapping stubMapping = StubMapping.buildFrom(STUB_MAPPING);
StubMapping result = new DefaultWireMockStubPostProcessor()