Skip to content

Commit

Permalink
- use PostOperation where possible
Browse files Browse the repository at this point in the history
- Use same unit JsoinSerializer tests for Net and NetCore
- Add extra datamodel version checks to JsonSerializers
  • Loading branch information
Alexander van Delft committed Nov 27, 2024
1 parent 53eb823 commit 64abba5
Show file tree
Hide file tree
Showing 153 changed files with 3,123 additions and 494 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
</ItemGroup>

<ItemGroup>
<None Update="PostOperation\CreateDerivedQuantityKindPostMessage.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="PostOperation\ReOderFactorsOfDerivedQuantityKindPostMessage.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\bigmodel.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"_delete": [],
"_create": [
{
"alias": [],
"category": [],
"classKind": "DerivedQuantityKind",
"defaultScale": "56863161-fa3a-47b8-a3cd-16465f734b27",
"definition": [],
"excludedDomain": [],
"excludedPerson": [],
"hyperLink": [],
"iid": "08c130ca-a270-408f-8069-b7e1d00659f0",
"isDeprecated": false,
"modifiedOn": "2018-08-12T14:10:55.560Z",
"name": "derivedQuantityKind",
"possibleScale": [ "56863161-fa3a-47b8-a3cd-16465f734b27" ],
"quantityDimensionSymbol": "dqk",
"quantityKindFactor": [
{
"k": -12551680,
"v": "11aa4db6-40ef-4368-92ac-bda8af9b27a9"
},
{
"k": 77606679,
"v": "125abfbd-f717-42c0-91da-738316159ea5"
}
],
"revisionNumber": 0,
"shortName": "derivedQuantityKind",
"symbol": "derivedQuantityKind"
},
{
"classKind": "QuantityKindFactor",
"excludedDomain": [],
"excludedPerson": [],
"exponent": "1",
"iid": "11aa4db6-40ef-4368-92ac-bda8af9b27a9",
"modifiedOn": "2018-08-12T14:10:45.476Z",
"quantityKind": "9513aca9-5397-48a1-8187-1683490bdb4f",
"revisionNumber": 0
},
{
"classKind": "QuantityKindFactor",
"excludedDomain": [],
"excludedPerson": [],
"exponent": "2",
"iid": "125abfbd-f717-42c0-91da-738316159ea5",
"modifiedOn": "2018-08-12T14:10:50.681Z",
"quantityKind": "1c53f8f4-024b-4f42-987a-439901a4bcb2",
"revisionNumber": 0
}
],
"_update": [
{
"classKind": "SiteReferenceDataLibrary",
"iid": "bff9f871-3b7f-4e57-ac82-5ab499f9baf5",
"parameterType": [ "08c130ca-a270-408f-8069-b7e1d00659f0" ]
}
],
"_copy": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="JsonSerializerPostOperationTestFixture.cs" company="Starion Group S.A.">
// Copyright (c) 2015-2024 Starion Group S.A.
//
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou
//
// This file is part of CDP4-COMET SDK Community Edition
//
// The CDP4-COMET SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-COMET SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CDP4JsonSerializer.NetCore.Tests.PostOperation
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using CDP4Common.DTO;
using CDP4Common.MetaInfo;
using CDP4Common.Types;

using CDP4DalCommon.Protocol.Operations;

using NUnit.Framework;

/// <summary>
/// Suite of tests to verify that PostOperation deserialization works as expected
/// </summary>
[TestFixture]
public class JsonSerializerPostOperationTestFixture
{
private IMetaDataProvider metaDataProvider;

private Version supportedVersion;

[SetUp]
public void SetUp()
{
this.metaDataProvider = new MetaDataProvider();
this.supportedVersion = new Version(1,1,0);
}

[Test]
public void Verify_that_post_message_with_move_keys_can_be_deserialized()
{
var postMessage = System.IO.File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "PostOperation/ReOderFactorsOfDerivedQuantityKindPostMessage.json"));
var byteArray = Encoding.UTF8.GetBytes(postMessage);
var stream = new MemoryStream(byteArray);

var cdp4JsonSerializer = new Cdp4DalJsonSerializer(this.metaDataProvider, this.supportedVersion, false);

var cdpPostOperation = cdp4JsonSerializer.Deserialize<PostOperation>(stream);

