Skip to content

Commit

Permalink
Merge branch 'release-0.11.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dnl-blkv committed Sep 6, 2017
2 parents 325af4c + dbc0895 commit 26823da
Show file tree
Hide file tree
Showing 94 changed files with 1,939 additions and 1,233 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BunqSdk/Model/Generated linguist-generated=true
44 changes: 42 additions & 2 deletions BunqSdk.Samples/PaymentListSample.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,61 @@
using System;
using System.Collections.Generic;
using Bunq.Sdk.Context;
using Bunq.Sdk.Http;
using Bunq.Sdk.Model.Generated;
using Bunq.Sdk.Samples.Utils;

namespace Bunq.Sdk.Samples
{
public class PaymentListSample : ISample
{
/// <summary>
/// Message constants.
/// </summary>
private const string MESSAGE_LATEST_PAGE_IDS = "Latest page IDs: ";
private const string MESSAGE_SECOND_LATEST_PAGE_IDS = "Second latest page IDs: ";
private const string MESSAGE_NO_PRIOR_PAYMENTS_FOUND = "No prior payments found!";

/// <summary>
/// Size of each page of payment listing.
/// </summary>
private const int PAGE_SIZE = 3;

/// <summary>
/// Constants to be changed to run the example.
/// </summary>
private const int USER_ITEM_ID = 0; // Put your user ID here
private const int MONETARY_ACCOUNT_ITEM_ID = 0; // Put your monetary account ID here

public void Run()
{
var apiContext = ApiContext.Restore();
var paymentList = Payment.List(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID).Value;
var paginationCountOnly = new Pagination
{
Count = PAGE_SIZE,
};
Console.WriteLine(MESSAGE_LATEST_PAGE_IDS);
var paymentResponse = Payment.List(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID,
paginationCountOnly.UrlParamsCountOnly);
PrintPayments(paymentResponse.Value);
var pagination = paymentResponse.Pagination;

if (pagination.HasPreviousPage())
{
Console.WriteLine(MESSAGE_SECOND_LATEST_PAGE_IDS);
var previousPaymentResponse = Payment.List(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID,
pagination.UrlParamsPreviousPage);
PrintPayments(previousPaymentResponse.Value);
}
else
{
Console.WriteLine(MESSAGE_NO_PRIOR_PAYMENTS_FOUND);
}
}

foreach (var payment in paymentList)
private static void PrintPayments(IEnumerable<Payment> payments)
{
foreach (var payment in payments)
{
Console.WriteLine(payment.Id);
}
Expand Down
7 changes: 3 additions & 4 deletions BunqSdk.Tests/BunqSdkTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class BunqSdkTestBase
/// Configuration items.
/// </summary>
private static readonly string API_KEY = Config.GetApiKey();
private static readonly string FIELD_PERMITTED_IP = Config.GetPermittedIp();
private static readonly string[] FIELD_PERMITTED_IPS = Config.GetPermittedIps();

/// <summary>
/// Gets an Api Context, re-creates if needed and returns it.
Expand Down Expand Up @@ -54,9 +54,8 @@ protected static ApiContext GetApiContext()

private static ApiContext CreateApiContext()
{
var permittedIps = new List<string> {FIELD_PERMITTED_IP};

return ApiContext.Create(ApiEnvironmentType.SANDBOX, API_KEY, DEVICE_DESCRIPTION_TEST, permittedIps);
return ApiContext.Create(ApiEnvironmentType.SANDBOX, API_KEY, DEVICE_DESCRIPTION_TEST,
new List<string>(FIELD_PERMITTED_IPS));
}
}
}
30 changes: 24 additions & 6 deletions BunqSdk.Tests/Config.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
using System.IO;
using System.Collections.Immutable;
using System.IO;
using Bunq.Sdk.Model.Generated.Object;
using Newtonsoft.Json.Linq;

namespace Bunq.Sdk.Tests
{
public class Config
{
/// <summary>
/// Delimiter between the IP addresses in the PERMITTED_IPS field.
/// </summary>
private const char DELIMITER_IPS = ',';

/// <summary>
/// Length of an empty array.
/// </summary>
private const int LENGTH_NONE = 0;

/// <summary>
/// Field constants.
/// </summary>
private const string FIELD_CONFIG_FILE_PATH = "../../../Resources/config.json";
private const string FIELD_USER_ID = "USER_ID";
private const string FIELD_API_KEY = "API_KEY";
private const string FIELD_PERMITTED_IP = "ipAddress";
private const string FIELD_PERMITTED_IPS = "PERMITTED_IPS";
private const string FIELD_ATTACHMENT_PUBLIC_TEST = "AttachmentPublicTest";
private const string FIELD_ATTACHMENT_PATH_IN = "PATH_IN";
private const string FIELD_ATTACHMENT_DESCRIPTION = "DESCRIPTION";
Expand All @@ -28,15 +42,15 @@ public static int GetCashRegisterId()
return GetConfig()[FIELD_TAB_USAGE_SINGLE][FIELD_CASH_REGISTER_ID].ToObject<int>();
}

public static Pointer GetCounterAliasOther()
public static Pointer GetCounterPartyAliasOther()
{
var alias = GetConfig()[FIELD_COUNTER_PARTY_OTHER][FIELD_COUNTER_ALIAS].ToString();
var type = GetConfig()[FIELD_COUNTER_PARTY_OTHER][FIELD_COUNTER_TYPE].ToString();

return new Pointer(type, alias);
}

public static Pointer GetCounterAliasSelf()
public static Pointer GetCounterPartyAliasSelf()
{
var alias = GetConfig()[FIELD_COUNTER_PARTY_SELF][FIELD_COUNTER_ALIAS].ToString();
var type = GetConfig()[FIELD_COUNTER_PARTY_SELF][FIELD_COUNTER_TYPE].ToString();
Expand Down Expand Up @@ -69,9 +83,13 @@ public static string GetAttachmentContentType()
return GetConfig()[FIELD_ATTACHMENT_PUBLIC_TEST][FIELD_ATTACHMENT_CONTENT_TYPE].ToString();
}

public static string GetPermittedIp()
public static string[] GetPermittedIps()
{
return GetConfig()[FIELD_PERMITTED_IP].ToString();
var permittedIpsString = GetConfig()[FIELD_PERMITTED_IPS].ToString();

return permittedIpsString.Length == LENGTH_NONE ?
new string[LENGTH_NONE] :
permittedIpsString.Split(DELIMITER_IPS);
}

public static string GetApiKey()
Expand Down
108 changes: 108 additions & 0 deletions BunqSdk.Tests/Http/PaginationScenarioTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Collections.Generic;
using Bunq.Sdk.Context;
using Bunq.Sdk.Http;
using Bunq.Sdk.Json;
using Bunq.Sdk.Model.Generated;
using Bunq.Sdk.Model.Generated.Object;
using Xunit;

namespace Bunq.Sdk.Tests.Http
{
/// <summary>
/// Tests:
/// Pagination
/// </summary>
public class PaginationScenarioTest : BunqSdkTestBase
{
/// <summary>
/// Config values.
/// </summary>
private static readonly int USER_ID = Config.GetUserId();
private static readonly int MONETARY_ACCOUNT_ID = Config.GetMonetarytAccountId();
private static readonly Pointer COUNTER_PARTY_OTHER = Config.GetCounterPartyAliasOther();

/// <summary>
/// Constants for scenario testing.
/// </summary>
private const int PAYMENT_LISTING_PAGE_SIZE = 2;
private const int PAYMENT_REQUIRED_COUNT_MINIMUM = PAYMENT_LISTING_PAGE_SIZE * 2;
private const int NUMBER_ZERO = 0;

/// <summary>
/// Constants for payment creation.
/// </summary>
private const string PAYMENT_AMOUNT_EUR = "0.01";
private const string PAYMENT_CURRENCY = "EUR";
private const string PAYMENT_DESCRIPTION = "C# test Payment";

/// <summary>
/// API context to use for the test API calls.
/// </summary>
private static readonly ApiContext API_CONTEXT = GetApiContext();

[Fact]
public void TestApiScenarioPaymentListingWithPagination()
{
EnsureEnoughPayments();
var paymentsExpected = new List<Payment>(GetPaymentsRequired());
var paginationCountOnly = new Pagination
{
Count = PAYMENT_LISTING_PAGE_SIZE
};

var responseLatest = ListPayments(paginationCountOnly.UrlParamsCountOnly);
var paginationLatest = responseLatest.Pagination;
var responsePrevious = ListPayments(paginationLatest.UrlParamsPreviousPage);
var paginationPrevious = responsePrevious.Pagination;
var responsePreviousNext = ListPayments(paginationPrevious.UrlParamsNextPage);

var paymentsActual = new List<Payment>();
paymentsActual.AddRange(responsePreviousNext.Value);
paymentsActual.AddRange(responsePrevious.Value);
var paymentsExpectedSerialized = BunqJsonConvert.SerializeObject(paymentsExpected);
var paymentsActualSerialized = BunqJsonConvert.SerializeObject(paymentsActual);

Assert.Equal(paymentsExpectedSerialized, paymentsActualSerialized);
}

private static void EnsureEnoughPayments()
{
for (var i = NUMBER_ZERO; i < GetPaymentsMissingCount(); ++i)
{
CreatePayment();
}
}

private static int GetPaymentsMissingCount()
{
return PAYMENT_REQUIRED_COUNT_MINIMUM - GetPaymentsRequired().Count;
}

private static IList<Payment> GetPaymentsRequired()
{
var pagination = new Pagination
{
Count = PAYMENT_REQUIRED_COUNT_MINIMUM
};

return ListPayments(pagination.UrlParamsCountOnly).Value;
}

private static BunqResponse<List<Payment>> ListPayments(IDictionary<string, string> urlParams)
{
return Payment.List(API_CONTEXT, USER_ID, MONETARY_ACCOUNT_ID, urlParams);
}

private static void CreatePayment()
{
var requestMap = new Dictionary<string, object>
{
{Payment.FIELD_AMOUNT, new Amount(PAYMENT_AMOUNT_EUR, PAYMENT_CURRENCY)},
{Payment.FIELD_DESCRIPTION, PAYMENT_DESCRIPTION},
{Payment.FIELD_COUNTERPARTY_ALIAS, COUNTER_PARTY_OTHER}
};

Payment.Create(API_CONTEXT, requestMap, USER_ID, MONETARY_ACCOUNT_ID);
}
}
}
Loading

0 comments on commit 26823da

Please sign in to comment.