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.dataflow.v1beta3; 18 19 import static com.google.dataflow.v1beta3.MessagesV1Beta3Client.ListJobMessagesPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 23 import com.google.api.gax.httpjson.testing.MockHttpService; 24 import com.google.api.gax.rpc.ApiClientHeaderProvider; 25 import com.google.api.gax.rpc.ApiException; 26 import com.google.api.gax.rpc.ApiExceptionFactory; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.api.gax.rpc.testing.FakeStatusCode; 30 import com.google.common.collect.Lists; 31 import com.google.dataflow.v1beta3.stub.HttpJsonMessagesV1Beta3Stub; 32 import com.google.protobuf.Timestamp; 33 import java.io.IOException; 34 import java.util.Arrays; 35 import java.util.List; 36 import javax.annotation.Generated; 37 import org.junit.After; 38 import org.junit.AfterClass; 39 import org.junit.Assert; 40 import org.junit.Before; 41 import org.junit.BeforeClass; 42 import org.junit.Test; 43 44 @Generated("by gapic-generator-java") 45 public class MessagesV1Beta3ClientHttpJsonTest { 46 private static MockHttpService mockService; 47 private static MessagesV1Beta3Client client; 48 49 @BeforeClass startStaticServer()50 public static void startStaticServer() throws IOException { 51 mockService = 52 new MockHttpService( 53 HttpJsonMessagesV1Beta3Stub.getMethodDescriptors(), 54 MessagesV1Beta3Settings.getDefaultEndpoint()); 55 MessagesV1Beta3Settings settings = 56 MessagesV1Beta3Settings.newHttpJsonBuilder() 57 .setTransportChannelProvider( 58 MessagesV1Beta3Settings.defaultHttpJsonTransportProviderBuilder() 59 .setHttpTransport(mockService) 60 .build()) 61 .setCredentialsProvider(NoCredentialsProvider.create()) 62 .build(); 63 client = MessagesV1Beta3Client.create(settings); 64 } 65 66 @AfterClass stopServer()67 public static void stopServer() { 68 client.close(); 69 } 70 71 @Before setUp()72 public void setUp() {} 73 74 @After tearDown()75 public void tearDown() throws Exception { 76 mockService.reset(); 77 } 78 79 @Test listJobMessagesTest()80 public void listJobMessagesTest() throws Exception { 81 JobMessage responsesElement = JobMessage.newBuilder().build(); 82 ListJobMessagesResponse expectedResponse = 83 ListJobMessagesResponse.newBuilder() 84 .setNextPageToken("") 85 .addAllJobMessages(Arrays.asList(responsesElement)) 86 .build(); 87 mockService.addResponse(expectedResponse); 88 89 ListJobMessagesRequest request = 90 ListJobMessagesRequest.newBuilder() 91 .setProjectId("projectId-1530") 92 .setJobId("jobId-3646") 93 .setMinimumImportance(JobMessageImportance.forNumber(0)) 94 .setPageSize(883849137) 95 .setPageToken("pageToken873572522") 96 .setStartTime(Timestamp.newBuilder().build()) 97 .setEndTime(Timestamp.newBuilder().build()) 98 .setLocation("location-9355") 99 .build(); 100 101 ListJobMessagesPagedResponse pagedListResponse = client.listJobMessages(request); 102 103 List<JobMessage> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 104 105 Assert.assertEquals(1, resources.size()); 106 Assert.assertEquals(expectedResponse.getJobMessagesList().get(0), resources.get(0)); 107 108 List<String> actualRequests = mockService.getRequestPaths(); 109 Assert.assertEquals(1, actualRequests.size()); 110 111 String apiClientHeaderKey = 112 mockService 113 .getRequestHeaders() 114 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 115 .iterator() 116 .next(); 117 Assert.assertTrue( 118 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 119 .matcher(apiClientHeaderKey) 120 .matches()); 121 } 122 123 @Test listJobMessagesExceptionTest()124 public void listJobMessagesExceptionTest() throws Exception { 125 ApiException exception = 126 ApiExceptionFactory.createException( 127 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 128 mockService.addException(exception); 129 130 try { 131 ListJobMessagesRequest request = 132 ListJobMessagesRequest.newBuilder() 133 .setProjectId("projectId-1530") 134 .setJobId("jobId-3646") 135 .setMinimumImportance(JobMessageImportance.forNumber(0)) 136 .setPageSize(883849137) 137 .setPageToken("pageToken873572522") 138 .setStartTime(Timestamp.newBuilder().build()) 139 .setEndTime(Timestamp.newBuilder().build()) 140 .setLocation("location-9355") 141 .build(); 142 client.listJobMessages(request); 143 Assert.fail("No exception raised"); 144 } catch (InvalidArgumentException e) { 145 // Expected exception. 146 } 147 } 148 } 149