Skip to content

Commit 6ad5c43

Browse files
committed
Release 1.0.0-rc1
Initial release candidate of the SDK targeting v3.1 of the AAS specification. The code is generated with: * [aas-core-meta 899add10] and * [aas-core-codegen 754e167]. [aas-core-meta 899add10]: aas-core-works/aas-core-meta@899add10 [aas-core-codegen 754e167]: aas-core-works/aas-core-codegen@754e167
1 parent 7ae525c commit 6ad5c43

File tree

17,015 files changed

+548660
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

17,015 files changed

+548660
-20
lines changed

dev_scripts/codegen/snippets/Types/Data_element/category_or_default.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/AasCore.Aas3_1.Tests/Common.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using Aas = AasCore.Aas3_1; // renamed
2+
3+
using System.Collections.Generic; // can't alias
4+
using System.Linq; // can't alias
5+
using NUnit.Framework; // can't alias
6+
7+
namespace AasCore.Aas3_1.Tests
8+
{
9+
/// <summary>
10+
/// Provide common functionality to be re-used across different tests
11+
/// such as reading of environment variables.
12+
/// </summary>
13+
public static class Common
14+
{
15+
public static readonly string RecordModeEnvironmentVariableName = (
16+
"AAS_CORE_AAS3_1_TESTS_RECORD_MODE"
17+
);
18+
19+
// NOTE (mristin, 2023-03-16):
20+
// It is tedious to record manually all the expected error messages. Therefore we include this variable
21+
// to steer the automatic recording. We intentionally inter-twine the recording code with the test code
22+
// to keep them close to each other so that they are easier to maintain.
23+
public static readonly bool RecordMode = (
24+
System.Environment.GetEnvironmentVariable(RecordModeEnvironmentVariableName)?.ToLower()
25+
== "true"
26+
|| System
27+
.Environment.GetEnvironmentVariable(RecordModeEnvironmentVariableName)
28+
?.ToLower() == "on"
29+
|| System
30+
.Environment.GetEnvironmentVariable(RecordModeEnvironmentVariableName)
31+
?.ToLower() == "1"
32+
|| System
33+
.Environment.GetEnvironmentVariable(RecordModeEnvironmentVariableName)
34+
?.ToLower() == "yes"
35+
);
36+
37+
public static readonly string TestDataDir = (
38+
System.Environment.GetEnvironmentVariable("AAS_CORE_AAS3_1_TESTS_TEST_DATA_DIR")
39+
?? throw new System.InvalidOperationException(
40+
"The path to the test data directory is missing in the environment: "
41+
+ "AAS_CORE_AAS3_1_TESTS_TEST_DATA_DIR"
42+
)
43+
);
44+
45+
/// <summary>
46+
/// Find the first instance of <typeparamref name="T"/> in the <paramref name="container" />,
47+
/// including the <paramref name="container" /> itself.
48+
/// </summary>
49+
public static T MustFind<T>(Aas.IClass container)
50+
where T : Aas.IClass
51+
{
52+
var instance = (
53+
(container is T)
54+
? container
55+
: container.Descend().First(something => something is T)
56+
?? throw new System.InvalidOperationException(
57+
$"No instance of {nameof(T)} could be found"
58+
)
59+
);
60+
61+
return (T)instance;
62+
}
63+
64+
public static void AssertNoVerificationErrors(List<Aas.Reporting.Error> errors, string path)
65+
{
66+
if (errors.Count > 0)
67+
{
68+
var builder = new System.Text.StringBuilder();
69+
builder.Append(
70+
$"Expected no errors when verifying the instance de-serialized from {path}, "
71+
+ $"but got {errors.Count} error(s):\n"
72+
);
73+
for (var i = 0; i < errors.Count; i++)
74+
{
75+
builder.Append(
76+
$"Error {i + 1}:\n"
77+
+ $"{Reporting.GenerateJsonPath(errors[i].PathSegments)}: {errors[i].Cause}\n"
78+
);
79+
}
80+
81+
Assert.Fail(builder.ToString());
82+
}
83+
}
84+
85+
public static void AssertEqualsExpectedOrRerecordVerificationErrors(
86+
List<Aas.Reporting.Error> errors,
87+
string path
88+
)
89+
{
90+
if (errors.Count == 0)
91+
{
92+
Assert.Fail(
93+
$"Expected at least one verification error when verifying {path}, but got none"
94+
);
95+
}
96+
97+
string got = string.Join(
98+
";\n",
99+
errors.Select(error =>
100+
$"{Reporting.GenerateJsonPath(error.PathSegments)}: {error.Cause}"
101+
)
102+
);
103+
104+
string errorsPath = path + ".errors";
105+
if (RecordMode)
106+
{
107+
System.IO.File.WriteAllText(errorsPath, got);
108+
}
109+
else
110+
{
111+
if (!System.IO.File.Exists(errorsPath))
112+
{
113+
throw new System.IO.FileNotFoundException(
114+
"The file with the recorded errors does not "
115+
+ $"exist: {errorsPath}; maybe you want to set the environment "
116+
+ $"variable {Aas.Tests.Common.RecordModeEnvironmentVariableName}?"
117+
);
118+
}
119+
120+
string expected = System.IO.File.ReadAllText(errorsPath);
121+
Assert.AreEqual(
122+
expected.Replace("\r\n", "\n"),
123+
got.Replace("\r\n", "\n"),
124+
$"The expected verification errors do not match the actual ones for the file {path}"
125+
);
126+
}
127+
}
128+
}
129+
}
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
using Aas = AasCore.Aas3_1; // renamed
2+
using FileMode = System.IO.FileMode;
3+
using FileStream = System.IO.FileStream;
4+
using JsonException = System.Text.Json.JsonException;
5+
using Nodes = System.Text.Json.Nodes;
6+
7+
using System.Collections.Generic; // can't alias
8+
using System.Linq; // can't alias
9+
10+
namespace AasCore.Aas3_1.Tests
11+
{
12+
public static class CommonJson
13+
{
14+
public static Nodes.JsonNode ReadFromFile(string path)
15+
{
16+
using var stream = new FileStream(path, FileMode.Open);
17+
Nodes.JsonNode? node;
18+
try
19+
{
20+
node = Nodes.JsonNode.Parse(stream);
21+
}
22+
catch (JsonException exception)
23+
{
24+
throw new System.InvalidOperationException(
25+
$"Expected the file to be a valid JSON, but it was not: {path}; "
26+
+ $"exception was: {exception}"
27+
);
28+
}
29+
30+
if (node is null)
31+
{
32+
throw new System.InvalidOperationException(
33+
"Expected the file to be a non-null JSON value, " + $"but it was null: {path}"
34+
);
35+
}
36+
37+
return node;
38+
}
39+
40+
/// <summary>
41+
/// Serialize <paremref name="something" /> to a uniform JSON text
42+
/// such that we can use it for comparisons in the tests.
43+
/// </summary>
44+
public static Nodes.JsonNode ToJson(object something)
45+
{
46+
switch (something)
47+
{
48+
case bool aBool:
49+
return Nodes.JsonValue.Create(aBool);
50+
case long aLong:
51+
return Nodes.JsonValue.Create(aLong);
52+
case double aDouble:
53+
return Nodes.JsonValue.Create(aDouble);
54+
case string aString:
55+
return Nodes.JsonValue.Create(aString)
56+
?? throw new System.InvalidOperationException(
57+
$"Could not convert {something} " + "to a JSON string"
58+
);
59+
case byte[] someBytes:
60+
return Nodes.JsonValue.Create(System.Convert.ToBase64String(someBytes))
61+
?? throw new System.InvalidOperationException(
62+
$"Could not convert {something} to " + "a base64-encoded JSON string"
63+
);
64+
case Aas.IClass instance:
65+
return Aas.Jsonization.Serialize.ToJsonObject(instance);
66+
default:
67+
throw new System.ArgumentException(
68+
$"The conversion of type {something.GetType()} "
69+
+ $"to a JSON node has not been defined: {something}"
70+
);
71+
}
72+
}
73+
74+
/// <summary>
75+
/// Infer the node kind of the JSON node.
76+
/// </summary>
77+
/// <remarks>
78+
/// This function is necessary since NET6 does not fully support node kinds yet.
79+
/// See:
80+
/// <ul>
81+
/// <li>https://github.com/dotnet/runtime/issues/53406</li>
82+
/// <li>https://github.com/dotnet/runtime/issues/55827</li>
83+
/// <li>https://github.com/dotnet/runtime/issues/56592</li>
84+
/// </ul>
85+
/// </remarks>
86+
private static string GetNodeKind(Nodes.JsonNode node)
87+
{
88+
switch (node)
89+
{
90+
case Nodes.JsonArray _:
91+
return "array";
92+
case Nodes.JsonObject _:
93+
return "object";
94+
case Nodes.JsonValue _:
95+
return "value";
96+
default:
97+
throw new System.InvalidOperationException(
98+
$"Unhandled JsonNode: {node.GetType()}"
99+
);
100+
}
101+
}
102+
103+
public static void CheckJsonNodesEqual(
104+
Nodes.JsonNode that,
105+
Nodes.JsonNode other,
106+
out Reporting.Error? error
107+
)
108+
{
109+
error = null;
110+
111+
var thatNodeKind = GetNodeKind(that);
112+
var otherNodeKind = GetNodeKind(other);
113+
114+
if (thatNodeKind != otherNodeKind)
115+
{
116+
error = new Reporting.Error(
117+
$"Mismatch in node kinds : {thatNodeKind} != {otherNodeKind}"
118+
);
119+
return;
120+
}
121+
122+
switch (that)
123+
{
124+
case Nodes.JsonArray thatArray:
125+
{
126+
var otherArray = (other as Nodes.JsonArray)!;
127+
if (thatArray.Count != otherArray.Count)
128+
{
129+
error = new Reporting.Error(
130+
$"Unequal array lengths: {thatArray.Count} != {otherArray.Count}"
131+
);
132+
return;
133+
}
134+
135+
for (int i = 0; i < thatArray.Count; i++)
136+
{
137+
CheckJsonNodesEqual(thatArray[i]!, otherArray[i]!, out error);
138+
if (error != null)
139+
{
140+
error.PrependSegment(new Reporting.IndexSegment(i));
141+
return;
142+
}
143+
}
144+
145+
break;
146+
}
147+
case Nodes.JsonObject thatObject:
148+
{
149+
var thatDictionary = thatObject as IDictionary<string, Nodes.JsonNode>;
150+
var otherDictionary = (other as IDictionary<string, Nodes.JsonNode>)!;
151+
152+
var thatKeys = thatDictionary.Keys.ToList();
153+
thatKeys.Sort();
154+
155+
var otherKeys = otherDictionary.Keys.ToList();
156+
otherKeys.Sort();
157+
158+
if (!thatKeys.SequenceEqual(otherKeys))
159+
{
160+
error = new Reporting.Error(
161+
"Objects with different properties: "
162+
+ $"{string.Join(", ", thatKeys)} != "
163+
+ $"{string.Join(", ", otherKeys)}"
164+
);
165+
return;
166+
}
167+
168+
foreach (var key in thatKeys)
169+
{
170+
CheckJsonNodesEqual(thatDictionary[key], otherDictionary[key], out error);
171+
if (error != null)
172+
{
173+
error.PrependSegment(new Reporting.NameSegment(key));
174+
return;
175+
}
176+
}
177+
178+
break;
179+
}
180+
case Nodes.JsonValue thatValue:
181+
{
182+
string thatAsJsonString = thatValue.ToJsonString();
183+
184+
// NOTE (mristin, 2023-03-16):
185+
// This is slow, but there is no way around it at the moment with NET6.
186+
// See:
187+
// * https://github.com/dotnet/runtime/issues/56592
188+
// * https://github.com/dotnet/runtime/issues/55827
189+
// * https://github.com/dotnet/runtime/issues/53406
190+
var otherValue = (other as Nodes.JsonValue)!;
191+
string otherAsJsonString = otherValue.ToJsonString();
192+
193+
if (thatAsJsonString != otherAsJsonString)
194+
{
195+
error = new Reporting.Error(
196+
$"Unequal values: {thatAsJsonString} != {otherAsJsonString}"
197+
);
198+
// ReSharper disable once RedundantJumpStatement
199+
return;
200+
}
201+
202+
break;
203+
}
204+
default:
205+
throw new System.InvalidOperationException(
206+
$"Unhandled JSON node: {that.GetType()}"
207+
);
208+
}
209+
}
210+
211+
/// <summary>
212+
/// Load an instance from a JSON file.
213+
/// </summary>
214+
public static Aas.IClass LoadInstance(string path)
215+
{
216+
Aas.IClass? instance;
217+
218+
{
219+
using var stream = new FileStream(path, FileMode.Open);
220+
var node =
221+
Nodes.JsonNode.Parse(stream)
222+
?? throw new System.InvalidOperationException("node unexpectedly null");
223+
224+
instance = AasCore.Aas3_1.Jsonization.Deserialize.EnvironmentFrom(node);
225+
}
226+
227+
if (instance == null)
228+
{
229+
throw new System.InvalidOperationException("environment unexpectedly null");
230+
}
231+
232+
return instance;
233+
}
234+
}
235+
}

0 commit comments

Comments
 (0)