Skip to content

Commit

Permalink
add tests for live en test environment and unsupported region
Browse files Browse the repository at this point in the history
  • Loading branch information
DjoykeAbyah committed Jan 7, 2025
1 parent 602c678 commit 0412c43
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Adyen.IntegrationTest/Adyen.IntegrationTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion Adyen.Test/Adyen.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>

<IsPackable>false</IsPackable>

Expand Down
65 changes: 65 additions & 0 deletions Adyen.Test/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,71 @@ public void TestLogLine()
Assert.AreEqual("testMessage", logLine);
}

[DataTestMethod]
// Valid Live Region Test Cases
[DataRow(Model.Environment.Live, Region.EU, "https://terminal-api-live.adyen.com", true)]
[DataRow(Model.Environment.Live, Region.AU, "https://terminal-api-live-au.adyen.com", true)]
[DataRow(Model.Environment.Live, Region.US, "https://terminal-api-live-us.adyen.com", true)]
[DataRow(Model.Environment.Live, Region.APSE, "https://terminal-api-live-apse.adyen.com", true)]
[DataRow(Model.Environment.Live, null, "https://terminal-api-live.adyen.com", true)] // Default to EU

public void TestGetCloudApiEndpoint(Model.Environment environment, Region? region, string expectedEndpoint, bool shouldSucceed)
{
var config = new Config
{
Environment = environment,
TerminalApiRegion = region
};
var client = new Client(config);

if (shouldSucceed)
{
var actualEndpoint = client.GetCloudApiEndpoint();

Assert.AreEqual(expectedEndpoint, actualEndpoint);
}
else
{
Assert.ThrowsException<ArgumentException>(() => client.GetCloudApiEndpoint());
}
}

[DataTestMethod]
[DataRow(Model.Environment.Test, Region.EU, "https://terminal-api-test.adyen.com")]
[DataRow(Model.Environment.Test, Region.AU, "https://terminal-api-test.adyen.com")]
[DataRow(Model.Environment.Test, Region.US, "https://terminal-api-test.adyen.com")]
[DataRow(Model.Environment.Test, Region.APSE, "https://terminal-api-test.adyen.com")]
[DataRow(Model.Environment.Test, null, "https://terminal-api-test.adyen.com")] // Defaults to Test endpoint
public void TestTestCloudApiEndpoints(Model.Environment environment, Region? region, string expectedEndpoint)
{
var config = new Config
{
Environment = environment,
TerminalApiRegion = region
};
var client = new Client(config);

var actualEndpoint = client.GetCloudApiEndpoint();

Assert.AreEqual(expectedEndpoint, actualEndpoint);
}

[TestMethod]
public void TestUnsupportedRegionDefaultsToEU()
{
var config = new Config
{
Environment = Model.Environment.Live,
TerminalApiRegion = Region.IN
};
var client = new Client(config);

var actualEndpoint = client.GetCloudApiEndpoint();

Assert.AreEqual("https://terminal-api-live.adyen.com", actualEndpoint,
"Unsupported regions should default to the EU endpoint.");
}

[TestMethod]
public void TestSetTerminalApiRegion()
{
Expand Down
2 changes: 1 addition & 1 deletion Adyen/Adyen.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Copyright>Adyen</Copyright>

Expand Down
64 changes: 50 additions & 14 deletions Adyen/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,69 @@ public void SetEnvironment(Environment environment, string liveEndpointUrlPrefix
{
Config.Environment = environment;
Config.LiveEndpointUrlPrefix = liveEndpointUrlPrefix;

// Always use GetCloudApiEndpoint to determine the correct endpoint
Config.CloudApiEndPoint = GetCloudApiEndpoint();
}

public string GetCloudApiEndpoint() {
// Check if the cloud api endpoint has not already been set
// public string GetCloudApiEndpoint() {
// // Check if the cloud api endpoint has not already been set
// if (Config.CloudApiEndPoint != null)
// {
// return Config.CloudApiEndPoint;
// }
// // If not switch through environment and return default EU
// if (Config.Environment == Environment.Live)
// {
// switch (Config.TerminalApiRegion)
// {
// case Region.AU:
// return ClientConfig.CloudApiEndPointAULive;
// case Region.US:
// return ClientConfig.CloudApiEndPointUSLive;
// case Region.APSE:
// return ClientConfig.CloudApiEndPointAPSELive;
// case Region.EU:
// default:
// return ClientConfig.CloudApiEndPointEULive;
// }
// }
// return ClientConfig.CloudApiEndPointTest;
// }

public string GetCloudApiEndpoint()
{
// Check if the cloud API endpoint has already been set
if (Config.CloudApiEndPoint != null)
{
return Config.CloudApiEndPoint;
}
// If not switch through environment and return default EU

// If environment is TEST, return the test endpoint
if (Config.Environment == Environment.Test)
{
return ClientConfig.CloudApiEndPointTest;
}

// For LIVE environment, handle region mapping
if (Config.Environment == Environment.Live)
{
switch (Config.TerminalApiRegion)
var region = Config.TerminalApiRegion ?? Region.EU; // Default to EU if region is null

if (!RegionMapping.TERMINAL_API_ENDPOINTS_MAPPING.ContainsKey(region))
{
case Region.AU:
return ClientConfig.CloudApiEndPointAULive;
case Region.US:
return ClientConfig.CloudApiEndPointUSLive;
case Region.APSE:
return ClientConfig.CloudApiEndPointAPSELive;
case Region.EU:
default:
return ClientConfig.CloudApiEndPointEULive;
// Log a warning and default to EU endpoint for unsupported regions
Console.WriteLine($"Warning: Region '{region}' is not supported. Defaulting to EU endpoint.");
return ClientConfig.CloudApiEndPointEULive;
}

return RegionMapping.TERMINAL_API_ENDPOINTS_MAPPING[region];
}

// Default to test endpoint if the environment is not set
return ClientConfig.CloudApiEndPointTest;
}

// Get a new HttpClient and set a timeout
private System.Net.Http.HttpClient GetHttpClient()
{
Expand Down
3 changes: 2 additions & 1 deletion Adyen/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Config
public bool HasApiKey => !string.IsNullOrEmpty(XApiKey);
public BaseUrlConfig BaseUrlConfig { get; set; }

public Region TerminalApiRegion { get; set; }
// public Region TerminalApiRegion { get; set; }
public Region? TerminalApiRegion { get; set; }
}
}

0 comments on commit 0412c43

Please sign in to comment.