• 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.trace.v2;
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.devtools.cloudtrace.v2.BatchWriteSpansRequest;
27 import com.google.devtools.cloudtrace.v2.ProjectName;
28 import com.google.devtools.cloudtrace.v2.Span;
29 import com.google.devtools.cloudtrace.v2.SpanName;
30 import com.google.devtools.cloudtrace.v2.StackTrace;
31 import com.google.devtools.cloudtrace.v2.TruncatableString;
32 import com.google.protobuf.AbstractMessage;
33 import com.google.protobuf.BoolValue;
34 import com.google.protobuf.Empty;
35 import com.google.protobuf.Int32Value;
36 import com.google.protobuf.Timestamp;
37 import com.google.rpc.Status;
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
batchWriteSpansTest()91   public void batchWriteSpansTest() throws Exception {
92     Empty expectedResponse = Empty.newBuilder().build();
93     mockTraceService.addResponse(expectedResponse);
94 
95     ProjectName name = ProjectName.of("[PROJECT]");
96     List<Span> spans = new ArrayList<>();
97 
98     client.batchWriteSpans(name, spans);
99 
100     List<AbstractMessage> actualRequests = mockTraceService.getRequests();
101     Assert.assertEquals(1, actualRequests.size());
102     BatchWriteSpansRequest actualRequest = ((BatchWriteSpansRequest) actualRequests.get(0));
103 
104     Assert.assertEquals(name.toString(), actualRequest.getName());
105     Assert.assertEquals(spans, actualRequest.getSpansList());
106     Assert.assertTrue(
107         channelProvider.isHeaderSent(
108             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
109             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
110   }
111 
112   @Test
batchWriteSpansExceptionTest()113   public void batchWriteSpansExceptionTest() throws Exception {
114     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
115     mockTraceService.addException(exception);
116 
117     try {
118       ProjectName name = ProjectName.of("[PROJECT]");
119       List<Span> spans = new ArrayList<>();
120       client.batchWriteSpans(name, spans);
121       Assert.fail("No exception raised");
122     } catch (InvalidArgumentException e) {
123       // Expected exception.
124     }
125   }
126 
127   @Test
batchWriteSpansTest2()128   public void batchWriteSpansTest2() throws Exception {
129     Empty expectedResponse = Empty.newBuilder().build();
130     mockTraceService.addResponse(expectedResponse);
131 
132     String name = "name3373707";
133     List<Span> spans = new ArrayList<>();
134 
135     client.batchWriteSpans(name, spans);
136 
137     List<AbstractMessage> actualRequests = mockTraceService.getRequests();
138     Assert.assertEquals(1, actualRequests.size());
139     BatchWriteSpansRequest actualRequest = ((BatchWriteSpansRequest) actualRequests.get(0));
140 
141     Assert.assertEquals(name, actualRequest.getName());
142     Assert.assertEquals(spans, actualRequest.getSpansList());
143     Assert.assertTrue(
144         channelProvider.isHeaderSent(
145             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
146             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
147   }
148 
149   @Test
batchWriteSpansExceptionTest2()150   public void batchWriteSpansExceptionTest2() throws Exception {
151     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
152     mockTraceService.addException(exception);
153 
154     try {
155       String name = "name3373707";
156       List<Span> spans = new ArrayList<>();
157       client.batchWriteSpans(name, spans);
158       Assert.fail("No exception raised");
159     } catch (InvalidArgumentException e) {
160       // Expected exception.
161     }
162   }
163 
164   @Test
createSpanTest()165   public void createSpanTest() throws Exception {
166     Span expectedResponse =
167         Span.newBuilder()
168             .setName(SpanName.of("[PROJECT]", "[TRACE]", "[SPAN]").toString())
169             .setSpanId("spanId-896182779")
170             .setParentSpanId("parentSpanId1059234639")
171             .setDisplayName(TruncatableString.newBuilder().build())
172             .setStartTime(Timestamp.newBuilder().build())
173             .setEndTime(Timestamp.newBuilder().build())
174             .setAttributes(Span.Attributes.newBuilder().build())
175             .setStackTrace(StackTrace.newBuilder().build())
176             .setTimeEvents(Span.TimeEvents.newBuilder().build())
177             .setLinks(Span.Links.newBuilder().build())
178             .setStatus(Status.newBuilder().build())
179             .setSameProcessAsParentSpan(BoolValue.newBuilder().build())
180             .setChildSpanCount(Int32Value.newBuilder().build())
181             .build();
182     mockTraceService.addResponse(expectedResponse);
183 
184     Span request =
185         Span.newBuilder()
186             .setName(SpanName.of("[PROJECT]", "[TRACE]", "[SPAN]").toString())
187             .setSpanId("spanId-896182779")
188             .setParentSpanId("parentSpanId1059234639")
189             .setDisplayName(TruncatableString.newBuilder().build())
190             .setStartTime(Timestamp.newBuilder().build())
191             .setEndTime(Timestamp.newBuilder().build())
192             .setAttributes(Span.Attributes.newBuilder().build())
193             .setStackTrace(StackTrace.newBuilder().build())
194             .setTimeEvents(Span.TimeEvents.newBuilder().build())
195             .setLinks(Span.Links.newBuilder().build())
196             .setStatus(Status.newBuilder().build())
197             .setSameProcessAsParentSpan(BoolValue.newBuilder().build())
198             .setChildSpanCount(Int32Value.newBuilder().build())
199             .build();
200 
201     Span actualResponse = client.createSpan(request);
202     Assert.assertEquals(expectedResponse, actualResponse);
203 
204     List<AbstractMessage> actualRequests = mockTraceService.getRequests();
205     Assert.assertEquals(1, actualRequests.size());
206     Span actualRequest = ((Span) actualRequests.get(0));
207 
208     Assert.assertEquals(request.getName(), actualRequest.getName());
209     Assert.assertEquals(request.getSpanId(), actualRequest.getSpanId());
210     Assert.assertEquals(request.getParentSpanId(), actualRequest.getParentSpanId());
211     Assert.assertEquals(request.getDisplayName(), actualRequest.getDisplayName());
212     Assert.assertEquals(request.getStartTime(), actualRequest.getStartTime());
213     Assert.assertEquals(request.getEndTime(), actualRequest.getEndTime());
214     Assert.assertEquals(request.getAttributes(), actualRequest.getAttributes());
215     Assert.assertEquals(request.getStackTrace(), actualRequest.getStackTrace());
216     Assert.assertEquals(request.getTimeEvents(), actualRequest.getTimeEvents());
217     Assert.assertEquals(request.getLinks(), actualRequest.getLinks());
218     Assert.assertEquals(request.getStatus(), actualRequest.getStatus());
219     Assert.assertEquals(
220         request.getSameProcessAsParentSpan(), actualRequest.getSameProcessAsParentSpan());
221     Assert.assertEquals(request.getChildSpanCount(), actualRequest.getChildSpanCount());
222     Assert.assertEquals(request.getSpanKind(), actualRequest.getSpanKind());
223     Assert.assertTrue(
224         channelProvider.isHeaderSent(
225             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
226             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
227   }
228 
229   @Test
createSpanExceptionTest()230   public void createSpanExceptionTest() throws Exception {
231     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
232     mockTraceService.addException(exception);
233 
234     try {
235       Span request =
236           Span.newBuilder()
237               .setName(SpanName.of("[PROJECT]", "[TRACE]", "[SPAN]").toString())
238               .setSpanId("spanId-896182779")
239               .setParentSpanId("parentSpanId1059234639")
240               .setDisplayName(TruncatableString.newBuilder().build())
241               .setStartTime(Timestamp.newBuilder().build())
242               .setEndTime(Timestamp.newBuilder().build())
243               .setAttributes(Span.Attributes.newBuilder().build())
244               .setStackTrace(StackTrace.newBuilder().build())
245               .setTimeEvents(Span.TimeEvents.newBuilder().build())
246               .setLinks(Span.Links.newBuilder().build())
247               .setStatus(Status.newBuilder().build())
248               .setSameProcessAsParentSpan(BoolValue.newBuilder().build())
249               .setChildSpanCount(Int32Value.newBuilder().build())
250               .build();
251       client.createSpan(request);
252       Assert.fail("No exception raised");
253     } catch (InvalidArgumentException e) {
254       // Expected exception.
255     }
256   }
257 }
258