• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *      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.cloud.talent.v4beta1;
18 
19 import com.google.api.gax.core.NoCredentialsProvider;
20 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
21 import com.google.api.gax.httpjson.testing.MockHttpService;
22 import com.google.api.gax.rpc.ApiClientHeaderProvider;
23 import com.google.api.gax.rpc.ApiException;
24 import com.google.api.gax.rpc.ApiExceptionFactory;
25 import com.google.api.gax.rpc.InvalidArgumentException;
26 import com.google.api.gax.rpc.StatusCode;
27 import com.google.api.gax.rpc.testing.FakeStatusCode;
28 import com.google.cloud.talent.v4beta1.stub.HttpJsonCompletionStub;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import javax.annotation.Generated;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 
40 @Generated("by gapic-generator-java")
41 public class CompletionClientHttpJsonTest {
42   private static MockHttpService mockService;
43   private static CompletionClient client;
44 
45   @BeforeClass
startStaticServer()46   public static void startStaticServer() throws IOException {
47     mockService =
48         new MockHttpService(
49             HttpJsonCompletionStub.getMethodDescriptors(), CompletionSettings.getDefaultEndpoint());
50     CompletionSettings settings =
51         CompletionSettings.newHttpJsonBuilder()
52             .setTransportChannelProvider(
53                 CompletionSettings.defaultHttpJsonTransportProviderBuilder()
54                     .setHttpTransport(mockService)
55                     .build())
56             .setCredentialsProvider(NoCredentialsProvider.create())
57             .build();
58     client = CompletionClient.create(settings);
59   }
60 
61   @AfterClass
stopServer()62   public static void stopServer() {
63     client.close();
64   }
65 
66   @Before
setUp()67   public void setUp() {}
68 
69   @After
tearDown()70   public void tearDown() throws Exception {
71     mockService.reset();
72   }
73 
74   @Test
completeQueryTest()75   public void completeQueryTest() throws Exception {
76     CompleteQueryResponse expectedResponse =
77         CompleteQueryResponse.newBuilder()
78             .addAllCompletionResults(new ArrayList<CompleteQueryResponse.CompletionResult>())
79             .setMetadata(ResponseMetadata.newBuilder().build())
80             .build();
81     mockService.addResponse(expectedResponse);
82 
83     CompleteQueryRequest request =
84         CompleteQueryRequest.newBuilder()
85             .setParent(TenantName.of("[PROJECT]", "[TENANT]").toString())
86             .setQuery("query107944136")
87             .addAllLanguageCodes(new ArrayList<String>())
88             .setPageSize(883849137)
89             .setCompany(
90                 CompanyName.ofProjectTenantCompanyName("[PROJECT]", "[TENANT]", "[COMPANY]")
91                     .toString())
92             .build();
93 
94     CompleteQueryResponse actualResponse = client.completeQuery(request);
95     Assert.assertEquals(expectedResponse, actualResponse);
96 
97     List<String> actualRequests = mockService.getRequestPaths();
98     Assert.assertEquals(1, actualRequests.size());
99 
100     String apiClientHeaderKey =
101         mockService
102             .getRequestHeaders()
103             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
104             .iterator()
105             .next();
106     Assert.assertTrue(
107         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
108             .matcher(apiClientHeaderKey)
109             .matches());
110   }
111 
112   @Test
completeQueryExceptionTest()113   public void completeQueryExceptionTest() throws Exception {
114     ApiException exception =
115         ApiExceptionFactory.createException(
116             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
117     mockService.addException(exception);
118 
119     try {
120       CompleteQueryRequest request =
121           CompleteQueryRequest.newBuilder()
122               .setParent(TenantName.of("[PROJECT]", "[TENANT]").toString())
123               .setQuery("query107944136")
124               .addAllLanguageCodes(new ArrayList<String>())
125               .setPageSize(883849137)
126               .setCompany(
127                   CompanyName.ofProjectTenantCompanyName("[PROJECT]", "[TENANT]", "[COMPANY]")
128                       .toString())
129               .build();
130       client.completeQuery(request);
131       Assert.fail("No exception raised");
132     } catch (InvalidArgumentException e) {
133       // Expected exception.
134     }
135   }
136 }
137