|
| 1 | +using System.Collections.Generic; |
| 2 | +using Bunq.Sdk.Context; |
| 3 | +using Bunq.Sdk.Exception; |
| 4 | +using Bunq.Sdk.Http; |
| 5 | +using Bunq.Sdk.Json; |
| 6 | +using Bunq.Sdk.Model.Generated; |
| 7 | +using Bunq.Sdk.Model.Generated.Object; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Bunq.Sdk.Tests.Http |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Tests: |
| 14 | + /// Pagination |
| 15 | + /// </summary> |
| 16 | + public class PaginationTest : BunqSdkTestBase |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Config values. |
| 20 | + /// </summary> |
| 21 | + private static readonly int USER_ID = Config.GetUserId(); |
| 22 | + private static readonly int MONETARY_ACCOUNT_ID = Config.GetMonetarytAccountId(); |
| 23 | + private static readonly Pointer COUNTER_PARTY_OTHER = Config.GetCounterAliasOther(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Values of pagination properties for testing. |
| 27 | + /// </summary> |
| 28 | + private const int PAGINATION_OLDER_ID_CUSTOM = 1; |
| 29 | + private const int PAGINATION_NEWER_ID_CUSTOM = 2; |
| 30 | + private const int PAGINATION_FUTURE_ID_CUSTOM = 3; |
| 31 | + private const int PAGINATION_COUNT_CUSTOM = 5; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Constants for scenario testing. |
| 35 | + /// </summary> |
| 36 | + private const int PAYMENT_LISTING_PAGE_SIZE = 2; |
| 37 | + private const int PAYMENT_REQUIRED_COUNT_MINIMUM = PAYMENT_LISTING_PAGE_SIZE * 2; |
| 38 | + private const int NUMBER_ZERO = 0; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Constants for payment creation. |
| 42 | + /// </summary> |
| 43 | + private const string AMOUNT_IN_EUR = "0.01"; |
| 44 | + private const string FIELD_CURRENCY = "EUR"; |
| 45 | + private const string FIELD_PAYMENT_DESCRIPTION = "C# test Payment"; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// API context to use for the test API calls. |
| 49 | + /// </summary> |
| 50 | + private static readonly ApiContext API_CONTEXT = GetApiContext(); |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void TestGetUrlParamsCountOnly() |
| 54 | + { |
| 55 | + var pagination = CreatePaginationWithAllPropertiesSet(); |
| 56 | + var urlParamsCountOnlyExpected = new Dictionary<string, string> |
| 57 | + { |
| 58 | + {Pagination.PARAM_COUNT, PAGINATION_COUNT_CUSTOM.ToString()} |
| 59 | + }; |
| 60 | + |
| 61 | + Assert.Equal(urlParamsCountOnlyExpected, pagination.UrlParamsCountOnly); |
| 62 | + } |
| 63 | + |
| 64 | + private static Pagination CreatePaginationWithAllPropertiesSet() |
| 65 | + { |
| 66 | + return new Pagination |
| 67 | + { |
| 68 | + OlderId = PAGINATION_OLDER_ID_CUSTOM, |
| 69 | + NewerId = PAGINATION_NEWER_ID_CUSTOM, |
| 70 | + FutureId = PAGINATION_FUTURE_ID_CUSTOM, |
| 71 | + Count = PAGINATION_COUNT_CUSTOM |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void TestGetUrlParamsPreviousPage() |
| 77 | + { |
| 78 | + var pagination = CreatePaginationWithAllPropertiesSet(); |
| 79 | + var urlParamsCountOnlyExpected = new Dictionary<string, string> |
| 80 | + { |
| 81 | + {Pagination.PARAM_COUNT, PAGINATION_COUNT_CUSTOM.ToString()}, |
| 82 | + {Pagination.PARAM_OLDER_ID, PAGINATION_OLDER_ID_CUSTOM.ToString()} |
| 83 | + }; |
| 84 | + |
| 85 | + Assert.Equal(urlParamsCountOnlyExpected, pagination.UrlParamsPreviousPage); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void TestGetUrlParamsNextPageNewer() |
| 90 | + { |
| 91 | + var pagination = CreatePaginationWithAllPropertiesSet(); |
| 92 | + var urlParamsCountOnlyExpected = new Dictionary<string, string> |
| 93 | + { |
| 94 | + {Pagination.PARAM_COUNT, PAGINATION_COUNT_CUSTOM.ToString()}, |
| 95 | + {Pagination.PARAM_NEWER_ID, PAGINATION_NEWER_ID_CUSTOM.ToString()} |
| 96 | + }; |
| 97 | + |
| 98 | + Assert.Equal(urlParamsCountOnlyExpected, pagination.UrlParamsNextPage); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void TestGetUrlParamsNextPageFuture() |
| 103 | + { |
| 104 | + var pagination = CreatePaginationWithNoNextPageAssured(); |
| 105 | + var urlParamsCountOnlyExpected = new Dictionary<string, string> |
| 106 | + { |
| 107 | + {Pagination.PARAM_COUNT, PAGINATION_COUNT_CUSTOM.ToString()}, |
| 108 | + {Pagination.PARAM_NEWER_ID, PAGINATION_FUTURE_ID_CUSTOM.ToString()} |
| 109 | + }; |
| 110 | + |
| 111 | + Assert.False(pagination.HasNextPageAssured()); |
| 112 | + Assert.Equal(urlParamsCountOnlyExpected, pagination.UrlParamsNextPage); |
| 113 | + } |
| 114 | + |
| 115 | + private static Pagination CreatePaginationWithNoNextPageAssured() |
| 116 | + { |
| 117 | + var pagination = CreatePaginationWithAllPropertiesSet(); |
| 118 | + pagination.NewerId = null; |
| 119 | + |
| 120 | + return pagination; |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void TestGetUrlParamsPreviousPageFromPaginationWithNoPreviousPage() |
| 125 | + { |
| 126 | + var pagination = CreatePaginationWithNoPreviousPage(); |
| 127 | + |
| 128 | + Assert.False(pagination.HasPreviousPage()); |
| 129 | + Assert.Throws<BunqException>(() => pagination.UrlParamsPreviousPage); |
| 130 | + } |
| 131 | + |
| 132 | + private static Pagination CreatePaginationWithNoPreviousPage() |
| 133 | + { |
| 134 | + var pagination = CreatePaginationWithAllPropertiesSet(); |
| 135 | + pagination.OlderId = null; |
| 136 | + |
| 137 | + return pagination; |
| 138 | + } |
| 139 | + |
| 140 | + [Fact] |
| 141 | + public void TestGetUrlParamsPreviousPageFromPaginationWithNoNextPage() |
| 142 | + { |
| 143 | + var pagination = CreatePaginationWithNoNextPage(); |
| 144 | + |
| 145 | + Assert.Throws<BunqException>(() => pagination.UrlParamsNextPage); |
| 146 | + } |
| 147 | + |
| 148 | + private static Pagination CreatePaginationWithNoNextPage() |
| 149 | + { |
| 150 | + var pagination = CreatePaginationWithNoNextPageAssured(); |
| 151 | + pagination.FutureId = null; |
| 152 | + |
| 153 | + return pagination; |
| 154 | + } |
| 155 | + |
| 156 | + [Fact] |
| 157 | + public void TestApiScenarioPaymentListingWithPagination() |
| 158 | + { |
| 159 | + EnsureEnoughPayments(); |
| 160 | + var paymentsExpected = new HashSet<Payment>(GetPaymentsRequired()); |
| 161 | + var paginationCountOnly = new Pagination |
| 162 | + { |
| 163 | + Count = PAYMENT_LISTING_PAGE_SIZE |
| 164 | + }; |
| 165 | + |
| 166 | + var paymentResponseLatest = ListPayments(paginationCountOnly.UrlParamsCountOnly); |
| 167 | + var paginationLatest = paymentResponseLatest.Pagination; |
| 168 | + var paymentResponsePrevious = ListPayments(paginationLatest.UrlParamsPreviousPage); |
| 169 | + var paginationPrevious = paymentResponsePrevious.Pagination; |
| 170 | + var paymentResponsePreviousNext = ListPayments(paginationPrevious.UrlParamsNextPage); |
| 171 | + |
| 172 | + var paymentsActual = new HashSet<Payment>(); |
| 173 | + paymentsActual.UnionWith(paymentResponsePreviousNext.Value); |
| 174 | + paymentsActual.UnionWith(paymentResponsePrevious.Value); |
| 175 | + var paymentsExpectedSerialized = BunqJsonConvert.SerializeObject(paymentsExpected); |
| 176 | + var paymentsActualSerialized = BunqJsonConvert.SerializeObject(paymentsActual); |
| 177 | + |
| 178 | + Assert.Equal(paymentsExpectedSerialized, paymentsActualSerialized); |
| 179 | + } |
| 180 | + |
| 181 | + private static void EnsureEnoughPayments() |
| 182 | + { |
| 183 | + for (var i = NUMBER_ZERO; i < GetPaymentsMissingCount(); ++i) |
| 184 | + { |
| 185 | + CreatePayment(); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private static int GetPaymentsMissingCount() |
| 190 | + { |
| 191 | + return PAYMENT_REQUIRED_COUNT_MINIMUM - GetPaymentsRequired().Count; |
| 192 | + } |
| 193 | + |
| 194 | + private static List<Payment> GetPaymentsRequired() |
| 195 | + { |
| 196 | + var pagination = new Pagination |
| 197 | + { |
| 198 | + Count = PAYMENT_REQUIRED_COUNT_MINIMUM |
| 199 | + }; |
| 200 | + |
| 201 | + return ListPayments(pagination.UrlParamsCountOnly).Value; |
| 202 | + } |
| 203 | + |
| 204 | + private static BunqResponse<List<Payment>> ListPayments(IDictionary<string, string> urlParams) |
| 205 | + { |
| 206 | + return Payment.List(API_CONTEXT, USER_ID, MONETARY_ACCOUNT_ID, urlParams); |
| 207 | + } |
| 208 | + |
| 209 | + private static void CreatePayment() |
| 210 | + { |
| 211 | + var requestMap = new Dictionary<string, object> |
| 212 | + { |
| 213 | + {Payment.FIELD_AMOUNT, new Amount(AMOUNT_IN_EUR, FIELD_CURRENCY)}, |
| 214 | + {Payment.FIELD_DESCRIPTION, FIELD_PAYMENT_DESCRIPTION}, |
| 215 | + {Payment.FIELD_COUNTERPARTY_ALIAS, COUNTER_PARTY_OTHER} |
| 216 | + }; |
| 217 | + |
| 218 | + Payment.Create(API_CONTEXT, requestMap, USER_ID, MONETARY_ACCOUNT_ID); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments