• 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.policytroubleshooter.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.cloud.policytroubleshooter.v1.stub.HttpJsonIamCheckerStub;
29 import google.cloud.policytroubleshooter.v1.Explanations;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
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 IamCheckerClientHttpJsonTest {
43   private static MockHttpService mockService;
44   private static IamCheckerClient client;
45 
46   @BeforeClass
startStaticServer()47   public static void startStaticServer() throws IOException {
48     mockService =
49         new MockHttpService(
50             HttpJsonIamCheckerStub.getMethodDescriptors(), IamCheckerSettings.getDefaultEndpoint());
51     IamCheckerSettings settings =
52         IamCheckerSettings.newHttpJsonBuilder()
53             .setTransportChannelProvider(
54                 IamCheckerSettings.defaultHttpJsonTransportProviderBuilder()
55                     .setHttpTransport(mockService)
56                     .build())
57             .setCredentialsProvider(NoCredentialsProvider.create())
58             .build();
59     client = IamCheckerClient.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
troubleshootIamPolicyTest()76   public void troubleshootIamPolicyTest() throws Exception {
77     TroubleshootIamPolicyResponse expectedResponse =
78         TroubleshootIamPolicyResponse.newBuilder()
79             .setAccess(Explanations.AccessState.forNumber(0))
80             .addAllExplainedPolicies(new ArrayList<Explanations.ExplainedPolicy>())
81             .build();
82     mockService.addResponse(expectedResponse);
83 
84     TroubleshootIamPolicyRequest request =
85         TroubleshootIamPolicyRequest.newBuilder()
86             .setAccessTuple(Explanations.AccessTuple.newBuilder().build())
87             .build();
88 
89     TroubleshootIamPolicyResponse actualResponse = client.troubleshootIamPolicy(request);
90     Assert.assertEquals(expectedResponse, actualResponse);
91 
92     List<String> actualRequests = mockService.getRequestPaths();
93     Assert.assertEquals(1, actualRequests.size());
94 
95     String apiClientHeaderKey =
96         mockService
97             .getRequestHeaders()
98             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
99             .iterator()
100             .next();
101     Assert.assertTrue(
102         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
103             .matcher(apiClientHeaderKey)
104             .matches());
105   }
106 
107   @Test
troubleshootIamPolicyExceptionTest()108   public void troubleshootIamPolicyExceptionTest() throws Exception {
109     ApiException exception =
110         ApiExceptionFactory.createException(
111             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
112     mockService.addException(exception);
113 
114     try {
115       TroubleshootIamPolicyRequest request =
116           TroubleshootIamPolicyRequest.newBuilder()
117               .setAccessTuple(Explanations.AccessTuple.newBuilder().build())
118               .build();
119       client.troubleshootIamPolicy(request);
120       Assert.fail("No exception raised");
121     } catch (InvalidArgumentException e) {
122       // Expected exception.
123     }
124   }
125 }
126