Skip to content

Commit

Permalink
Makes process.bpmn env config pattern more generic and reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
danielskovli committed Jan 17, 2025
1 parent f58c0ca commit 0845a67
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 55 deletions.
4 changes: 3 additions & 1 deletion src/Altinn.App.Core/Features/Action/SigningUserAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ string recipient
)
{
HostingEnvironment env = AltinnEnvironments.GetHostingEnvironment(hostEnvironment);
string? resource = signatureConfiguration?.GetCorrespondenceResourceForEnvironment(env)?.ResourceId;
var resource = AltinnTaskExtension
.GetConfigForEnvironment(env, signatureConfiguration?.CorrespondenceResources)
?.Value;
if (string.IsNullOrEmpty(resource))
{
throw new ConfigurationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Xml.Serialization;

namespace Altinn.App.Core.Internal.Process.Elements.AltinnExtensionProperties;

/// <summary>
/// Wrapper for environment specific configuration values
/// </summary>
public sealed class AltinnEnvironmentConfig
{
/// <summary>
/// The environment the configuration is applicable for. An omitted value indicates validity for all environments.
/// </summary>
[XmlAttribute("env")]
public string? Environment { get; set; }

/// <summary>
/// The configuration value
/// </summary>
[XmlText]
public required string Value { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Xml.Serialization;
using Altinn.App.Core.Constants;

namespace Altinn.App.Core.Internal.Process.Elements.AltinnExtensionProperties;

Expand Down Expand Up @@ -54,42 +53,5 @@ public class AltinnSignatureConfiguration
/// Correspondence resource details
/// </summary>
[XmlElement(ElementName = "correspondenceResource", Namespace = "http://altinn.no/process")]
public List<CorrespondenceResource> CorrespondenceResources { get; set; } = [];

/// <summary>
/// Retrieve a correspondence resource for the given environment, in a predictable manner.
/// Specific configurations (those specifying an environment) takes precedence over global configurations.
/// </summary>
internal CorrespondenceResource? GetCorrespondenceResourceForEnvironment(HostingEnvironment env)
{
const string globalKey = "__global__";
Dictionary<string, CorrespondenceResource> lookup = new();
foreach (var entry in CorrespondenceResources)
{
var key = string.IsNullOrWhiteSpace(entry.Environment)
? globalKey
: AltinnEnvironments.GetHostingEnvironment(entry.Environment).ToString();
lookup[key] = entry;
}

return lookup.GetValueOrDefault(env.ToString()) ?? lookup.GetValueOrDefault(globalKey);
}

/// <summary>
/// Correspondence resource details
/// </summary>
public class CorrespondenceResource
{
/// <summary>
/// The environment the configuration is applicable for. An omitted value indicates validity for all environments.
/// </summary>
[XmlAttribute("env")]
public string? Environment { get; set; }

/// <summary>
/// The resource to use for correspondence (receipts)
/// </summary>
[XmlText]
public required string ResourceId { get; set; }
}
public List<AltinnEnvironmentConfig> CorrespondenceResources { get; set; } = [];
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Xml.Serialization;
using Altinn.App.Core.Constants;

namespace Altinn.App.Core.Internal.Process.Elements.AltinnExtensionProperties;

Expand Down Expand Up @@ -32,4 +33,32 @@ public class AltinnTaskExtension
/// </summary>
[XmlElement("paymentConfig", Namespace = "http://altinn.no/process")]
public AltinnPaymentConfiguration? PaymentConfiguration { get; set; } = new AltinnPaymentConfiguration();

/// <summary>
/// Retrieves a configuration item for given environment, in a predictable manner.
/// Specific configurations (those specifying an environment) takes precedence over global configurations.
/// </summary>
/// <remarks>
/// For usage examples, refer to <see cref="AltinnSignatureConfiguration.CorrespondenceResources"/>
/// </remarks>
internal static AltinnEnvironmentConfig? GetConfigForEnvironment(
HostingEnvironment env,
IEnumerable<AltinnEnvironmentConfig>? candidates
)
{
if (candidates.IsNullOrEmpty())
return null;

const string globalKey = "__global__";
Dictionary<string, AltinnEnvironmentConfig> lookup = new();
foreach (var candidate in candidates)
{
var key = string.IsNullOrWhiteSpace(candidate.Environment)
? globalKey
: AltinnEnvironments.GetHostingEnvironment(candidate.Environment).ToString();
lookup[key] = candidate;
}

return lookup.GetValueOrDefault(env.ToString()) ?? lookup.GetValueOrDefault(globalKey);
}
}
18 changes: 3 additions & 15 deletions test/Altinn.App.Core.Tests/Internal/Process/ProcessReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,21 +441,9 @@ public void SignatureConfiguration_WorksAsExpected()
SigneeStatesDataTypeId = "signeeStatesDataTypeId",
CorrespondenceResources =
[
new AltinnSignatureConfiguration.CorrespondenceResource
{
Environment = null,
ResourceId = "correspondenceResource",
},
new AltinnSignatureConfiguration.CorrespondenceResource
{
Environment = "tt02",
ResourceId = "correspondenceResourceTt02",
},
new AltinnSignatureConfiguration.CorrespondenceResource
{
Environment = "prod",
ResourceId = "correspondenceResourceProd",
},
new AltinnEnvironmentConfig { Environment = null, Value = "correspondenceResource" },
new AltinnEnvironmentConfig { Environment = "tt02", Value = "correspondenceResourceTt02" },
new AltinnEnvironmentConfig { Environment = "prod", Value = "correspondenceResourceProd" },
],
}
);
Expand Down

0 comments on commit 0845a67

Please sign in to comment.