• 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.httpjson.GaxHttpJsonProperties;
21 import com.google.api.gax.httpjson.testing.MockHttpService;
22 import com.google.api.gax.rpc.ApiClientHeaderProvider;
23 import com.google.api.gax.rpc.ApiException;
24 import com.google.api.gax.rpc.ApiExceptionFactory;
25 import com.google.api.gax.rpc.InvalidArgumentException;
26 import com.google.api.gax.rpc.StatusCode;
27 import com.google.api.gax.rpc.testing.FakeStatusCode;
28 import com.google.cloud.eventarc.publishing.v1.stub.HttpJsonPublisherStub;
29 import com.google.protobuf.Any;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
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 PublisherClientHttpJsonTest {
43   private static MockHttpService mockService;
44   private static PublisherClient client;
45 
46   @BeforeClass
startStaticServer()47   public static void startStaticServer() throws IOException {
48     mockService =
49         new MockHttpService(
50             HttpJsonPublisherStub.getMethodDescriptors(), PublisherSettings.getDefaultEndpoint());
51     PublisherSettings settings =
52         PublisherSettings.newHttpJsonBuilder()
53             .setTransportChannelProvider(
54                 PublisherSettings.defaultHttpJsonTransportProviderBuilder()
55                     .setHttpTransport(mockService)
56                     .build())
57             .setCredentialsProvider(NoCredentialsProvider.create())
58             .build();
59     client = PublisherClient.create(settings);
60   }
61 
62   @AfterClass
stopServer()63   public static void stopServer() {
64     client.close();
65   }
66 
67   @Before
setUp()68   public void setUp() {}
69 
70   @After
tearDown()71   public void tearDown() throws Exception {
72     mockService.reset();
73   }
74 
75   @Test
publishChannelConnectionEventsTest()76   public void publishChannelConnectionEventsTest() throws Exception {
77     PublishChannelConnectionEventsResponse expectedResponse =
78         PublishChannelConnectionEventsResponse.newBuilder().build();
79     mockService.addResponse(expectedResponse);
80 
81     PublishChannelConnectionEventsRequest request =
82         PublishChannelConnectionEventsRequest.newBuilder()
83             .setChannelConnection(
84                 "projects/project-748/locations/location-748/channelConnections/channelConnection-748")
85             .addAllEvents(new ArrayList<Any>())
86             .addAllTextEvents(new ArrayList<String>())
87             .build();
88 
89     PublishChannelConnectionEventsResponse actualResponse =
90         client.publishChannelConnectionEvents(request);
91     Assert.assertEquals(expectedResponse, actualResponse);
92 
93     List<String> actualRequests = mockService.getRequestPaths();
94     Assert.assertEquals(1, actualRequests.size());
95 
96     String apiClientHeaderKey =
97         mockService
98             .getRequestHeaders()
99             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
100             .iterator()
101             .next();
102     Assert.assertTrue(
103         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
104             .matcher(apiClientHeaderKey)
105             .matches());
106   }
107 
108   @Test
publishChannelConnectionEventsExceptionTest()109   public void publishChannelConnectionEventsExceptionTest() throws Exception {
110     ApiException exception =
111         ApiExceptionFactory.createException(
112             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
113     mockService.addException(exception);
114 
115     try {
116       PublishChannelConnectionEventsRequest request =
117           PublishChannelConnectionEventsRequest.newBuilder()
118               .setChannelConnection(
119                   "projects/project-748/locations/location-748/channelConnections/channelConnection-748")
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     mockService.addResponse(expectedResponse);
134 
135     PublishEventsRequest request =
136         PublishEventsRequest.newBuilder()
137             .setChannel("projects/project-2616/locations/location-2616/channels/channel-2616")
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<String> actualRequests = mockService.getRequestPaths();
146     Assert.assertEquals(1, actualRequests.size());
147 
148     String apiClientHeaderKey =
149         mockService
150             .getRequestHeaders()
151             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
152             .iterator()
153             .next();
154     Assert.assertTrue(
155         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
156             .matcher(apiClientHeaderKey)
157             .matches());
158   }
159 
160   @Test
publishEventsExceptionTest()161   public void publishEventsExceptionTest() throws Exception {
162     ApiException exception =
163         ApiExceptionFactory.createException(
164             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
165     mockService.addException(exception);
166 
167     try {
168       PublishEventsRequest request =
169           PublishEventsRequest.newBuilder()
170               .setChannel("projects/project-2616/locations/location-2616/channels/channel-2616")
171               .addAllEvents(new ArrayList<Any>())
172               .addAllTextEvents(new ArrayList<String>())
173               .build();
174       client.publishEvents(request);
175       Assert.fail("No exception raised");
176     } catch (InvalidArgumentException e) {
177       // Expected exception.
178     }
179   }
180 }
181