• 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.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.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.UUID;
33 import javax.annotation.Generated;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 
41 @Generated("by gapic-generator-java")
42 public class ErrorGroupServiceClientTest {
43   private static MockErrorGroupService mockErrorGroupService;
44   private static MockServiceHelper mockServiceHelper;
45   private LocalChannelProvider channelProvider;
46   private ErrorGroupServiceClient client;
47 
48   @BeforeClass
startStaticServer()49   public static void startStaticServer() {
50     mockErrorGroupService = new MockErrorGroupService();
51     mockServiceHelper =
52         new MockServiceHelper(
53             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockErrorGroupService));
54     mockServiceHelper.start();
55   }
56 
57   @AfterClass
stopServer()58   public static void stopServer() {
59     mockServiceHelper.stop();
60   }
61 
62   @Before
setUp()63   public void setUp() throws IOException {
64     mockServiceHelper.reset();
65     channelProvider = mockServiceHelper.createChannelProvider();
66     ErrorGroupServiceSettings settings =
67         ErrorGroupServiceSettings.newBuilder()
68             .setTransportChannelProvider(channelProvider)
69             .setCredentialsProvider(NoCredentialsProvider.create())
70             .build();
71     client = ErrorGroupServiceClient.create(settings);
72   }
73 
74   @After
tearDown()75   public void tearDown() throws Exception {
76     client.close();
77   }
78 
79   @Test
getGroupTest()80   public void getGroupTest() throws Exception {
81     ErrorGroup expectedResponse =
82         ErrorGroup.newBuilder()
83             .setName(ErrorGroupName.of("[PROJECT]", "[GROUP]").toString())
84             .setGroupId("groupId293428218")
85             .addAllTrackingIssues(new ArrayList<TrackingIssue>())
86             .setResolutionStatus(ResolutionStatus.forNumber(0))
87             .build();
88     mockErrorGroupService.addResponse(expectedResponse);
89 
90     ErrorGroupName groupName = ErrorGroupName.of("[PROJECT]", "[GROUP]");
91 
92     ErrorGroup actualResponse = client.getGroup(groupName);
93     Assert.assertEquals(expectedResponse, actualResponse);
94 
95     List<AbstractMessage> actualRequests = mockErrorGroupService.getRequests();
96     Assert.assertEquals(1, actualRequests.size());
97     GetGroupRequest actualRequest = ((GetGroupRequest) actualRequests.get(0));
98 
99     Assert.assertEquals(groupName.toString(), actualRequest.getGroupName());
100     Assert.assertTrue(
101         channelProvider.isHeaderSent(
102             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
103             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
104   }
105 
106   @Test
getGroupExceptionTest()107   public void getGroupExceptionTest() throws Exception {
108     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
109     mockErrorGroupService.addException(exception);
110 
111     try {
112       ErrorGroupName groupName = ErrorGroupName.of("[PROJECT]", "[GROUP]");
113       client.getGroup(groupName);
114       Assert.fail("No exception raised");
115     } catch (InvalidArgumentException e) {
116       // Expected exception.
117     }
118   }
119 
120   @Test
getGroupTest2()121   public void getGroupTest2() throws Exception {
122     ErrorGroup expectedResponse =
123         ErrorGroup.newBuilder()
124             .setName(ErrorGroupName.of("[PROJECT]", "[GROUP]").toString())
125             .setGroupId("groupId293428218")
126             .addAllTrackingIssues(new ArrayList<TrackingIssue>())
127             .setResolutionStatus(ResolutionStatus.forNumber(0))
128             .build();
129     mockErrorGroupService.addResponse(expectedResponse);
130 
131     String groupName = "groupName-1483174486";
132 
133     ErrorGroup actualResponse = client.getGroup(groupName);
134     Assert.assertEquals(expectedResponse, actualResponse);
135 
136     List<AbstractMessage> actualRequests = mockErrorGroupService.getRequests();
137     Assert.assertEquals(1, actualRequests.size());
138     GetGroupRequest actualRequest = ((GetGroupRequest) actualRequests.get(0));
139 
140     Assert.assertEquals(groupName, actualRequest.getGroupName());
141     Assert.assertTrue(
142         channelProvider.isHeaderSent(
143             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
144             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
145   }
146 
147   @Test
getGroupExceptionTest2()148   public void getGroupExceptionTest2() throws Exception {
149     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
150     mockErrorGroupService.addException(exception);
151 
152     try {
153       String groupName = "groupName-1483174486";
154       client.getGroup(groupName);
155       Assert.fail("No exception raised");
156     } catch (InvalidArgumentException e) {
157       // Expected exception.
158     }
159   }
160 
161   @Test
updateGroupTest()162   public void updateGroupTest() throws Exception {
163     ErrorGroup expectedResponse =
164         ErrorGroup.newBuilder()
165             .setName(ErrorGroupName.of("[PROJECT]", "[GROUP]").toString())
166             .setGroupId("groupId293428218")
167             .addAllTrackingIssues(new ArrayList<TrackingIssue>())
168             .setResolutionStatus(ResolutionStatus.forNumber(0))
169             .build();
170     mockErrorGroupService.addResponse(expectedResponse);
171 
172     ErrorGroup group = ErrorGroup.newBuilder().build();
173 
174     ErrorGroup actualResponse = client.updateGroup(group);
175     Assert.assertEquals(expectedResponse, actualResponse);
176 
177     List<AbstractMessage> actualRequests = mockErrorGroupService.getRequests();
178     Assert.assertEquals(1, actualRequests.size());
179     UpdateGroupRequest actualRequest = ((UpdateGroupRequest) actualRequests.get(0));
180 
181     Assert.assertEquals(group, actualRequest.getGroup());
182     Assert.assertTrue(
183         channelProvider.isHeaderSent(
184             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
185             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
186   }
187 
188   @Test
updateGroupExceptionTest()189   public void updateGroupExceptionTest() throws Exception {
190     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
191     mockErrorGroupService.addException(exception);
192 
193     try {
194       ErrorGroup group = ErrorGroup.newBuilder().build();
195       client.updateGroup(group);
196       Assert.fail("No exception raised");
197     } catch (InvalidArgumentException e) {
198       // Expected exception.
199     }
200   }
201 }
202