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.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.recommendationengine.v1beta1.stub.HttpJsonPredictionServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.protobuf.Value; 33 import java.io.IOException; 34 import java.util.Collections; 35 import java.util.List; 36 import java.util.Map; 37 import javax.annotation.Generated; 38 import org.junit.After; 39 import org.junit.AfterClass; 40 import org.junit.Assert; 41 import org.junit.Before; 42 import org.junit.BeforeClass; 43 import org.junit.Test; 44 45 @Generated("by gapic-generator-java") 46 public class PredictionServiceClientHttpJsonTest { 47 private static MockHttpService mockService; 48 private static PredictionServiceClient client; 49 50 @BeforeClass startStaticServer()51 public static void startStaticServer() throws IOException { 52 mockService = 53 new MockHttpService( 54 HttpJsonPredictionServiceStub.getMethodDescriptors(), 55 PredictionServiceSettings.getDefaultEndpoint()); 56 PredictionServiceSettings settings = 57 PredictionServiceSettings.newHttpJsonBuilder() 58 .setTransportChannelProvider( 59 PredictionServiceSettings.defaultHttpJsonTransportProviderBuilder() 60 .setHttpTransport(mockService) 61 .build()) 62 .setCredentialsProvider(NoCredentialsProvider.create()) 63 .build(); 64 client = PredictionServiceClient.create(settings); 65 } 66 67 @AfterClass stopServer()68 public static void stopServer() { 69 client.close(); 70 } 71 72 @Before setUp()73 public void setUp() {} 74 75 @After tearDown()76 public void tearDown() throws Exception { 77 mockService.reset(); 78 } 79 80 @Test predictTest()81 public void predictTest() throws Exception { 82 Value responsesElement = Value.newBuilder().setBoolValue(true).build(); 83 PredictResponse expectedResponse = 84 PredictResponse.newBuilder() 85 .setNextPageToken("") 86 .putAllMetadata(Collections.singletonMap("metadata", responsesElement)) 87 .build(); 88 mockService.addResponse(expectedResponse); 89 90 PlacementName name = 91 PlacementName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]"); 92 UserEvent userEvent = UserEvent.newBuilder().build(); 93 94 PredictPagedResponse pagedListResponse = client.predict(name, userEvent); 95 96 List<Map.Entry<String, Value>> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 97 98 Assert.assertEquals(1, resources.size()); 99 Assert.assertEquals( 100 expectedResponse.getMetadataMap().entrySet().iterator().next(), resources.get(0)); 101 102 List<String> actualRequests = mockService.getRequestPaths(); 103 Assert.assertEquals(1, actualRequests.size()); 104 105 String apiClientHeaderKey = 106 mockService 107 .getRequestHeaders() 108 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 109 .iterator() 110 .next(); 111 Assert.assertTrue( 112 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 113 .matcher(apiClientHeaderKey) 114 .matches()); 115 } 116 117 @Test predictExceptionTest()118 public void predictExceptionTest() throws Exception { 119 ApiException exception = 120 ApiExceptionFactory.createException( 121 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 122 mockService.addException(exception); 123 124 try { 125 PlacementName name = 126 PlacementName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]"); 127 UserEvent userEvent = UserEvent.newBuilder().build(); 128 client.predict(name, userEvent); 129 Assert.fail("No exception raised"); 130 } catch (InvalidArgumentException e) { 131 // Expected exception. 132 } 133 } 134 135 @Test predictTest2()136 public void predictTest2() throws Exception { 137 Value responsesElement = Value.newBuilder().setBoolValue(true).build(); 138 PredictResponse expectedResponse = 139 PredictResponse.newBuilder() 140 .setNextPageToken("") 141 .putAllMetadata(Collections.singletonMap("metadata", responsesElement)) 142 .build(); 143 mockService.addResponse(expectedResponse); 144 145 String name = 146 "projects/project-9174/locations/location-9174/catalogs/catalog-9174/eventStores/eventStore-9174/placements/placement-9174"; 147 UserEvent userEvent = UserEvent.newBuilder().build(); 148 149 PredictPagedResponse pagedListResponse = client.predict(name, userEvent); 150 151 List<Map.Entry<String, Value>> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 152 153 Assert.assertEquals(1, resources.size()); 154 Assert.assertEquals( 155 expectedResponse.getMetadataMap().entrySet().iterator().next(), resources.get(0)); 156 157 List<String> actualRequests = mockService.getRequestPaths(); 158 Assert.assertEquals(1, actualRequests.size()); 159 160 String apiClientHeaderKey = 161 mockService 162 .getRequestHeaders() 163 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 164 .iterator() 165 .next(); 166 Assert.assertTrue( 167 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 168 .matcher(apiClientHeaderKey) 169 .matches()); 170 } 171 172 @Test predictExceptionTest2()173 public void predictExceptionTest2() throws Exception { 174 ApiException exception = 175 ApiExceptionFactory.createException( 176 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 177 mockService.addException(exception); 178 179 try { 180 String name = 181 "projects/project-9174/locations/location-9174/catalogs/catalog-9174/eventStores/eventStore-9174/placements/placement-9174"; 182 UserEvent userEvent = UserEvent.newBuilder().build(); 183 client.predict(name, userEvent); 184 Assert.fail("No exception raised"); 185 } catch (InvalidArgumentException e) { 186 // Expected exception. 187 } 188 } 189 } 190