• 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.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.type.PostalAddress;
28 import io.grpc.StatusRuntimeException;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.UUID;
33 import javax.annotation.Generated;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 
41 @Generated("by gapic-generator-java")
42 public class AddressValidationClientTest {
43   private static MockAddressValidation mockAddressValidation;
44   private static MockServiceHelper mockServiceHelper;
45   private LocalChannelProvider channelProvider;
46   private AddressValidationClient client;
47 
48   @BeforeClass
startStaticServer()49   public static void startStaticServer() {
50     mockAddressValidation = new MockAddressValidation();
51     mockServiceHelper =
52         new MockServiceHelper(
53             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockAddressValidation));
54     mockServiceHelper.start();
55   }
56 
57   @AfterClass
stopServer()58   public static void stopServer() {
59     mockServiceHelper.stop();
60   }
61 
62   @Before
setUp()63   public void setUp() throws IOException {
64     mockServiceHelper.reset();
65     channelProvider = mockServiceHelper.createChannelProvider();
66     AddressValidationSettings settings =
67         AddressValidationSettings.newBuilder()
68             .setTransportChannelProvider(channelProvider)
69             .setCredentialsProvider(NoCredentialsProvider.create())
70             .build();
71     client = AddressValidationClient.create(settings);
72   }
73 
74   @After
tearDown()75   public void tearDown() throws Exception {
76     client.close();
77   }
78 
79   @Test
validateAddressTest()80   public void validateAddressTest() throws Exception {
81     ValidateAddressResponse expectedResponse =
82         ValidateAddressResponse.newBuilder()
83             .setResult(ValidationResult.newBuilder().build())
84             .setResponseId("responseId-633138884")
85             .build();
86     mockAddressValidation.addResponse(expectedResponse);
87 
88     ValidateAddressRequest request =
89         ValidateAddressRequest.newBuilder()
90             .setAddress(PostalAddress.newBuilder().build())
91             .setPreviousResponseId("previousResponseId468754323")
92             .setEnableUspsCass(true)
93             .build();
94 
95     ValidateAddressResponse actualResponse = client.validateAddress(request);
96     Assert.assertEquals(expectedResponse, actualResponse);
97 
98     List<AbstractMessage> actualRequests = mockAddressValidation.getRequests();
99     Assert.assertEquals(1, actualRequests.size());
100     ValidateAddressRequest actualRequest = ((ValidateAddressRequest) actualRequests.get(0));
101 
102     Assert.assertEquals(request.getAddress(), actualRequest.getAddress());
103     Assert.assertEquals(request.getPreviousResponseId(), actualRequest.getPreviousResponseId());
104     Assert.assertEquals(request.getEnableUspsCass(), actualRequest.getEnableUspsCass());
105     Assert.assertTrue(
106         channelProvider.isHeaderSent(
107             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
108             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
109   }
110 
111   @Test
validateAddressExceptionTest()112   public void validateAddressExceptionTest() throws Exception {
113     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
114     mockAddressValidation.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     mockAddressValidation.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<AbstractMessage> actualRequests = mockAddressValidation.getRequests();
143     Assert.assertEquals(1, actualRequests.size());
144     ProvideValidationFeedbackRequest actualRequest =
145         ((ProvideValidationFeedbackRequest) actualRequests.get(0));
146 
147     Assert.assertEquals(request.getConclusion(), actualRequest.getConclusion());
148     Assert.assertEquals(request.getResponseId(), actualRequest.getResponseId());
149     Assert.assertTrue(
150         channelProvider.isHeaderSent(
151             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
152             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
153   }
154 
155   @Test
provideValidationFeedbackExceptionTest()156   public void provideValidationFeedbackExceptionTest() throws Exception {
157     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
158     mockAddressValidation.addException(exception);
159 
160     try {
161       ProvideValidationFeedbackRequest request =
162           ProvideValidationFeedbackRequest.newBuilder()
163               .setResponseId("responseId-633138884")
164               .build();
165       client.provideValidationFeedback(request);
166       Assert.fail("No exception raised");
167     } catch (InvalidArgumentException e) {
168       // Expected exception.
169     }
170   }
171 }
172