-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathCreateResourceTests.cs
More file actions
214 lines (180 loc) · 8.94 KB
/
CreateResourceTests.cs
File metadata and controls
214 lines (180 loc) · 8.94 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System.Net;
using FluentAssertions;
using JsonApiDotNetCore.OpenApi.Client.NSwag;
using Microsoft.EntityFrameworkCore;
using OpenApiNSwagEndToEndTests.RestrictedControllers.GeneratedCode;
using OpenApiTests;
using OpenApiTests.RestrictedControllers;
using TestBuildingBlocks;
using Xunit;
using Xunit.Abstractions;
namespace OpenApiNSwagEndToEndTests.RestrictedControllers;
public sealed class CreateResourceTests : IClassFixture<IntegrationTestContext<OpenApiStartup<RestrictionDbContext>, RestrictionDbContext>>, IDisposable
{
private readonly IntegrationTestContext<OpenApiStartup<RestrictionDbContext>, RestrictionDbContext> _testContext;
private readonly XUnitLogHttpMessageHandler _logHttpMessageHandler;
private readonly RestrictionFakers _fakers = new();
public CreateResourceTests(IntegrationTestContext<OpenApiStartup<RestrictionDbContext>, RestrictionDbContext> testContext,
ITestOutputHelper testOutputHelper)
{
_testContext = testContext;
_logHttpMessageHandler = new XUnitLogHttpMessageHandler(testOutputHelper);
testContext.UseController<WriteOnlyChannelsController>();
}
[Fact]
public async Task Can_create_resource_with_includes_and_fieldsets()
{
// Arrange
DataStream existingVideoStream = _fakers.DataStream.GenerateOne();
DataStream existingAudioStream = _fakers.DataStream.GenerateOne();
WriteOnlyChannel newChannel = _fakers.WriteOnlyChannel.GenerateOne();
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.DataStreams.AddRange(existingVideoStream, existingAudioStream);
await dbContext.SaveChangesAsync();
});
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
var apiClient = new RestrictedControllersClient(httpClient);
var requestBody = new CreateWriteOnlyChannelRequestDocument
{
Data = new DataInCreateWriteOnlyChannelRequest
{
Attributes = new AttributesInCreateWriteOnlyChannelRequest
{
Name = newChannel.Name,
IsAdultOnly = newChannel.IsAdultOnly
},
Relationships = new RelationshipsInCreateWriteOnlyChannelRequest
{
VideoStream = new ToOneDataStreamInRequest
{
Data = new DataStreamIdentifierInRequest
{
Id = existingVideoStream.StringId!
}
},
AudioStreams = new ToManyDataStreamInRequest
{
Data =
[
new DataStreamIdentifierInRequest
{
Id = existingAudioStream.StringId!
}
]
}
}
}
};
var queryString = new Dictionary<string, string?>
{
["include"] = "videoStream,audioStreams",
["fields[writeOnlyChannels]"] = "name,isCommercial,videoStream,audioStreams",
["fields[dataStreams]"] = "bytesTransmitted"
};
// Act
PrimaryWriteOnlyChannelResponseDocument? response =
await ApiResponse.TranslateAsync(async () => await apiClient.PostWriteOnlyChannelAsync(requestBody, queryString));
response.Should().NotBeNull();
response.Data.Attributes.Should().NotBeNull();
response.Data.Attributes.Name.Should().Be(newChannel.Name);
response.Data.Attributes.IsCommercial.Should().BeNull();
response.Data.Attributes.IsAdultOnly.Should().BeNull();
response.Data.Relationships.Should().NotBeNull();
response.Data.Relationships.VideoStream.Should().NotBeNull();
response.Data.Relationships.VideoStream.Data.Should().NotBeNull();
response.Data.Relationships.VideoStream.Data.Id.Should().Be(existingVideoStream.StringId);
response.Data.Relationships.UltraHighDefinitionVideoStream.Should().BeNull();
response.Data.Relationships.AudioStreams.Should().NotBeNull();
response.Data.Relationships.AudioStreams.Data.Should().HaveCount(1);
response.Data.Relationships.AudioStreams.Data.ElementAt(0).Id.Should().Be(existingAudioStream.StringId);
response.Included.Should().HaveCount(2);
response.Included.OfType<DataInDataStreamResponse>().Should().ContainSingle(include => include.Id == existingVideoStream.StringId).Subject
.With(include =>
{
include.Attributes.Should().NotBeNull();
include.Attributes.BytesTransmitted.Should().Be((long?)existingVideoStream.BytesTransmitted);
});
response.Included.OfType<DataInDataStreamResponse>().Should().ContainSingle(include => include.Id == existingAudioStream.StringId).Subject
.With(include =>
{
include.Attributes.Should().NotBeNull();
include.Attributes.BytesTransmitted.Should().Be((long?)existingAudioStream.BytesTransmitted);
});
long newChannelId = long.Parse(response.Data.Id.Should().NotBeNull().And.Subject);
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
// @formatter:wrap_chained_method_calls chop_always
// @formatter:keep_existing_linebreaks true
WriteOnlyChannel channelInDatabase = await dbContext.WriteOnlyChannels
.Include(channel => channel.VideoStream)
.Include(channel => channel.AudioStreams)
.FirstWithIdAsync(newChannelId);
// @formatter:keep_existing_linebreaks restore
// @formatter:wrap_chained_method_calls restore
channelInDatabase.Name.Should().Be(newChannel.Name);
channelInDatabase.IsCommercial.Should().BeNull();
channelInDatabase.IsAdultOnly.Should().Be(newChannel.IsAdultOnly);
channelInDatabase.VideoStream.Should().NotBeNull();
channelInDatabase.VideoStream.Id.Should().Be(existingVideoStream.Id);
channelInDatabase.AudioStreams.Should().HaveCount(1);
channelInDatabase.AudioStreams.ElementAt(0).Id.Should().Be(existingAudioStream.Id);
});
}
[Fact]
public async Task Cannot_create_resource_for_missing_request_body()
{
// Arrange
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
var apiClient = new RestrictedControllersClient(httpClient);
CreateWriteOnlyChannelRequestDocument requestBody = null!;
// Act
Func<Task> action = async () => _ = await apiClient.PostWriteOnlyChannelAsync(requestBody);
// Assert
await action.Should().ThrowExactlyAsync<ArgumentNullException>().WithParameterName("body");
}
[Fact]
public async Task Cannot_create_resource_with_unknown_relationship_ID()
{
// Arrange
WriteOnlyChannel newChannel = _fakers.WriteOnlyChannel.GenerateOne();
string unknownVideoStreamId = Unknown.StringId.For<DataStream, long>();
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
var apiClient = new RestrictedControllersClient(httpClient);
var requestBody = new CreateWriteOnlyChannelRequestDocument
{
Data = new DataInCreateWriteOnlyChannelRequest
{
Attributes = new AttributesInCreateWriteOnlyChannelRequest
{
Name = newChannel.Name
},
Relationships = new RelationshipsInCreateWriteOnlyChannelRequest
{
VideoStream = new ToOneDataStreamInRequest
{
Data = new DataStreamIdentifierInRequest
{
Id = unknownVideoStreamId
}
}
}
}
};
// Act
Func<Task> action = async () => _ = await apiClient.PostWriteOnlyChannelAsync(requestBody);
// Assert
ApiException<ErrorResponseDocument> exception = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which;
exception.StatusCode.Should().Be((int)HttpStatusCode.NotFound);
exception.Message.Should().Be("HTTP 404: A related resource does not exist.");
exception.Result.Errors.Should().HaveCount(1);
ErrorObject error = exception.Result.Errors.ElementAt(0);
error.Status.Should().Be("404");
error.Title.Should().Be("A related resource does not exist.");
error.Detail.Should().Be($"Related resource of type 'dataStreams' with ID '{unknownVideoStreamId}' in relationship 'videoStream' does not exist.");
}
public void Dispose()
{
_logHttpMessageHandler.Dispose();
}
}