• 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.recommendationengine.v1beta1;
18 
19 import static com.google.cloud.recommendationengine.v1beta1.PredictionServiceClient.PredictPagedResponse;
20 
21 import com.google.api.gax.core.NoCredentialsProvider;
22 import com.google.api.gax.grpc.GaxGrpcProperties;
23 import com.google.api.gax.grpc.testing.LocalChannelProvider;
24 import com.google.api.gax.grpc.testing.MockGrpcService;
25 import com.google.api.gax.grpc.testing.MockServiceHelper;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.common.collect.Lists;
29 import com.google.protobuf.AbstractMessage;
30 import com.google.protobuf.Value;
31 import io.grpc.StatusRuntimeException;
32 import java.io.IOException;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.UUID;
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 PredictionServiceClientTest {
48   private static MockPredictionService mockPredictionService;
49   private static MockServiceHelper mockServiceHelper;
50   private LocalChannelProvider channelProvider;
51   private PredictionServiceClient client;
52 
53   @BeforeClass
startStaticServer()54   public static void startStaticServer() {
55     mockPredictionService = new MockPredictionService();
56     mockServiceHelper =
57         new MockServiceHelper(
58             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockPredictionService));
59     mockServiceHelper.start();
60   }
61 
62   @AfterClass
stopServer()63   public static void stopServer() {
64     mockServiceHelper.stop();
65   }
66 
67   @Before
setUp()68   public void setUp() throws IOException {
69     mockServiceHelper.reset();
70     channelProvider = mockServiceHelper.createChannelProvider();
71     PredictionServiceSettings settings =
72         PredictionServiceSettings.newBuilder()
73             .setTransportChannelProvider(channelProvider)
74             .setCredentialsProvider(NoCredentialsProvider.create())
75             .build();
76     client = PredictionServiceClient.create(settings);
77   }
78 
79   @After
tearDown()80   public void tearDown() throws Exception {
81     client.close();
82   }
83 
84   @Test
predictTest()85   public void predictTest() throws Exception {
86     Value responsesElement = Value.newBuilder().setBoolValue(true).build();
87     PredictResponse expectedResponse =
88         PredictResponse.newBuilder()
89             .setNextPageToken("")
90             .putAllMetadata(Collections.singletonMap("metadata", responsesElement))
91             .build();
92     mockPredictionService.addResponse(expectedResponse);
93 
94     PlacementName name =
95         PlacementName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]");
96     UserEvent userEvent = UserEvent.newBuilder().build();
97 
98     PredictPagedResponse pagedListResponse = client.predict(name, userEvent);
99 
100     List<Map.Entry<String, Value>> resources = Lists.newArrayList(pagedListResponse.iterateAll());
101 
102     Assert.assertEquals(1, resources.size());
103     Assert.assertEquals(
104         expectedResponse.getMetadataMap().entrySet().iterator().next(), resources.get(0));
105 
106     List<AbstractMessage> actualRequests = mockPredictionService.getRequests();
107     Assert.assertEquals(1, actualRequests.size());
108     PredictRequest actualRequest = ((PredictRequest) actualRequests.get(0));
109 
110     Assert.assertEquals(name.toString(), actualRequest.getName());
111     Assert.assertEquals(userEvent, actualRequest.getUserEvent());
112     Assert.assertTrue(
113         channelProvider.isHeaderSent(
114             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
115             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
116   }
117 
118   @Test
predictExceptionTest()119   public void predictExceptionTest() throws Exception {
120     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
121     mockPredictionService.addException(exception);
122 
123     try {
124       PlacementName name =
125           PlacementName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]");
126       UserEvent userEvent = UserEvent.newBuilder().build();
127       client.predict(name, userEvent);
128       Assert.fail("No exception raised");
129     } catch (InvalidArgumentException e) {
130       // Expected exception.
131     }
132   }
133 
134   @Test
predictTest2()135   public void predictTest2() throws Exception {
136     Value responsesElement = Value.newBuilder().setBoolValue(true).build();
137     PredictResponse expectedResponse =
138         PredictResponse.newBuilder()
139             .setNextPageToken("")
140             .putAllMetadata(Collections.singletonMap("metadata", responsesElement))
141             .build();
142     mockPredictionService.addResponse(expectedResponse);
143 
144     String name = "name3373707";
145     UserEvent userEvent = UserEvent.newBuilder().build();
146 
147     PredictPagedResponse pagedListResponse = client.predict(name, userEvent);
148 
149     List<Map.Entry<String, Value>> resources = Lists.newArrayList(pagedListResponse.iterateAll());
150 
151     Assert.assertEquals(1, resources.size());
152     Assert.assertEquals(
153         expectedResponse.getMetadataMap().entrySet().iterator().next(), resources.get(0));
154 
155     List<AbstractMessage> actualRequests = mockPredictionService.getRequests();
156     Assert.assertEquals(1, actualRequests.size());
157     PredictRequest actualRequest = ((PredictRequest) actualRequests.get(0));
158 
159     Assert.assertEquals(name, actualRequest.getName());
160     Assert.assertEquals(userEvent, actualRequest.getUserEvent());
161     Assert.assertTrue(
162         channelProvider.isHeaderSent(
163             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
164             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
165   }
166 
167   @Test
predictExceptionTest2()168   public void predictExceptionTest2() throws Exception {
169     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
170     mockPredictionService.addException(exception);
171 
172     try {
173       String name = "name3373707";
174       UserEvent userEvent = UserEvent.newBuilder().build();
175       client.predict(name, userEvent);
176       Assert.fail("No exception raised");
177     } catch (InvalidArgumentException e) {
178       // Expected exception.
179     }
180   }
181 }
182