• 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.grpc.GaxGrpcProperties;
21 import com.google.api.gax.grpc.testing.LocalChannelProvider;
22 import com.google.api.gax.grpc.testing.MockGrpcService;
23 import com.google.api.gax.grpc.testing.MockServiceHelper;
24 import com.google.api.gax.rpc.ApiClientHeaderProvider;
25 import com.google.api.gax.rpc.InvalidArgumentException;
26 import com.google.protobuf.AbstractMessage;
27 import com.google.protobuf.Value;
28 import io.grpc.StatusRuntimeException;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.UUID;
35 import javax.annotation.Generated;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 
43 @Generated("by gapic-generator-java")
44 public class RecommendationServiceClientTest {
45   private static MockRecommendationService mockRecommendationService;
46   private static MockServiceHelper mockServiceHelper;
47   private LocalChannelProvider channelProvider;
48   private RecommendationServiceClient client;
49 
50   @BeforeClass
startStaticServer()51   public static void startStaticServer() {
52     mockRecommendationService = new MockRecommendationService();
53     mockServiceHelper =
54         new MockServiceHelper(
55             UUID.randomUUID().toString(),
56             Arrays.<MockGrpcService>asList(mockRecommendationService));
57     mockServiceHelper.start();
58   }
59 
60   @AfterClass
stopServer()61   public static void stopServer() {
62     mockServiceHelper.stop();
63   }
64 
65   @Before
setUp()66   public void setUp() throws IOException {
67     mockServiceHelper.reset();
68     channelProvider = mockServiceHelper.createChannelProvider();
69     RecommendationServiceSettings settings =
70         RecommendationServiceSettings.newBuilder()
71             .setTransportChannelProvider(channelProvider)
72             .setCredentialsProvider(NoCredentialsProvider.create())
73             .build();
74     client = RecommendationServiceClient.create(settings);
75   }
76 
77   @After
tearDown()78   public void tearDown() throws Exception {
79     client.close();
80   }
81 
82   @Test
recommendTest()83   public void recommendTest() throws Exception {
84     RecommendResponse expectedResponse =
85         RecommendResponse.newBuilder()
86             .addAllResults(new ArrayList<RecommendResponse.RecommendationResult>())
87             .setAttributionToken("attributionToken104706234")
88             .addAllMissingIds(new ArrayList<String>())
89             .setValidateOnly(true)
90             .build();
91     mockRecommendationService.addResponse(expectedResponse);
92 
93     RecommendRequest request =
94         RecommendRequest.newBuilder()
95             .setServingConfig(
96                 ServingConfigName.ofProjectLocationDataStoreServingConfigName(
97                         "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SERVING_CONFIG]")
98                     .toString())
99             .setUserEvent(UserEvent.newBuilder().build())
100             .setPageSize(883849137)
101             .setFilter("filter-1274492040")
102             .setValidateOnly(true)
103             .putAllParams(new HashMap<String, Value>())
104             .putAllUserLabels(new HashMap<String, String>())
105             .build();
106 
107     RecommendResponse actualResponse = client.recommend(request);
108     Assert.assertEquals(expectedResponse, actualResponse);
109 
110     List<AbstractMessage> actualRequests = mockRecommendationService.getRequests();
111     Assert.assertEquals(1, actualRequests.size());
112     RecommendRequest actualRequest = ((RecommendRequest) actualRequests.get(0));
113 
114     Assert.assertEquals(request.getServingConfig(), actualRequest.getServingConfig());
115     Assert.assertEquals(request.getUserEvent(), actualRequest.getUserEvent());
116     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
117     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
118     Assert.assertEquals(request.getValidateOnly(), actualRequest.getValidateOnly());
119     Assert.assertEquals(request.getParamsMap(), actualRequest.getParamsMap());
120     Assert.assertEquals(request.getUserLabelsMap(), actualRequest.getUserLabelsMap());
121     Assert.assertTrue(
122         channelProvider.isHeaderSent(
123             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
124             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
125   }
126 
127   @Test
recommendExceptionTest()128   public void recommendExceptionTest() throws Exception {
129     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
130     mockRecommendationService.addException(exception);
131 
132     try {
133       RecommendRequest request =
134           RecommendRequest.newBuilder()
135               .setServingConfig(
136                   ServingConfigName.ofProjectLocationDataStoreServingConfigName(
137                           "[PROJECT]", "[LOCATION]", "[DATA_STORE]", "[SERVING_CONFIG]")
138                       .toString())
139               .setUserEvent(UserEvent.newBuilder().build())
140               .setPageSize(883849137)
141               .setFilter("filter-1274492040")
142               .setValidateOnly(true)
143               .putAllParams(new HashMap<String, Value>())
144               .putAllUserLabels(new HashMap<String, String>())
145               .build();
146       client.recommend(request);
147       Assert.fail("No exception raised");
148     } catch (InvalidArgumentException e) {
149       // Expected exception.
150     }
151   }
152 }
153