Skip to content

Commit c269e0f

Browse files
ludochgae-java-bot
authored andcommitted
Re-enable and migrate RemoteDatastoreTest to Proto2 API and move remaining internal tests to open source.
PiperOrigin-RevId: 865415153 Change-Id: I26706754398304cfe4567583ffb84e0d9f2d73f1
1 parent 4574b98 commit c269e0f

File tree

14 files changed

+2965
-12
lines changed

14 files changed

+2965
-12
lines changed

remoteapi/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@
5656
<groupId>com.google.appengine</groupId>
5757
<artifactId>appengine-api-1.0-sdk</artifactId>
5858
</dependency>
59+
<dependency>
60+
<groupId>junit</groupId>
61+
<artifactId>junit</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.google.truth</groupId>
66+
<artifactId>truth</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.google.truth.extensions</groupId>
71+
<artifactId>truth-proto-extension</artifactId>
72+
<version>1.4.5</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.mockito</groupId>
77+
<artifactId>mockito-core</artifactId>
78+
<scope>test</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.google.protobuf</groupId>
82+
<artifactId>protobuf-java</artifactId>
83+
<optional>true</optional>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.apache.httpcomponents</groupId>
87+
<artifactId>httpclient</artifactId>
88+
<optional>true</optional>
89+
</dependency>
5990
</dependencies>
6091
<build>
6192
<plugins>

