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.retail.v2beta; 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 PredictionServiceClientTest { 45 private static MockPredictionService mockPredictionService; 46 private static MockServiceHelper mockServiceHelper; 47 private LocalChannelProvider channelProvider; 48 private PredictionServiceClient client; 49 50 @BeforeClass startStaticServer()51 public static void startStaticServer() { 52 mockPredictionService = new MockPredictionService(); 53 mockServiceHelper = 54 new MockServiceHelper( 55 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockPredictionService)); 56 mockServiceHelper.start(); 57 } 58 59 @AfterClass stopServer()60 public static void stopServer() { 61 mockServiceHelper.stop(); 62 } 63 64 @Before setUp()65 public void setUp() throws IOException { 66 mockServiceHelper.reset(); 67 channelProvider = mockServiceHelper.createChannelProvider(); 68 PredictionServiceSettings settings = 69 PredictionServiceSettings.newBuilder() 70 .setTransportChannelProvider(channelProvider) 71 .setCredentialsProvider(NoCredentialsProvider.create()) 72 .build(); 73 client = PredictionServiceClient.create(settings); 74 } 75 76 @After tearDown()77 public void tearDown() throws Exception { 78 client.close(); 79 } 80 81 @Test predictTest()82 public void predictTest() throws Exception { 83 PredictResponse expectedResponse = 84 PredictResponse.newBuilder() 85 .addAllResults(new ArrayList<PredictResponse.PredictionResult>()) 86 .setAttributionToken("attributionToken104706234") 87 .addAllMissingIds(new ArrayList<String>()) 88 .setValidateOnly(true) 89 .build(); 90 mockPredictionService.addResponse(expectedResponse); 91 92 PredictRequest request = 93 PredictRequest.newBuilder() 94 .setPlacement("placement1792938725") 95 .setUserEvent(UserEvent.newBuilder().build()) 96 .setPageSize(883849137) 97 .setPageToken("pageToken873572522") 98 .setFilter("filter-1274492040") 99 .setValidateOnly(true) 100 .putAllParams(new HashMap<String, Value>()) 101 .putAllLabels(new HashMap<String, String>()) 102 .build(); 103 104 PredictResponse actualResponse = client.predict(request); 105 Assert.assertEquals(expectedResponse, actualResponse); 106 107 List<AbstractMessage> actualRequests = mockPredictionService.getRequests(); 108 Assert.assertEquals(1, actualRequests.size()); 109 PredictRequest actualRequest = ((PredictRequest) actualRequests.get(0)); 110 111 Assert.assertEquals(request.getPlacement(), actualRequest.getPlacement()); 112 Assert.assertEquals(request.getUserEvent(), actualRequest.getUserEvent()); 113 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 114 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 115 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 116 Assert.assertEquals(request.getValidateOnly(), actualRequest.getValidateOnly()); 117 Assert.assertEquals(request.getParamsMap(), actualRequest.getParamsMap()); 118 Assert.assertEquals(request.getLabelsMap(), actualRequest.getLabelsMap()); 119 Assert.assertTrue( 120 channelProvider.isHeaderSent( 121 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 122 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 123 } 124 125 @Test predictExceptionTest()126 public void predictExceptionTest() throws Exception { 127 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 128 mockPredictionService.addException(exception); 129 130 try { 131 PredictRequest request = 132 PredictRequest.newBuilder() 133 .setPlacement("placement1792938725") 134 .setUserEvent(UserEvent.newBuilder().build()) 135 .setPageSize(883849137) 136 .setPageToken("pageToken873572522") 137 .setFilter("filter-1274492040") 138 .setValidateOnly(true) 139 .putAllParams(new HashMap<String, Value>()) 140 .putAllLabels(new HashMap<String, String>()) 141 .build(); 142 client.predict(request); 143 Assert.fail("No exception raised"); 144 } catch (InvalidArgumentException e) { 145 // Expected exception. 146 } 147 } 148 } 149