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.trace.v1; 18 19 import static com.google.cloud.trace.v1.TraceServiceClient.ListTracesPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.grpc.GaxGrpcProperties; 23 import com.google.api.gax.grpc.testing.LocalChannelProvider; 24 import com.google.api.gax.grpc.testing.MockGrpcService; 25 import com.google.api.gax.grpc.testing.MockServiceHelper; 26 import com.google.api.gax.rpc.ApiClientHeaderProvider; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.common.collect.Lists; 29 import com.google.devtools.cloudtrace.v1.GetTraceRequest; 30 import com.google.devtools.cloudtrace.v1.ListTracesRequest; 31 import com.google.devtools.cloudtrace.v1.ListTracesResponse; 32 import com.google.devtools.cloudtrace.v1.PatchTracesRequest; 33 import com.google.devtools.cloudtrace.v1.Trace; 34 import com.google.devtools.cloudtrace.v1.TraceSpan; 35 import com.google.devtools.cloudtrace.v1.Traces; 36 import com.google.protobuf.AbstractMessage; 37 import com.google.protobuf.Empty; 38 import io.grpc.StatusRuntimeException; 39 import java.io.IOException; 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.List; 43 import java.util.UUID; 44 import javax.annotation.Generated; 45 import org.junit.After; 46 import org.junit.AfterClass; 47 import org.junit.Assert; 48 import org.junit.Before; 49 import org.junit.BeforeClass; 50 import org.junit.Test; 51 52 @Generated("by gapic-generator-java") 53 public class TraceServiceClientTest { 54 private static MockServiceHelper mockServiceHelper; 55 private static MockTraceService mockTraceService; 56 private LocalChannelProvider channelProvider; 57 private TraceServiceClient client; 58 59 @BeforeClass startStaticServer()60 public static void startStaticServer() { 61 mockTraceService = new MockTraceService(); 62 mockServiceHelper = 63 new MockServiceHelper( 64 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockTraceService)); 65 mockServiceHelper.start(); 66 } 67 68 @AfterClass stopServer()69 public static void stopServer() { 70 mockServiceHelper.stop(); 71 } 72 73 @Before setUp()74 public void setUp() throws IOException { 75 mockServiceHelper.reset(); 76 channelProvider = mockServiceHelper.createChannelProvider(); 77 TraceServiceSettings settings = 78 TraceServiceSettings.newBuilder() 79 .setTransportChannelProvider(channelProvider) 80 .setCredentialsProvider(NoCredentialsProvider.create()) 81 .build(); 82 client = TraceServiceClient.create(settings); 83 } 84 85 @After tearDown()86 public void tearDown() throws Exception { 87 client.close(); 88 } 89 90 @Test listTracesTest()91 public void listTracesTest() throws Exception { 92 Trace responsesElement = Trace.newBuilder().build(); 93 ListTracesResponse expectedResponse = 94 ListTracesResponse.newBuilder() 95 .setNextPageToken("") 96 .addAllTraces(Arrays.asList(responsesElement)) 97 .build(); 98 mockTraceService.addResponse(expectedResponse); 99 100 String projectId = "projectId-894832108"; 101 102 ListTracesPagedResponse pagedListResponse = client.listTraces(projectId); 103 104 List<Trace> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 105 106 Assert.assertEquals(1, resources.size()); 107 Assert.assertEquals(expectedResponse.getTracesList().get(0), resources.get(0)); 108 109 List<AbstractMessage> actualRequests = mockTraceService.getRequests(); 110 Assert.assertEquals(1, actualRequests.size()); 111 ListTracesRequest actualRequest = ((ListTracesRequest) actualRequests.get(0)); 112 113 Assert.assertEquals(projectId, actualRequest.getProjectId()); 114 Assert.assertTrue( 115 channelProvider.isHeaderSent( 116 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 117 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 118 } 119 120 @Test listTracesExceptionTest()121 public void listTracesExceptionTest() throws Exception { 122 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 123 mockTraceService.addException(exception); 124 125 try { 126 String projectId = "projectId-894832108"; 127 client.listTraces(projectId); 128 Assert.fail("No exception raised"); 129 } catch (InvalidArgumentException e) { 130 // Expected exception. 131 } 132 } 133 134 @Test getTraceTest()135 public void getTraceTest() throws Exception { 136 Trace expectedResponse = 137 Trace.newBuilder() 138 .setProjectId("projectId-894832108") 139 .setTraceId("traceId-1067401920") 140 .addAllSpans(new ArrayList<TraceSpan>()) 141 .build(); 142 mockTraceService.addResponse(expectedResponse); 143 144 String projectId = "projectId-894832108"; 145 String traceId = "traceId-1067401920"; 146 147 Trace actualResponse = client.getTrace(projectId, traceId); 148 Assert.assertEquals(expectedResponse, actualResponse); 149 150 List<AbstractMessage> actualRequests = mockTraceService.getRequests(); 151 Assert.assertEquals(1, actualRequests.size()); 152 GetTraceRequest actualRequest = ((GetTraceRequest) actualRequests.get(0)); 153 154 Assert.assertEquals(projectId, actualRequest.getProjectId()); 155 Assert.assertEquals(traceId, actualRequest.getTraceId()); 156 Assert.assertTrue( 157 channelProvider.isHeaderSent( 158 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 159 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 160 } 161 162 @Test getTraceExceptionTest()163 public void getTraceExceptionTest() throws Exception { 164 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 165 mockTraceService.addException(exception); 166 167 try { 168 String projectId = "projectId-894832108"; 169 String traceId = "traceId-1067401920"; 170 client.getTrace(projectId, traceId); 171 Assert.fail("No exception raised"); 172 } catch (InvalidArgumentException e) { 173 // Expected exception. 174 } 175 } 176 177 @Test patchTracesTest()178 public void patchTracesTest() throws Exception { 179 Empty expectedResponse = Empty.newBuilder().build(); 180 mockTraceService.addResponse(expectedResponse); 181 182 String projectId = "projectId-894832108"; 183 Traces traces = Traces.newBuilder().build(); 184 185 client.patchTraces(projectId, traces); 186 187 List<AbstractMessage> actualRequests = mockTraceService.getRequests(); 188 Assert.assertEquals(1, actualRequests.size()); 189 PatchTracesRequest actualRequest = ((PatchTracesRequest) actualRequests.get(0)); 190 191 Assert.assertEquals(projectId, actualRequest.getProjectId()); 192 Assert.assertEquals(traces, actualRequest.getTraces()); 193 Assert.assertTrue( 194 channelProvider.isHeaderSent( 195 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 196 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 197 } 198 199 @Test patchTracesExceptionTest()200 public void patchTracesExceptionTest() throws Exception { 201 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 202 mockTraceService.addException(exception); 203 204 try { 205 String projectId = "projectId-894832108"; 206 Traces traces = Traces.newBuilder().build(); 207 client.patchTraces(projectId, traces); 208 Assert.fail("No exception raised"); 209 } catch (InvalidArgumentException e) { 210 // Expected exception. 211 } 212 } 213 } 214