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.devtools.clouderrorreporting.v1beta1; 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 ReportErrorsServiceClientTest { 42 private static MockReportErrorsService mockReportErrorsService; 43 private static MockServiceHelper mockServiceHelper; 44 private LocalChannelProvider channelProvider; 45 private ReportErrorsServiceClient client; 46 47 @BeforeClass startStaticServer()48 public static void startStaticServer() { 49 mockReportErrorsService = new MockReportErrorsService(); 50 mockServiceHelper = 51 new MockServiceHelper( 52 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockReportErrorsService)); 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 ReportErrorsServiceSettings settings = 66 ReportErrorsServiceSettings.newBuilder() 67 .setTransportChannelProvider(channelProvider) 68 .setCredentialsProvider(NoCredentialsProvider.create()) 69 .build(); 70 client = ReportErrorsServiceClient.create(settings); 71 } 72 73 @After tearDown()74 public void tearDown() throws Exception { 75 client.close(); 76 } 77 78 @Test reportErrorEventTest()79 public void reportErrorEventTest() throws Exception { 80 ReportErrorEventResponse expectedResponse = ReportErrorEventResponse.newBuilder().build(); 81 mockReportErrorsService.addResponse(expectedResponse); 82 83 ProjectName projectName = ProjectName.of("[PROJECT]"); 84 ReportedErrorEvent event = ReportedErrorEvent.newBuilder().build(); 85 86 ReportErrorEventResponse actualResponse = client.reportErrorEvent(projectName, event); 87 Assert.assertEquals(expectedResponse, actualResponse); 88 89 List<AbstractMessage> actualRequests = mockReportErrorsService.getRequests(); 90 Assert.assertEquals(1, actualRequests.size()); 91 ReportErrorEventRequest actualRequest = ((ReportErrorEventRequest) actualRequests.get(0)); 92 93 Assert.assertEquals(projectName.toString(), actualRequest.getProjectName()); 94 Assert.assertEquals(event, actualRequest.getEvent()); 95 Assert.assertTrue( 96 channelProvider.isHeaderSent( 97 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 98 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 99 } 100 101 @Test reportErrorEventExceptionTest()102 public void reportErrorEventExceptionTest() throws Exception { 103 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 104 mockReportErrorsService.addException(exception); 105 106 try { 107 ProjectName projectName = ProjectName.of("[PROJECT]"); 108 ReportedErrorEvent event = ReportedErrorEvent.newBuilder().build(); 109 client.reportErrorEvent(projectName, event); 110 Assert.fail("No exception raised"); 111 } catch (InvalidArgumentException e) { 112 // Expected exception. 113 } 114 } 115 116 @Test reportErrorEventTest2()117 public void reportErrorEventTest2() throws Exception { 118 ReportErrorEventResponse expectedResponse = ReportErrorEventResponse.newBuilder().build(); 119 mockReportErrorsService.addResponse(expectedResponse); 120 121 String projectName = "projectName-940047036"; 122 ReportedErrorEvent event = ReportedErrorEvent.newBuilder().build(); 123 124 ReportErrorEventResponse actualResponse = client.reportErrorEvent(projectName, event); 125 Assert.assertEquals(expectedResponse, actualResponse); 126 127 List<AbstractMessage> actualRequests = mockReportErrorsService.getRequests(); 128 Assert.assertEquals(1, actualRequests.size()); 129 ReportErrorEventRequest actualRequest = ((ReportErrorEventRequest) actualRequests.get(0)); 130 131 Assert.assertEquals(projectName, actualRequest.getProjectName()); 132 Assert.assertEquals(event, actualRequest.getEvent()); 133 Assert.assertTrue( 134 channelProvider.isHeaderSent( 135 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 136 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 137 } 138 139 @Test reportErrorEventExceptionTest2()140 public void reportErrorEventExceptionTest2() throws Exception { 141 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 142 mockReportErrorsService.addException(exception); 143 144 try { 145 String projectName = "projectName-940047036"; 146 ReportedErrorEvent event = ReportedErrorEvent.newBuilder().build(); 147 client.reportErrorEvent(projectName, event); 148 Assert.fail("No exception raised"); 149 } catch (InvalidArgumentException e) { 150 // Expected exception. 151 } 152 } 153 } 154