|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.Collections.Generic; |
5 | 6 | using System.IO; |
6 | 7 | using System.Net; |
@@ -58,83 +59,59 @@ public async Task GetUser(string objId) |
58 | 59 | Assert.That(stringResult, Does.Contain(objId)); |
59 | 60 | } |
60 | 61 |
|
61 | | - //[RecordedTest] |
62 | | - //public async Task GetLedgerEntries() |
63 | | - //{ |
64 | | - // await PostLedgerEntry(); |
65 | | - |
66 | | - // var result = await Client.GetLedgerEntriesAsync(); |
67 | | - |
68 | | - // var nextLinkDetails = GetNextLinkDetails(result); |
69 | | - // while (nextLinkDetails != null) |
70 | | - // { |
71 | | - // var fromId = nextLinkDetails["fromTransactionId"]; |
72 | | - // var subId = nextLinkDetails["subLedgerId"]; |
73 | | - // result = await Client.GetLedgerEntriesAsync(subId, fromId).ConfigureAwait(false); |
74 | | - // Assert.AreEqual((int)HttpStatusCode.OK, result.Status); |
75 | | - // nextLinkDetails = GetNextLinkDetails(result); |
76 | | - // } |
77 | | - //} |
78 | | - |
79 | | - //[RecordedTest] |
80 | | - //public async Task GetLedgerEntry() |
81 | | - //{ |
82 | | - // await PostLedgerEntry(); |
83 | | - |
84 | | - // var result = await Client.GetLedgerEntriesAsync(); |
85 | | - // var stringResult = new StreamReader(result.ContentStream).ReadToEnd(); |
86 | | - |
87 | | - // while (stringResult.Contains("Loading")) |
88 | | - // { |
89 | | - // result = await Client.GetLedgerEntriesAsync().ConfigureAwait(false); |
90 | | - // stringResult = new StreamReader(result.ContentStream).ReadToEnd(); |
91 | | - // } |
92 | | - // var transactionId = GetFirstTransactionId(result); |
93 | | - // result = await Client.GetLedgerEntryAsync(transactionId).ConfigureAwait(false); |
94 | | - |
95 | | - // Assert.AreEqual((int)HttpStatusCode.OK, result.Status); |
96 | | - // Assert.That(stringResult, Does.Contain(transactionId)); |
97 | | - //} |
98 | | - |
99 | | - //[RecordedTest] |
100 | | - //public async Task GetReceipt() |
101 | | - //{ |
102 | | - // await PostLedgerEntry(); |
103 | | - |
104 | | - // var result = await Client.GetLedgerEntriesAsync(); |
105 | | - // var stringResult = new StreamReader(result.ContentStream).ReadToEnd(); |
106 | | - |
107 | | - // while (stringResult.Contains("Loading")) |
108 | | - // { |
109 | | - // result = await Client.GetLedgerEntriesAsync().ConfigureAwait(false); |
110 | | - // stringResult = new StreamReader(result.ContentStream).ReadToEnd(); |
111 | | - // } |
112 | | - // var transactionId = GetFirstTransactionId(result); |
113 | | - // result = await Client.GetReceiptAsync(transactionId).ConfigureAwait(false); |
114 | | - |
115 | | - // Assert.AreEqual((int)HttpStatusCode.OK, result.Status); |
116 | | - // Assert.That(stringResult, Does.Contain(transactionId)); |
117 | | - //} |
118 | | - |
119 | | - //[RecordedTest] |
120 | | - //public async Task GetTransactionStatus() |
121 | | - //{ |
122 | | - // await PostLedgerEntry(); |
123 | | - |
124 | | - // var result = await Client.GetLedgerEntriesAsync(); |
125 | | - // var stringResult = new StreamReader(result.ContentStream).ReadToEnd(); |
126 | | - |
127 | | - // while (stringResult.Contains("Loading")) |
128 | | - // { |
129 | | - // result = await Client.GetLedgerEntriesAsync().ConfigureAwait(false); |
130 | | - // stringResult = new StreamReader(result.ContentStream).ReadToEnd(); |
131 | | - // } |
132 | | - // var transactionId = GetFirstTransactionId(result); |
133 | | - // result = await Client.GetTransactionStatusAsync(transactionId).ConfigureAwait(false); |
134 | | - |
135 | | - // Assert.AreEqual((int)HttpStatusCode.OK, result.Status); |
136 | | - // Assert.That(stringResult, Does.Contain(transactionId)); |
137 | | - //} |
| 62 | + [RecordedTest] |
| 63 | + public async Task GetLedgerEntries() |
| 64 | + { |
| 65 | + await PostLedgerEntry(); |
| 66 | + |
| 67 | + await foreach (var entry in Client.GetLedgerEntriesAsync(new RequestOptions())) |
| 68 | + { |
| 69 | + Assert.NotNull(entry); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + [RecordedTest] |
| 74 | + public async Task GetLedgerEntry() |
| 75 | + { |
| 76 | + await PostLedgerEntry(); |
| 77 | + var tuple = await GetFirstTransactionIdFromGetEntries(); |
| 78 | + string transactionId = tuple.TransactionId; |
| 79 | + string stringResult = tuple.StringResult; |
| 80 | + Response response = await Client.GetLedgerEntryAsync(transactionId, new RequestOptions()); |
| 81 | + |
| 82 | + Assert.AreEqual((int)HttpStatusCode.OK, response.Status); |
| 83 | + Assert.That(stringResult, Does.Contain(transactionId)); |
| 84 | + } |
| 85 | + |
| 86 | + [RecordedTest] |
| 87 | + public async Task GetReceipt() |
| 88 | + { |
| 89 | + await PostLedgerEntry(); |
| 90 | + |
| 91 | + var tuple = await GetFirstTransactionIdFromGetEntries(); |
| 92 | + string transactionId = tuple.TransactionId; |
| 93 | + string stringResult = tuple.StringResult; |
| 94 | + |
| 95 | + var result = await Client.GetReceiptAsync(transactionId, new RequestOptions()).ConfigureAwait(false); |
| 96 | + |
| 97 | + Assert.AreEqual((int)HttpStatusCode.OK, result.Status); |
| 98 | + Assert.That(stringResult, Does.Contain(transactionId)); |
| 99 | + } |
| 100 | + |
| 101 | + [RecordedTest] |
| 102 | + public async Task GetTransactionStatus() |
| 103 | + { |
| 104 | + await PostLedgerEntry(); |
| 105 | + |
| 106 | + var tuple = await GetFirstTransactionIdFromGetEntries(); |
| 107 | + string transactionId = tuple.TransactionId; |
| 108 | + string stringResult = tuple.StringResult; |
| 109 | + |
| 110 | + var result = await Client.GetTransactionStatusAsync(transactionId, new RequestOptions()).ConfigureAwait(false); |
| 111 | + |
| 112 | + Assert.AreEqual((int)HttpStatusCode.OK, result.Status); |
| 113 | + Assert.That(stringResult, Does.Contain(transactionId)); |
| 114 | + } |
138 | 115 |
|
139 | 116 | [RecordedTest] |
140 | 117 | public async Task GetConstitution() |
@@ -231,22 +208,56 @@ private Dictionary<string, string> GetQueryStringKvps(string s) |
231 | 208 | return result; |
232 | 209 | } |
233 | 210 |
|
234 | | - private string GetFirstTransactionId(Response response) |
| 211 | + private async Task<(string TransactionId, string StringResult)> GetFirstTransactionIdFromGetEntries() |
235 | 212 | { |
236 | | - response.ContentStream.Position = 0; |
237 | | - var stringResult = new StreamReader(response.ContentStream).ReadToEnd(); |
238 | | - var doc = JsonDocument.Parse(stringResult); |
239 | | - if (doc.RootElement.TryGetProperty("entries", out var prop)) |
| 213 | + string stringResult = "Loading"; |
| 214 | + var result = Client.GetLedgerEntriesAsync(new RequestOptions()); |
| 215 | + bool first = true; |
| 216 | + Response response = null; |
| 217 | + |
| 218 | + await foreach (var page in result.AsPages()) |
| 219 | + { |
| 220 | + if (first) |
| 221 | + { |
| 222 | + response = page.GetRawResponse(); |
| 223 | + } |
| 224 | + foreach (var entry in page.Values) |
| 225 | + { |
| 226 | + stringResult = new StreamReader(entry.ToStream()).ReadToEnd(); |
| 227 | + break; |
| 228 | + } |
| 229 | + first = false; |
| 230 | + } |
| 231 | + |
| 232 | + while (stringResult.Contains("Loading")) |
240 | 233 | { |
241 | | - foreach (JsonElement entry in prop.EnumerateArray()) |
| 234 | + first = true; |
| 235 | + result = Client.GetLedgerEntriesAsync(new RequestOptions()); |
| 236 | + await foreach (var page in result.AsPages()) |
242 | 237 | { |
243 | | - if (entry.TryGetProperty("transactionId", out var tid)) |
| 238 | + if (first) |
244 | 239 | { |
245 | | - return tid.GetString(); |
| 240 | + response = page.GetRawResponse(); |
246 | 241 | } |
| 242 | + foreach (var entry in page.Values) |
| 243 | + { |
| 244 | + stringResult = new StreamReader(entry.ToStream()).ReadToEnd(); |
| 245 | + break; |
| 246 | + } |
| 247 | + first = false; |
247 | 248 | } |
248 | 249 | } |
249 | | - return default; |
| 250 | + return (GetFirstTransactionId(stringResult), stringResult); |
| 251 | + } |
| 252 | + |
| 253 | + private string GetFirstTransactionId(string stringResult) |
| 254 | + { |
| 255 | + var doc = JsonDocument.Parse(stringResult); |
| 256 | + if (doc.RootElement.TryGetProperty("transactionId", out var tid)) |
| 257 | + { |
| 258 | + return tid.GetString(); |
| 259 | + } |
| 260 | + throw new Exception($"Could not parse transationId from response:\n{stringResult}"); |
250 | 261 | } |
251 | 262 |
|
252 | 263 | private Dictionary<string, string> GetNextLinkDetails(Response response) |
|
0 commit comments