|
| 1 | +/** |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * <p>Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file |
| 7 | + * except in compliance with the License. A copy of the License is located at |
| 8 | + * |
| 9 | + * <p>http://aws.amazon.com/apache2.0 |
| 10 | + * |
| 11 | + * <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | + * specific language governing permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package com.uber.cadence.serviceclient; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +import static org.junit.Assert.assertThrows; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import com.uber.cadence.*; |
| 22 | +import com.uber.tchannel.api.ResponseCode; |
| 23 | +import com.uber.tchannel.api.SubChannel; |
| 24 | +import com.uber.tchannel.api.TFuture; |
| 25 | +import com.uber.tchannel.api.errors.TChannelError; |
| 26 | +import com.uber.tchannel.headers.ArgScheme; |
| 27 | +import com.uber.tchannel.messages.ThriftRequest; |
| 28 | +import com.uber.tchannel.messages.ThriftResponse; |
| 29 | +import java.util.Arrays; |
| 30 | +import org.apache.thrift.TException; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.experimental.runners.Enclosed; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.Parameterized; |
| 36 | + |
| 37 | +@RunWith(Enclosed.class) |
| 38 | +public class WorkflowServiceTChannelTest { |
| 39 | + @FunctionalInterface |
| 40 | + private interface Method<T> { |
| 41 | + T get() throws TException; |
| 42 | + } |
| 43 | + |
| 44 | + private static class BaseTest<T, R> { |
| 45 | + SubChannel mockSubChannel; |
| 46 | + WorkflowServiceTChannel service; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() { |
| 50 | + mockSubChannel = mock(SubChannel.class); |
| 51 | + service = new WorkflowServiceTChannel(mockSubChannel, ClientOptions.newBuilder().build()); |
| 52 | + } |
| 53 | + |
| 54 | + @Parameterized.Parameter(0) |
| 55 | + public ResponseCode responseCode; |
| 56 | + |
| 57 | + @Parameterized.Parameter(1) |
| 58 | + public T responseBody; |
| 59 | + |
| 60 | + @Parameterized.Parameter(2) |
| 61 | + public Class<? extends TException> expectedException; |
| 62 | + |
| 63 | + @Parameterized.Parameter(3) |
| 64 | + public R expectedResponse; |
| 65 | + |
| 66 | + TFuture<ThriftResponse<T>> mockResponse(ResponseCode responseCode, T body) { |
| 67 | + // TFuture |
| 68 | + TFuture<ThriftResponse<T>> tFuture = TFuture.create(ArgScheme.THRIFT, null); |
| 69 | + tFuture.set( |
| 70 | + new ThriftResponse.Builder<T>(new ThriftRequest.Builder<>("", "").build()) |
| 71 | + .setResponseCode(responseCode) |
| 72 | + .setBody(body) |
| 73 | + .build()); |
| 74 | + return tFuture; |
| 75 | + } |
| 76 | + |
| 77 | + void testHelper(Method<R> method) throws TException, TChannelError { |
| 78 | + when(mockSubChannel.send(any(ThriftRequest.class))) |
| 79 | + .thenReturn(mockResponse(responseCode, responseBody)); |
| 80 | + if (expectedException != null) { |
| 81 | + assertThrows(expectedException, method::get); |
| 82 | + } else { |
| 83 | + assertEquals(expectedResponse, method.get()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @RunWith(Parameterized.class) |
| 89 | + public static class RegisterDomainTest |
| 90 | + extends BaseTest<WorkflowService.RegisterDomain_result, Void> { |
| 91 | + @Parameterized.Parameters(name = "{index}: Response Code {0}, Response {1}") |
| 92 | + public static Iterable<Object[]> data() { |
| 93 | + return Arrays.asList( |
| 94 | + new Object[][] { |
| 95 | + {ResponseCode.OK, new WorkflowService.RegisterDomain_result(), null, null}, |
| 96 | + { |
| 97 | + ResponseCode.Error, |
| 98 | + new WorkflowService.RegisterDomain_result() |
| 99 | + .setBadRequestError(new BadRequestError("")), |
| 100 | + BadRequestError.class, |
| 101 | + null, |
| 102 | + }, |
| 103 | + { |
| 104 | + ResponseCode.Error, |
| 105 | + new WorkflowService.RegisterDomain_result() |
| 106 | + .setDomainExistsError(new DomainAlreadyExistsError("")), |
| 107 | + DomainAlreadyExistsError.class, |
| 108 | + null |
| 109 | + }, |
| 110 | + { |
| 111 | + ResponseCode.Error, |
| 112 | + new WorkflowService.RegisterDomain_result() |
| 113 | + .setServiceBusyError(new ServiceBusyError("")), |
| 114 | + ServiceBusyError.class, |
| 115 | + null |
| 116 | + }, |
| 117 | + { |
| 118 | + ResponseCode.Error, |
| 119 | + new WorkflowService.RegisterDomain_result(), |
| 120 | + TException.class, |
| 121 | + null |
| 122 | + }, |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testResponse() throws Exception { |
| 128 | + testHelper( |
| 129 | + () -> { |
| 130 | + service.RegisterDomain(new RegisterDomainRequest()); |
| 131 | + return null; |
| 132 | + }); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments