• 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.compute.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.compute.v1.stub.HttpJsonLicenseCodesStub;
29 import java.io.IOException;
30 import java.util.ArrayList;
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 LicenseCodesClientTest {
42   private static MockHttpService mockService;
43   private static LicenseCodesClient client;
44 
45   @BeforeClass
startStaticServer()46   public static void startStaticServer() throws IOException {
47     mockService =
48         new MockHttpService(
49             HttpJsonLicenseCodesStub.getMethodDescriptors(),
50             LicenseCodesSettings.getDefaultEndpoint());
51     LicenseCodesSettings settings =
52         LicenseCodesSettings.newBuilder()
53             .setTransportChannelProvider(
54                 LicenseCodesSettings.defaultHttpJsonTransportProviderBuilder()
55                     .setHttpTransport(mockService)
56                     .build())
57             .setCredentialsProvider(NoCredentialsProvider.create())
58             .build();
59     client = LicenseCodesClient.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
getTest()76   public void getTest() throws Exception {
77     LicenseCode expectedResponse =
78         LicenseCode.newBuilder()
79             .setCreationTimestamp("creationTimestamp-370203401")
80             .setDescription("description-1724546052")
81             .setId(3355)
82             .setKind("kind3292052")
83             .addAllLicenseAlias(new ArrayList<LicenseCodeLicenseAlias>())
84             .setName("name3373707")
85             .setSelfLink("selfLink1191800166")
86             .setState("state109757585")
87             .setTransferable(true)
88             .build();
89     mockService.addResponse(expectedResponse);
90 
91     String project = "project-6911";
92     String licenseCode = "licenseCode-8220";
93 
94     LicenseCode actualResponse = client.get(project, licenseCode);
95     Assert.assertEquals(expectedResponse, actualResponse);
96 
97     List<String> actualRequests = mockService.getRequestPaths();
98     Assert.assertEquals(1, actualRequests.size());
99 
100     String apiClientHeaderKey =
101         mockService
102             .getRequestHeaders()
103             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
104             .iterator()
105             .next();
106     Assert.assertTrue(
107         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
108             .matcher(apiClientHeaderKey)
109             .matches());
110   }
111 
112   @Test
getExceptionTest()113   public void getExceptionTest() throws Exception {
114     ApiException exception =
115         ApiExceptionFactory.createException(
116             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
117     mockService.addException(exception);
118 
119     try {
120       String project = "project-6911";
121       String licenseCode = "licenseCode-8220";
122       client.get(project, licenseCode);
123       Assert.fail("No exception raised");
124     } catch (InvalidArgumentException e) {
125       // Expected exception.
126     }
127   }
128 
129   @Test
testIamPermissionsTest()130   public void testIamPermissionsTest() throws Exception {
131     TestPermissionsResponse expectedResponse =
132         TestPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build();
133     mockService.addResponse(expectedResponse);
134 
135     String project = "project-6911";
136     String resource = "resource-756";
137     TestPermissionsRequest testPermissionsRequestResource =
138         TestPermissionsRequest.newBuilder().build();
139 
140     TestPermissionsResponse actualResponse =
141         client.testIamPermissions(project, resource, testPermissionsRequestResource);
142     Assert.assertEquals(expectedResponse, actualResponse);
143 
144     List<String> actualRequests = mockService.getRequestPaths();
145     Assert.assertEquals(1, actualRequests.size());
146 
147     String apiClientHeaderKey =
148         mockService
149             .getRequestHeaders()
150             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
151             .iterator()
152             .next();
153     Assert.assertTrue(
154         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
155             .matcher(apiClientHeaderKey)
156             .matches());
157   }
158 
159   @Test
testIamPermissionsExceptionTest()160   public void testIamPermissionsExceptionTest() throws Exception {
161     ApiException exception =
162         ApiExceptionFactory.createException(
163             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
164     mockService.addException(exception);
165 
166     try {
167       String project = "project-6911";
168       String resource = "resource-756";
169       TestPermissionsRequest testPermissionsRequestResource =
170           TestPermissionsRequest.newBuilder().build();
171       client.testIamPermissions(project, resource, testPermissionsRequestResource);
172       Assert.fail("No exception raised");
173     } catch (InvalidArgumentException e) {
174       // Expected exception.
175     }
176   }
177 }
178