Skip to content

Commit cbe16ea

Browse files
test(deps): update dependency com.google.cloud:google-cloud-conformance-tests to v0.3.5 (#1956)
* test(deps): update dependency com.google.cloud:google-cloud-conformance-tests to v0.3.5 * test: update assertion of V4SigningTest to consider query string parameters order independent --------- Co-authored-by: BenWhitehead <[email protected]>
1 parent af90f16 commit cbe16ea

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/V4SigningTest.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
package com.google.cloud.storage;
1818

19-
import static org.junit.Assert.assertEquals;
19+
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertNotNull;
2121

2222
import com.google.api.core.ApiClock;
2323
import com.google.auth.oauth2.ServiceAccountCredentials;
24+
import com.google.cloud.Tuple;
2425
import com.google.cloud.conformance.storage.v1.SigningV4Test;
2526
import com.google.cloud.conformance.storage.v1.TestFile;
2627
import com.google.cloud.conformance.storage.v1.UrlStyle;
@@ -32,11 +33,16 @@
3233
import java.io.IOException;
3334
import java.io.InputStream;
3435
import java.io.InputStreamReader;
36+
import java.net.URI;
3537
import java.util.ArrayList;
38+
import java.util.Arrays;
3639
import java.util.Collection;
3740
import java.util.List;
41+
import java.util.Map;
42+
import java.util.Objects;
3843
import java.util.concurrent.TimeUnit;
3944
import java.util.concurrent.atomic.AtomicLong;
45+
import java.util.stream.Collectors;
4046
import org.junit.Rule;
4147
import org.junit.Test;
4248
import org.junit.rules.TestName;
@@ -128,7 +134,9 @@ public void test() {
128134
SignUrlOption.withQueryParams(testData.getQueryParametersMap()),
129135
style)
130136
.toString();
131-
assertEquals(testData.getExpectedUrl(), signedUrl);
137+
SmarterUrl expected = SmarterUrl.of(URI.create(testData.getExpectedUrl()));
138+
SmarterUrl actual = SmarterUrl.of(URI.create(signedUrl));
139+
assertThat(actual).isEqualTo(expected);
132140
}
133141

134142
/**
@@ -166,4 +174,56 @@ public static Collection<Object[]> testCases() throws IOException {
166174
}
167175
return data;
168176
}
177+
178+
/**
179+
* Equals on {@link URI} or {@link java.net.URL} perform string comparison on the full query
180+
* string. However, query strings are not order dependent. This class essentially provides a
181+
* smarter equals and hashcode for a url taking into account a query string is not order
182+
* dependent.
183+
*/
184+
private static final class SmarterUrl {
185+
private final String path;
186+
private final Map<String, String> queryStringParameters;
187+
188+
private SmarterUrl(String path, Map<String, String> queryStringParameters) {
189+
this.path = path;
190+
this.queryStringParameters = queryStringParameters;
191+
}
192+
193+
@Override
194+
public boolean equals(Object o) {
195+
if (this == o) {
196+
return true;
197+
}
198+
if (!(o instanceof SmarterUrl)) {
199+
return false;
200+
}
201+
SmarterUrl that = (SmarterUrl) o;
202+
return Objects.equals(path, that.path)
203+
&& Objects.equals(queryStringParameters, that.queryStringParameters);
204+
}
205+
206+
@Override
207+
public int hashCode() {
208+
return Objects.hash(path, queryStringParameters);
209+
}
210+
211+
private static SmarterUrl of(URI uri) {
212+
String path = uri.getRawPath();
213+
String rawQuery = uri.getRawQuery();
214+
String[] split = rawQuery.split("&");
215+
Map<String, String> queryStringParameters =
216+
Arrays.stream(split)
217+
.map(
218+
qp -> {
219+
// use indexOf instead of split, just in case an equals is part of the value
220+
int i = qp.indexOf('=');
221+
String k = qp.substring(0, i);
222+
String v = qp.substring(i + 1);
223+
return Tuple.of(k, v);
224+
})
225+
.collect(Collectors.toMap(Tuple::x, Tuple::y));
226+
return new SmarterUrl(path, queryStringParameters);
227+
}
228+
}
169229
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
<dependency>
127127
<groupId>com.google.cloud</groupId>
128128
<artifactId>google-cloud-conformance-tests</artifactId>
129-
<version>0.3.4</version>
129+
<version>0.3.5</version>
130130
<scope>test</scope>
131131
<exclusions>
132132
<exclusion>

0 commit comments

Comments
 (0)