Assert.That(cdpPostOperation.Create, Is.Empty);
Assert.That(cdpPostOperation.Delete, Is.Empty);
Assert.That(cdpPostOperation.Copy, Is.Empty);

var classlessDto = cdpPostOperation.Update.First();

var orderedItems = classlessDto["QuantityKindFactor"] as IEnumerable<OrderedItem>;

var orderedItem_1 = orderedItems.First();
orderedItem_1.M = 77606679;

var orderedItem_2 = orderedItems.Last();
orderedItem_2.M = -12551680;
}

[Test]
public void Verify_that_post_message_can_be_deserialized()
{
var postMessage = System.IO.File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "PostOperation/CreateDerivedQuantityKindPostMessage.json"));
var byteArray = Encoding.UTF8.GetBytes(postMessage);
var stream = new MemoryStream(byteArray);

var cdp4JsonSerializer = new Cdp4DalJsonSerializer(this.metaDataProvider, this.supportedVersion, false);

var cdpPostOperation = cdp4JsonSerializer.Deserialize<PostOperation>(stream);

Assert.That(cdpPostOperation.Delete, Is.Empty);
Assert.That(cdpPostOperation.Copy, Is.Empty);

var derivedQuantityKind = cdpPostOperation.Create.First() as DerivedQuantityKind;
var qkf_1 = derivedQuantityKind.QuantityKindFactor.Single(x => x.K == -12551680);

Assert.That(qkf_1.V, Is.Not.Null);

Assert.That(qkf_1.M, Is.Null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"_delete": [],
"_create": [],
"_update": [
{
"classKind": "DerivedQuantityKind",
"iid": "08c130ca-a270-408f-8069-b7e1d00659f0",
"modifiedOn": "2018-08-12T14:11:56.580Z",
"quantityKindFactor": [
{
"k": -12551680,
"m": 77606679,
"v": "11aa4db6-40ef-4368-92ac-bda8af9b27a9"
},
{
"k": 77606679,
"m": -12551680,
"v": "125abfbd-f717-42c0-91da-738316159ea5"
}
]
}
],
"_copy": []
}
50 changes: 0 additions & 50 deletions CDP4JsonSerializer.Tests/PostOperation/CdpPostOperation.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace CDP4JsonSerializer.Tests.PostOperation
using CDP4Common.MetaInfo;
using CDP4Common.Types;

using CDP4JsonSerializer.Tests.Cdp4PostOperation;
using CDP4DalCommon.Protocol.Operations;

using NUnit.Framework;

Expand Down Expand Up @@ -62,9 +62,9 @@ public void Verify_that_post_message_with_move_keys_can_be_deserialized()
var byteArray = Encoding.UTF8.GetBytes(postMessage);
var stream = new MemoryStream(byteArray);

var cdp4JsonSerializer = new Cdp4JsonSerializer(this.metaDataProvider, this.supportedVersion);
var cdp4JsonSerializer = new Cdp4DalJsonSerializer(this.metaDataProvider, this.supportedVersion, false);

var cdpPostOperation = cdp4JsonSerializer.Deserialize<CdpPostOperation>(stream);
var cdpPostOperation = cdp4JsonSerializer.Deserialize<PostOperation>(stream);

Assert.That(cdpPostOperation.Create, Is.Empty);
Assert.That(cdpPostOperation.Delete, Is.Empty);
Expand All @@ -88,9 +88,9 @@ public void Verify_that_post_message_can_be_deserialized()
var byteArray = Encoding.UTF8.GetBytes(postMessage);
var stream = new MemoryStream(byteArray);

var cdp4JsonSerializer = new Cdp4JsonSerializer(this.metaDataProvider, this.supportedVersion);
var cdp4JsonSerializer = new Cdp4DalJsonSerializer(this.metaDataProvider, this.supportedVersion, false);

var cdpPostOperation = cdp4JsonSerializer.Deserialize<CdpPostOperation>(stream);
var cdpPostOperation = cdp4JsonSerializer.Deserialize<PostOperation>(stream);

