Skip to content

Commit

Permalink
Requested changes for changing seeding files and attribute value chec…
Browse files Browse the repository at this point in the history
…k added
  • Loading branch information
AnuragNagpure committed Apr 17, 2024
1 parent 7196061 commit 517b82d
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ public interface IPolicyRepository
IAsyncEnumerable<(string TechnicalKey, string LeftOperand, (AttributeKeyId? Key, IEnumerable<string> Values) Attributes, string? RightOperandValue)> GetPolicyForOperandContent(PolicyTypeId type, IEnumerable<string> technicalKeys);
Task<bool> CheckPolicyByTechnicalKeys(PolicyTypeId type, IEnumerable<string> technicalKeys);
IAsyncEnumerable<string> GetAllTechnicalKeys();
Task<bool> CheckPolicyAttributeValue(PolicyTypeId type, IEnumerable<string> values);

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ public IAsyncEnumerable<string> GetAllTechnicalKeys() =>
.Where(x => x.IsActive)
.Select(x => x.TechnicalKey)
.AsAsyncEnumerable();

public Task<bool> CheckPolicyAttributeValue(PolicyTypeId type, IEnumerable<string> values) =>
_dbContext.PolicyAttributes
.Where(p =>
p.IsActive &&
p.Policy!.Types.Any(t => t.IsActive && t.Id == type) &&
values.Contains(p.AttributeValue)).AnyAsync();

}
26 changes: 1 addition & 25 deletions src/database/PolicyHub.Migrations/Seeder/Data/policies.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,7 @@
"description": "With the Framework Credential, only those participants which have signed the respective framework agreement (general or via a specific version) are allowed to view or negotiate the respective data offer. Generic: \"rightOperand\": \"active\"; specific \"rightOperand\": \"active:{version}\"",
"is_active": true,
"attribute_key_id": 5
},
{
"id": "01a0fba3-9b6e-435a-b045-e0e890c300b7",
"kind_id": 4,
"technical_key": "purpose.trace.v1.TraceBattery",
"description": "Facilitating compliance with mandatory regulatory requirements for tracking and reporting battery cells, modules & high-voltage batteries.",
"is_active": true,
"attribute_key_id": 2
},
{
"id": "01a0fba3-9b6e-435a-b045-e0e890c300b8",
"kind_id": 4,
"technical_key": "purpose.trace.v1.aspects",
"description": "Establishing a digital representation of the automotive supply chain to enable a component specific data exchange.",
"is_active": true,
"attribute_key_id": 2
},
{
"id": "01a0fba3-9b6e-435a-b045-e0e890c300b9",
"kind_id": 4,
"technical_key": "purpose.trace.v1.qualityanalysis",
"description": " The data can be used for quality analysis to identify and select affected components and to send quality notifications to affected customers or suppliers.",
"is_active": true,
"attribute_key_id": 2
},
},
{
"id": "01a0fba3-9b6e-435a-b045-e0e890c300c1",
"kind_id": 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b4", "policy_type_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b5", "policy_type_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b6", "policy_type_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b7", "policy_type_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b8", "policy_type_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b9", "policy_type_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c1", "policy_type_id": 1, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c1", "policy_type_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c2", "policy_type_id": 2, "is_active": true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b4", "use_case_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b5", "use_case_id": 3, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b6", "use_case_id": 4, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b7", "use_case_id": 1, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b8", "use_case_id": 1, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b9", "use_case_id": 1, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c1", "use_case_id": 1, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c1", "use_case_id": 2, "is_active": true },
{ "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c1", "use_case_id": 3, "is_active": true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@
"is_active": true
},
{
"policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b7",
"policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c2",
"key": 2,
"attribute_value": "purpose.trace.v1.TraceBattery",
"is_active": true
},
{
"policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b8",
"policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c2",
"key": 2,
"attribute_value": "purpose.trace.v1.aspects",
"is_active": true
},
{
"policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300b9",
"policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c2",
"key": 2,
"attribute_value": "purpose.trace.v1.qualityanalysis",
"is_active": true
Expand All @@ -82,11 +82,5 @@
"key": 4,
"attribute_value": "VW",
"is_active": true
},
{
"policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c2",
"key": 2,
"attribute_value": "ID Trace 3.1",
"is_active": true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ private static PolicyFileContent CreateFileContent(PolicyTypeId type, OperatorId

public async Task<PolicyResponse> GetPolicyContentAsync(PolicyContentRequest requestData)
{
var IsAttributeValueExists = await _hubRepositories.GetInstance<IPolicyRepository>().CheckPolicyAttributeValue(requestData.PolicyType, requestData.Constraints.Select(x => x.Value)).ConfigureAwait(false);
if (!IsAttributeValueExists)
{
throw new ControllerArgumentException($"Policy for type {requestData.PolicyType} and requested attribute values does not exists.");
}

var keyCounts = requestData.Constraints
.GroupBy(pair => pair.Key)
.ToDictionary(group => group.Key, group => group.Count());
Expand Down
19 changes: 8 additions & 11 deletions tests/database/PolicyHub.DbAccess.Tests/PolicyRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,14 @@ public async Task GetPolicyTypes_ReturnsExpectedResult()
var result = await sut.GetPolicyTypes(null, null).ToListAsync();

// Assert
result.Should().NotBeEmpty().And.HaveCount(11).And.Satisfy(
result.Should().NotBeEmpty().And.HaveCount(8).And.Satisfy(
x => x.TechnicalKey == "BusinessPartnerNumber",
x => x.TechnicalKey == "Membership",
x => x.TechnicalKey == "FrameworkAgreement.traceability",
x => x.TechnicalKey == "FrameworkAgreement.quality",
x => x.TechnicalKey == "FrameworkAgreement.pcf",
x => x.TechnicalKey == "FrameworkAgreement.behavioraltwin",
x => x.TechnicalKey == "purpose.trace.v1.TraceBattery",
x => x.TechnicalKey == "purpose.trace.v1.aspects",
x => x.TechnicalKey == "companyRole.dismantler",
x => x.TechnicalKey == "purpose.trace.v1.qualityanalysis",
x => x.TechnicalKey == "purpose"
);
}
Expand Down Expand Up @@ -132,14 +129,14 @@ public async Task GetPolicyContentAsync_WithoutRightOperand_ReturnsExpectedResul
var sut = await CreateSut();

// Act
var result = await sut.GetPolicyContentAsync(null, PolicyTypeId.Usage, "purpose.trace.v1.TraceBattery");
var result = await sut.GetPolicyContentAsync(null, PolicyTypeId.Usage, "Membership");

// Assert
result.Exists.Should().BeTrue();
result.Attributes.Key.Should().Be(AttributeKeyId.Static);
result.Attributes.Values.Should().ContainSingle()
.And.Satisfy(x => x == "purpose.trace.v1.TraceBattery");
result.LeftOperand.Should().Be("purpose.trace.v1.TraceBattery");
.And.Satisfy(x => x == "active");
result.LeftOperand.Should().Be("Membership");
result.RightOperandValue.Should().BeNull();
}

Expand Down Expand Up @@ -172,15 +169,15 @@ public async Task GetPolicyForOperandContent__ReturnsExpectedResult()
var sut = await CreateSut();

// Act
var result = await sut.GetPolicyForOperandContent(PolicyTypeId.Usage, Enumerable.Repeat("purpose.trace.v1.TraceBattery", 1)).ToListAsync();
var result = await sut.GetPolicyForOperandContent(PolicyTypeId.Usage, Enumerable.Repeat("Membership", 1)).ToListAsync();

// Assert
result.Should().ContainSingle()
.And.Satisfy(
x => x.TechnicalKey == "purpose.trace.v1.TraceBattery" &&
x => x.TechnicalKey == "Membership" &&
x.Attributes.Key == AttributeKeyId.Static &&
x.Attributes.Values.Single() == "purpose.trace.v1.TraceBattery" &&
x.LeftOperand == "purpose.trace.v1.TraceBattery" &&
x.Attributes.Values.Single() == "active" &&
x.LeftOperand == "Membership" &&
x.RightOperandValue == null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ public async Task GetPolicyContentAsync_WithUnmatchingPoliciesAndConstraints_Thr
new Constraints("test", OperatorId.In, null),
new Constraints("abc", OperatorId.Equals, null)
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.CheckPolicyByTechnicalKeys(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
Expand All @@ -225,6 +227,8 @@ public async Task GetPolicyContentAsync_WithUnmatchingTechnicalKeys_ThrowsContro
new Constraints("test", OperatorId.In, null),
new Constraints("abc", OperatorId.Equals, null)
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.CheckPolicyByTechnicalKeys(data.PolicyType, A<IEnumerable<string>>._))
.Returns(false);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
Expand All @@ -238,6 +242,31 @@ public async Task GetPolicyContentAsync_WithUnmatchingTechnicalKeys_ThrowsContro
ex.Message.Should().Be($"Policy for type {data.PolicyType} and requested technicalKeys does not exists. TechnicalKeys are allowed");
}

[Fact]
public async Task GetPolicyContentAsync_WithUnmatchingAttributeValues_ThrowsControllerArgumentException()
{
// Arrange
var data = new PolicyContentRequest(PolicyTypeId.Access, ConstraintOperandId.Or,
new[]
{
new Constraints("test", OperatorId.In, null),
new Constraints("abc", OperatorId.Equals, null)
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(false);
A.CallTo(() => _policyRepository.CheckPolicyByTechnicalKeys(data.PolicyType, A<IEnumerable<string>>._))
.Returns(false);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
.Returns(Enumerable.Repeat(new ValueTuple<string, string, ValueTuple<AttributeKeyId?, IEnumerable<string>>, string?>("test", "active", default, null), 1).ToAsyncEnumerable());
async Task Act() => await _sut.GetPolicyContentAsync(data);

// Act
var ex = await Assert.ThrowsAsync<ControllerArgumentException>(Act);

// Assert
ex.Message.Should().Be($"Policy for type {data.PolicyType} and requested attribute values does not exists.");
}

[Fact]
public async Task GetPolicyContentAsync_WithAttributeAndRightOperandNull_ThrowsUnexpectedConditionException()
{
Expand All @@ -247,6 +276,8 @@ public async Task GetPolicyContentAsync_WithAttributeAndRightOperandNull_ThrowsU
{
new Constraints("test", OperatorId.In, null),
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.CheckPolicyByTechnicalKeys(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
Expand All @@ -269,6 +300,8 @@ public async Task GetPolicyContentAsync_WithRegexWithoutValue_ThrowsControllerAr
{
new Constraints("test", OperatorId.Equals, null),
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.CheckPolicyByTechnicalKeys(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
Expand All @@ -292,6 +325,8 @@ public async Task GetPolicyContentAsync_WithRegexWithoutMatchingRegexPattern_Thr
{
new Constraints("test", OperatorId.Equals, "testRegValue"),
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.CheckPolicyByTechnicalKeys(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
Expand All @@ -316,6 +351,8 @@ public async Task GetPolicyContentAsync_WithMultipleDefinedKeys_ThrowsNotFoundEx
new Constraints("test", OperatorId.In, null),
new Constraints("test", OperatorId.Equals, null)
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
.Returns(new[]
{
Expand All @@ -342,6 +379,8 @@ public async Task GetPolicyContentAsync_WithValid_ReturnsExpected()
new Constraints("dynamicWithoutValue", OperatorId.Equals, null),
new Constraints("dynamicWithValue", OperatorId.Equals, "test")
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.CheckPolicyByTechnicalKeys(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
Expand Down Expand Up @@ -380,6 +419,8 @@ public async Task GetPolicyContentAsync_WithMultipleValues_ReturnsExpected()
new Constraints("multipleAdditionalValues", OperatorId.Equals, null),
new Constraints("test", OperatorId.In, null)
});
A.CallTo(() => _policyRepository.CheckPolicyAttributeValue(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.CheckPolicyByTechnicalKeys(data.PolicyType, A<IEnumerable<string>>._))
.Returns(true);
A.CallTo(() => _policyRepository.GetPolicyForOperandContent(data.PolicyType, A<IEnumerable<string>>._))
Expand Down
Loading

0 comments on commit 517b82d

Please sign in to comment.