Skip to content

Commit

Permalink
Correção de retorno da função ICardsHubClient.GetCardsAsync, com adiç…
Browse files Browse the repository at this point in the history
…ão de teste automatizado
  • Loading branch information
thiagomoises committed Jul 1, 2019
1 parent 8c21cfd commit 459a370
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using MercadoPago.NetCore.Model.DataStructures.Search;
using MercadoPago.NetCore.Model.Resources;
using prmToolkit.NotificationPattern;
Expand All @@ -9,7 +10,7 @@ public interface ICardsHubClient : INotifiable
{
Task<Card> DeleteAsync(Card card);
Task<Card> FindAsync(string customerId, string cardId);
Task<SearchResult<Card>> GetCardsAsync(string customerId);
Task<IEnumerable<Card>> GetCardsAsync(string customerId);
Task<Card> SaveAsync(Card card);
Task<Card> UpdateAsync(Card card);
}
Expand Down
33 changes: 21 additions & 12 deletions src/MercadoPago.NetCore/HubClients/CardsHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using MercadoPago.NetCore.Model.Resources;
using Moises.Toolkit.MercadoPago.NetCore.HubClients.Abstracts;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

Expand All @@ -13,22 +15,29 @@ public CardsHubClient(IHttpClientFactory httpClientFactory, MPOptions options, I
{
}

public async System.Threading.Tasks.Task<SearchResult<Card>> GetCardsAsync(string customerId)
public async System.Threading.Tasks.Task<IEnumerable<Card>> GetCardsAsync(string customerId)
{
if (string.IsNullOrEmpty(customerId))
try
{
this.AddNotification("customerId", "customerId is Required");
if (string.IsNullOrEmpty(customerId))
{
this.AddNotification("customerId", "customerId is Required");
}

if (this.IsInvalid())
return null;

var url = await MPUrlBuildAsync($"/v1/customers/{customerId}/cards");
var response = await Client.GetAsync(url);
string stringResponse = await this.ExtractResponseAsync(response);
if (this.IsInvalid())
return null;
return JsonConvert.DeserializeObject<IEnumerable<Card>>(stringResponse, MPUtil.JsonSerializerSettings);
}

if (this.IsInvalid())
return null;

var url = await MPUrlBuildAsync($"/v1/customers/{customerId}/cards");
var response = await Client.GetAsync(url);
string stringResponse = await this.ExtractResponseAsync(response);
if (this.IsInvalid())
catch (Exception ex)
{
return null;
return JsonConvert.DeserializeObject<SearchResult<Card>>(stringResponse, MPUtil.JsonSerializerSettings);
}
}

public async System.Threading.Tasks.Task<Card> FindAsync(string customerId, string cardId)
Expand Down
18 changes: 17 additions & 1 deletion test/MercadoPago.NetCore.Tests/CardsHubClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Setup()
customerHub.Setup();
if (customerHub.CustomerHubClient.SearchAsync(null).TryExecute(out var customers, 10000))
CustomerId = customers.Results.Select(x => x.Id).FirstOrDefault();

tokenHubClient.Setup(x => x.GetTicketAsync())
.Returns(Task.FromResult(new Ticket() { AccessToken = "TEST-8600607042428103-060407-f91bbb3d5d0029bc342657a83aa08ee5-397002962" }));
_CardHubClient = new CardsHubClient(_httpClientFactory.Object, TicketHelperTest.GetMPOptions(), tokenHubClient.Object);
Expand Down Expand Up @@ -60,6 +60,22 @@ public async Task SaveAsync_Test()
Assert.IsTrue(firstCondition || secondCondition);
}
*/

//442194730-fZXyKVGsxiI7ct
[Test]
public async Task GetCardsAsync_Test_Valid()
{
var result = await _CardHubClient.GetCardsAsync("442194730-fZXyKVGsxiI7ct");
Assert.IsTrue(_CardHubClient.IsValid());
}

[Test]
public async Task GetCardsAsync_Test_Null()
{
var result = await _CardHubClient.GetCardsAsync(null);
Assert.IsTrue(_CardHubClient.IsInvalid());
}

[Test]
public async Task FindAsync_Test_Null()
{
Expand Down

0 comments on commit 459a370

Please sign in to comment.