Skip to content

Commit

Permalink
test: cleaning up deprecated usages
Browse files Browse the repository at this point in the history
removed deprecated `ExpectedException.none()` usage across unit test cases.

chore: address feedback comments
  • Loading branch information
rahulKQL committed Mar 31, 2020
1 parent a676891 commit bb4a7b7
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,26 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Rule;
import org.junit.Assert;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class GrpcCallContextTest {
@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void testNullToSelfWrongType() {
thrown.expect(IllegalArgumentException.class);
GrpcCallContext.createDefault().nullToSelf(FakeCallContext.createDefault());
try {
GrpcCallContext.createDefault().nullToSelf(FakeCallContext.createDefault());
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of GrpcCallContext");
}
}

@Test
Expand All @@ -83,15 +87,25 @@ public void testWithTransportChannel() {

@Test
public void testWithTransportChannelWrongType() {
thrown.expect(IllegalArgumentException.class);
FakeChannel channel = new FakeChannel();
GrpcCallContext.createDefault().withTransportChannel(FakeTransportChannel.create(channel));
try {
GrpcCallContext.createDefault().withTransportChannel(FakeTransportChannel.create(channel));
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Expected GrpcTransportChannel");
}
}

@Test
public void testMergeWrongType() {
thrown.expect(IllegalArgumentException.class);
GrpcCallContext.createDefault().merge(FakeCallContext.createDefault());
try {
GrpcCallContext.createDefault().merge(FakeCallContext.createDefault());
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of " + "GrpcCallContext");
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -79,8 +78,6 @@ public class GrpcDirectServerStreamingCallableTest {
private ServerStreamingCallSettings<Color, Money> streamingCallSettings;
private ServerStreamingCallable<Color, Money> streamingCallable;

@Rule public ExpectedException thrown = ExpectedException.none();

@Before
public void setUp() throws InstantiationException, IllegalAccessException, IOException {
String serverName = "fakeservice";
Expand Down Expand Up @@ -121,9 +118,14 @@ public void testBadContext() {

CountDownLatch latch = new CountDownLatch(1);
MoneyObserver observer = new MoneyObserver(true, latch);

thrown.expect(IllegalArgumentException.class);
streamingCallable.call(DEFAULT_REQUEST, observer);
try {
streamingCallable.call(DEFAULT_REQUEST, observer);
Assert.fail("Callable should have thrown an exception");
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of GrpcCallContext");
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -68,8 +66,6 @@ public class GrpcDirectStreamingCallableTest {
private FakeServiceImpl serviceImpl;
private ClientContext clientContext;

@Rule public ExpectedException thrown = ExpectedException.none();

@Before
public void setUp() throws InstantiationException, IllegalAccessException, IOException {
String serverName = "fakeservice";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -59,8 +57,6 @@ public class GrpcTransportDescriptorTest {
private static boolean NOT_RETRYABLE = false;
private static boolean IS_RETRYABLE = true;

@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void translateException_StatusException_noRetry() throws Exception {
Throwable originalException = new StatusException(Status.INVALID_ARGUMENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,26 @@
*/
package com.google.api.gax.grpc;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.grpc.ProtoOperationTransformers.MetadataTransformer;
import com.google.api.gax.grpc.ProtoOperationTransformers.ResponseTransformer;
import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.UnavailableException;
import com.google.common.truth.Truth;
import com.google.longrunning.Operation;
import com.google.protobuf.Any;
import com.google.rpc.Status;
import com.google.type.Color;
import com.google.type.Money;
import io.grpc.Status.Code;
import org.junit.Rule;
import org.junit.Assert;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ProtoOperationTransformersTest {
@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void testResponseTransformer() {
Expand All @@ -58,35 +57,43 @@ public void testResponseTransformer() {
OperationSnapshot operationSnapshot =
GrpcOperationSnapshot.create(
Operation.newBuilder().setResponse(Any.pack(inputMoney)).build());
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
}

@Test
public void testResponseTransformer_exception() {
thrown.expect(UnavailableException.class);
ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
Status status = Status.newBuilder().setCode(Code.UNAVAILABLE.value()).build();
OperationSnapshot operationSnapshot =
GrpcOperationSnapshot.create(
Operation.newBuilder().setResponse(Any.pack(inputMoney)).setError(status).build());
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (UnavailableException expected) {
assertThat(expected)
.hasMessageThat()
.contains("failed with status = GrpcStatusCode{transportCode=UNAVAILABLE}");
}
}

@Test
public void testResponseTransformer_mismatchedTypes() {
thrown.expect(ApiException.class);
thrown.expectMessage("Failed to unpack object");
ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
Status status = Status.newBuilder().setCode(Code.OK.value()).build();
OperationSnapshot operationSnapshot =
GrpcOperationSnapshot.create(
Operation.newBuilder()
.setResponse(Any.pack(Color.getDefaultInstance()))
.setError(status)
.build());
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (ApiException expected) {
assertThat(expected).hasMessageThat().contains("Failed to unpack object");
}
}

@Test
Expand All @@ -96,22 +103,24 @@ public void testMetadataTransformer() {
OperationSnapshot operationSnapshot =
GrpcOperationSnapshot.create(
Operation.newBuilder().setMetadata(Any.pack(inputMoney)).build());
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
}

@Test
public void testMetadataTransformer_mismatchedTypes() {
thrown.expect(ApiException.class);
thrown.expectMessage("Failed to unpack object");
MetadataTransformer<Money> transformer = MetadataTransformer.create(Money.class);
Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
Status status = Status.newBuilder().setCode(Code.OK.value()).build();
OperationSnapshot operationSnapshot =
GrpcOperationSnapshot.create(
Operation.newBuilder()
.setMetadata(Any.pack(Color.getDefaultInstance()))
.setError(status)
.build());
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
try {
transformer.apply(operationSnapshot);
Assert.fail("MetadataTransformer should have thrown an exception");
} catch (ApiException expected) {
assertThat(expected).hasMessageThat().contains("Failed to unpack object");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@
import com.google.common.truth.Truth;
import java.io.IOException;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
Expand All @@ -68,8 +66,6 @@
@RunWith(JUnit4.class)
public class SettingsTest {

@Rule public ExpectedException thrown = ExpectedException.none();

interface FakePagedListResponse extends PagedListResponse<Integer> {}

private static class FakeStubSettings extends StubSettings<FakeStubSettings> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
package com.google.api.gax.httpjson;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.httpjson.ApiMessageOperationTransformers.MetadataTransformer;
import com.google.api.gax.httpjson.ApiMessageOperationTransformers.ResponseTransformer;
import com.google.api.gax.httpjson.testing.FakeApiMessage;
Expand All @@ -38,18 +40,15 @@
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.api.gax.rpc.UnavailableException;
import com.google.common.collect.ImmutableMap;
import com.google.common.truth.Truth;
import java.util.List;
import org.junit.Rule;
import org.junit.Assert;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for ApiMessageOperationTransformers. */
@RunWith(JUnit4.class)
public class ApiMessageOperationTransformersTest {
@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void testResponseTransformer() {
Expand All @@ -61,35 +60,40 @@ public void testResponseTransformer() {
new OperationSnapshotImpl(
new FakeOperationMessage<>("Pending; no response method", emptyResponse, metadata));

Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse);
assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse);
}

@Test
public void testResponseTransformer_exception() {
thrown.expect(UnavailableException.class);
ResponseTransformer<EmptyMessage> transformer = ResponseTransformer.create(EmptyMessage.class);
EmptyMessage emptyResponse = EmptyMessage.getDefaultInstance();
FakeMetadataMessage metadata = new FakeMetadataMessage(Status.PENDING, Code.UNAVAILABLE);
OperationSnapshot operationSnapshot =
new OperationSnapshotImpl(
new FakeOperationMessage<>("Unavailable; no response method", emptyResponse, metadata));

Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse);
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (UnavailableException expected) {
assertThat(expected).hasMessageThat().contains("Unavailable; no response method");
}
}

@Test
public void testResponseTransformer_mismatchedTypes() {
thrown.expect(ApiException.class);
thrown.expectMessage("cannot be cast");
ResponseTransformer<EmptyMessage> transformer = ResponseTransformer.create(EmptyMessage.class);
FakeMetadataMessage metadata = new FakeMetadataMessage(Status.PENDING, Code.OK);
ApiMessage bananaResponse =
new FakeApiMessage(ImmutableMap.<String, Object>of("name", "banana"), null, null);
EmptyMessage emptyResponse = EmptyMessage.getDefaultInstance();
OperationSnapshot operationSnapshot =
new OperationSnapshotImpl(
new FakeOperationMessage<>("No response method", bananaResponse, metadata));
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(emptyResponse);
try {
transformer.apply(operationSnapshot);
Assert.fail("ResponseTransformer should have thrown an exception");
} catch (ApiException expected) {
assertThat(expected).hasMessageThat().contains("cannot be cast");
}
}

@Test
Expand All @@ -100,13 +104,11 @@ public void testMetadataTransformer() {
FakeMetadataMessage metadataMessage = new FakeMetadataMessage(Status.PENDING, Code.OK);
FakeOperationMessage operation = new FakeOperationMessage<>("foo", returnType, metadataMessage);
OperationSnapshot operationSnapshot = new OperationSnapshotImpl(operation);
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(metadataMessage);
assertThat(transformer.apply(operationSnapshot)).isEqualTo(metadataMessage);
}

@Test
public void testMetadataTransformer_mismatchedTypes() {
thrown.expect(ApiException.class);
thrown.expectMessage("cannot be cast");
MetadataTransformer<FakeOperationMessage> transformer =
MetadataTransformer.create(FakeOperationMessage.class);
FakeMetadataMessage metadataMessage = new FakeMetadataMessage(Status.PENDING, Code.OK);
Expand All @@ -115,7 +117,12 @@ public void testMetadataTransformer_mismatchedTypes() {
FakeOperationMessage metadata =
new FakeOperationMessage<>("No response method", bananaResponse, metadataMessage);
OperationSnapshot operationSnapshot = new OperationSnapshotImpl(metadata);
Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(bananaResponse);
try {
transformer.apply(operationSnapshot);
Assert.fail("MetadataTransformer should have thrown an exception");
} catch (ApiException expected) {
assertThat(expected).hasMessageThat().contains("cannot be cast");
}
}

private enum Status {
Expand Down
Loading

0 comments on commit bb4a7b7

Please sign in to comment.