• 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.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.HttpJsonRecommendationServiceStub;
29 import com.google.protobuf.Value;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import javax.annotation.Generated;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 
42 @Generated("by gapic-generator-java")
43 public class RecommendationServiceClientHttpJsonTest {
44   private static MockHttpService mockService;
45   private static RecommendationServiceClient client;
46 
47   @BeforeClass
startStaticServer()48   public static void startStaticServer() throws IOException {
49     mockService =
50         new MockHttpService(
51             HttpJsonRecommendationServiceStub.getMethodDescriptors(),
52             RecommendationServiceSettings.getDefaultEndpoint());
53     RecommendationServiceSettings settings =
54         RecommendationServiceSettings.newHttpJsonBuilder()
55             .setTransportChannelProvider(
56                 RecommendationServiceSettings.defaultHttpJsonTransportProviderBuilder()
57                     .setHttpTransport(mockService)
58                     .build())
59             .setCredentialsProvider(NoCredentialsProvider.create())
60             .build();
61     client = RecommendationServiceClient.create(settings);
62   }
63 
64   @AfterClass
stopServer()65   public static void stopServer() {
66     client.close();
67   }
68 
69   @Before
setUp()70   public void setUp() {}
71 
72   @After
tearDown()73   public void tearDown() throws Exception {
74     mockService.reset();
75   }
76 
77   @Test
recommendTest()78   public void recommendTest() throws Exception {
79     RecommendResponse expectedResponse =
80         RecommendResponse.newBuilder()
81             .addAllResults(new ArrayList<RecommendResponse.RecommendationResult>())
82             .setAttributionToken("attributionToken104706234")
83             .addAllMissingIds(new ArrayList<String>())
84             .setValidateOnly(true)
85             .build();
86     mockService.addResponse(expectedResponse);
87 
88     RecommendRequest request =
89         RecommendRequest.newBuilder()
90             .setServingConfig(
91                 ServingConfigName.ofProjectLocationDataStoreServingConfigName(
92                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SERVING_CONFIG]")
93                     .toString())
94             .setUserEvent(UserEvent.newBuilder().build())
95             .setPageSize(883849137)
96             .setFilter("filter-1274492040")
97             .setValidateOnly(true)
98             .putAllParams(new HashMap<String, Value>())
99             .putAllUserLabels(new HashMap<String, String>())
100             .build();
101 
102     RecommendResponse actualResponse = client.recommend(request);
103     Assert.assertEquals(expectedResponse, actualResponse);
104 
105     List<String> actualRequests = mockService.getRequestPaths();
106     Assert.assertEquals(1, actualRequests.size());
107 
108     String apiClientHeaderKey =
109         mockService
110             .getRequestHeaders()
111             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
112             .iterator()
113             .next();
114     Assert.assertTrue(
115         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
116             .matcher(apiClientHeaderKey)
117             .matches());
118   }
119 
120   @Test
recommendExceptionTest()121   public void recommendExceptionTest() throws Exception {
122     ApiException exception =
123         ApiExceptionFactory.createException(
124             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
125     mockService.addException(exception);
126 
127     try {
128       RecommendRequest request =
129           RecommendRequest.newBuilder()
130               .setServingConfig(
131                   ServingConfigName.ofProjectLocationDataStoreServingConfigName(
132                           "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SERVING_CONFIG]")
133                       .toString())
134               .setUserEvent(UserEvent.newBuilder().build())
135               .setPageSize(883849137)
136               .setFilter("filter-1274492040")
137               .setValidateOnly(true)
138               .putAllParams(new HashMap<String, Value>())
139               .putAllUserLabels(new HashMap<String, String>())
140               .build();
141       client.recommend(request);
142       Assert.fail("No exception raised");
143     } catch (InvalidArgumentException e) {
144       // Expected exception.
145     }
146   }
147 }
148