Skip to content

Commit

Permalink
Merge branch 'main' into cbs/add_workflow_for_c4_model
Browse files Browse the repository at this point in the history
  • Loading branch information
djorgensendk authored Mar 23, 2023
2 parents 8f01da4 + 81d730b commit 5e92ed9
Show file tree
Hide file tree
Showing 29 changed files with 210 additions and 233 deletions.
27 changes: 9 additions & 18 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,44 @@ updates:
- package-ecosystem: "nuget"
directory: "/source/ApplyDBMigrationsApp"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"

- package-ecosystem: "nuget"
directory: "/source/Api"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"

- package-ecosystem: "nuget"
directory: "/source/Application"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"

- package-ecosystem: "nuget"
directory: "/source/ArchitectureTests"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"

- package-ecosystem: "nuget"
directory: "/source/CimMessageAdapter"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"

- package-ecosystem: "nuget"
directory: "/source/CimMessageAdapter.Tests"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"

- package-ecosystem: "nuget"
directory: "/source/Infrastructure"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"

- package-ecosystem: "nuget"
directory: "/source/IntegrationTests"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"

- package-ecosystem: "nuget"
directory: "/source/Tests"
schedule:
interval: "weekly"
open-pull-requests-limit: 0
interval: "monthly"
6 changes: 3 additions & 3 deletions source/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ limitations under the License.
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Energinet.DataHub.Core.App.Common.Security" Version="7.3.4" />
<PackageReference Include="Energinet.DataHub.Core.App.FunctionApp" Version="7.3.4" />
<PackageReference Include="Energinet.DataHub.Core.App.Common.Security" Version="7.4.0" />
<PackageReference Include="Energinet.DataHub.Core.App.FunctionApp" Version="7.4.0" />
<PackageReference Include="Energinet.DataHub.Core.Logging" Version="2.2.3" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Abstractions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.7.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.10.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.10.1" />
<PackageReference Update="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion source/Api/OutgoingMessages/DequeueRequestListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task<HttpResponseData> RunAsync(
FunctionContext executionContext,
string messageId)
{
var result = await _mediator.Send(new DequeueRequest(Guid.Parse(messageId))).ConfigureAwait(false);
var result = await _mediator.Send(new DequeueRequest(messageId)).ConfigureAwait(false);
return result.Success
? request.CreateResponse(HttpStatusCode.OK)
: request.CreateResponse(HttpStatusCode.BadRequest);
Expand Down
4 changes: 2 additions & 2 deletions source/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ limitations under the License.
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="MediatR" Version="12.0.1" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="NodaTime" Version="3.1.6" />
<PackageReference Update="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ public Task<DequeueResult> Handle(DequeueRequest request, CancellationToken canc
{
ArgumentNullException.ThrowIfNull(request);

return _bundledMessages.DequeueAsync(request.MessageId);
if (Guid.TryParse(request.MessageId, out var messageId) == false)
{
return Task.FromResult(new DequeueResult(false));
}

return _bundledMessages.DequeueAsync(messageId);
}
}

public record DequeueRequest(Guid MessageId) : ICommand<DequeueResult>;
public record DequeueRequest(string MessageId) : ICommand<DequeueResult>;

public record DequeueResult(bool Success);
4 changes: 2 additions & 2 deletions source/CimMessageAdapter/CimMessageAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ limitations under the License.
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.12.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.13.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Update="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion source/DocumentValidation/DocumentValidation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonSchema.Net" Version="3.3.2" />
<PackageReference Include="JsonSchema.Net" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
// limitations under the License.

using System;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Newtonsoft.Json;

namespace Infrastructure.Configuration.MessageBus.RemoteBusinessServices;

Expand All @@ -35,13 +35,13 @@ public RemoteBusinessService(IRemoteBusinessServiceRequestSenderAdapter<TRequest
public Task SendRequestAsync(TRequest message, string correlationId)
{
ArgumentNullException.ThrowIfNull(message);
var serviceBusMessage = new ServiceBusMessage(JsonConvert.SerializeObject(message))

var serviceBusMessage = new ServiceBusMessage(JsonSerializer.Serialize(message))
{
ContentType = "application/json",
CorrelationId = correlationId,
ReplyTo = _responseQueueName,
};

return _requestSender.SendAsync(serviceBusMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Xml;
using Application.IncomingMessages.RequestChangeOfSupplier;
using CimMessageAdapter.Errors;
using CimMessageAdapter.Messages;
using DocumentValidation;
using Domain.OutgoingMessages;
using Json.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using JsonException = Newtonsoft.Json.JsonException;
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
using MessageHeader = Application.IncomingMessages.MessageHeader;

namespace Infrastructure.IncomingMessages.RequestChangeOfSupplier;
Expand All @@ -36,6 +35,7 @@ public class JsonMessageParser : IMessageParser<MarketActivityRecord, RequestCha
{
private const string MarketActivityRecordElementName = "MktActivityRecord";
private const string HeaderElementName = "RequestChangeOfSupplier_MarketDocument";
private const string DocumentName = "RequestChangeOfSupplier";
private readonly ISchemaProvider _schemaProvider;
private readonly List<ValidationError> _errors = new();

Expand All @@ -50,20 +50,10 @@ public async Task<MessageParserResult<MarketActivityRecord, RequestChangeOfSuppl
{
if (message == null) throw new ArgumentNullException(nameof(message));

string processType;
try
{
processType = GetBusinessProcessType(message);
}
catch (JsonException exception)
{
return InvalidJsonFailure(exception);
}

var schema = await _schemaProvider.GetSchemaAsync<JsonSchema>(processType, "0").ConfigureAwait(false);
var schema = await _schemaProvider.GetSchemaAsync<JsonSchema>(DocumentName.ToUpper(CultureInfo.InvariantCulture), "0").ConfigureAwait(false);
if (schema is null)
{
return new MessageParserResult<MarketActivityRecord, RequestChangeOfSupplierTransaction>(new UnknownBusinessProcessTypeOrVersion(processType, "0"));
return new MessageParserResult<MarketActivityRecord, RequestChangeOfSupplierTransaction>(new UnknownBusinessProcessTypeOrVersion(DocumentName, "0"));
}

ResetMessagePosition(message);
Expand All @@ -75,14 +65,13 @@ public async Task<MessageParserResult<MarketActivityRecord, RequestChangeOfSuppl
return new MessageParserResult<MarketActivityRecord, RequestChangeOfSupplierTransaction>(_errors.ToArray());
}

var streamReader = new StreamReader(message, leaveOpen: true);
try
{
using (var jsonTextReader = new JsonTextReader(streamReader))
using (var document = await JsonDocument.ParseAsync(message).ConfigureAwait(false))
{
try
{
return ParseJsonData(jsonTextReader);
return ParseJsonData(document);
}
catch (JsonException exception)
{
Expand All @@ -100,45 +89,20 @@ public async Task<MessageParserResult<MarketActivityRecord, RequestChangeOfSuppl
}
finally
{
streamReader.Dispose();
}
}

private static string[] SplitNamespace(Stream message)
private static MessageParserResult<MarketActivityRecord, RequestChangeOfSupplierTransaction> ParseJsonData(JsonDocument document)
{
if (message == null) throw new ArgumentNullException(nameof(message));

string[] split;
ResetMessagePosition(message);
var streamReader = new StreamReader(message, leaveOpen: true);
using (var jsonTextReader = new JsonTextReader(streamReader))
var marketActivityRecords = new List<MarketActivityRecord>();
var messageHeader = MessageHeaderFrom(document.RootElement.GetProperty(HeaderElementName));
var marketActivityRecord = document.RootElement.GetProperty(HeaderElementName).GetProperty(MarketActivityRecordElementName);
var records = marketActivityRecord.EnumerateArray();
foreach (var jsonElement in records)
{
var serializer = new JsonSerializer();
var deserialized = (JObject)serializer.Deserialize(jsonTextReader);
var path = deserialized.First.Path;
split = path.Split('_');
marketActivityRecords.Add(MarketActivityRecordFrom(jsonElement));
}

return split;
}

private static string GetBusinessProcessType(Stream message)
{
if (message == null) throw new ArgumentNullException(nameof(message));
var split = SplitNamespace(message);
var processType = split[0];
return processType;
}

private static MessageParserResult<MarketActivityRecord, RequestChangeOfSupplierTransaction> ParseJsonData(JsonTextReader jsonTextReader)
{
var marketActivityRecords = new List<MarketActivityRecord>();
var serializer = new JsonSerializer();
var jsonRequest = serializer.Deserialize<JObject>(jsonTextReader);
var headerToken = jsonRequest.SelectToken(HeaderElementName);
var messageHeader = MessageHeaderFrom(headerToken);
marketActivityRecords.AddRange(headerToken[MarketActivityRecordElementName].Select(MarketActivityRecordFrom));

return new MessageParserResult<MarketActivityRecord, RequestChangeOfSupplierTransaction>(new RequestChangeOfSupplierIncomingMarketDocument(messageHeader, marketActivityRecords));
}

Expand All @@ -153,67 +117,71 @@ private static MessageParserResult<MarketActivityRecord, RequestChangeOfSupplier
return new MessageParserResult<MarketActivityRecord, RequestChangeOfSupplierTransaction>(InvalidMessageStructure.From(exception));
}

private static MessageHeader MessageHeaderFrom(JToken token)
private static MessageHeader MessageHeaderFrom(JsonElement element)
{
return new MessageHeader(
token["mRID"].ToString(),
token["process.processType"]["value"].ToString(),
token["sender_MarketParticipant.mRID"]["value"].ToString(),
token["sender_MarketParticipant.marketRole.type"]["value"].ToString(),
token["receiver_MarketParticipant.mRID"]["value"].ToString(),
token["receiver_MarketParticipant.marketRole.type"]["value"].ToString(),
GetJsonDateStringWithoutQuotes(token["createdDateTime"]));
element.GetProperty("mRID").ToString(),
element.GetProperty("process.processType").GetProperty("value").ToString(),
element.GetProperty("sender_MarketParticipant.mRID").GetProperty("value").ToString(),
element.GetProperty("sender_MarketParticipant.marketRole.type").GetProperty("value").ToString(),
element.GetProperty("receiver_MarketParticipant.mRID").GetProperty("value").ToString(),
element.GetProperty("receiver_MarketParticipant.marketRole.type").GetProperty("value").ToString(),
GetJsonDateStringWithoutQuotes(element.GetProperty("createdDateTime")));
}

private static MarketActivityRecord MarketActivityRecordFrom(JToken token)
private static MarketActivityRecord MarketActivityRecordFrom(JsonElement element)
{
return new MarketActivityRecord()
{
Id = token["mRID"].ToString(),
ConsumerId = token["marketEvaluationPoint.customer_MarketParticipant.mRID"]["value"].ToString(),
ConsumerIdType = token["marketEvaluationPoint.customer_MarketParticipant.mRID"]["codingScheme"].ToString(),
BalanceResponsibleId = token["marketEvaluationPoint.balanceResponsibleParty_MarketParticipant.mRID"]["value"].ToString(),
EnergySupplierId = token["marketEvaluationPoint.energySupplier_MarketParticipant.mRID"]["value"].ToString(),
MarketEvaluationPointId = token["marketEvaluationPoint.mRID"]["value"].ToString(),
ConsumerName = token["marketEvaluationPoint.customer_MarketParticipant.name"].ToString(),
EffectiveDate = GetJsonDateStringWithoutQuotes(token["start_DateAndOrTime.dateTime"]),
Id = element.GetProperty("mRID").ToString(),
ConsumerId = element.GetProperty("marketEvaluationPoint.customer_MarketParticipant.mRID").GetProperty("value").ToString(),
ConsumerIdType = element.GetProperty("marketEvaluationPoint.customer_MarketParticipant.mRID").GetProperty("codingScheme").ToString(),
BalanceResponsibleId = element.GetProperty("marketEvaluationPoint.balanceResponsibleParty_MarketParticipant.mRID").GetProperty("value").ToString(),
EnergySupplierId = element.GetProperty("marketEvaluationPoint.energySupplier_MarketParticipant.mRID").GetProperty("value").ToString(),
MarketEvaluationPointId = element.GetProperty("marketEvaluationPoint.mRID").GetProperty("value").ToString(),
ConsumerName = element.GetProperty("marketEvaluationPoint.customer_MarketParticipant.name").ToString(),
EffectiveDate = GetJsonDateStringWithoutQuotes(element.GetProperty("start_DateAndOrTime.dateTime")),
};
}

private static string GetJsonDateStringWithoutQuotes(JToken token)
private static string GetJsonDateStringWithoutQuotes(JsonElement element)
{
return token.ToString(Formatting.None).Trim('"');
return element.ToString().Trim('"');
}

private static bool IsValid(JsonDocument document, JsonSchema schema)
{
return schema.Evaluate(document, new EvaluationOptions() { OutputFormat = OutputFormat.Flag, }).IsValid;
}

private async Task ValidateMessageAsync(JsonSchema schema, Stream message)
{
var jsonDocument = await JsonDocument.ParseAsync(message).ConfigureAwait(false);

var validationOptions = new ValidationOptions()
if (IsValid(jsonDocument, schema) == false)
{
OutputFormat = OutputFormat.Detailed,
};

var validationResult = schema.Validate(jsonDocument, validationOptions);

if (!validationResult.IsValid)
{
AddValidationErrors(validationResult);
ExtractValidationErrors(jsonDocument, schema);
}

ResetMessagePosition(message);
}

private void AddValidationErrors(ValidationResults validationResult)
private void ExtractValidationErrors(JsonDocument jsonDocument, JsonSchema schema)
{
AddValidationError(validationResult.Message);
var result = schema.Evaluate(jsonDocument, new EvaluationOptions() { OutputFormat = OutputFormat.List, });
result
.Details
.Where(detail => detail.HasErrors)
.ToList().ForEach(AddValidationErrors);
}

if (validationResult.HasNestedResults)
private void AddValidationErrors(EvaluationResults validationResult)
{
var propertyName = validationResult.InstanceLocation.ToString();
var errorsValues = validationResult.Errors ?? new Dictionary<string, string>();
foreach (var error in errorsValues)
{
foreach (var result in validationResult.NestedResults)
{
AddValidationError(result.Message);
}
AddValidationError($"{propertyName}: {error}");
}
}

Expand Down
Loading

0 comments on commit 5e92ed9

Please sign in to comment.