Assert.That(cdpPostOperation.Delete, Is.Empty);
Assert.That(cdpPostOperation.Copy, Is.Empty);
Expand Down
23 changes: 20 additions & 3 deletions CDP4JsonSerializer/AutoGenDtoSerializer/ActionItemSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="ActionItemSerializer.cs" company="Starion Group S.A.">
// -------------------------------------------------------------------------------------------------------------------------------// <copyright file="ActionItemSerializer.cs" company="Starion Group S.A.">
// Copyright (c) 2015-2024 Starion Group S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary, Jaime Bernar
Expand Down Expand Up @@ -50,6 +49,19 @@ namespace CDP4JsonSerializer
/// </summary>
public class ActionItemSerializer : BaseThingSerializer, IThingSerializer
{
/// <summary>
/// The minimal <see cref="Version" /> that is allowed for serialization of a <see cref="ActionItem" />.
/// An error will be thrown when a Requested Data Model version for Serialization is lower than this.
/// </summary>
private static Version minimalAllowedDataModelVersion = Version.Parse("1.0.0");

/// <summary>
/// The minimal <see cref="Version" /> that is allowed for serialization of a <see cref="ActionItem" />.
/// When a Requested Data Model version for Serialization is lower than this, the object will not be Serialized, just ignored.
/// NO error will be thrown when a Requested Data Model version for Serialization is lower than this.
/// </summary>
private static Version thingMinimalAllowedDataModelVersion = Version.Parse("1.1.0");

/// <summary>
/// Serializes a <see cref="Thing" /> into an <see cref="Utf8JsonWriter" />
/// </summary>
Expand All @@ -65,7 +77,12 @@ public void Serialize(Thing thing, Utf8JsonWriter writer, Version requestedDataM
throw new ArgumentException("The thing shall be a ActionItem", nameof(thing));
}

if (requestedDataModelVersion < Version.Parse("1.1.0"))
if (requestedDataModelVersion < minimalAllowedDataModelVersion)
{
throw new NotSupportedException($"The provided version {requestedDataModelVersion.ToString(3)} is not supported for serialization of ActionItem.");
}

if (requestedDataModelVersion < thingMinimalAllowedDataModelVersion)
{
Logger.Log(LogLevel.Info, "Skipping serialization of ActionItem since Version is below 1.1.0");
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="ActualFiniteStateListSerializer.cs" company="Starion Group S.A.">
// -------------------------------------------------------------------------------------------------------------------------------// <copyright file="ActualFiniteStateListSerializer.cs" company="Starion Group S.A.">
// Copyright (c) 2015-2024 Starion Group S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary, Jaime Bernar
Expand Down Expand Up @@ -50,6 +49,19 @@ namespace CDP4JsonSerializer
/// </summary>
public class ActualFiniteStateListSerializer : BaseThingSerializer, IThingSerializer
{
/// <summary>
/// The minimal <see cref="Version" /> that is allowed for serialization of a <see cref="ActualFiniteStateList" />.
/// An error will be thrown when a Requested Data Model version for Serialization is lower than this.
/// </summary>
private static Version minimalAllowedDataModelVersion = Version.Parse("1.0.0");

/// <summary>
/// The minimal <see cref="Version" /> that is allowed for serialization of a <see cref="ActualFiniteStateList" />.
/// When a Requested Data Model version for Serialization is lower than this, the object will not be Serialized, just ignored.
/// NO error will be thrown when a Requested Data Model version for Serialization is lower than this.
/// </summary>
private static Version thingMinimalAllowedDataModelVersion = Version.Parse("1.0.0");

/// <summary>
/// Serializes a <see cref="Thing" /> into an <see cref="Utf8JsonWriter" />
/// </summary>
Expand All @@ -65,7 +77,12 @@ public void Serialize(Thing thing, Utf8JsonWriter writer, Version requestedDataM
throw new ArgumentException("The thing shall be a ActualFiniteStateList", nameof(thing));
}

if (requestedDataModelVersion < Version.Parse("1.0.0"))
if (requestedDataModelVersion < minimalAllowedDataModelVersion)
{
throw new NotSupportedException($"The provided version {requestedDataModelVersion.ToString(3)} is not supported for serialization of ActualFiniteStateList.");
}

if (requestedDataModelVersion < thingMinimalAllowedDataModelVersion)
{
Logger.Log(LogLevel.Info, "Skipping serialization of ActualFiniteStateList since Version is below 1.0.0");
return;
Expand Down
Loading

0 comments on commit 64abba5

Please sign in to comment.