|
| 1 | +using DtmCommon; |
| 2 | +using Grpc.Core; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Dtmgrpc.Tests |
| 8 | +{ |
| 9 | + public class UtilsTests |
| 10 | + { |
| 11 | + private static DtmFailureException DtmFailure = new DtmFailureException(); |
| 12 | + private static DtmOngingException DtmOnging = new DtmOngingException(); |
| 13 | + |
| 14 | + [Fact] |
| 15 | + public void DtmError2GrpcError_Should_Throw_Aborted_RpcException() |
| 16 | + { |
| 17 | + var ex = Assert.Throws<RpcException>(()=> DtmGImp.Utils.DtmError2GrpcError(DtmFailure)); |
| 18 | + Assert.Equal(StatusCode.Aborted, ex.StatusCode); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void DtmError2GrpcError_Should_Throw_FailedPrecondition_RpcException() |
| 23 | + { |
| 24 | + var ex = Assert.Throws<RpcException>(() => DtmGImp.Utils.DtmError2GrpcError(DtmOnging)); |
| 25 | + Assert.Equal(StatusCode.FailedPrecondition, ex.StatusCode); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void DtmError2GrpcError_Should_Throw_Unknown_RpcException() |
| 30 | + { |
| 31 | + var ex = Assert.Throws<RpcException>(() => DtmGImp.Utils.DtmError2GrpcError(new System.ArgumentNullException())); |
| 32 | + Assert.Equal(StatusCode.Unknown, ex.StatusCode); |
| 33 | + } |
| 34 | + |
| 35 | + [Theory] |
| 36 | + [InlineData(StatusCode.Aborted, "ONGOING")] |
| 37 | + [InlineData(StatusCode.FailedPrecondition, "other")] |
| 38 | + public void GrpcError2DtmError_Should_Be_DtmOngingException(StatusCode code, string msg) |
| 39 | + { |
| 40 | + var rpcEx = new RpcException(new Status(code, msg), msg); |
| 41 | + var ex = DtmGImp.Utils.GrpcError2DtmError(rpcEx); |
| 42 | + Assert.IsType<DtmOngingException>(ex); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void GrpcError2DtmError_Should_Be_DtmFailureException() |
| 47 | + { |
| 48 | + var rpcEx = new RpcException(new Status(StatusCode.Aborted, "Other")); |
| 49 | + var ex = DtmGImp.Utils.GrpcError2DtmError(rpcEx); |
| 50 | + Assert.IsType<DtmFailureException>(ex); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void GrpcError2DtmError_Should_Be_RawException() |
| 55 | + { |
| 56 | + var rpcEx = new System.ArgumentNullException(); |
| 57 | + var ex = DtmGImp.Utils.GrpcError2DtmError(rpcEx); |
| 58 | + Assert.IsType<System.ArgumentNullException>(ex); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void String2DtmError_Should_Succeed() |
| 63 | + { |
| 64 | + var fEx = DtmGImp.Utils.String2DtmError(Constant.ResultFailure); |
| 65 | + Assert.IsType<DtmFailureException>(fEx); |
| 66 | + |
| 67 | + var oEx = DtmGImp.Utils.String2DtmError(Constant.ResultOngoing); |
| 68 | + Assert.IsType<DtmOngingException>(oEx); |
| 69 | + |
| 70 | + var nullEx = DtmGImp.Utils.String2DtmError(Constant.ResultSuccess); |
| 71 | + Assert.Null(nullEx); |
| 72 | + |
| 73 | + nullEx = DtmGImp.Utils.String2DtmError(string.Empty); |
| 74 | + Assert.Null(nullEx); |
| 75 | + |
| 76 | + nullEx = DtmGImp.Utils.String2DtmError("other"); |
| 77 | + Assert.Null(nullEx); |
| 78 | + } |
| 79 | + |
| 80 | + [Theory] |
| 81 | + [InlineData(null, "")] |
| 82 | + [InlineData("http://a.b.com/", "a.b.com")] |
| 83 | + [InlineData("https://a.b.com", "a.b.com")] |
| 84 | + public void GetWithoutPrefixgRPCUrl_Should_Succeed(string url, string exp) |
| 85 | + { |
| 86 | + var res = DtmGImp.Utils.GetWithoutPrefixgRPCUrl(url); |
| 87 | + Assert.Equal(exp, res); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void TransInfo2Metadata_Should_Succeed() |
| 92 | + { |
| 93 | + var meta = DtmGImp.Utils.TransInfo2Metadata("1", "2", "3", "4", "5"); |
| 94 | + |
| 95 | + Assert.Equal("1", meta.Get(Constant.Md.Gid).Value); |
| 96 | + Assert.Equal("2", meta.Get(Constant.Md.TransType).Value); |
| 97 | + Assert.Equal("3", meta.Get(Constant.Md.BranchId).Value); |
| 98 | + Assert.Equal("4", meta.Get(Constant.Md.Op).Value); |
| 99 | + Assert.Equal("5", meta.Get(Constant.Md.Dtm).Value); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void DtmGet_Should_Succeed() |
| 104 | + { |
| 105 | + var meta = new Metadata(); |
| 106 | + meta.Add("a", "b"); |
| 107 | + meta.Add("c", "d"); |
| 108 | + var context = new CusServerCallContext(meta); |
| 109 | + |
| 110 | + var str1 = DtmGImp.Utils.DtmGet(context, "a"); |
| 111 | + Assert.Equal("b", str1); |
| 112 | + |
| 113 | + var str2 = DtmGImp.Utils.DtmGet(context, "d"); |
| 114 | + Assert.Null(str2); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void DtmGet_When_Header_IsNull_Should_Succeed() |
| 119 | + { |
| 120 | + var context = new CusServerCallContext(null); |
| 121 | + |
| 122 | + var str = DtmGImp.Utils.DtmGet(context, "a"); |
| 123 | + Assert.Null(str); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments