Skip to content

Commit

Permalink
replace commented Live tests (#24802)
Browse files Browse the repository at this point in the history
* replace commented Live tests
  • Loading branch information
christothes authored Oct 19, 2021
1 parent 44f593e commit ae99a7c
Show file tree
Hide file tree
Showing 7 changed files with 526 additions and 842 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -58,83 +59,59 @@ public async Task GetUser(string objId)
Assert.That(stringResult, Does.Contain(objId));
}

//[RecordedTest]
//public async Task GetLedgerEntries()
//{
// await PostLedgerEntry();

// var result = await Client.GetLedgerEntriesAsync();

// var nextLinkDetails = GetNextLinkDetails(result);
// while (nextLinkDetails != null)
// {
// var fromId = nextLinkDetails["fromTransactionId"];
// var subId = nextLinkDetails["subLedgerId"];
// result = await Client.GetLedgerEntriesAsync(subId, fromId).ConfigureAwait(false);
// Assert.AreEqual((int)HttpStatusCode.OK, result.Status);
// nextLinkDetails = GetNextLinkDetails(result);
// }
//}

//[RecordedTest]
//public async Task GetLedgerEntry()
//{
// await PostLedgerEntry();

// var result = await Client.GetLedgerEntriesAsync();
// var stringResult = new StreamReader(result.ContentStream).ReadToEnd();

// while (stringResult.Contains("Loading"))
// {
// result = await Client.GetLedgerEntriesAsync().ConfigureAwait(false);
// stringResult = new StreamReader(result.ContentStream).ReadToEnd();
// }
// var transactionId = GetFirstTransactionId(result);
// result = await Client.GetLedgerEntryAsync(transactionId).ConfigureAwait(false);

// Assert.AreEqual((int)HttpStatusCode.OK, result.Status);
// Assert.That(stringResult, Does.Contain(transactionId));
//}

//[RecordedTest]
//public async Task GetReceipt()
//{
// await PostLedgerEntry();

// var result = await Client.GetLedgerEntriesAsync();
// var stringResult = new StreamReader(result.ContentStream).ReadToEnd();

// while (stringResult.Contains("Loading"))
// {
// result = await Client.GetLedgerEntriesAsync().ConfigureAwait(false);
// stringResult = new StreamReader(result.ContentStream).ReadToEnd();
// }
// var transactionId = GetFirstTransactionId(result);
// result = await Client.GetReceiptAsync(transactionId).ConfigureAwait(false);

// Assert.AreEqual((int)HttpStatusCode.OK, result.Status);
// Assert.That(stringResult, Does.Contain(transactionId));
//}

//[RecordedTest]
//public async Task GetTransactionStatus()
//{
// await PostLedgerEntry();

// var result = await Client.GetLedgerEntriesAsync();
// var stringResult = new StreamReader(result.ContentStream).ReadToEnd();

// while (stringResult.Contains("Loading"))
// {
// result = await Client.GetLedgerEntriesAsync().ConfigureAwait(false);
// stringResult = new StreamReader(result.ContentStream).ReadToEnd();
// }
// var transactionId = GetFirstTransactionId(result);
// result = await Client.GetTransactionStatusAsync(transactionId).ConfigureAwait(false);

// Assert.AreEqual((int)HttpStatusCode.OK, result.Status);
// Assert.That(stringResult, Does.Contain(transactionId));
//}
[RecordedTest]
public async Task GetLedgerEntries()
{
await PostLedgerEntry();

await foreach (var entry in Client.GetLedgerEntriesAsync(new RequestOptions()))
{
Assert.NotNull(entry);
}
}

[RecordedTest]
public async Task GetLedgerEntry()
{
await PostLedgerEntry();
var tuple = await GetFirstTransactionIdFromGetEntries();
string transactionId = tuple.TransactionId;
string stringResult = tuple.StringResult;
Response response = await Client.GetLedgerEntryAsync(transactionId, new RequestOptions());

Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
Assert.That(stringResult, Does.Contain(transactionId));
}

[RecordedTest]
public async Task GetReceipt()
{
await PostLedgerEntry();

var tuple = await GetFirstTransactionIdFromGetEntries();
string transactionId = tuple.TransactionId;
string stringResult = tuple.StringResult;

var result = await Client.GetReceiptAsync(transactionId, new RequestOptions()).ConfigureAwait(false);

Assert.AreEqual((int)HttpStatusCode.OK, result.Status);
Assert.That(stringResult, Does.Contain(transactionId));
}

[RecordedTest]
public async Task GetTransactionStatus()
{
await PostLedgerEntry();

var tuple = await GetFirstTransactionIdFromGetEntries();
string transactionId = tuple.TransactionId;
string stringResult = tuple.StringResult;

var result = await Client.GetTransactionStatusAsync(transactionId, new RequestOptions()).ConfigureAwait(false);

Assert.AreEqual((int)HttpStatusCode.OK, result.Status);
Assert.That(stringResult, Does.Contain(transactionId));
}

[RecordedTest]
public async Task GetConstitution()
Expand Down Expand Up @@ -231,22 +208,56 @@ private Dictionary<string, string> GetQueryStringKvps(string s)
return result;
}

private string GetFirstTransactionId(Response response)
private async Task<(string TransactionId, string StringResult)> GetFirstTransactionIdFromGetEntries()
{
response.ContentStream.Position = 0;
var stringResult = new StreamReader(response.ContentStream).ReadToEnd();
var doc = JsonDocument.Parse(stringResult);
if (doc.RootElement.TryGetProperty("entries", out var prop))
string stringResult = "Loading";
var result = Client.GetLedgerEntriesAsync(new RequestOptions());
bool first = true;
Response response = null;

await foreach (var page in result.AsPages())
{
if (first)
{
response = page.GetRawResponse();
}
foreach (var entry in page.Values)
{
stringResult = new StreamReader(entry.ToStream()).ReadToEnd();
break;
}
first = false;
}

while (stringResult.Contains("Loading"))
{
foreach (JsonElement entry in prop.EnumerateArray())
first = true;
result = Client.GetLedgerEntriesAsync(new RequestOptions());
await foreach (var page in result.AsPages())
{
if (entry.TryGetProperty("transactionId", out var tid))
if (first)
{
return tid.GetString();
response = page.GetRawResponse();
}
foreach (var entry in page.Values)
{
stringResult = new StreamReader(entry.ToStream()).ReadToEnd();
break;
}
first = false;
}
}
return default;
return (GetFirstTransactionId(stringResult), stringResult);
}

private string GetFirstTransactionId(string stringResult)
{
var doc = JsonDocument.Parse(stringResult);
if (doc.RootElement.TryGetProperty("transactionId", out var tid))
{
return tid.GetString();
}
throw new Exception($"Could not parse transationId from response:\n{stringResult}");
}

private Dictionary<string, string> GetNextLinkDetails(Response response)
Expand Down
Loading

0 comments on commit ae99a7c

Please sign in to comment.