• 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.maps.addressvalidation.v1;
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.maps.addressvalidation.v1.stub.HttpJsonAddressValidationStub;
29 import com.google.type.PostalAddress;
30 import java.io.IOException;
31 import java.util.List;
32 import javax.annotation.Generated;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 
40 @Generated("by gapic-generator-java")
41 public class AddressValidationClientHttpJsonTest {
42   private static MockHttpService mockService;
43   private static AddressValidationClient client;
44 
45   @BeforeClass
startStaticServer()46   public static void startStaticServer() throws IOException {
47     mockService =
48         new MockHttpService(
49             HttpJsonAddressValidationStub.getMethodDescriptors(),
50             AddressValidationSettings.getDefaultEndpoint());
51     AddressValidationSettings settings =
52         AddressValidationSettings.newHttpJsonBuilder()
53             .setTransportChannelProvider(
54                 AddressValidationSettings.defaultHttpJsonTransportProviderBuilder()
55                     .setHttpTransport(mockService)
56                     .build())
57             .setCredentialsProvider(NoCredentialsProvider.create())
58             .build();
59     client = AddressValidationClient.create(settings);
60   }
61 
62   @AfterClass
stopServer()63   public static void stopServer() {
64     client.close();
65   }
66 
67   @Before
setUp()68   public void setUp() {}
69 
70   @After
tearDown()71   public void tearDown() throws Exception {
72     mockService.reset();
73   }
74 
75   @Test
validateAddressTest()76   public void validateAddressTest() throws Exception {
77     ValidateAddressResponse expectedResponse =
78         ValidateAddressResponse.newBuilder()
79             .setResult(ValidationResult.newBuilder().build())
80             .setResponseId("responseId-633138884")
81             .build();
82     mockService.addResponse(expectedResponse);
83 
84     ValidateAddressRequest request =
85         ValidateAddressRequest.newBuilder()
86             .setAddress(PostalAddress.newBuilder().build())
87             .setPreviousResponseId("previousResponseId468754323")
88             .setEnableUspsCass(true)
89             .build();
90 
91     ValidateAddressResponse actualResponse = client.validateAddress(request);
92     Assert.assertEquals(expectedResponse, actualResponse);
93 
94     List<String> actualRequests = mockService.getRequestPaths();
95     Assert.assertEquals(1, actualRequests.size());
96 
97     String apiClientHeaderKey =
98         mockService
99             .getRequestHeaders()
100             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
101             .iterator()
102             .next();
103     Assert.assertTrue(
104         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
105             .matcher(apiClientHeaderKey)
106             .matches());
107   }
108 
109   @Test
validateAddressExceptionTest()110   public void validateAddressExceptionTest() throws Exception {
111     ApiException exception =
112         ApiExceptionFactory.createException(
113             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
114     mockService.addException(exception);
115 
116     try {
117       ValidateAddressRequest request =
118           ValidateAddressRequest.newBuilder()
119               .setAddress(PostalAddress.newBuilder().build())
120               .setPreviousResponseId("previousResponseId468754323")
121               .setEnableUspsCass(true)
122               .build();
123       client.validateAddress(request);
124       Assert.fail("No exception raised");
125     } catch (InvalidArgumentException e) {
126       // Expected exception.
127     }
128   }
129 
130   @Test
provideValidationFeedbackTest()131   public void provideValidationFeedbackTest() throws Exception {
132     ProvideValidationFeedbackResponse expectedResponse =
133         ProvideValidationFeedbackResponse.newBuilder().build();
134     mockService.addResponse(expectedResponse);
135 
136     ProvideValidationFeedbackRequest request =
137         ProvideValidationFeedbackRequest.newBuilder().setResponseId("responseId-633138884").build();
138 
139     ProvideValidationFeedbackResponse actualResponse = client.provideValidationFeedback(request);
140     Assert.assertEquals(expectedResponse, actualResponse);
141 
142     List<String> actualRequests = mockService.getRequestPaths();
143     Assert.assertEquals(1, actualRequests.size());
144 
145     String apiClientHeaderKey =
146         mockService
147             .getRequestHeaders()
148             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
149             .iterator()
150             .next();
151     Assert.assertTrue(
152         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
153             .matcher(apiClientHeaderKey)
154             .matches());
155   }
156 
157   @Test
provideValidationFeedbackExceptionTest()158   public void provideValidationFeedbackExceptionTest() throws Exception {
159     ApiException exception =
160         ApiExceptionFactory.createException(
161             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
162     mockService.addException(exception);
163 
164     try {
165       ProvideValidationFeedbackRequest request =
166           ProvideValidationFeedbackRequest.newBuilder()
167               .setResponseId("responseId-633138884")
168               .build();
169       client.provideValidationFeedback(request);
170       Assert.fail("No exception raised");
171     } catch (InvalidArgumentException e) {
172       // Expected exception.
173     }
174   }
175 }
176