|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2020 Michael Zhang <[email protected]> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 5 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 7 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 10 | + * Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 13 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 14 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 15 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +package net.devh.boot.grpc.test.advice; |
| 19 | + |
| 20 | +import static net.devh.boot.grpc.test.util.FutureAssertions.assertFutureThrows; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 24 | + |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import javax.annotation.PostConstruct; |
| 28 | + |
| 29 | +import com.google.protobuf.Empty; |
| 30 | + |
| 31 | +import io.grpc.Channel; |
| 32 | +import io.grpc.Metadata; |
| 33 | +import io.grpc.Status; |
| 34 | +import io.grpc.StatusRuntimeException; |
| 35 | +import io.grpc.internal.testing.StreamRecorder; |
| 36 | +import lombok.extern.slf4j.Slf4j; |
| 37 | +import net.devh.boot.grpc.client.inject.GrpcClient; |
| 38 | +import net.devh.boot.grpc.test.proto.SomeType; |
| 39 | +import net.devh.boot.grpc.test.proto.TestServiceGrpc; |
| 40 | +import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceBlockingStub; |
| 41 | +import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceFutureStub; |
| 42 | +import net.devh.boot.grpc.test.proto.TestServiceGrpc.TestServiceStub; |
| 43 | + |
| 44 | +/** |
| 45 | + * A test checking that the server and client can start and connect to each other with proper exception handling. |
| 46 | + * |
| 47 | + * @author Andjelko Perisic ([email protected]) |
| 48 | + */ |
| 49 | +@Slf4j |
| 50 | +abstract class AbstractSimpleServerClientTest { |
| 51 | + |
| 52 | + @GrpcClient("test") |
| 53 | + protected Channel channel; |
| 54 | + @GrpcClient("test") |
| 55 | + protected TestServiceStub testServiceStub; |
| 56 | + @GrpcClient("test") |
| 57 | + protected TestServiceBlockingStub testServiceBlockingStub; |
| 58 | + @GrpcClient("test") |
| 59 | + protected TestServiceFutureStub testServiceFutureStub; |
| 60 | + |
| 61 | + @PostConstruct |
| 62 | + protected void init() { |
| 63 | + // Test injection |
| 64 | + assertNotNull(this.channel, "channel"); |
| 65 | + assertNotNull(this.testServiceBlockingStub, "testServiceBlockingStub"); |
| 66 | + assertNotNull(this.testServiceFutureStub, "testServiceFutureStub"); |
| 67 | + assertNotNull(this.testServiceStub, "testServiceStub"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test template call to check for every exception. |
| 72 | + */ |
| 73 | + <E extends RuntimeException> void testGrpcCallAndVerifyMappedException(Status expectedStatus, Metadata metadata) { |
| 74 | + |
| 75 | + verifyManualBlockingStubCall(expectedStatus, metadata); |
| 76 | + verifyBlockingStubCall(expectedStatus, metadata); |
| 77 | + verifyManualFutureStubCall(expectedStatus, metadata); |
| 78 | + verifyFutureStubCall(expectedStatus, metadata); |
| 79 | + } |
| 80 | + |
| 81 | + private <E extends RuntimeException> void verifyManualBlockingStubCall( |
| 82 | + Status expectedStatus, Metadata expectedMetadata) { |
| 83 | + |
| 84 | + StatusRuntimeException actualException = |
| 85 | + assertThrows(StatusRuntimeException.class, |
| 86 | + () -> TestServiceGrpc.newBlockingStub(this.channel).normal(Empty.getDefaultInstance())); |
| 87 | + |
| 88 | + verifyStatusAndMetadata(actualException, expectedStatus, expectedMetadata); |
| 89 | + } |
| 90 | + |
| 91 | + private <E extends RuntimeException> void verifyBlockingStubCall(Status expectedStatus, Metadata expectedMetadata) { |
| 92 | + |
| 93 | + StatusRuntimeException actualException = |
| 94 | + assertThrows(StatusRuntimeException.class, |
| 95 | + () -> this.testServiceBlockingStub.normal(Empty.getDefaultInstance())); |
| 96 | + |
| 97 | + verifyStatusAndMetadata(actualException, expectedStatus, expectedMetadata); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + private <E extends RuntimeException> void verifyManualFutureStubCall( |
| 102 | + Status expectedStatus, Metadata expectedMetadata) { |
| 103 | + |
| 104 | + final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create(); |
| 105 | + this.testServiceStub.normal(Empty.getDefaultInstance(), streamRecorder); |
| 106 | + StatusRuntimeException actualException = |
| 107 | + assertFutureThrows(StatusRuntimeException.class, streamRecorder.firstValue(), 5, TimeUnit.SECONDS); |
| 108 | + |
| 109 | + verifyStatusAndMetadata(actualException, expectedStatus, expectedMetadata); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + private <E extends RuntimeException> void verifyFutureStubCall( |
| 114 | + Status expectedStatus, Metadata expectedMetadata) { |
| 115 | + |
| 116 | + StatusRuntimeException actualException = |
| 117 | + assertFutureThrows(StatusRuntimeException.class, |
| 118 | + this.testServiceFutureStub.normal(Empty.getDefaultInstance()), |
| 119 | + 5, |
| 120 | + TimeUnit.SECONDS); |
| 121 | + |
| 122 | + verifyStatusAndMetadata(actualException, expectedStatus, expectedMetadata); |
| 123 | + } |
| 124 | + |
| 125 | + private void verifyStatusAndMetadata( |
| 126 | + StatusRuntimeException actualException, Status expectedStatus, Metadata expectedMetadata) { |
| 127 | + |
| 128 | + assertThat(actualException.getTrailers()) |
| 129 | + .usingRecursiveComparison() |
| 130 | + .isEqualTo(expectedMetadata); |
| 131 | + assertThat(actualException.getStatus()) |
| 132 | + .usingRecursiveComparison() |
| 133 | + .isEqualTo(expectedStatus); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments