Skip to content

Commit

Permalink
fix: Read Point from Period and not Series elements
Browse files Browse the repository at this point in the history
  • Loading branch information
MWO1024 committed Dec 19, 2024
1 parent f3d098e commit 25642a3
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,48 @@ protected override IIncomingMessageSeries ParseTransaction(JsonElement transacti
var meteringPointLocationId = transactionElement.GetProperty(MeteringPointIdentificationElementName)
.GetProperty(ValueElementName)
.ToString();

var meteringPointType =
transactionElement.GetProperty(MeteringPointTypeElementName).GetProperty(ValueElementName).ToString();

var registrationDateAndOrTime =
transactionElement.GetProperty(RegistrationDateAndOrTimeElementName).ToString();

var productNumber =
transactionElement.GetProperty(ProductNumberElementName).ToString();

var productUnitType =
transactionElement.GetProperty(ProductUnitTypeElementName).GetProperty(ValueElementName).ToString();

var periodElement = transactionElement.GetProperty(PeriodElementName);
var resolution = periodElement.GetProperty(ResolutionElementName).ToString();
var startDateAndOrTimeDateTime = periodElement.GetProperty(TimeIntervalElementName).GetProperty(StartElementName).GetProperty(ValueElementName).ToString();
var endDateAndOrTimeDateTime = periodElement.GetProperty(TimeIntervalElementName).GetProperty(EndElementName).GetProperty(ValueElementName).ToString();

var energyObservations = new List<EnergyObservation>();
JsonElement? pointsElements = transactionElement.TryGetProperty(PointElementName, out var pointsElement)
JsonElement? pointsElements = periodElement.TryGetProperty(PointElementName, out var pointsElement)
? pointsElement
: null;

if (pointsElements != null)
{
foreach (var pointElement in pointsElements.Value.EnumerateArray())
{
energyObservations.Add(new EnergyObservation(
pointElement.GetProperty(PositionElementName).GetProperty(ValueElementName).ToString(),
pointElement.GetProperty(QualityElementName).GetProperty(ValueElementName).ToString(),
pointElement.GetProperty(QuantityElementName).ToString()));
var position = pointElement.GetProperty(PositionElementName).GetProperty(ValueElementName).ToString();
string? quality = null;
string? quantity = null;

if (pointElement.TryGetProperty(QualityElementName, out var qualityElement))
{
quality = qualityElement.GetProperty(ValueElementName).ToString();
}

if (pointElement.TryGetProperty(QuantityElementName, out var quantityElement))
{
quantity = quantityElement.ToString();
}

energyObservations.Add(new EnergyObservation(position, quantity, quality));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public static IncomingMarketMessageStream CreateIncomingMessage(
receiverNumber,
schema ?? "urn:ediel.org:measure:notifyvalidatedmeasuredata:0:1");
}
else if (format == DocumentFormat.Json)
{
content = GetJson(
senderActorNumber,
series,
messageType,
processType,
businessType,
messageId,
senderRole,
receiverNumber);
}
else
{
throw new ArgumentOutOfRangeException(nameof(format), format, "Unsupported document format");
Expand Down Expand Up @@ -190,6 +202,94 @@ private static string GetXml(
return doc.OuterXml;
}

private static string GetJson(
ActorNumber senderActorNumber,
IReadOnlyCollection<(string TransactionId, Instant PeriodStart, Instant PeriodEnd, Resolution Resolution)>
series,
string messageType,
string processType,
string businessType,
string messageId,
string senderRole,
string receiverNumber) =>
$$"""
{
"NotifyValidatedMeasureData_MarketDocument": {
"mRID": "{{messageId}}",
"businessSector.type": {
"value": "{{businessType}}"
},
"createdDateTime": "2022-12-17T09:30:47Z",
"process.processType": {
"value": "{{processType}}"
},
"receiver_MarketParticipant.mRID": {
"codingScheme": "A10",
"value": "{{receiverNumber}}"
},
"receiver_MarketParticipant.marketRole.type": {
"value": "DGL"
},
"sender_MarketParticipant.mRID": {
"codingScheme": "A10",
"value": "{{senderActorNumber.Value}}"
},
"sender_MarketParticipant.marketRole.type": {
"value": "{{senderRole}}"
},
"type": {
"value": "{{messageType}}"
},
"Series": [
{{string.Join(",\n", series.Select(s =>
$$"""
{
"mRID": "{{s.TransactionId}}",
"marketEvaluationPoint.mRID": {
"codingScheme": "A10",
"value": "579999993331812345"
},
"marketEvaluationPoint.type": {
"value": "E17"
},
"originalTransactionIDReference_Series.mRID": "C1875000",
"product": "8716867000030",
"quantity_Measure_Unit.name": {
"value": "KWH"
},
"registration_DateAndOrTime.dateTime": "2022-12-17T07:30:00Z",
"Period": {
"resolution": "{{s.Resolution.Code}}",
"timeInterval": {
"start": {
"value": "{{s.PeriodStart.ToString("yyyy-MM-ddTHH:mm'Z'", null)}}"
},
"end": {
"value": "{{s.PeriodEnd.ToString("yyyy-MM-ddTHH:mm'Z'", null)}}"
}
},
"Point": [
{{string.Join(",\n", GetEnergyObservations(s.Resolution).Select(e =>
$$"""
{
"position": {
"value": {{e.Position}}
},
"quality": {
"value": "A03"
},
"quantity": {{e.Quantity}}
}
"""))}}
]
}
}
"""))}}
]
}
}
""";

private static ReadOnlyCollection<(int Position, int Quantity)> GetEnergyObservations(Resolution resolution)
{
var observations = new List<(int Position, int Quantity)>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,26 @@ public sealed class GivenMeteredDataForMeasurementPointTests(
ITestOutputHelper testOutputHelper)
: MeteredDataForMeasurementPointBehaviourTestBase(integrationTestFixture, testOutputHelper)
{
public static TheoryData<DocumentFormat> PeekFormats =>
[
DocumentFormat.Json,
DocumentFormat.Xml,
];
public static TheoryData<DocumentFormat, DocumentFormat> RequestAndPeekFormatPairs
{
get
{
var data = new TheoryData<DocumentFormat, DocumentFormat>();
foreach (var requestFormat in (DocumentFormat[])[DocumentFormat.Json, DocumentFormat.Xml])
{
foreach (var peekFormat in (DocumentFormat[])[DocumentFormat.Json, DocumentFormat.Xml])
{
data.Add(requestFormat, peekFormat);
}
}

return data;
}
}

[Theory]
[MemberData(nameof(PeekFormats))]
public async Task When_ActorPeeksAllMessages_Then_ReceivesOneDocumentWithCorrectContent(DocumentFormat peekFormat)
[MemberData(nameof(RequestAndPeekFormatPairs))]
public async Task When_ActorPeeksAllMessages_Then_ReceivesOneDocumentWithCorrectContent(DocumentFormat requestFormat, DocumentFormat peekFormat)
{
// Arrange
var senderSpy = CreateServiceBusSenderSpy();
Expand All @@ -60,7 +71,7 @@ public async Task When_ActorPeeksAllMessages_Then_ReceivesOneDocumentWithCorrect
var transactionId2 = $"{transactionIdPrefix}-2";

await GivenReceivedMeteredDataForMeasurementPoint(
documentFormat: DocumentFormat.Xml,
documentFormat: requestFormat,
senderActorNumber: currentActor.ActorNumber,
[
(transactionId1,
Expand Down Expand Up @@ -164,8 +175,9 @@ await ThenNotifyValidatedMeasureDataDocumentIsCorrect(
}

[Theory]
[MemberData(nameof(PeekFormats))]
[MemberData(nameof(RequestAndPeekFormatPairs))]
public async Task AndGiven_MessageIsEmpty_When_ActorPeeksAllMessages_Then_ReceivesNoMessages(
DocumentFormat requestFormat,
DocumentFormat peekFormat)
{
// Arrange
Expand All @@ -176,7 +188,7 @@ public async Task AndGiven_MessageIsEmpty_When_ActorPeeksAllMessages_Then_Receiv
GivenAuthenticatedActorIs(currentActor.ActorNumber, currentActor.ActorRole);

await GivenReceivedMeteredDataForMeasurementPoint(
documentFormat: DocumentFormat.Xml,
documentFormat: requestFormat,
senderActorNumber: currentActor.ActorNumber,
[]);

Expand Down

0 comments on commit 25642a3

Please sign in to comment.