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.discoveryengine.v1beta; 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.discoveryengine.v1beta.stub.HttpJsonCompletionServiceStub; 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 CompletionServiceClientHttpJsonTest { 42 private static MockHttpService mockService; 43 private static CompletionServiceClient client; 44 45 @BeforeClass startStaticServer()46 public static void startStaticServer() throws IOException { 47 mockService = 48 new MockHttpService( 49 HttpJsonCompletionServiceStub.getMethodDescriptors(), 50 CompletionServiceSettings.getDefaultEndpoint()); 51 CompletionServiceSettings settings = 52 CompletionServiceSettings.newHttpJsonBuilder() 53 .setTransportChannelProvider( 54 CompletionServiceSettings.defaultHttpJsonTransportProviderBuilder() 55 .setHttpTransport(mockService) 56 .build()) 57 .setCredentialsProvider(NoCredentialsProvider.create()) 58 .build(); 59 client = CompletionServiceClient.create(settings); 60 } 61 62 @AfterClass stopServer()63 public static void stopServer() { 64 client.close(); 65 } 66 67 @Before setUp()68 public void setUp() {} 69 70 @After tearDown()71 public void tearDown() throws Exception { 72 mockService.reset(); 73 } 74 75 @Test completeQueryTest()76 public void completeQueryTest() throws Exception { 77 CompleteQueryResponse expectedResponse = 78 CompleteQueryResponse.newBuilder() 79 .addAllQuerySuggestions(new ArrayList<CompleteQueryResponse.QuerySuggestion>()) 80 .build(); 81 mockService.addResponse(expectedResponse); 82 83 CompleteQueryRequest request = 84 CompleteQueryRequest.newBuilder() 85 .setDataStore( 86 DataStoreName.ofProjectLocationDataStoreName( 87 "[PROJECT]", "[LOCATION]", "[DATA_STORE]") 88 .toString()) 89 .setQuery("query107944136") 90 .setQueryModel("queryModel-184930495") 91 .setUserPseudoId("userPseudoId-1155274652") 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 .setDataStore( 123 DataStoreName.ofProjectLocationDataStoreName( 124 "[PROJECT]", "[LOCATION]", "[DATA_STORE]") 125 .toString()) 126 .setQuery("query107944136") 127 .setQueryModel("queryModel-184930495") 128 .setUserPseudoId("userPseudoId-1155274652") 129 .build(); 130 client.completeQuery(request); 131 Assert.fail("No exception raised"); 132 } catch (InvalidArgumentException e) { 133 // Expected exception. 134 } 135 } 136 } 137