Skip to content

Commit ee631b0

Browse files
authored
Add property ["instanceContext", "instanceOwnerPartyType"] (#149)
Co-authored-by: Ivar <ivar.nesje@finanstilsynet.no>
1 parent d8a7e08 commit ee631b0

13 files changed

Lines changed: 94 additions & 58 deletions

File tree

src/Altinn.App.Core/Internal/Expressions/LayoutEvaluatorState.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ public string GetInstanceContext(string key)
170170
"instanceOwnerPartyId" => _instanceContext.InstanceOwner.PartyId,
171171
"appId" => _instanceContext.AppId,
172172
"instanceId" => _instanceContext.Id,
173+
"instanceOwnerPartyType" =>
174+
(
175+
!string.IsNullOrWhiteSpace(_instanceContext.InstanceOwner.OrganisationNumber)
176+
? "org"
177+
: !string.IsNullOrWhiteSpace(_instanceContext.InstanceOwner.PersonNumber)
178+
? "person"
179+
: !string.IsNullOrWhiteSpace(_instanceContext.InstanceOwner.Username)
180+
? "selfIdentified"
181+
: "unknown"
182+
),
173183
_ => throw new ExpressionEvaluatorTypeErrorException($"Unknown Instance context property {key}"),
174184
};
175185
}

test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/ExpressionTestCaseRoot.cs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,55 +51,15 @@ public class ExpressionTestCaseRoot
5151
[JsonPropertyName("frontendSettings")]
5252
public FrontEndSettings? FrontEndSettings { get; set; }
5353

54-
[JsonPropertyName("instanceContext")]
55-
[JsonConverter(typeof(InstanceConverter))]
56-
public Instance? InstanceContext { get; set; }
54+
[JsonPropertyName("instance")]
55+
public Instance? Instance { get; set; }
5756

5857
public override string ToString()
5958
{
6059
return $"{Filename}: {Name}";
6160
}
6261
}
6362

64-
public class InstanceConverter : JsonConverter<Instance>
65-
{
66-
public override Instance? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
67-
{
68-
var testInstance = JsonSerializer.Deserialize<InstanceForTestSpec>(ref reader, options);
69-
if (testInstance is null)
70-
{
71-
return null;
72-
}
73-
74-
return new Instance
75-
{
76-
AppId = testInstance.AppId,
77-
Id = testInstance.InstanceId,
78-
InstanceOwner = new()
79-
{
80-
PartyId = testInstance.InstanceOwnerPartyId,
81-
}
82-
};
83-
}
84-
85-
public override void Write(Utf8JsonWriter writer, Instance value, JsonSerializerOptions options)
86-
{
87-
throw new NotImplementedException();
88-
}
89-
90-
public class InstanceForTestSpec
91-
{
92-
[JsonPropertyName("instanceId")]
93-
public string InstanceId { get; set; } = default!;
94-
95-
[JsonPropertyName("appId")]
96-
public string AppId { get; set; } = default!;
97-
98-
[JsonPropertyName("instanceOwnerPartyId")]
99-
public string InstanceOwnerPartyId { get; set; } = default!;
100-
}
101-
}
102-
10363
public class ComponentContextForTestSpec
10464
{
10565
[JsonPropertyName("component")]

test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private void RunTestCase(ExpressionTestCaseRoot test)
9393
new JsonDataModel(test.DataModel),
9494
test.ComponentModel,
9595
test.FrontEndSettings ?? new(),
96-
test.InstanceContext ?? new());
96+
test.Instance ?? new());
9797

9898
if (test.ExpectsFailure is not null)
9999
{
@@ -176,6 +176,7 @@ public override IEnumerable<object[]> GetData(MethodInfo methodInfo)
176176
new JsonSerializerOptions
177177
{
178178
ReadCommentHandling = JsonCommentHandling.Skip,
179+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
179180
})!;
180181
}
181182
catch (Exception e)

test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestInvalid.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ public void Simple_Theory(InvalidTestCase testCase)
2929
_output.WriteLine(testCase.FullPath);
3030
Action act = () =>
3131
{
32-
var test = JsonSerializer.Deserialize<ExpressionTestCaseRoot>(testCase.RawJson!)!;
32+
var test = JsonSerializer.Deserialize<ExpressionTestCaseRoot>(
33+
testCase.RawJson!,
34+
new JsonSerializerOptions
35+
{
36+
ReadCommentHandling = JsonCommentHandling.Skip,
37+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
38+
})!;
3339
var state = new LayoutEvaluatorState(
3440
new JsonDataModel(test.DataModel),
3541
test.ComponentModel,
3642
test.FrontEndSettings ?? new(),
37-
test.InstanceContext ?? new());
43+
test.Instance ?? new());
3844
ExpressionEvaluator.EvaluateExpression(state, test.Expression, test.Context?.ToContext(test.ComponentModel) ?? null!);
3945
};
4046
act.Should().Throw<Exception>().WithMessage(testCase.ExpectsFailure);

test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/instanceContext/null.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
"name": "Looking up null",
33
"expression": ["instanceContext", null],
44
"expectsFailure": "Unknown Instance context property ",
5-
"instanceContext": {
6-
"instanceId": "d00ce51c-800b-416a-a906-ccab55f597e9",
5+
"instance": {
6+
"id": "d00ce51c-800b-416a-a906-ccab55f597e9",
77
"appId": "org/app-name",
8-
"instanceOwnerPartyId": "12345"
8+
"instanceOwner": {
9+
"partyId": "12345"
10+
}
911
}
1012
}

test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/instanceContext/only-dict.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Only works on one level (as a dictionary)",
33
"expression": ["equals", ["instanceContext", "deep.key"], null],
44
"expectsFailure": "Unknown Instance context property deep.key",
5-
"instanceContext": {
5+
"instance": {
66
"deep": {
77
"key": "should not be found"
88
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "PartyType is Org",
3+
"expression": ["instanceContext", "instanceOwnerPartyType"],
4+
"expects": "org",
5+
"instance": {
6+
"id": "d00ce51c-800b-416a-a906-ccab55f597e9",
7+
"appId": "org/app-name",
8+
"instanceOwner": {
9+
"partyId": "12345",
10+
"organisationNumber": "1234567"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "PartyType is Person",
3+
"expression": ["instanceContext", "instanceOwnerPartyType"],
4+
"expects": "person",
5+
"instance": {
6+
"id": "d00ce51c-800b-416a-a906-ccab55f597e9",
7+
"appId": "org/app-name",
8+
"instanceOwner": {
9+
"partyId": "12345",
10+
"personNumber": "1234567"
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "PartyType is Self Identified",
3+
"expression": ["instanceContext", "instanceOwnerPartyType"],
4+
"expects": "selfIdentified",
5+
"instance": {
6+
"id": "d00ce51c-800b-416a-a906-ccab55f597e9",
7+
"appId": "org/app-name",
8+
"instanceOwner": {
9+
"partyId": "12345",
10+
"username": "1234567"
11+
}
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "Party type unknown",
3+
"expression": ["instanceContext", "instanceOwnerPartyType"],
4+
"expects": "unknown",
5+
"instance": {
6+
"id": "d00ce51c-800b-416a-a906-ccab55f597e9",
7+
"appId": "org/app-name",
8+
"instanceOwner": {
9+
"partyId": "12345"
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)