Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Handle AggregatedMeasuredData only in EDI #1311

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/B2BApi.AppTests/Fixtures/B2BApiAppFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private FunctionAppHostSettings CreateAppHostSettings(string csprojName, ref int

appHostSettings.ProcessEnvironmentVariables.Add(
"FeatureManagement__RequestStaysInEdi",
false.ToString().ToLower());
true.ToString().ToLower());

appHostSettings.ProcessEnvironmentVariables.Add(
"FeatureManagement__ReceiveMeteredDataForMeasurementPoints",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Diagnostics;
using System.Dynamic;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using Energinet.DataHub.Core.FunctionApp.TestCommon.FunctionAppHost;
using Energinet.DataHub.EDI.AuditLog.AuditLogger;
using Energinet.DataHub.EDI.AuditLog.AuditLogOutbox;
using Energinet.DataHub.EDI.B2BApi.AppTests.Fixtures;
Expand All @@ -40,6 +42,10 @@ namespace Energinet.DataHub.EDI.B2BApi.AppTests.IncomingMessages;
[Collection(nameof(B2BApiAppCollectionFixture))]
public class IncomingMessageReceiverTests : IAsyncLifetime
{
private readonly ActorNumber _actorNumber = ActorNumber.Create("5790000392551");
private readonly ActorRole _actorRole = ActorRole.EnergySupplier;
private readonly string _externalId = Guid.NewGuid().ToString();

public IncomingMessageReceiverTests(B2BApiAppFixture fixture, ITestOutputHelper testOutputHelper)
{
Fixture = fixture;
Expand All @@ -53,6 +59,7 @@ public async Task InitializeAsync()
Fixture.AppHostManager.ClearHostLog();
await using var context = Fixture.DatabaseManager.CreateDbContext<OutboxContext>();
await context.Outbox.ExecuteDeleteAsync();
await Fixture.DatabaseManager.AddActorAsync(_actorNumber, _externalId);
}

public Task DisposeAsync()
Expand Down Expand Up @@ -83,6 +90,52 @@ public async Task Given_PersistedActor_When_CallingIncomingMessagesWithValidDocu
actualResponse.StatusCode.Should().Be(HttpStatusCode.Accepted);
}

[Fact]
public async Task Given_PersistedActor_When_SendingRequestForAggregatedMeasuredData_Then_ResponseShouldBeAccepted_AndRequestCanBePeeked()
{
// Arrange
var b2BToken = new JwtBuilder()
.WithRole(ClaimsMap.RoleFrom(_actorRole).Value)
.WithClaim(ClaimsMap.ActorId, _externalId)
.CreateToken();

using var request = await CreateHttpRequest(
"TestData/Messages/json/RequestAggregatedMeasureData.json",
IncomingDocumentType.RequestAggregatedMeasureData.Name,
"application/json");

// Act
await Fixture.AppHostManager.HttpClient.SendAsync(request);

// Assert
await Fixture.AppHostManager.TriggerFunctionAsync("TenSecondsHasPassed");

var stopWatch = Stopwatch.StartNew();
var timeoutAfter = TimeSpan.FromMinutes(1);

while (stopWatch.ElapsedMilliseconds < timeoutAfter.TotalMilliseconds)
{
using var peekRequest = new HttpRequestMessage(HttpMethod.Get, $"api/peek/aggregations");
peekRequest.Content = new StringContent(
string.Empty,
Encoding.UTF8,
"application/json");
peekRequest.Headers.Authorization = new AuthenticationHeaderValue("bearer", b2BToken);
using var response = await Fixture.AppHostManager.HttpClient.SendAsync(peekRequest);

if (response.StatusCode == HttpStatusCode.OK)
{
// Assert
using var assertionScope = new AssertionScope();
response.Should().NotBeNull();
response.StatusCode.Should().Be(HttpStatusCode.OK);
(await response.Content.ReadAsStringAsync()).Should().Contain("RejectRequestAggregatedMeasureData_MarketDocument");
}

await Task.Delay(500);
}
}

[Fact]
public async Task
Given_PersistedActor_When_CallingIncomingMessagesWithInvalidDocumentAndBearerToken_Then_ResponseShouldBeBadRequest()
Expand Down Expand Up @@ -169,24 +222,15 @@ private async Task<HttpRequestMessage> CreateHttpRequest(string filePath, string
HttpRequestMessage? request = null;
try
{
// The following must match with the JSON/XML document content
var actorNumber = ActorNumber.Create("5790000392551");
var actorRole = ActorRole.EnergySupplier;
// Add new Ids to ensure the request is accepted.
var document = await File.ReadAllTextAsync(filePath);
document = document
.Replace("{MessageId}", Guid.NewGuid().ToString())
.Replace("{TransactionId}", Guid.NewGuid().ToString());

// The actor must exist in the database
var externalId = Guid.NewGuid().ToString();
await Fixture.DatabaseManager.AddActorAsync(actorNumber, externalId);

// The bearer token must contain:
// * the actor role matching the document content
// * the external id matching the actor in the database
var b2bToken = new JwtBuilder()
.WithRole(ClaimsMap.RoleFrom(actorRole).Value)
.WithClaim(ClaimsMap.ActorId, externalId)
.WithRole(ClaimsMap.RoleFrom(_actorRole).Value)
.WithClaim(ClaimsMap.ActorId, _externalId)
.CreateToken();

request = new HttpRequestMessage(HttpMethod.Post, $"api/incomingMessages/{documentType}")
Expand Down
Loading