• 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.servicedirectory.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 io.grpc.StatusRuntimeException;
28 import java.io.IOException;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.UUID;
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 LookupServiceClientTest {
42   private static MockLookupService mockLookupService;
43   private static MockServiceHelper mockServiceHelper;
44   private LocalChannelProvider channelProvider;
45   private LookupServiceClient client;
46 
47   @BeforeClass
startStaticServer()48   public static void startStaticServer() {
49     mockLookupService = new MockLookupService();
50     mockServiceHelper =
51         new MockServiceHelper(
52             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockLookupService));
53     mockServiceHelper.start();
54   }
55 
56   @AfterClass
stopServer()57   public static void stopServer() {
58     mockServiceHelper.stop();
59   }
60 
61   @Before
setUp()62   public void setUp() throws IOException {
63     mockServiceHelper.reset();
64     channelProvider = mockServiceHelper.createChannelProvider();
65     LookupServiceSettings settings =
66         LookupServiceSettings.newBuilder()
67             .setTransportChannelProvider(channelProvider)
68             .setCredentialsProvider(NoCredentialsProvider.create())
69             .build();
70     client = LookupServiceClient.create(settings);
71   }
72 
73   @After
tearDown()74   public void tearDown() throws Exception {
75     client.close();
76   }
77 
78   @Test
resolveServiceTest()79   public void resolveServiceTest() throws Exception {
80     ResolveServiceResponse expectedResponse =
81         ResolveServiceResponse.newBuilder().setService(Service.newBuilder().build()).build();
82     mockLookupService.addResponse(expectedResponse);
83 
84     ResolveServiceRequest request =
85         ResolveServiceRequest.newBuilder()
86             .setName(
87                 ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
88             .setMaxEndpoints(2074789987)
89             .setEndpointFilter("endpointFilter-1834249875")
90             .build();
91 
92     ResolveServiceResponse actualResponse = client.resolveService(request);
93     Assert.assertEquals(expectedResponse, actualResponse);
94 
95     List<AbstractMessage> actualRequests = mockLookupService.getRequests();
96     Assert.assertEquals(1, actualRequests.size());
97     ResolveServiceRequest actualRequest = ((ResolveServiceRequest) actualRequests.get(0));
98 
99     Assert.assertEquals(request.getName(), actualRequest.getName());
100     Assert.assertEquals(request.getMaxEndpoints(), actualRequest.getMaxEndpoints());
101     Assert.assertEquals(request.getEndpointFilter(), actualRequest.getEndpointFilter());
102     Assert.assertTrue(
103         channelProvider.isHeaderSent(
104             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
105             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
106   }
107 
108   @Test
resolveServiceExceptionTest()109   public void resolveServiceExceptionTest() throws Exception {
110     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
111     mockLookupService.addException(exception);
112 
113     try {
114       ResolveServiceRequest request =
115           ResolveServiceRequest.newBuilder()
116               .setName(
117                   ServiceName.of("[PROJECT]", "[LOCATION]", "[NAMESPACE]", "[SERVICE]").toString())
118               .setMaxEndpoints(2074789987)
119               .setEndpointFilter("endpointFilter-1834249875")
120               .build();
121       client.resolveService(request);
122       Assert.fail("No exception raised");
123     } catch (InvalidArgumentException e) {
124       // Expected exception.
125     }
126   }
127 }
128