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 static com.google.cloud.discoveryengine.v1beta.SearchServiceClient.SearchPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 23 import com.google.api.gax.httpjson.testing.MockHttpService; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.ApiException; 26 import com.google.api.gax.rpc.ApiExceptionFactory; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.api.gax.rpc.testing.FakeStatusCode; 30 import com.google.cloud.discoveryengine.v1beta.stub.HttpJsonSearchServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.protobuf.Value; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.HashMap; 37 import java.util.List; 38 import javax.annotation.Generated; 39 import org.junit.After; 40 import org.junit.AfterClass; 41 import org.junit.Assert; 42 import org.junit.Before; 43 import org.junit.BeforeClass; 44 import org.junit.Test; 45 46 @Generated("by gapic-generator-java") 47 public class SearchServiceClientHttpJsonTest { 48 private static MockHttpService mockService; 49 private static SearchServiceClient client; 50 51 @BeforeClass startStaticServer()52 public static void startStaticServer() throws IOException { 53 mockService = 54 new MockHttpService( 55 HttpJsonSearchServiceStub.getMethodDescriptors(), 56 SearchServiceSettings.getDefaultEndpoint()); 57 SearchServiceSettings settings = 58 SearchServiceSettings.newHttpJsonBuilder() 59 .setTransportChannelProvider( 60 SearchServiceSettings.defaultHttpJsonTransportProviderBuilder() 61 .setHttpTransport(mockService) 62 .build()) 63 .setCredentialsProvider(NoCredentialsProvider.create()) 64 .build(); 65 client = SearchServiceClient.create(settings); 66 } 67 68 @AfterClass stopServer()69 public static void stopServer() { 70 client.close(); 71 } 72 73 @Before setUp()74 public void setUp() {} 75 76 @After tearDown()77 public void tearDown() throws Exception { 78 mockService.reset(); 79 } 80 81 @Test searchTest()82 public void searchTest() throws Exception { 83 SearchResponse.SearchResult responsesElement = SearchResponse.SearchResult.newBuilder().build(); 84 SearchResponse expectedResponse = 85 SearchResponse.newBuilder() 86 .setNextPageToken("") 87 .addAllResults(Arrays.asList(responsesElement)) 88 .build(); 89 mockService.addResponse(expectedResponse); 90 91 SearchRequest request = 92 SearchRequest.newBuilder() 93 .setServingConfig( 94 ServingConfigName.ofProjectLocationDataStoreServingConfigName( 95 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SERVING_CONFIG]") 96 .toString()) 97 .setBranch( 98 BranchName.ofProjectLocationDataStoreBranchName( 99 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]") 100 .toString()) 101 .setQuery("query107944136") 102 .setPageSize(883849137) 103 .setPageToken("pageToken873572522") 104 .setOffset(-1019779949) 105 .setFilter("filter-1274492040") 106 .setOrderBy("orderBy-1207110587") 107 .addAllFacetSpecs(new ArrayList<SearchRequest.FacetSpec>()) 108 .setBoostSpec(SearchRequest.BoostSpec.newBuilder().build()) 109 .putAllParams(new HashMap<String, Value>()) 110 .setQueryExpansionSpec(SearchRequest.QueryExpansionSpec.newBuilder().build()) 111 .setSpellCorrectionSpec(SearchRequest.SpellCorrectionSpec.newBuilder().build()) 112 .setUserPseudoId("userPseudoId-1155274652") 113 .setContentSearchSpec(SearchRequest.ContentSearchSpec.newBuilder().build()) 114 .build(); 115 116 SearchPagedResponse pagedListResponse = client.search(request); 117 118 List<SearchResponse.SearchResult> resources = 119 Lists.newArrayList(pagedListResponse.iterateAll()); 120 121 Assert.assertEquals(1, resources.size()); 122 Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0)); 123 124 List<String> actualRequests = mockService.getRequestPaths(); 125 Assert.assertEquals(1, actualRequests.size()); 126 127 String apiClientHeaderKey = 128 mockService 129 .getRequestHeaders() 130 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 131 .iterator() 132 .next(); 133 Assert.assertTrue( 134 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 135 .matcher(apiClientHeaderKey) 136 .matches()); 137 } 138 139 @Test searchExceptionTest()140 public void searchExceptionTest() throws Exception { 141 ApiException exception = 142 ApiExceptionFactory.createException( 143 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 144 mockService.addException(exception); 145 146 try { 147 SearchRequest request = 148 SearchRequest.newBuilder() 149 .setServingConfig( 150 ServingConfigName.ofProjectLocationDataStoreServingConfigName( 151 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SERVING_CONFIG]") 152 .toString()) 153 .setBranch( 154 BranchName.ofProjectLocationDataStoreBranchName( 155 "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[BRANCH]") 156 .toString()) 157 .setQuery("query107944136") 158 .setPageSize(883849137) 159 .setPageToken("pageToken873572522") 160 .setOffset(-1019779949) 161 .setFilter("filter-1274492040") 162 .setOrderBy("orderBy-1207110587") 163 .addAllFacetSpecs(new ArrayList<SearchRequest.FacetSpec>()) 164 .setBoostSpec(SearchRequest.BoostSpec.newBuilder().build()) 165 .putAllParams(new HashMap<String, Value>()) 166 .setQueryExpansionSpec(SearchRequest.QueryExpansionSpec.newBuilder().build()) 167 .setSpellCorrectionSpec(SearchRequest.SpellCorrectionSpec.newBuilder().build()) 168 .setUserPseudoId("userPseudoId-1155274652") 169 .setContentSearchSpec(SearchRequest.ContentSearchSpec.newBuilder().build()) 170 .build(); 171 client.search(request); 172 Assert.fail("No exception raised"); 173 } catch (InvalidArgumentException e) { 174 // Expected exception. 175 } 176 } 177 } 178