Skip to content

Commit 59e0325

Browse files
committed
Language v1beta2 Release
1 parent 7074c6d commit 59e0325

File tree

8 files changed

+1506
-1
lines changed

8 files changed

+1506
-1
lines changed

google-cloud-language/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
</exclusion>
4141
</exclusions>
4242
</dependency>
43+
<dependency>
44+
<groupId>com.google.api.grpc</groupId>
45+
<artifactId>grpc-google-cloud-language-v1beta2</artifactId>
46+
<version>0.1.6</version>
47+
<exclusions>
48+
<exclusion>
49+
<groupId>io.grpc</groupId>
50+
<artifactId>grpc-all</artifactId>
51+
</exclusion>
52+
</exclusions>
53+
</dependency>
4354
<dependency>
4455
<groupId>io.grpc</groupId>
4556
<artifactId>grpc-netty</artifactId>

google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/LanguageServiceClient.java

Lines changed: 589 additions & 0 deletions
Large diffs are not rendered by default.

google-cloud-language/src/main/java/com/google/cloud/language/spi/v1beta2/LanguageServiceSettings.java

Lines changed: 388 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2017, 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+
/**
18+
* A client to Google Cloud Natural Language API.
19+
*
20+
* <p>The interfaces provided are listed below, along with usage samples.
21+
*
22+
* <p>===================== LanguageServiceClient =====================
23+
*
24+
* <p>Service Description: Provides text analysis operations such as sentiment analysis and entity
25+
* recognition.
26+
*
27+
* <p>Sample for LanguageServiceClient:
28+
*
29+
* <pre>
30+
* <code>
31+
* try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
32+
* Document document = Document.newBuilder().build();
33+
* AnalyzeSentimentResponse response = languageServiceClient.analyzeSentiment(document);
34+
* }
35+
* </code>
36+
* </pre>
37+
*/
38+
package com.google.cloud.language.spi.v1beta2;
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/*
2+
* Copyright 2017, 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+
package com.google.cloud.language.spi.v1beta2;
17+
18+
import com.google.api.gax.grpc.ApiException;
19+
import com.google.api.gax.testing.MockGrpcService;
20+
import com.google.api.gax.testing.MockServiceHelper;
21+
import com.google.cloud.language.v1beta2.AnalyzeEntitiesRequest;
22+
import com.google.cloud.language.v1beta2.AnalyzeEntitiesResponse;
23+
import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest;
24+
import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse;
25+
import com.google.cloud.language.v1beta2.AnalyzeSentimentRequest;
26+
import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse;
27+
import com.google.cloud.language.v1beta2.AnalyzeSyntaxRequest;
28+
import com.google.cloud.language.v1beta2.AnalyzeSyntaxResponse;
29+
import com.google.cloud.language.v1beta2.AnnotateTextRequest;
30+
import com.google.cloud.language.v1beta2.AnnotateTextRequest.Features;
31+
import com.google.cloud.language.v1beta2.AnnotateTextResponse;
32+
import com.google.cloud.language.v1beta2.Document;
33+
import com.google.cloud.language.v1beta2.EncodingType;
34+
import com.google.protobuf.GeneratedMessageV3;
35+
import io.grpc.Status;
36+
import io.grpc.StatusRuntimeException;
37+
import java.io.IOException;
38+
import java.util.Arrays;
39+
import java.util.List;
40+
import org.junit.After;
41+
import org.junit.AfterClass;
42+
import org.junit.Assert;
43+
import org.junit.Before;
44+
import org.junit.BeforeClass;
45+
import org.junit.Test;
46+
47+
@javax.annotation.Generated("by GAPIC")
48+
public class LanguageServiceClientTest {
49+
private static MockLanguageService mockLanguageService;
50+
private static MockServiceHelper serviceHelper;
51+
private LanguageServiceClient client;
52+
53+
@BeforeClass
54+
public static void startStaticServer() {
55+
mockLanguageService = new MockLanguageService();
56+
serviceHelper =
57+
new MockServiceHelper("in-process-1", Arrays.<MockGrpcService>asList(mockLanguageService));
58+
serviceHelper.start();
59+
}
60+
61+
@AfterClass
62+
public static void stopServer() {
63+
serviceHelper.stop();
64+
}
65+
66+
@Before
67+
public void setUp() throws IOException {
68+
serviceHelper.reset();
69+
LanguageServiceSettings settings =
70+
LanguageServiceSettings.defaultBuilder()
71+
.setChannelProvider(serviceHelper.createChannelProvider())
72+
.build();
73+
client = LanguageServiceClient.create(settings);
74+
}
75+
76+
@After
77+
public void tearDown() throws Exception {
78+
client.close();
79+
}
80+
81+
@Test
82+
@SuppressWarnings("all")
83+
public void analyzeSentimentTest() {
84+
String language = "language-1613589672";
85+
AnalyzeSentimentResponse expectedResponse =
86+
AnalyzeSentimentResponse.newBuilder().setLanguage(language).build();
87+
mockLanguageService.addResponse(expectedResponse);
88+
89+
Document document = Document.newBuilder().build();
90+
91+
AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document);
92+
Assert.assertEquals(expectedResponse, actualResponse);
93+
94+
List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
95+
Assert.assertEquals(1, actualRequests.size());
96+
AnalyzeSentimentRequest actualRequest = (AnalyzeSentimentRequest) actualRequests.get(0);
97+
98+
Assert.assertEquals(document, actualRequest.getDocument());
99+
}
100+
101+
@Test
102+
@SuppressWarnings("all")
103+
public void analyzeSentimentExceptionTest() throws Exception {
104+
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
105+
mockLanguageService.addException(exception);
106+
107+
try {
108+
Document document = Document.newBuilder().build();
109+
110+
client.analyzeSentiment(document);
111+
Assert.fail("No exception raised");
112+
} catch (ApiException e) {
113+
Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
114+
}
115+
}
116+
117+
@Test
118+
@SuppressWarnings("all")
119+
public void analyzeEntitiesTest() {
120+
String language = "language-1613589672";
121+
AnalyzeEntitiesResponse expectedResponse =
122+
AnalyzeEntitiesResponse.newBuilder().setLanguage(language).build();
123+
mockLanguageService.addResponse(expectedResponse);
124+
125+
Document document = Document.newBuilder().build();
126+
EncodingType encodingType = EncodingType.NONE;
127+
128+
AnalyzeEntitiesResponse actualResponse = client.analyzeEntities(document, encodingType);
129+
Assert.assertEquals(expectedResponse, actualResponse);
130+
131+
List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
132+
Assert.assertEquals(1, actualRequests.size());
133+
AnalyzeEntitiesRequest actualRequest = (AnalyzeEntitiesRequest) actualRequests.get(0);
134+
135+
Assert.assertEquals(document, actualRequest.getDocument());
136+
Assert.assertEquals(encodingType, actualRequest.getEncodingType());
137+
}
138+
139+
@Test
140+
@SuppressWarnings("all")
141+
public void analyzeEntitiesExceptionTest() throws Exception {
142+
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
143+
mockLanguageService.addException(exception);
144+
145+
try {
146+
Document document = Document.newBuilder().build();
147+
EncodingType encodingType = EncodingType.NONE;
148+
149+
client.analyzeEntities(document, encodingType);
150+
Assert.fail("No exception raised");
151+
} catch (ApiException e) {
152+
Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
153+
}
154+
}
155+
156+
@Test
157+
@SuppressWarnings("all")
158+
public void analyzeEntitySentimentTest() {
159+
String language = "language-1613589672";
160+
AnalyzeEntitySentimentResponse expectedResponse =
161+
AnalyzeEntitySentimentResponse.newBuilder().setLanguage(language).build();
162+
mockLanguageService.addResponse(expectedResponse);
163+
164+
Document document = Document.newBuilder().build();
165+
EncodingType encodingType = EncodingType.NONE;
166+
167+
AnalyzeEntitySentimentResponse actualResponse =
168+
client.analyzeEntitySentiment(document, encodingType);
169+
Assert.assertEquals(expectedResponse, actualResponse);
170+
171+
List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
172+
Assert.assertEquals(1, actualRequests.size());
173+
AnalyzeEntitySentimentRequest actualRequest =
174+
(AnalyzeEntitySentimentRequest) actualRequests.get(0);
175+
176+
Assert.assertEquals(document, actualRequest.getDocument());
177+
Assert.assertEquals(encodingType, actualRequest.getEncodingType());
178+
}
179+
180+
@Test
181+
@SuppressWarnings("all")
182+
public void analyzeEntitySentimentExceptionTest() throws Exception {
183+
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
184+
mockLanguageService.addException(exception);
185+
186+
try {
187+
Document document = Document.newBuilder().build();
188+
EncodingType encodingType = EncodingType.NONE;
189+
190+
client.analyzeEntitySentiment(document, encodingType);
191+
Assert.fail("No exception raised");
192+
} catch (ApiException e) {
193+
Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
194+
}
195+
}
196+
197+
@Test
198+
@SuppressWarnings("all")
199+
public void analyzeSyntaxTest() {
200+
String language = "language-1613589672";
201+
AnalyzeSyntaxResponse expectedResponse =
202+
AnalyzeSyntaxResponse.newBuilder().setLanguage(language).build();
203+
mockLanguageService.addResponse(expectedResponse);
204+
205+
Document document = Document.newBuilder().build();
206+
EncodingType encodingType = EncodingType.NONE;
207+
208+
AnalyzeSyntaxResponse actualResponse = client.analyzeSyntax(document, encodingType);
209+
Assert.assertEquals(expectedResponse, actualResponse);
210+
211+
List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
212+
Assert.assertEquals(1, actualRequests.size());
213+
AnalyzeSyntaxRequest actualRequest = (AnalyzeSyntaxRequest) actualRequests.get(0);
214+
215+
Assert.assertEquals(document, actualRequest.getDocument());
216+
Assert.assertEquals(encodingType, actualRequest.getEncodingType());
217+
}
218+
219+
@Test
220+
@SuppressWarnings("all")
221+
public void analyzeSyntaxExceptionTest() throws Exception {
222+
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
223+
mockLanguageService.addException(exception);
224+
225+
try {
226+
Document document = Document.newBuilder().build();
227+
EncodingType encodingType = EncodingType.NONE;
228+
229+
client.analyzeSyntax(document, encodingType);
230+
Assert.fail("No exception raised");
231+
} catch (ApiException e) {
232+
Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
233+
}
234+
}
235+
236+
@Test
237+
@SuppressWarnings("all")
238+
public void annotateTextTest() {
239+
String language = "language-1613589672";
240+
AnnotateTextResponse expectedResponse =
241+
AnnotateTextResponse.newBuilder().setLanguage(language).build();
242+
mockLanguageService.addResponse(expectedResponse);
243+
244+
Document document = Document.newBuilder().build();
245+
AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build();
246+
EncodingType encodingType = EncodingType.NONE;
247+
248+
AnnotateTextResponse actualResponse = client.annotateText(document, features, encodingType);
249+
Assert.assertEquals(expectedResponse, actualResponse);
250+
251+
List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
252+
Assert.assertEquals(1, actualRequests.size());
253+
AnnotateTextRequest actualRequest = (AnnotateTextRequest) actualRequests.get(0);
254+
255+
Assert.assertEquals(document, actualRequest.getDocument());
256+
Assert.assertEquals(features, actualRequest.getFeatures());
257+
Assert.assertEquals(encodingType, actualRequest.getEncodingType());
258+
}
259+
260+
@Test
261+
@SuppressWarnings("all")
262+
public void annotateTextExceptionTest() throws Exception {
263+
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
264+
mockLanguageService.addException(exception);
265+
266+
try {
267+
Document document = Document.newBuilder().build();
268+
AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build();
269+
EncodingType encodingType = EncodingType.NONE;
270+
271+
client.annotateText(document, features, encodingType);
272+
Assert.fail("No exception raised");
273+
} catch (ApiException e) {
274+
Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
275+
}
276+
}
277+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2017, 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+
package com.google.cloud.language.spi.v1beta2;
17+
18+
import com.google.api.gax.testing.MockGrpcService;
19+
import com.google.protobuf.GeneratedMessageV3;
20+
import io.grpc.ServerServiceDefinition;
21+
import java.util.List;
22+
23+
@javax.annotation.Generated("by GAPIC")
24+
public class MockLanguageService implements MockGrpcService {
25+
private final MockLanguageServiceImpl serviceImpl;
26+
27+
public MockLanguageService() {
28+
serviceImpl = new MockLanguageServiceImpl();
29+
}
30+
31+
@Override
32+
public List<GeneratedMessageV3> getRequests() {
33+
return serviceImpl.getRequests();
34+
}
35+
36+
@Override
37+
public void addResponse(GeneratedMessageV3 response) {
38+
serviceImpl.addResponse(response);
39+
}
40+
41+
@Override
42+
public void addException(Exception exception) {
43+
serviceImpl.addException(exception);
44+
}
45+
46+
public void setResponses(List<GeneratedMessageV3> responses) {
47+
serviceImpl.setResponses(responses);
48+
}
49+
50+
@Override
51+
public ServerServiceDefinition getServiceDefinition() {
52+
return serviceImpl.bindService();
53+
}
54+
55+
@Override
56+
public void reset() {
57+
serviceImpl.reset();
58+
}
59+
}

0 commit comments

Comments
 (0)