• 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.eventarc.publishing.v1;
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 com.google.protobuf.Any;
28 import io.grpc.StatusRuntimeException;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.UUID;
34 import javax.annotation.Generated;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 
42 @Generated("by gapic-generator-java")
43 public class PublisherClientTest {
44   private static MockPublisher mockPublisher;
45   private static MockServiceHelper mockServiceHelper;
46   private LocalChannelProvider channelProvider;
47   private PublisherClient client;
48 
49   @BeforeClass
startStaticServer()50   public static void startStaticServer() {
51     mockPublisher = new MockPublisher();
52     mockServiceHelper =
53         new MockServiceHelper(
54             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockPublisher));
55     mockServiceHelper.start();
56   }
57 
58   @AfterClass
stopServer()59   public static void stopServer() {
60     mockServiceHelper.stop();
61   }
62 
63   @Before
setUp()64   public void setUp() throws IOException {
65     mockServiceHelper.reset();
66     channelProvider = mockServiceHelper.createChannelProvider();
67     PublisherSettings settings =
68         PublisherSettings.newBuilder()
69             .setTransportChannelProvider(channelProvider)
70             .setCredentialsProvider(NoCredentialsProvider.create())
71             .build();
72     client = PublisherClient.create(settings);
73   }
74 
75   @After
tearDown()76   public void tearDown() throws Exception {
77     client.close();
78   }
79 
80   @Test
publishChannelConnectionEventsTest()81   public void publishChannelConnectionEventsTest() throws Exception {
82     PublishChannelConnectionEventsResponse expectedResponse =
83         PublishChannelConnectionEventsResponse.newBuilder().build();
84     mockPublisher.addResponse(expectedResponse);
85 
86     PublishChannelConnectionEventsRequest request =
87         PublishChannelConnectionEventsRequest.newBuilder()
88             .setChannelConnection("channelConnection-1932021695")
89             .addAllEvents(new ArrayList<Any>())
90             .addAllTextEvents(new ArrayList<String>())
91             .build();
92 
93     PublishChannelConnectionEventsResponse actualResponse =
94         client.publishChannelConnectionEvents(request);
95     Assert.assertEquals(expectedResponse, actualResponse);
96 
97     List<AbstractMessage> actualRequests = mockPublisher.getRequests();
98     Assert.assertEquals(1, actualRequests.size());
99     PublishChannelConnectionEventsRequest actualRequest =
100         ((PublishChannelConnectionEventsRequest) actualRequests.get(0));
101 
102     Assert.assertEquals(request.getChannelConnection(), actualRequest.getChannelConnection());
103     Assert.assertEquals(request.getEventsList(), actualRequest.getEventsList());
104     Assert.assertEquals(request.getTextEventsList(), actualRequest.getTextEventsList());
105     Assert.assertTrue(
106         channelProvider.isHeaderSent(
107             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
108             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
109   }
110 
111   @Test
publishChannelConnectionEventsExceptionTest()112   public void publishChannelConnectionEventsExceptionTest() throws Exception {
113     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
114     mockPublisher.addException(exception);
115 
116     try {
117       PublishChannelConnectionEventsRequest request =
118           PublishChannelConnectionEventsRequest.newBuilder()
119               .setChannelConnection("channelConnection-1932021695")
120               .addAllEvents(new ArrayList<Any>())
121               .addAllTextEvents(new ArrayList<String>())
122               .build();
123       client.publishChannelConnectionEvents(request);
124       Assert.fail("No exception raised");
125     } catch (InvalidArgumentException e) {
126       // Expected exception.
127     }
128   }
129 
130   @Test
publishEventsTest()131   public void publishEventsTest() throws Exception {
132     PublishEventsResponse expectedResponse = PublishEventsResponse.newBuilder().build();
133     mockPublisher.addResponse(expectedResponse);
134 
135     PublishEventsRequest request =
136         PublishEventsRequest.newBuilder()
137             .setChannel("channel738950403")
138             .addAllEvents(new ArrayList<Any>())
139             .addAllTextEvents(new ArrayList<String>())
140             .build();
141 
142     PublishEventsResponse actualResponse = client.publishEvents(request);
143     Assert.assertEquals(expectedResponse, actualResponse);
144 
145     List<AbstractMessage> actualRequests = mockPublisher.getRequests();
146     Assert.assertEquals(1, actualRequests.size());
147     PublishEventsRequest actualRequest = ((PublishEventsRequest) actualRequests.get(0));
148 
149     Assert.assertEquals(request.getChannel(), actualRequest.getChannel());
150     Assert.assertEquals(request.getEventsList(), actualRequest.getEventsList());
151     Assert.assertEquals(request.getTextEventsList(), actualRequest.getTextEventsList());
152     Assert.assertTrue(
153         channelProvider.isHeaderSent(
154             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
155             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
156   }
157 
158   @Test
publishEventsExceptionTest()159   public void publishEventsExceptionTest() throws Exception {
160     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
161     mockPublisher.addException(exception);
162 
163     try {
164       PublishEventsRequest request =
165           PublishEventsRequest.newBuilder()
166               .setChannel("channel738950403")
167               .addAllEvents(new ArrayList<Any>())
168               .addAllTextEvents(new ArrayList<String>())
169               .build();
170       client.publishEvents(request);
171       Assert.fail("No exception raised");
172     } catch (InvalidArgumentException e) {
173       // Expected exception.
174     }
175   }
176 }
177