remoteapi/src/main/java/com/google/appengine/tools/remoteapi/RemoteDatastore.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.protobuf.Message;
2424
import com.google.storage.onestore.v3_bytes.proto2api.OnestoreEntity;
2525
import java.util.ArrayList;
26-
import java.util.Collection;
2726
import java.util.HashSet;
2827
import java.util.Iterator;
2928
import java.util.List;
@@ -195,12 +194,11 @@ static boolean rewriteQueryAppIds(DatastoreV3Pb.Query.Builder query, String remo
195194
reserialize = true;
196195
query.getAncestorBuilder().setApp(remoteAppId);
197196
}
198-
for (DatastoreV3Pb.Query.Filter filter : query.getFilterList()) {
199-
for (OnestoreEntity.Property prop : filter.getPropertyList()) {
200-
OnestoreEntity.PropertyValue propValue = prop.getValue();
201-
if (propValue.hasReferenceValue()) {
197+
for (DatastoreV3Pb.Query.Filter.Builder filter : query.getFilterBuilderList()) {
198+
for (OnestoreEntity.Property.Builder prop : filter.getPropertyBuilderList()) {
199+
if (prop.getValue().hasReferenceValue()) {
202200
OnestoreEntity.PropertyValue.ReferenceValue.Builder ref =
203-
propValue.getReferenceValue().toBuilder();
201+
prop.getValueBuilder().getReferenceValueBuilder();
204202
if (!ref.getApp().equals(remoteAppId)) {
205203
reserialize = true;
206204
ref.setApp(remoteAppId);
@@ -268,7 +266,7 @@ private byte[] handleGet(byte[] originalRequestBytes) {
268266
mergeFromBytes(rewrittenReq, originalRequestBytes);
269267

270268
// Update the Request so that all References have the remoteAppId.
271-
boolean reserialize = rewriteRequestReferences(rewrittenReq.getKeyList(), remoteAppId);
269+
boolean reserialize = rewriteRequestReferences(rewrittenReq.getKeyBuilderList(), remoteAppId);
272270
if (rewrittenReq.hasTransaction()) {
273271
return handleGetWithTransaction(rewrittenReq.build());
274272
} else {
@@ -326,7 +324,7 @@ private byte[] handleDelete(byte[] requestBytes) {
326324
DatastoreV3Pb.DeleteRequest.Builder request = DatastoreV3Pb.DeleteRequest.newBuilder();
327325
mergeFromBytes(request, requestBytes);
328326

329-
boolean reserialize = rewriteRequestReferences(request.getKeyList(), remoteAppId);
327+
boolean reserialize = rewriteRequestReferences(request.getKeyBuilderList(), remoteAppId);
330328
if (reserialize) {
331329
// The request was mutated, so we need to reserialize it.
332330
requestBytes = request.build().toByteArray();
@@ -348,12 +346,12 @@ private byte[] handleDelete(byte[] requestBytes) {
348346
*/
349347
/* @VisibleForTesting */
350348
static boolean rewriteRequestReferences(
351-
Collection<OnestoreEntity.Reference> references, String remoteAppId) {
349+
List<OnestoreEntity.Reference.Builder> references, String remoteAppId) {
352350

353351
boolean reserialize = false;
354-
for (OnestoreEntity.Reference refToCheck : references) {
352+
for (OnestoreEntity.Reference.Builder refToCheck : references) {
355353
if (!refToCheck.getApp().equals(remoteAppId)) {
356-
refToCheck = refToCheck.toBuilder().setApp(remoteAppId).build();
354+
refToCheck.setApp(remoteAppId);
357355
reserialize = true;
358356
}
359357
}

remoteapi/src/main/java/com/google/appengine/tools/remoteapi/RemoteRpc.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*
32
* Copyright 2021 Google LLC
43
*
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2021 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+
* https://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+
17+
package com.google.appengine.tools.remoteapi;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertThrows;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import com.google.api.client.http.LowLevelHttpRequest;
24+
import com.google.api.client.http.LowLevelHttpResponse;
25+
import com.google.api.client.testing.http.MockHttpTransport;
26+
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
27+
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
28+
import com.google.appengine.tools.remoteapi.AppEngineClient.Response;
29+
import com.google.appengine.tools.remoteapi.testing.StubCredential;
30+
import java.io.IOException;
31+
import java.util.Arrays;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
/**
37+
* Test for {@link OAuthClient}.
38+
*/
39+
@RunWith(JUnit4.class)
40+
public class OAuthClientTest {
41+
42+
private static final String APP_ID = "appid";
43+
44+
private static class SimpleHttpTransport extends MockHttpTransport {
45+
private final byte[] responseBytes;
46+
47+
private String lastMethod = null;
48+
private String lastUrl = null;
49+
50+
SimpleHttpTransport(byte[] responseBytes) {
51+
this.responseBytes = responseBytes;
52+
}
53+
54+
@Override
55+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
56+
lastMethod = method;
57+
lastUrl = url;
58+
return new MockLowLevelHttpRequest() {
59+
@Override
60+
public LowLevelHttpResponse execute() throws IOException {
61+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
62+
response.setContent(Arrays.copyOf(responseBytes, responseBytes.length));
63+
return response;
64+
}
65+
};
66+
}
67+
68+
String getLastMethod() {
69+
return lastMethod;
70+
}
71+
72+
String getLastUrl() {
73+
return lastUrl;
74+
}
75+
}
76+
77+
@Test
78+
public void testConstructor() {
79+
RemoteApiOptions noOAuthCredential = new RemoteApiOptions()
80+
.credentials("email", "password")
81+
.httpTransport(new SimpleHttpTransport(new byte[] {}));
82+
assertThrows(IllegalArgumentException.class, () -> new OAuthClient(noOAuthCredential, APP_ID));
83+
84+
RemoteApiOptions noHttpTransport = new RemoteApiOptions()
85+
.oauthCredential(new StubCredential());
86+
assertThrows(IllegalStateException.class, () -> new OAuthClient(noHttpTransport, APP_ID));
87+
}
88+
89+
@Test
90+
public void testGet() throws Exception {
91+
byte[] expectedResponseBytes = new byte[] {42};
92+
93+
SimpleHttpTransport transport = new SimpleHttpTransport(expectedResponseBytes);
94+
RemoteApiOptions options = new RemoteApiOptions()
95+
.server("example.com", 8080)
96+
.oauthCredential(new StubCredential())
97+
.httpTransport(transport);
98+
99+
Response response = new OAuthClient(options, APP_ID)
100+
.get("/foo");
101+
assertTrue(Arrays.equals(expectedResponseBytes, response.getBodyAsBytes()));
102+
assertEquals("GET", transport.getLastMethod());
103+
assertEquals("http://example.com:8080/foo", transport.getLastUrl());
104+
}
105+
106+
@Test
107+
public void testPost() throws Exception {
108+
byte[] expectedResponseBytes = new byte[] {42};
109+
110+
SimpleHttpTransport transport = new SimpleHttpTransport(expectedResponseBytes);
111+
RemoteApiOptions options = new RemoteApiOptions()
112+
.server("example.com", 443)
113+
.oauthCredential(new StubCredential())
114+
.httpTransport(transport);
115+
116+
Response response = new OAuthClient(options, APP_ID)
117+
.post("/foo", "application/json", new byte[]{1});
118+
assertTrue(Arrays.equals(expectedResponseBytes, response.getBodyAsBytes()));
119+
assertEquals("POST", transport.getLastMethod());
120+
assertEquals("https://example.com:443/foo", transport.getLastUrl());
121+
}
122+
}

0 commit comments

Comments
 (0)