Skip to content

Commit

Permalink
test(deps): update dependency com.google.cloud:google-cloud-conforman…
Browse files Browse the repository at this point in the history
…ce-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]>
  • Loading branch information
renovate-bot and BenWhitehead committed Mar 29, 2023
1 parent af90f16 commit cbe16ea
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package com.google.cloud.storage;

import static org.junit.Assert.assertEquals;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;

import com.google.api.core.ApiClock;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.Tuple;
import com.google.cloud.conformance.storage.v1.SigningV4Test;
import com.google.cloud.conformance.storage.v1.TestFile;
import com.google.cloud.conformance.storage.v1.UrlStyle;
Expand All @@ -32,11 +33,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
Expand Down Expand Up @@ -128,7 +134,9 @@ public void test() {
SignUrlOption.withQueryParams(testData.getQueryParametersMap()),
style)
.toString();
assertEquals(testData.getExpectedUrl(), signedUrl);
SmarterUrl expected = SmarterUrl.of(URI.create(testData.getExpectedUrl()));
SmarterUrl actual = SmarterUrl.of(URI.create(signedUrl));
assertThat(actual).isEqualTo(expected);
}

/**
Expand Down Expand Up @@ -166,4 +174,56 @@ public static Collection<Object[]> testCases() throws IOException {
}
return data;
}

/**
* Equals on {@link URI} or {@link java.net.URL} perform string comparison on the full query
* string. However, query strings are not order dependent. This class essentially provides a
* smarter equals and hashcode for a url taking into account a query string is not order
* dependent.
*/
private static final class SmarterUrl {
private final String path;
private final Map<String, String> queryStringParameters;

private SmarterUrl(String path, Map<String, String> queryStringParameters) {
this.path = path;
this.queryStringParameters = queryStringParameters;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SmarterUrl)) {
return false;
}
SmarterUrl that = (SmarterUrl) o;
return Objects.equals(path, that.path)
&& Objects.equals(queryStringParameters, that.queryStringParameters);
}

@Override
public int hashCode() {
return Objects.hash(path, queryStringParameters);
}

private static SmarterUrl of(URI uri) {
String path = uri.getRawPath();
String rawQuery = uri.getRawQuery();
String[] split = rawQuery.split("&");
Map<String, String> queryStringParameters =
Arrays.stream(split)
.map(
qp -> {
// use indexOf instead of split, just in case an equals is part of the value
int i = qp.indexOf('=');
String k = qp.substring(0, i);
String v = qp.substring(i + 1);
return Tuple.of(k, v);
})
.collect(Collectors.toMap(Tuple::x, Tuple::y));
return new SmarterUrl(path, queryStringParameters);
}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-conformance-tests</artifactId>
<version>0.3.4</version>
<version>0.3.5</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down

0 comments on commit cbe16ea

Please sign in to comment.