• 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.retail.v2beta;
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.retail.v2beta.stub.HttpJsonPredictionServiceStub;
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 PredictionServiceClientHttpJsonTest {
44   private static MockHttpService mockService;
45   private static PredictionServiceClient client;
46 
47   @BeforeClass
startStaticServer()48   public static void startStaticServer() throws IOException {
49     mockService =
50         new MockHttpService(
51             HttpJsonPredictionServiceStub.getMethodDescriptors(),
52             PredictionServiceSettings.getDefaultEndpoint());
53     PredictionServiceSettings settings =
54         PredictionServiceSettings.newHttpJsonBuilder()
55             .setTransportChannelProvider(
56                 PredictionServiceSettings.defaultHttpJsonTransportProviderBuilder()
57                     .setHttpTransport(mockService)
58                     .build())
59             .setCredentialsProvider(NoCredentialsProvider.create())
60             .build();
61     client = PredictionServiceClient.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
predictTest()78   public void predictTest() throws Exception {
79     PredictResponse expectedResponse =
80         PredictResponse.newBuilder()
81             .addAllResults(new ArrayList<PredictResponse.PredictionResult>())
82             .setAttributionToken("attributionToken104706234")
83             .addAllMissingIds(new ArrayList<String>())
84             .setValidateOnly(true)
85             .build();
86     mockService.addResponse(expectedResponse);
87 
88     PredictRequest request =
89         PredictRequest.newBuilder()
90             .setPlacement(
91                 "projects/project-9512/locations/location-9512/catalogs/catalog-9512/placements/placement-9512")
92             .setUserEvent(UserEvent.newBuilder().build())
93             .setPageSize(883849137)
94             .setPageToken("pageToken873572522")
95             .setFilter("filter-1274492040")
96             .setValidateOnly(true)
97             .putAllParams(new HashMap<String, Value>())
98             .putAllLabels(new HashMap<String, String>())
99             .build();
100 
101     PredictResponse actualResponse = client.predict(request);
102     Assert.assertEquals(expectedResponse, actualResponse);
103 
104     List<String> actualRequests = mockService.getRequestPaths();
105     Assert.assertEquals(1, actualRequests.size());
106 
107     String apiClientHeaderKey =
108         mockService
109             .getRequestHeaders()
110             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
111             .iterator()
112             .next();
113     Assert.assertTrue(
114         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
115             .matcher(apiClientHeaderKey)
116             .matches());
117   }
118 
119   @Test
predictExceptionTest()120   public void predictExceptionTest() throws Exception {
121     ApiException exception =
122         ApiExceptionFactory.createException(
123             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
124     mockService.addException(exception);
125 
126     try {
127       PredictRequest request =
128           PredictRequest.newBuilder()
129               .setPlacement(
130                   "projects/project-9512/locations/location-9512/catalogs/catalog-9512/placements/placement-9512")
131               .setUserEvent(UserEvent.newBuilder().build())
132               .setPageSize(883849137)
133               .setPageToken("pageToken873572522")
134               .setFilter("filter-1274492040")
135               .setValidateOnly(true)
136               .putAllParams(new HashMap<String, Value>())
137               .putAllLabels(new HashMap<String, String>())
138               .build();
139       client.predict(request);
140       Assert.fail("No exception raised");
141     } catch (InvalidArgumentException e) {
142       // Expected exception.
143     }
144   }
145 }
146