Skip to content

Commit 6534599

Browse files
committed
Add support for new smithy translated protocol test format
1 parent d77cb8b commit 6534599

File tree

6 files changed

+158
-23
lines changed

6 files changed

+158
-23
lines changed

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/asserts/marshalling/HeadersAssertion.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class HeadersAssertion extends MarshallingAssertion {
3232
private Map<String, List<String>> contains;
3333

3434
private List<String> doesNotContain;
35+
private List<String> mustContain;
3536

3637
public void setContains(Map<String, List<String>> contains) {
3738
this.contains = contains;
@@ -41,6 +42,10 @@ public void setDoesNotContain(List<String> doesNotContain) {
4142
this.doesNotContain = doesNotContain;
4243
}
4344

45+
public void setMustContain(List<String> mustContain) {
46+
this.mustContain = mustContain;
47+
}
48+
4449
@Override
4550
protected void doAssert(LoggedRequest actual) throws Exception {
4651
if (contains != null) {
@@ -49,6 +54,9 @@ protected void doAssert(LoggedRequest actual) throws Exception {
4954
if (doesNotContain != null) {
5055
assertDoesNotContainHeaders(actual.getHeaders());
5156
}
57+
if (mustContain != null) {
58+
assertMustContainHeaders(actual.getHeaders());
59+
}
5260
}
5361

5462
private void assertHeadersContains(HttpHeaders actual) {
@@ -66,4 +74,11 @@ private void assertDoesNotContainHeaders(HttpHeaders actual) {
6674
actual.getHeader(headerName).isPresent());
6775
});
6876
}
77+
78+
private void assertMustContainHeaders(HttpHeaders actual) {
79+
doesNotContain.forEach(headerName -> {
80+
assertTrue(String.format("Header '%s' was expected to be present", headerName),
81+
actual.getHeader(headerName).isPresent());
82+
});
83+
}
6984
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.protocol.asserts.marshalling;
17+
18+
import static org.hamcrest.Matchers.containsInAnyOrder;
19+
import static org.hamcrest.Matchers.equalTo;
20+
import static org.junit.Assert.assertThat;
21+
import static software.amazon.awssdk.protocol.asserts.marshalling.QueryUtils.parseQueryParams;
22+
import static software.amazon.awssdk.protocol.asserts.marshalling.QueryUtils.parseQueryParamsFromBody;
23+
24+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
public class QueryBodyAssertion extends MarshallingAssertion {
29+
private final String queryEquals;
30+
31+
public QueryBodyAssertion(String queryEquals) {
32+
this.queryEquals = queryEquals;
33+
}
34+
35+
@Override
36+
protected void doAssert(LoggedRequest actual) throws Exception {
37+
Map<String, List<String>> expectedParams = parseQueryParamsFromBody(queryEquals);
38+
try {
39+
Map<String, List<String>> actualParams = parseQueryParams(actual);
40+
doAssert(expectedParams, actualParams);
41+
} catch (AssertionError error) {
42+
// We may send the query params in the body if there is no other content. Try
43+
// decoding body as params and rerun the assertions.
44+
Map<String, List<String>> actualParams = parseQueryParamsFromBody(
45+
actual.getBodyAsString());
46+
doAssert(expectedParams, actualParams);
47+
}
48+
}
49+
50+
private void doAssert(Map<String, List<String>> expectedParams, Map<String, List<String>> actualParams) {
51+
assertThat(actualParams.keySet(), equalTo(expectedParams.keySet()));
52+
expectedParams.forEach((key, value) -> assertThat(
53+
actualParams.get(key), containsInAnyOrder(value.toArray())
54+
));
55+
}
56+
}

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/asserts/marshalling/QueryParamsAssertion.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.hamcrest.Matchers.not;
2121
import static org.hamcrest.collection.IsMapContaining.hasKey;
2222
import static org.junit.Assert.assertThat;
23+
import static software.amazon.awssdk.protocol.asserts.marshalling.QueryUtils.parseQueryParams;
24+
import static software.amazon.awssdk.protocol.asserts.marshalling.QueryUtils.parseQueryParamsFromBody;
2325

2426
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
2527
import java.net.URI;
@@ -40,6 +42,7 @@ public class QueryParamsAssertion extends MarshallingAssertion {
4042
private Map<String, List<String>> contains;
4143
private Map<String, List<String>> containsOnly;
4244
private List<String> doesNotContain;
45+
private List<String> mustContain;
4346

4447
public void setContains(Map<String, List<String>> contains) {
4548
this.contains = contains;
@@ -53,6 +56,10 @@ public void setDoesNotContain(List<String> doesNotContain) {
5356
this.doesNotContain = doesNotContain;
5457
}
5558

59+
public void setMustContain(List<String> mustContain) {
60+
this.mustContain = mustContain;
61+
}
62+
5663
@Override
5764
protected void doAssert(LoggedRequest actual) throws Exception {
5865
try {
@@ -79,32 +86,13 @@ private void doAssert(Map<String, List<String>> actualParams) {
7986
if (containsOnly != null) {
8087
assertContainsOnly(actualParams);
8188
}
82-
}
83-
84-
private Map<String, List<String>> parseQueryParamsFromBody(String body) {
85-
return toQueryParamMap(URLEncodedUtils.parse(body, StandardCharsets.UTF_8));
86-
}
87-
88-
private Map<String, List<String>> parseQueryParams(LoggedRequest actual) {
89-
return toQueryParamMap(parseNameValuePairsFromQuery(actual));
90-
}
91-
92-
/**
93-
* Group the list of {@link NameValuePair} by parameter name.
94-
*/
95-
private Map<String, List<String>> toQueryParamMap(List<NameValuePair> queryParams) {
96-
return queryParams.stream().collect(Collectors.groupingBy(NameValuePair::getName, Collectors
97-
.mapping(NameValuePair::getValue, Collectors.toList())));
98-
}
9989

100-
private List<NameValuePair> parseNameValuePairsFromQuery(LoggedRequest actual) {
101-
String queryParams = URI.create(actual.getUrl()).getQuery();
102-
if (StringUtils.isEmpty(queryParams)) {
103-
return Collections.emptyList();
90+
if (mustContain != null) {
91+
assertMustContain(actualParams);
10492
}
105-
return URLEncodedUtils.parse(queryParams, StandardCharsets.UTF_8);
10693
}
10794

95+
10896
private void assertContains(Map<String, List<String>> actualParams) {
10997
contains.entrySet().forEach(e -> assertThat(actualParams.get(e.getKey()), containsInAnyOrder(e.getValue().toArray())));
11098
}
@@ -113,6 +101,10 @@ private void assertDoesNotContain(Map<String, List<String>> actualParams) {
113101
doesNotContain.forEach(key -> assertThat(actualParams, not(hasKey(key))));
114102
}
115103

104+
private void assertMustContain(Map<String, List<String>> actualParams) {
105+
doesNotContain.forEach(key -> assertThat(actualParams, hasKey(key)));
106+
}
107+
116108
private void assertContainsOnly(Map<String, List<String>> actualParams) {
117109
assertThat(actualParams.keySet(), equalTo(containsOnly.keySet()));
118110
containsOnly.entrySet().forEach(e -> assertThat(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.protocol.asserts.marshalling;
17+
18+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
19+
import java.net.URI;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.stream.Collectors;
25+
import org.apache.http.NameValuePair;
26+
import org.apache.http.client.utils.URLEncodedUtils;
27+
import software.amazon.awssdk.utils.StringUtils;
28+
29+
public final class QueryUtils {
30+
private QueryUtils() {}
31+
32+
public static Map<String, List<String>> parseQueryParamsFromBody(String body) {
33+
return toQueryParamMap(URLEncodedUtils.parse(body, StandardCharsets.UTF_8));
34+
}
35+
36+
public static Map<String, List<String>> parseQueryParams(LoggedRequest actual) {
37+
return toQueryParamMap(parseNameValuePairsFromQuery(actual));
38+
}
39+
40+
/**
41+
* Group the list of {@link NameValuePair} by parameter name.
42+
*/
43+
private static Map<String, List<String>> toQueryParamMap(List<NameValuePair> queryParams) {
44+
return queryParams.stream().collect(Collectors.groupingBy(NameValuePair::getName, Collectors
45+
.mapping(NameValuePair::getValue, Collectors.toList())));
46+
}
47+
48+
private static List<NameValuePair> parseNameValuePairsFromQuery(LoggedRequest actual) {
49+
String queryParams = URI.create(actual.getUrl()).getQuery();
50+
if (StringUtils.isEmpty(queryParams)) {
51+
return Collections.emptyList();
52+
}
53+
return URLEncodedUtils.parse(queryParams, StandardCharsets.UTF_8);
54+
}
55+
}

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/asserts/marshalling/RequestBodyAssertion.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public void setXmlEquals(String xmlEquals) {
3333
addAssertion(new XmlBodyAssertion(xmlEquals));
3434
}
3535

36+
public void setQueryEquals(String queryEquals) {
37+
addAssertion(new QueryBodyAssertion(queryEquals));
38+
}
39+
3640
public void setEquals(String equals) {
3741
addAssertion(new RawBodyAssertion(equals));
3842
}

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/model/TestCase.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,21 @@
1717

1818
public class TestCase {
1919

20+
private String id;
2021
private String description;
2122
// Given is optional
2223
private Given given = new Given();
2324
private When when;
2425
private Then then;
2526

27+
public String getId() {
28+
return id;
29+
}
30+
31+
public void setId(String id) {
32+
this.id = id;
33+
}
34+
2635
public String getDescription() {
2736
return description;
2837
}
@@ -57,7 +66,11 @@ public void setThen(Then then) {
5766

5867
@Override
5968
public String toString() {
60-
return description;
69+
if (id != null) {
70+
return id + ": " + description;
71+
} else {
72+
return description;
73+
}
6174
}
6275

6376
}

0 commit comments

Comments
 (0)