This repository has been archived by the owner on May 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
AddToPortfolioFunctionShould.cs
95 lines (75 loc) · 3.53 KB
/
AddToPortfolioFunctionShould.cs
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
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace InvestFunctionApp.EndToEndTests
{
public class AddToPortfolioFunctionShould
{
private string CreateInvestorFunctionKey;
private string GetInvestorFunctionKey;
private string PortfolioFunctionKey;
private readonly ITestOutputHelper Output;
public AddToPortfolioFunctionShould(ITestOutputHelper output)
{
Output = output;
ReadTestEnvironmentVariables();
}
[Fact]
public async Task BuyStocks()
{
const int initialValueOfStocks = 100;
const int amountToInvest = 42;
var startingInvestorDetails = new Investor
{
PartitionKey = "IndividualInvestor",
RowKey = Guid.NewGuid().ToString(),
CurrentValueOfStocks = initialValueOfStocks,
CurrentValueOfBonds = 100,
TargetPercentageAllocationToStocks = 75,
TargetPercentageAllocationToBonds = 25
};
await CreateTestInvestorInTableStorage(startingInvestorDetails);
await InvokeAddToPortfolioFunction(startingInvestorDetails.RowKey, amountToInvest);
// Wait for a while
await Task.Delay(TimeSpan.FromMinutes(2));
var resultingInvestor = await GetInvestor(startingInvestorDetails.RowKey);
Assert.Equal(initialValueOfStocks + amountToInvest, resultingInvestor.CurrentValueOfStocks);
}
private async Task CreateTestInvestorInTableStorage(Investor investor)
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsJsonAsync($"https://investfunctionappdemotest.azurewebsites.net/api/testing/createinvestor?code={CreateInvestorFunctionKey}", investor);
response.EnsureSuccessStatusCode();
}
private async Task InvokeAddToPortfolioFunction(string investorId, int amount)
{
var url = $"https://investfunctionappdemotest.azurewebsites.net/api/portfolio/{investorId}?code={PortfolioFunctionKey}";
var deposit = new Deposit { Amount = amount };
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsJsonAsync(url, deposit);
response.EnsureSuccessStatusCode();
}
private async Task<Investor> GetInvestor(string investorId)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync($"https://investfunctionappdemotest.azurewebsites.net/api/testing/getinvestor/{investorId}?code={GetInvestorFunctionKey}");
response.EnsureSuccessStatusCode();
return JsonConvert.DeserializeObject<Investor>(await response.Content.ReadAsStringAsync());
}
private void ReadTestEnvironmentVariables()
{
CreateInvestorFunctionKey = ReadEnvironmentVariable("CreateInvestorFunctionKey");
GetInvestorFunctionKey = ReadEnvironmentVariable("GetInvestorInvestorFunctionKey");
PortfolioFunctionKey = ReadEnvironmentVariable("PortfolioFunctionKey");
}
private string ReadEnvironmentVariable(string variableName)
{
var value = Environment.GetEnvironmentVariable(variableName);
Output.WriteLine($"'{variableName}'='{value ?? "NOT SET"}'");
return value;
}
}
}