Skip to content

Commit

Permalink
add test (which doesn't really cover the case we're interested in)
Browse files Browse the repository at this point in the history
anonymize
  • Loading branch information
hf-kklein committed Jan 17, 2024
1 parent 9315268 commit 4337aaa
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,39 @@ public VerwendungszweckJsonConverter()
{
if (reader.TokenType == JsonTokenType.StartArray)
{
var retList = new List<BO4E.ENUM.Verwendungszweck>();
var retList = new List<ENUM.Verwendungszweck>();
reader.Read();
while (reader.TokenType != JsonTokenType.EndArray)
{
retList.Add(System.Text.Json.JsonSerializer.Deserialize<BO4E.ENUM.Verwendungszweck>(ref reader, defaultOptions));
retList.Add(JsonSerializer.Deserialize<ENUM.Verwendungszweck>(ref reader, defaultOptions));
//read end object
reader.Read();
}

return new BO4E.COM.Verwendungszweck { Marktrolle = Marktrolle.LF, Zweck = retList };
return new Verwendungszweck { Marktrolle = Marktrolle.LF, Zweck = retList };
}

if (reader.TokenType == JsonTokenType.String)
{
var retList = new List<BO4E.ENUM.Verwendungszweck>
var retList = new List<ENUM.Verwendungszweck>
{
Enum.Parse<BO4E.ENUM.Verwendungszweck>(reader.GetString().ToUpper())
Enum.Parse<ENUM.Verwendungszweck>(reader.GetString().ToUpper())
};
return new BO4E.COM.Verwendungszweck { Marktrolle = Marktrolle.LF, Zweck = retList };
return new Verwendungszweck { Marktrolle = Marktrolle.LF, Zweck = retList };
}

return reader.TokenType switch
{
JsonTokenType.Null => null,
JsonTokenType.StartObject => System.Text.Json.JsonSerializer.Deserialize<BO4E.COM.Verwendungszweck>(ref reader, defaultOptions),
JsonTokenType.StartObject => JsonSerializer.Deserialize<Verwendungszweck>(ref reader, defaultOptions),
_ => null
};
}

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, BO4E.COM.Verwendungszweck value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, Verwendungszweck value, JsonSerializerOptions options)
{
System.Text.Json.JsonSerializer.Serialize(writer, value, defaultOptions);
JsonSerializer.Serialize(writer, value, defaultOptions);
}
}
}
3 changes: 3 additions & 0 deletions BO4ETestProject/TestBO4E.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
<None Update="testjsons\zähler.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\marktlokation_with_verwendungszwecke.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions BO4ETestProject/TestVerwendungszweckeDeserialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using BO4E.BO;
using BO4E.meta.LenientConverters;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using JsonConverter = Newtonsoft.Json.JsonConverter;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace TestBO4E
{
[TestClass]
public class TestVerwendungszweckeDeserialization
{
private void _TestVerwendungszweckeDeserialization(Func<Marktlokation, string> serializer, Func<string, Marktlokation> deserializer)
{
var jsonString = File.ReadAllText("testjsons/marktlokation_with_verwendungszwecke.json");
var malo = deserializer(jsonString);
malo.Should().NotBeNull();
malo.Zaehlwerke.Single().Verwendungszwecke.Should().NotBeEmpty();
}

[TestMethod]
public void TestVerwendunszweckDeserializationNewtonsoft()
{
var settings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> { new VerwendungszweckJsonNetConverter(), new StringEnumConverter() }
};
_TestVerwendungszweckeDeserialization(_malo => JsonConvert.SerializeObject(_malo, settings), str => JsonConvert.DeserializeObject<Marktlokation>(str, settings));
}

[TestMethod]
public void TestVerwendunszweckDeserializationSystemText()
{
var jsonOptions = new JsonSerializerOptions
{
Converters = { new JsonStringEnumConverter(), new VerwendungszweckJsonConverter() }
};
_TestVerwendungszweckeDeserialization(_malo => JsonSerializer.Serialize(_malo, jsonOptions), str => JsonSerializer.Deserialize<Marktlokation>(str, jsonOptions));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"boTyp": "MARKTLOKATION",
"versionStruktur": "1",
"marktlokationsId": "VerwendungszweckJsonConverter",
"sparte": "STROM",
"energierichtung": "AUSSP",
"netzebene": "NSP",
"bilanzierungsgebiet": "11YN10000762-01E",
"lokationsadresse": {
"postleitzahl": "12345",
"ort": "Teststadt",
"strasse": "Teststr.",
"hausnummer": "6B",
"landescode": "DE",
"ortsteil": "Blub"
},
"marktrollen": [
{
"code": "4033872000058",
"marktrolle": "UENB"
},
{
"code": "9906214000003",
"marktrolle": "MSB",
"weiterverpflichtet": false
}
],
"regelzone": "10YDE-EON------1",
"zeitreihentyp": "SLS",
"zaehlwerke": [
{
"obisKennzahl": "1-1:1.9.0",
"verwendungszwecke": [
{
"marktrolle": "LF",
"zweck": [
"ENDKUNDENABRECHNUNG",
"NETZNUTZUNGSABRECHNUNG",
"MEHRMINDERMENGENABRECHNUNG"
]
}
],
"verbrauchsart": "KL",
"unterbrechbarkeit": "NUV",
"anzahlAblesungen": 1
}
]
}

0 comments on commit 4337aaa

Please sign in to comment.