Skip to content

Commit 73f0677

Browse files
committed
Add initial tests and utilities
1 parent 05c4166 commit 73f0677

File tree

5 files changed

+653
-0
lines changed

5 files changed

+653
-0
lines changed

google-cloud-datastore-utils/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@
6868
<artifactId>jsr305</artifactId>
6969
</dependency>
7070
<!-- Test dependencies -->
71+
<dependency>
72+
<groupId>junit</groupId>
73+
<artifactId>junit</artifactId>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.google.truth</groupId>
78+
<artifactId>truth</artifactId>
79+
<version>1.4.2</version>
80+
<scope>test</scope>
81+
<exclusions>
82+
<exclusion>
83+
<groupId>org.checkerframework</groupId>
84+
<artifactId>checker-qual</artifactId>
85+
</exclusion>
86+
</exclusions>
87+
</dependency>
7188
</dependencies>
7289

7390
<build>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.datastore.utils.testing;
17+
18+
import com.google.api.client.auth.oauth2.Credential;
19+
import com.google.api.client.http.HttpRequest;
20+
import java.io.IOException;
21+
22+
/** Fake credential used for testing purpose. */
23+
public class MockCredential extends Credential {
24+
public MockCredential() {
25+
super(
26+
new AccessMethod() {
27+
@Override
28+
public void intercept(HttpRequest request, String accessToken) throws IOException {}
29+
30+
@Override
31+
public String getAccessTokenFromRequest(HttpRequest request) {
32+
return "MockAccessToken";
33+
}
34+
});
35+
}
36+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2022 Google LLC
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+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.datastore.utils.testing;
17+
18+
import static com.google.common.base.Preconditions.checkState;
19+
20+
import com.google.api.client.auth.oauth2.Credential;
21+
import com.google.api.client.http.*;
22+
import com.google.api.client.testing.http.MockHttpTransport;
23+
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
24+
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
25+
import com.google.api.client.testing.util.TestableByteArrayInputStream;
26+
import com.google.common.collect.Iterables;
27+
import com.google.datastore.utils.DatastoreFactory;
28+
import com.google.datastore.utils.DatastoreOptions;
29+
import com.google.protobuf.Message;
30+
import com.google.rpc.Code;
31+
import com.google.rpc.Status;
32+
import java.io.ByteArrayOutputStream;
33+
import java.io.IOException;
34+
import java.util.List;
35+
36+
/** Fake Datastore factory used for testing purposes when a true Datastore service is not needed. */
37+
public class MockDatastoreFactory extends DatastoreFactory {
38+
private int nextStatus;
39+
private Message nextResponse;
40+
private Status nextError;
41+
private IOException nextException;
42+
43+
private String lastPath;
44+
private String lastMimeType;
45+
private byte[] lastBody;
46+
private List<String> lastCookies;
47+
private String lastApiFormatHeaderValue;
48+
49+
public void setNextResponse(Message response) {
50+
nextStatus = HttpStatusCodes.STATUS_CODE_OK;
51+
nextResponse = response;
52+
nextError = null;
53+
nextException = null;
54+
}
55+
56+
public void setNextError(int status, Code code, String message) {
57+
nextStatus = status;
58+
nextResponse = null;
59+
nextError = makeErrorContent(message, code);
60+
nextException = null;
61+
}
62+
63+
public void setNextException(IOException exception) {
64+
nextStatus = 0;
65+
nextResponse = null;
66+
nextError = null;
67+
nextException = exception;
68+
}
69+
70+
@Override
71+
public HttpRequestFactory makeClient(DatastoreOptions options) {
72+
HttpTransport transport =
73+
new MockHttpTransport() {
74+
@Override
75+
public LowLevelHttpRequest buildRequest(String method, String url) {
76+
return new MockLowLevelHttpRequest(url) {
77+
@Override
78+
public LowLevelHttpResponse execute() throws IOException {
79+
lastPath = new GenericUrl(getUrl()).getRawPath();
80+
lastMimeType = getContentType();
81+
lastCookies = getHeaderValues("Cookie");
82+
lastApiFormatHeaderValue =
83+
Iterables.getOnlyElement(getHeaderValues("X-Goog-Api-Format-Version"));
84+
ByteArrayOutputStream out = new ByteArrayOutputStream();
85+
getStreamingContent().writeTo(out);
86+
lastBody = out.toByteArray();
87+
if (nextException != null) {
88+
throw nextException;
89+
}
90+
MockLowLevelHttpResponse response =
91+
new MockLowLevelHttpResponse()
92+
.setStatusCode(nextStatus)
93+
.setContentType("application/x-protobuf");
94+
if (nextError != null) {
95+
checkState(nextResponse == null);
96+
response.setContent(new TestableByteArrayInputStream(nextError.toByteArray()));
97+
} else {
98+
response.setContent(new TestableByteArrayInputStream(nextResponse.toByteArray()));
99+
}
100+
return response;
101+
}
102+
};
103+
}
104+
};
105+
Credential credential = options.getCredential();
106+
return transport.createRequestFactory(credential);
107+
}
108+
109+
public String getLastPath() {
110+
return lastPath;
111+
}
112+
113+
public String getLastMimeType() {
114+
return lastMimeType;
115+
}
116+
117+
public String getLastApiFormatHeaderValue() {
118+
return lastApiFormatHeaderValue;
119+
}
120+
121+
public byte[] getLastBody() {
122+
return lastBody;
123+
}
124+
125+
public List<String> getLastCookies() {
126+
return lastCookies;
127+
}
128+
129+
private static Status makeErrorContent(String message, Code code) {
130+
return Status.newBuilder().setCode(code.getNumber()).setMessage(message).build();
131+
}
132+
}

0 commit comments

Comments
 (0)