Skip to content

Commit

Permalink
refactor(Db2): Update WithAcceptLicenseAgreement(bool)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Feb 14, 2025
1 parent 5d5b0e0 commit 2b098b9
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/Testcontainers.Db2/Db2Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ public sealed class Db2Builder : ContainerBuilder<Db2Builder, Db2Container, Db2C

public const string DefaultPassword = "db2inst1";

private const string AcceptLicenseAgreementEnvVar = "LICENSE";

private const string AcceptLicenseAgreement = "accept";

private const string DeclineLicenseAgreement = "decline";

/// <summary>
/// Initializes a new instance of the <see cref="Db2Builder" /> class.
/// </summary>
Expand All @@ -42,6 +36,15 @@ private Db2Builder(Db2Configuration resourceConfiguration)
/// <inheritdoc />
protected override Db2Configuration DockerResourceConfiguration { get; }

/// <inheritdoc />
protected override string AcceptLicenseAgreementEnvVar { get; } = "LICENSE";

/// <inheritdoc />
protected override string AcceptLicenseAgreement { get; } = "accept";

/// <inheritdoc />
protected override string DeclineLicenseAgreement { get; } = "decline";

/// <summary>
/// Accepts the license agreement.
/// </summary>
Expand All @@ -50,7 +53,7 @@ private Db2Builder(Db2Configuration resourceConfiguration)
/// </remarks>
/// <param name="acceptLicenseAgreement">A boolean value indicating whether the Db2 license agreement is accepted.</param>
/// <returns>A configured instance of <see cref="Db2Builder" />.</returns>
public Db2Builder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
public override Db2Builder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
{
var licenseAgreement = acceptLicenseAgreement ? AcceptLicenseAgreement : DeclineLicenseAgreement;
return WithEnvironment(AcceptLicenseAgreementEnvVar, licenseAgreement);
Expand Down Expand Up @@ -94,6 +97,7 @@ public Db2Builder WithPassword(string password)
public override Db2Container Build()
{
Validate();
ValidateLicenseAgreement();
return new Db2Container(DockerResourceConfiguration);
}

Expand All @@ -110,16 +114,8 @@ protected override Db2Builder Init() => base.Init()
/// <inheritdoc />
protected override void Validate()
{
const string message = "The image '{0}' requires you to accept a license agreement.";

base.Validate();

Predicate<Db2Configuration> licenseAgreementNotAccepted = value =>
!value.Environments.TryGetValue(AcceptLicenseAgreementEnvVar, out var licenseAgreementValue) || !AcceptLicenseAgreement.Equals(licenseAgreementValue, StringComparison.Ordinal);

_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Image))
.ThrowIf(argument => licenseAgreementNotAccepted(argument.Value), argument => throw new ArgumentException(string.Format(message, DockerResourceConfiguration.Image.FullName), argument.Name));

_ = Guard.Argument(DockerResourceConfiguration.Username, nameof(DockerResourceConfiguration.Username))
.NotNull()
.NotEmpty();
Expand Down

0 comments on commit 2b098b9

Please sign in to comment.