Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Region class with valid regions plus endpoint mappings for terminal api live #1407

Merged
merged 40 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1e64ea7
added valid regions in Region.java, defined region endpoints in Clien…
DjoykeAbyah Dec 16, 2024
5c26c9d
added tests for Region
DjoykeAbyah Dec 16, 2024
3172679
refactored client.java and region.java
DjoykeAbyah Dec 17, 2024
080cb20
adjusted regions.java to contain enums and regiontest.java
DjoykeAbyah Dec 17, 2024
e500663
added getCloudEndpoint method
DjoykeAbyah Dec 17, 2024
ad0c80d
adjust ClientTest
DjoykeAbyah Dec 17, 2024
f538178
testConfigLiveClient fails
DjoykeAbyah Dec 23, 2024
90c5e11
tests are working
DjoykeAbyah Dec 24, 2024
ea50ede
adjusted ClientTest
DjoykeAbyah Dec 24, 2024
34dae73
adjusted ClientTest
DjoykeAbyah Dec 24, 2024
5315994
adjusted Client.java
DjoykeAbyah Dec 24, 2024
f812c6a
adjusted ClientTest.java
DjoykeAbyah Dec 24, 2024
a6c58fd
added dependency for jupiter-params, live and test endpoint test in C…
DjoykeAbyah Dec 24, 2024
e8823d9
removed default-endpoint test replaced by live-test in ClientTest.java
DjoykeAbyah Dec 24, 2024
269e122
removed unused test in ClientTest.java
DjoykeAbyah Dec 24, 2024
889c2bf
removed custom endpoint test in ClientTest.java
DjoykeAbyah Dec 24, 2024
59fe7ef
removed test environment test replaced by parameterized test, refacto…
DjoykeAbyah Dec 24, 2024
854a0c8
added test for regions without endpoint
DjoykeAbyah Dec 30, 2024
01bf379
updated pom.xml
DjoykeAbyah Dec 30, 2024
591ab92
Update ClientTest.java
DjoykeAbyah Dec 31, 2024
39393de
Update RegionTest.java
DjoykeAbyah Dec 31, 2024
140ca46
added indentation and removed comments
DjoykeAbyah Dec 31, 2024
fc7fdff
added indentation and removed comments
DjoykeAbyah Dec 31, 2024
458e811
removed regions class, added valid regions and regions map to region …
DjoykeAbyah Dec 31, 2024
4d32873
removed regions class, added valid regions and regions map to region …
DjoykeAbyah Dec 31, 2024
52bea08
removed regions class, added valid regions and regions map to region …
DjoykeAbyah Dec 31, 2024
cd3dddc
replaced unmapped regions test with unmapped india region test
DjoykeAbyah Dec 31, 2024
a060e6a
Merge branch 'main' into add-terminal-api-live-apse
DjoykeAbyah Dec 31, 2024
8602bd8
Update Region.java
DjoykeAbyah Dec 31, 2024
eda24af
changed order of enums
DjoykeAbyah Dec 31, 2024
f3189de
changed order of enums
DjoykeAbyah Dec 31, 2024
754566d
changed order of enums
DjoykeAbyah Dec 31, 2024
f57d9f1
adjusted set environment to call getCloudEndpoint
DjoykeAbyah Dec 31, 2024
efc4a85
adjusted set environment to call getCloudEndpoint
DjoykeAbyah Dec 31, 2024
c65e795
refactor tests
DjoykeAbyah Dec 31, 2024
dcb3f13
Simplify tests
Dec 31, 2024
97ea3f3
updated readme, removed unused imports
DjoykeAbyah Dec 31, 2024
13f2935
updated readme
DjoykeAbyah Dec 31, 2024
ae8cea3
updated readme
DjoykeAbyah Dec 31, 2024
4b61280
default to test
DjoykeAbyah Dec 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,11 @@
<version>${swagger-core-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
25 changes: 25 additions & 0 deletions src/main/java/com/adyen/Client.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.adyen;

import com.adyen.enums.Environment;
import com.adyen.enums.Regions;
import com.adyen.enums.Regions.Region;
import com.adyen.httpclient.AdyenHttpClient;
import com.adyen.httpclient.ClientInterface;

Expand All @@ -13,6 +15,9 @@ public class Client {
public static final String LIB_VERSION = "32.0.0";
public static final String TERMINAL_API_ENDPOINT_TEST = "https://terminal-api-test.adyen.com";
public static final String TERMINAL_API_ENDPOINT_LIVE = "https://terminal-api-live.adyen.com";
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved
public static final String TERMINAL_API_ENDPOINT_US = "https://terminal-api-live-us.adyen.com";
public static final String TERMINAL_API_ENDPOINT_AU = "https://terminal-api-live-au.adyen.com";
public static final String TERMINAL_API_ENDPOINT_APSE = "https://terminal-api-live-apse.adyen.com";

public Client() {
this.config = new Config();
Expand Down Expand Up @@ -130,6 +135,7 @@ public void setEnvironment(Environment environment, String liveEndpointUrlPrefix
if (liveEndpointUrlPrefix != null) {
config.setLiveEndpointUrlPrefix(liveEndpointUrlPrefix);
}

if (Environment.TEST.equals(environment)) {
this.config.setEnvironment(environment);
this.config.setTerminalApiCloudEndpoint(TERMINAL_API_ENDPOINT_TEST);
Expand All @@ -139,6 +145,25 @@ public void setEnvironment(Environment environment, String liveEndpointUrlPrefix
}
}

/**
* @param region The region for which the endpoint is requested. If null or the region is not found, defaults to default EU endpoint.
*/
public String getCloudEndpoint(Region region, Environment environment) {
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved
if (region != null && !Regions.TERMINAL_API_ENDPOINTS_MAPPING.containsKey(region)) {
throw new IllegalArgumentException("Region " + region + " is not supported yet");
}
// Check the environment for TEST and get the endpoint
if (environment.equals(Environment.TEST)) {
return Client.TERMINAL_API_ENDPOINT_TEST;
}
// For LIVE environment, lookup the endpoint using the map
if (environment.equals(Environment.LIVE)) {
return Regions.TERMINAL_API_ENDPOINTS_MAPPING.getOrDefault(region, TERMINAL_API_ENDPOINT_LIVE);
}
// Default to EU endpoint if no environment or region is specified
return Regions.TERMINAL_API_ENDPOINTS_MAPPING.get(Region.EU);
}

@Override
public String toString() {
return "Client [webServiceUser=" + this.config.username + ", environment=" + this.config.environment + "]";
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/adyen/Config.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.adyen;

import com.adyen.enums.Environment;

import com.adyen.enums.Regions.Region;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

Expand All @@ -22,6 +22,7 @@ public class Config {
protected String terminalApiCloudEndpoint;
protected String terminalApiLocalEndpoint;
protected String liveEndpointUrlPrefix;
protected Region terminalApiRegion;
protected SSLContext sslContext;
protected HostnameVerifier hostnameVerifier;

Expand Down Expand Up @@ -69,7 +70,7 @@ public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public String getTerminalApiCloudEndpoint() {
public String getTerminalApiCloudEndpoint() {
return terminalApiCloudEndpoint;
}

Expand All @@ -85,6 +86,14 @@ public void setTerminalApiLocalEndpoint(String terminalApiLocalEndpoint) {
this.terminalApiLocalEndpoint = terminalApiLocalEndpoint;
}

public Region getTerminalApiRegion() {
return terminalApiRegion;
}

public void setTerminalApiRegion(Region terminalApiRegion) {
this.terminalApiRegion = terminalApiRegion;
}

public int getConnectionTimeoutMillis() {
return connectionTimeoutMillis;
}
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/adyen/enums/Region.java

This file was deleted.

62 changes: 62 additions & 0 deletions src/main/java/com/adyen/enums/Regions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.adyen.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.adyen.Client;

public final class Regions {

// Prevent instantiation
private Regions() {
throw new UnsupportedOperationException("Utility class");
}

public enum Region {

/**
* European Union region
*/
EU,

/**
* United States region
*/
US,

/**
* Australia region
*/
AU,

/**
* India region
*/
IN,

/**
* Asia-Pacific, South East region
*/
APSE
}

/**
* List of all valid regions
*/
public static final List<Region> VALID_REGIONS =
Collections.unmodifiableList(List.of(Region.values()));

/**
* Maps regions to their respective Terminal API endpoints.
*/
public static final Map<Region, String> TERMINAL_API_ENDPOINTS_MAPPING;

static {
Map<Region, String> endpointsMapping = new HashMap<>();
endpointsMapping.put(Region.EU, Client.TERMINAL_API_ENDPOINT_LIVE);
endpointsMapping.put(Region.US, Client.TERMINAL_API_ENDPOINT_US);
endpointsMapping.put(Region.AU, Client.TERMINAL_API_ENDPOINT_AU);
endpointsMapping.put(Region.APSE, Client.TERMINAL_API_ENDPOINT_APSE);
TERMINAL_API_ENDPOINTS_MAPPING = Collections.unmodifiableMap(endpointsMapping);
}
}
105 changes: 94 additions & 11 deletions src/test/java/com/adyen/ClientTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.adyen;

import com.adyen.enums.Environment;
import com.adyen.enums.Region;
import com.adyen.model.RequestOptions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

import javax.net.ssl.SSLContext;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mock;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import javax.net.ssl.SSLContext;
import java.util.HashMap;
import com.adyen.enums.Environment;
import com.adyen.enums.Regions;
import com.adyen.enums.Regions.Region;
import com.adyen.model.RequestOptions;

public class ClientTest {

Expand All @@ -18,16 +27,17 @@ public class ClientTest {
@Mock
private String apiKey;


@Test
public void testConfigTestClient() {
Config config = new Config();
config.setEnvironment(Environment.TEST);
config.setApiKey(apiKey);
Client client = new Client(config);

Assert.assertEquals(Environment.TEST, client.getConfig().getEnvironment());
}

@Test
public void testConfigLiveClient() {
Config config = new Config();
Expand All @@ -38,6 +48,83 @@ public void testConfigLiveClient() {
Assert.assertEquals(Environment.LIVE, client.getConfig().getEnvironment());
}

private static Stream<Arguments> provideCloudTestEndpointTestCases() {
return Stream.of(
Arguments.of(null, Environment.TEST, "https://terminal-api-test.adyen.com"),
Arguments.of(Region.EU, Environment.TEST, "https://terminal-api-test.adyen.com"),
Arguments.of(Region.AU, Environment.TEST, "https://terminal-api-test.adyen.com"),
Arguments.of(Region.US, Environment.TEST, "https://terminal-api-test.adyen.com"),
Arguments.of(Region.APSE, Environment.TEST, "https://terminal-api-test.adyen.com")
);
}

@ParameterizedTest
@MethodSource("provideCloudTestEndpointTestCases")
public void testGetCloudEndpointForTestEnvironment(Region region, Environment environment, String expectedEndpoint) {
Config testConfig = new Config();
testConfig.setEnvironment(Environment.TEST);
Client testClient = new Client(testConfig);

// call the method and assert the result
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved
String result = testClient.getCloudEndpoint(region, testConfig.getEnvironment());
Assert.assertEquals("Unexpected endpoint for region: " + region, expectedEndpoint, result);
}

private static Stream<Arguments> provideCloudLiveEndpointTestCases() {
return Stream.of(
Arguments.of(null, Environment.LIVE, "https://terminal-api-live.adyen.com"),
Arguments.of(Region.EU, Environment.LIVE, "https://terminal-api-live.adyen.com"),
Arguments.of(Region.AU, Environment.LIVE, "https://terminal-api-live-au.adyen.com"),
Arguments.of(Region.US, Environment.LIVE, "https://terminal-api-live-us.adyen.com"),
Arguments.of(Region.APSE, Environment.LIVE, "https://terminal-api-live-apse.adyen.com")
);
}

@ParameterizedTest
@MethodSource("provideCloudLiveEndpointTestCases")
public void testGetCloudEndpointForLiveEnvironment(Region region, Environment environment, String expectedEndpoint) {
// Create a Config object for the LIVE environment
Config liveConfig = new Config();
liveConfig.setEnvironment(Environment.LIVE);
Client liveClient = new Client(liveConfig);

// call the method and assert the result
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved
String result = liveClient.getCloudEndpoint(region, liveConfig.getEnvironment());
Assert.assertEquals("Unexpected endpoint for region: " + region, expectedEndpoint, result);
}

@Test
public void testRegionsNotInMappingThrowException() {
List<Region> unmappedRegions = new ArrayList<>();
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved

// Find unmapped regions
for (Region region : Regions.VALID_REGIONS) {
if (!Regions.TERMINAL_API_ENDPOINTS_MAPPING.containsKey(region)) {
unmappedRegions.add(region);
}
}

// If no unmapped regions exist, skip the test
if (unmappedRegions.isEmpty()) {
System.out.println("Skipping test: All regions are in the mapping.");
return;
}

// Iterate through unmapped regions and test
for (Region region : unmappedRegions) {
Config config = new Config();
config.setEnvironment(Environment.LIVE);
Client client = new Client(config);

try {
client.getCloudEndpoint(region, Environment.LIVE);
Assert.fail("Expected IllegalArgumentException to be thrown for region: " + region);
} catch (IllegalArgumentException e) {
Assert.assertEquals("Region " + region + " is not supported yet", e.getMessage());
}
}
}

@Test
public void testClientCertificateAuth() {
Client client = new Client(clientCertificateAuthSSLContext, apiKey);
Expand All @@ -54,8 +141,4 @@ public void testRequestOptionsBuilderPattern() {
.additionalServiceHeaders(map);
Assert.assertEquals(requestOptions.getAdditionalServiceHeaders(), map);
}

private void assertCommonEndpoints(Config config) {
Assert.assertEquals(Client.TERMINAL_API_ENDPOINT_LIVE, config.getTerminalApiCloudEndpoint());
}
}
64 changes: 64 additions & 0 deletions src/test/java/com/adyen/RegionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.adyen;

import com.adyen.enums.Regions.Region;
import com.adyen.enums.Regions;
import java.util.stream.Collectors;
import org.junit.Test;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class RegionTest {
@Test
public void testValidRegions() {

// Convert Regions.VALID_REGIONS to lowercase strings for comparison
List<String> actual = Regions.VALID_REGIONS.stream()
.map(region -> region.name().toLowerCase())
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toList());

// Define the expected list of valid regions
List<String> expected = List.of(
"eu",
"us",
"au",
"in",
"apse"
);

// Assert that the VALID_REGIONS in Region matches the expected list
assertEquals(
"VALID_REGIONS should match the expected regions.",
expected,
actual
);
}

@Test
public void testTerminalApiEndpointsMapping() {

// Convert TERMINAL_API_ENDPOINTS_MAPPING keys to lowercase strings for comparison
Map<String, String> actual = Regions.TERMINAL_API_ENDPOINTS_MAPPING.entrySet()
.stream()
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toMap(
entry -> entry.getKey().name().toLowerCase(), // Convert key (Region enum) to lowercase
Map.Entry::getValue
));

// Define the expected map of region to endpoint mappings
Map<String, String> expected = Map.of(
"eu", "https://terminal-api-live.adyen.com",
"us", "https://terminal-api-live-us.adyen.com",
"au", "https://terminal-api-live-au.adyen.com",
"apse", "https://terminal-api-live-apse.adyen.com"
);

// Assert that the TERMINAL_API_ENDPOINTS_MAPPING in Region matches the expected map
assertEquals(
"TERMINAL_API_ENDPOINTS_MAPPING should match the expected mappings.",
expected,
actual
);
}
}
Loading