Skip to content

Commit 107ad49

Browse files
author
Ajay Kannan
committed
---
yaml --- r: 1559 b: refs/heads/master c: 3ddbcf1 h: refs/heads/master i: 1557: b874142 1555: 97cf600 1551: 2614531
1 parent 3d60e5b commit 107ad49

3 files changed

Lines changed: 238 additions & 4 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 2d1e83693589a1b0658a50d044c8928430df90f0
2+
refs/heads/master: 3ddbcf1f80fc497a155f0950b10eb58199f78d9d
33
refs/heads/travis: e21ee7b88a5edc3f3d8c71f90c3fc32abf7e8dd6
44
refs/heads/gh-pages: d1b373c30c176edc08692348167bec3a244bb823
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3

trunk/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,11 @@ public AuthCredentials authCredentials() {
511511
}
512512

513513
/**
514-
* Returns configuration parameters for request retries. By default requests are not retried:
515-
* {@link RetryParams#noRetries()} is used.
514+
* Returns configuration parameters for request retries. By default requests are retried:
515+
* {@link RetryParams#defaultInstance()} is used.
516516
*/
517517
public RetryParams retryParams() {
518-
return retryParams != null ? retryParams : RetryParams.noRetries();
518+
return retryParams;
519519
}
520520

521521
/**
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/*
2+
* Copyright 2015 Google Inc. 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+
* 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+
17+
package com.google.gcloud;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotEquals;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.fail;
23+
24+
import com.google.gcloud.ServiceOptions.Clock;
25+
import com.google.gcloud.ServiceOptions.DefaultHttpTransportFactory;
26+
import com.google.gcloud.ServiceOptions.HttpTransportFactory;
27+
import com.google.gcloud.spi.ServiceRpcFactory;
28+
29+
import org.easymock.EasyMock;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
import java.io.ByteArrayInputStream;
35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.util.Set;
38+
39+
@RunWith(JUnit4.class)
40+
public class ServiceOptionsTest {
41+
private static final String JSON_KEY =
42+
"{\n"
43+
+ " \"private_key_id\": \"somekeyid\",\n"
44+
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
45+
+ "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg"
46+
+ "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4"
47+
+ "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2"
48+
+ "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa"
49+
+ "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16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lerreP\\nJ7gSi"
61+
+ "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG"
62+
+ "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk"
63+
+ "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n"
64+
+ " \"client_email\": \"[email protected]\",\n"
65+
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n"
66+
+ " \"type\": \"service_account\"\n"
67+
+ "}";
68+
private static final InputStream JSON_KEY_STREAM = new ByteArrayInputStream(JSON_KEY.getBytes());
69+
private static AuthCredentials authCredentials;
70+
static {
71+
try {
72+
authCredentials = AuthCredentials.createForJson(JSON_KEY_STREAM);
73+
} catch (IOException e) {
74+
fail("Couldn't create fake JSON credentials.");
75+
}
76+
}
77+
private static final HttpTransportFactory MOCK_HTTP_TRANSPORT_FACTORY =
78+
EasyMock.createMock(HttpTransportFactory.class);
79+
private static final TestServiceOptions OPTIONS =
80+
TestServiceOptions.builder()
81+
.authCredentials(authCredentials)
82+
.connectTimeout(1234)
83+
.host("host")
84+
.httpTransportFactory(MOCK_HTTP_TRANSPORT_FACTORY)
85+
.projectId("project-id")
86+
.readTimeout(5678)
87+
.retryParams(RetryParams.noRetries())
88+
.build();
89+
private static final TestServiceOptions DEFAULT_OPTIONS =
90+
TestServiceOptions.builder().projectId("project-id").build();
91+
private static final TestServiceOptions OPTIONS_COPY = OPTIONS.toBuilder().build();
92+
93+
interface TestService extends Service<TestServiceOptions> {}
94+
95+
static class TestServiceImpl
96+
extends BaseService<TestServiceOptions> implements TestService {
97+
TestServiceImpl(TestServiceOptions options) {
98+
super(options);
99+
}
100+
}
101+
102+
interface TestServiceFactory extends ServiceFactory<TestService, TestServiceOptions> {}
103+
104+
static class DefaultTestServiceFactory implements TestServiceFactory {
105+
private static final TestServiceFactory INSTANCE = new DefaultTestServiceFactory();
106+
107+
@Override
108+
public TestService create(TestServiceOptions options) {
109+
return new TestServiceImpl(options);
110+
}
111+
}
112+
113+
interface TestServiceRpcFactory
114+
extends ServiceRpcFactory<TestServiceRpc, TestServiceOptions> {}
115+
116+
static class DefaultTestServiceRpcFactory implements TestServiceRpcFactory {
117+
private static final TestServiceRpcFactory INSTANCE = new DefaultTestServiceRpcFactory();
118+
119+
@Override
120+
public TestServiceRpc create(TestServiceOptions options) {
121+
return new DefaultTestServiceRpc(options);
122+
}
123+
}
124+
125+
interface TestServiceRpc {}
126+
127+
static class DefaultTestServiceRpc implements TestServiceRpc {
128+
DefaultTestServiceRpc(TestServiceOptions options) {}
129+
}
130+
131+
static class TestServiceOptions extends ServiceOptions<TestService, TestServiceRpc, TestServiceOptions> {
132+
static class Builder
133+
extends ServiceOptions.Builder<TestService, TestServiceRpc, TestServiceOptions, Builder> {
134+
private Builder() {}
135+
136+
private Builder(TestServiceOptions options) {
137+
super(options);
138+
}
139+
140+
@Override
141+
protected TestServiceOptions build() {
142+
return new TestServiceOptions(this);
143+
}
144+
}
145+
146+
protected TestServiceOptions(Builder builder) {
147+
super(TestServiceFactory.class, TestServiceRpcFactory.class, builder);
148+
}
149+
150+
public static TestServiceOptions defaultInstance() {
151+
return builder().build();
152+
}
153+
154+
@Override
155+
protected TestServiceFactory defaultServiceFactory() {
156+
return DefaultTestServiceFactory.INSTANCE;
157+
}
158+
159+
@Override
160+
protected TestServiceRpcFactory defaultRpcFactory() {
161+
return DefaultTestServiceRpcFactory.INSTANCE;
162+
}
163+
164+
@Override
165+
protected Set<String> scopes() {
166+
return null;
167+
}
168+
169+
@Override
170+
public Builder toBuilder() {
171+
return new Builder(this);
172+
}
173+
174+
public static Builder builder() {
175+
return new Builder();
176+
}
177+
178+
@Override
179+
public boolean equals(Object obj) {
180+
return obj instanceof TestServiceOptions && baseEquals((TestServiceOptions) obj);
181+
}
182+
183+
@Override
184+
public int hashCode() {
185+
return baseHashCode();
186+
}
187+
}
188+
189+
@Test
190+
public void testBuilder() {
191+
assertEquals(authCredentials, OPTIONS.authCredentials());
192+
assertEquals(Clock.defaultClock(), OPTIONS.clock());
193+
assertEquals(1234, OPTIONS.connectTimeout());
194+
assertEquals("host", OPTIONS.host());
195+
assertEquals(MOCK_HTTP_TRANSPORT_FACTORY, OPTIONS.httpTransportFactory());
196+
assertEquals("project-id", OPTIONS.projectId());
197+
assertEquals(5678, OPTIONS.readTimeout());
198+
assertEquals(RetryParams.noRetries(), OPTIONS.retryParams());
199+
200+
assertEquals(Clock.defaultClock(), DEFAULT_OPTIONS.clock());
201+
assertEquals(-1, DEFAULT_OPTIONS.connectTimeout());
202+
assertEquals("https://www.googleapis.com", DEFAULT_OPTIONS.host());
203+
assertTrue(DEFAULT_OPTIONS.httpTransportFactory() instanceof DefaultHttpTransportFactory);
204+
assertEquals(-1, DEFAULT_OPTIONS.readTimeout());
205+
assertEquals(RetryParams.defaultInstance(), DEFAULT_OPTIONS.retryParams());
206+
}
207+
208+
@Test
209+
public void testGetProjectIdRequired() {
210+
assertTrue(OPTIONS.projectIdRequired());
211+
}
212+
213+
@Test
214+
public void testService() {
215+
assertTrue(OPTIONS.service() instanceof TestServiceImpl);
216+
}
217+
218+
@Test
219+
public void testRpc() {
220+
assertTrue(OPTIONS.rpc() instanceof DefaultTestServiceRpc);
221+
}
222+
223+
@Test
224+
public void testBaseEquals() {
225+
assertEquals(OPTIONS, OPTIONS_COPY);
226+
assertNotEquals(DEFAULT_OPTIONS, OPTIONS);
227+
}
228+
229+
@Test
230+
public void testBaseHashCode() {
231+
assertEquals(OPTIONS.hashCode(), OPTIONS_COPY.hashCode());
232+
assertNotEquals(DEFAULT_OPTIONS.hashCode(), OPTIONS.hashCode());
233+
}
234+
}

0 commit comments

Comments
 (0)