Skip to content

Commit

Permalink
fix: policy hub post get policy rules response structuring error valu…
Browse files Browse the repository at this point in the history
…e map (#201)

Co-authored-by: Phil Schneider <info@philschneider.de>
Reviewed-by: Phil Schneider <info@philschneider.de>
  • Loading branch information
leandro-cavalcante and Phil91 authored Sep 19, 2024
1 parent 64b2f38 commit b5a1ec3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
31 changes: 22 additions & 9 deletions src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,30 @@ private static (object rightOperand, AdditionalAttributes? additionalAttribute)
AttributeKeyId.DynamicValue => (value ?? "{dynamicValue}", null),
AttributeKeyId.Regex => (GetRegexValue(attributes, value), null),
_ => operatorId == OperatorId.Equals
? rightOperands.Count() > 1 ?
($"@{leftOperand}{(useCase != null ?
useCase.Value.ToString().Insert(0, ".") :
string.Empty)}-{attributes.Key}",
new AdditionalAttributes($"@{leftOperand}{(useCase != null ?
useCase.Value.ToString().Insert(0, ".") :
string.Empty)}-{attributes.Key}", rightOperands)) :
(rightOperands.Single(), null)
? processEqualsOperator(attributes, rightOperands, value, leftOperand, useCase)
: (rightOperands, null)
};

private static (object rightOperand, AdditionalAttributes? additionalAttribute) processEqualsOperator((AttributeKeyId? Key, IEnumerable<string> Values) attributes, IEnumerable<string> rightOperands, string? value, string leftOperand, UseCaseId? useCase)
{
if (value != null)
{
if (!rightOperands.Any(r => r == value))
{
throw new ControllerArgumentException($"Invalid values [{value}] set for key {leftOperand}. Possible values [{string.Join(",", rightOperands)}]");
}
rightOperands = rightOperands.Where(r => r.Equals(value));
}

var useCaseValue = useCase != null ?
useCase.Value.ToString().Insert(0, ".") :
string.Empty;
var rightOperand = $"@{leftOperand}{useCaseValue}-{attributes.Key}";
return rightOperands.Count() > 1 ?
(rightOperand, new AdditionalAttributes(rightOperand, rightOperands)) :
(rightOperands.Single(), null);
}

private static object GetRegexValue((AttributeKeyId? Key, IEnumerable<string> Values) attributes, string? value)
{
if (string.IsNullOrWhiteSpace(value))
Expand Down Expand Up @@ -163,7 +176,7 @@ public async Task<PolicyResponse> GetPolicyContentAsync(PolicyContentRequest req
if (invalidValues.Any())
{
var x = missingValues.Where(x => invalidValues.Contains(x.TechnicalKey)).Select(x =>
$"Key: {x.TechnicalKey}, invalid values: {string.Join(',', x.Values)}");
$"Key: {x.TechnicalKey}, requested value[{string.Join(',', x.Values)}] Possible Values[{string.Join(',', attributeValuesForTechnicalKeys.Where(a => a.TechnicalKey.Equals(x.TechnicalKey)).Select(a => a.Values).First())}]");
throw new ControllerArgumentException($"Invalid values set for {string.Join(',', x)}");
}

Expand Down
5 changes: 4 additions & 1 deletion src/hub/PolicyHub.Service/Controllers/PolicyHubController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Org.Eclipse.TractusX.PolicyHub.Service.BusinessLogic;
using Org.Eclipse.TractusX.PolicyHub.Service.Extensions;
using Org.Eclipse.TractusX.PolicyHub.Service.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Web;
using System.Diagnostics.CodeAnalysis;

Expand Down Expand Up @@ -71,6 +72,7 @@ public static RouteGroupBuilder MapPolicyHubApi(this RouteGroupBuilder group)
.RequireAuthorization()
.WithDefaultResponses()
.Produces(StatusCodes.Status200OK, typeof(PolicyResponse), Constants.JsonContentType)
.Produces(StatusCodes.Status400BadRequest, typeof(ErrorResponse), Constants.JsonContentType)
.Produces(StatusCodes.Status404NotFound, typeof(ErrorResponse), Constants.JsonContentType);

policyHub.MapPost("policy-content", ([FromBody] PolicyContentRequest requestData, IPolicyHubBusinessLogic logic) => logic.GetPolicyContentAsync(requestData))
Expand All @@ -80,7 +82,8 @@ public static RouteGroupBuilder MapPolicyHubApi(this RouteGroupBuilder group)
.RequireAuthorization()
.WithDefaultResponses()
.Produces(StatusCodes.Status200OK, typeof(PolicyResponse), Constants.JsonContentType)
.Produces(StatusCodes.Status404NotFound, typeof(ErrorResponse), Constants.JsonContentType);
.Produces(StatusCodes.Status400BadRequest, typeof(ErrorResponse), Constants.JsonContentType)
.Produces(StatusCodes.Status404NotFound, typeof(ControllerArgumentException), Constants.JsonContentType);

return group;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public async Task GetPolicyContentWithFiltersAsync_WithMultipleValues_ReturnsExp
.Returns(new ValueTuple<bool, string, ValueTuple<AttributeKeyId?, IEnumerable<string>>, string?>(true, "multipleAdditionalValues", new ValueTuple<AttributeKeyId, IEnumerable<string>>(AttributeKeyId.Static, new[] { "value1", "value2", "value3" }), null));

// Act
var result = await _sut.GetPolicyContentWithFiltersAsync(UseCaseId.Traceability, PolicyTypeId.Usage, "multipleAdditionalValues", OperatorId.Equals, "test");
var result = await _sut.GetPolicyContentWithFiltersAsync(UseCaseId.Traceability, PolicyTypeId.Usage, "multipleAdditionalValues", OperatorId.Equals, null);

// Assert
result.Content.Id.Should().Be("....");
Expand All @@ -188,6 +188,23 @@ public async Task GetPolicyContentWithFiltersAsync_WithMultipleValues_ReturnsExp
result.Content.Permission.Constraint.OrOperands.Should().BeNull();
}

[Fact]
public async Task GetPolicyContentWithFiltersAsync_WithInvalidValue_ExceptionExpected()
{
// Arrange
A.CallTo(() => _policyRepository.GetPolicyContentAsync(UseCaseId.Traceability, PolicyTypeId.Usage, "multipleAdditionalValues"))
.Returns(new ValueTuple<bool, string, ValueTuple<AttributeKeyId?, IEnumerable<string>>, string?>(true, "multipleAdditionalValues", new ValueTuple<AttributeKeyId, IEnumerable<string>>(AttributeKeyId.Static, new[] { "value1", "value2", "value3" }), null));

// Act
async Task Act() => await _sut.GetPolicyContentWithFiltersAsync(UseCaseId.Traceability, PolicyTypeId.Usage, "multipleAdditionalValues", OperatorId.Equals, "test");

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

// Assert
ex.Message.Should().Be("Invalid values [test] set for key multipleAdditionalValues. Possible values [value1,value2,value3]");
}

#endregion

#region GetPolicyContentAsync
Expand Down Expand Up @@ -264,7 +281,7 @@ public async Task GetPolicyContentAsync_WithUnmatchingAttributeValues_ThrowsCont
var ex = await Assert.ThrowsAsync<ControllerArgumentException>(Act);

// Assert
ex.Message.Should().Be("Invalid values set for Key: test, invalid values: abc");
ex.Message.Should().Be("Invalid values set for Key: test, requested value[abc] Possible Values[test]");
}

[Fact]
Expand Down

0 comments on commit b5a1ec3

Please # to comment.