forked from gematik/lib-vau-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVauClientTest.cs
More file actions
150 lines (116 loc) · 6.18 KB
/
Copy pathVauClientTest.cs
File metadata and controls
150 lines (116 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections;
using System.Net.Http;
using System.Threading.Tasks;
using lib_vau_csharp;
using lib_vau_csharp_test.EpaApiClients.Auth;
using lib_vau_csharp_test.EpaApiClients.EntitlementManagement;
using Mauve.Erezept.API.EpaServiceClients.MedicationService;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using VerifyNUnit;
using VerifyTests;
using ErrorType = lib_vau_csharp_test.EpaApiClients.EntitlementManagement.ErrorType;
namespace lib_vau_csharp_test
{
[Explicit("Needs running epa deployment.")]
public class VauClientTest
{
private VauClient vauClient;
[OneTimeSetUp]
public void InitVerifyPdfPig() => VerifyPdfPig.Initialize();
[SetUp]
public void Setup()
{
var vauClientHttpClient = new HttpClient { BaseAddress = Constants.EpaDeploymentUrl };
vauClient = new VauClient(vauClientHttpClient);
}
[Test]
public async Task ConnectionIdIsSetAfterHandshake()
{
await vauClient.DoHandshake();
Assert.That(vauClient.ConnectionId, Is.Not.Null);
}
[Test]
public async Task CanGetStatus()
{
await vauClient.DoHandshake();
var vauStatus = await vauClient.GetStatus("Test/1.0");
Assert.That(vauStatus, Is.Not.Null);
}
[Test]
public async Task VauHttpClientHandlerDoesDecryptionAndEncryptionWhenCallingAnApiMethod()
{
var vauHttpClientHandler = new VauHttpClientHandler(new ReturnInstanceVauClientProvider(vauClient, true));
var httpClient = new HttpClient(vauHttpClientHandler) { BaseAddress = Constants.EpaDeploymentUrl };
var authorizationServiceClient = new AuthorizationServiceClient(httpClient);
var response = await authorizationServiceClient.GetNonceAsync("Test/1.0");
Console.WriteLine(response.Nonce);
}
[Test]
public void UsageWithIHttpClientFactory()
{
var services = new ServiceCollection();
services.AddTransient<VauHttpClientHandler>();
services.AddSingleton<IVauClientProvider, VauClientProviderSingleInstance>();
services.AddHttpClient("VAU").ConfigurePrimaryHttpMessageHandler<VauHttpClientHandler>();
services.AddTransient<IEntitlementManagementClient>(sp =>
{
var httpClientFactory = sp.GetService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("VAU");
httpClient.BaseAddress = Constants.EpaDeploymentUrl;
return ActivatorUtilities.CreateInstance<EntitlementManagementClient>(sp, httpClient);
});
var sp = services.BuildServiceProvider();
var entitlementManagementClient = sp.GetService<IEntitlementManagementClient>();
var request = new EntitlementRequestType { Jwt = "An invalid JWT" };
Assert.ThrowsAsync<EntitlementManagementException<ErrorType>>(() => entitlementManagementClient.SetEntitlementPsAsync(request, "Z123456783", "Test/1.0"));
}
[Test]
[TestCaseSource(nameof(ThrowingMethods))]
public void ThrowsInvalidOperationExceptionIfHandshakeWasNotPerformed(Func<Task> method, string methodName)
{
Assert.ThrowsAsync<InvalidOperationException>(() => method(), $"Expected method {methodName} to throw an InvalidOperationException.");
}
[Test(Description = "Expects medication data to be imported.")]
public async Task GetMedicationListPdf()
{
var vauHttpClientHandler = new VauHttpClientHandler(new ReturnInstanceVauClientProvider(vauClient, true));
var httpClient = new HttpClient(vauHttpClientHandler) { BaseAddress = Constants.EpaDeploymentUrl };
var medicationServiceClient = new MedicationServiceClient(httpClient);
using FileResponse response = await medicationServiceClient.RenderEMLAsPDFAsync(Guid.NewGuid(), "Z123456783", "Test/1.0");
await Verifier.Verify(response.Stream, "pdf");
}
private static IEnumerable ThrowingMethods()
{
var client = new VauClient(new HttpClient());
yield return new TestCaseData(() => client.DecryptResponse(new HttpResponseMessage()), nameof(client.DecryptResponse));
yield return new TestCaseData(() => client.EncryptRequest(new HttpRequestMessage { RequestUri = new Uri("https://example.com") }), nameof(client.EncryptRequest));
yield return new TestCaseData(() => client.GetStatus("Test/1.0"), nameof(client.GetStatus));
yield return new TestCaseData(() => client.SendMessage([]), nameof(client.SendMessage));
}
private class ReturnInstanceVauClientProvider(VauClient vauClient, bool doHandshake = false) : IVauClientProvider
{
public async Task<VauClient> GetVauClient(Uri uri)
{
if (doHandshake)
await vauClient.DoHandshake();
return vauClient;
}
}
private class VauClientProviderSingleInstance(IHttpClientFactory httpClientFactory) : IVauClientProvider
{
private VauClient vauClient;
public async Task<VauClient> GetVauClient(Uri uri)
{
if (vauClient != null)
return vauClient;
var httpClient = httpClientFactory.CreateClient();
httpClient.BaseAddress = new Uri(uri.GetLeftPart(UriPartial.Authority)); //Extract record system url
vauClient = new VauClient(httpClient);
await vauClient.DoHandshake();
return vauClient;
}
}
}
}