From 1e64ea78aa8e86b0afa609cf43231a90f0fb6976 Mon Sep 17 00:00:00 2001 From: Djoyke Reijans <115019123+DjoykeAbyah@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:58:15 +0000 Subject: [PATCH 1/6] added valid regions in Region.java, defined region endpoints in Client.java --- src/main/java/com/adyen/Client.java | 4 ++ src/main/java/com/adyen/enums/Region.java | 63 ++++++++++++++++++++--- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/adyen/Client.java b/src/main/java/com/adyen/Client.java index 48c00ce45..d39314998 100644 --- a/src/main/java/com/adyen/Client.java +++ b/src/main/java/com/adyen/Client.java @@ -13,6 +13,10 @@ 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"; + public static final String ENDPOINT_TERMINAL_CLOUD_LIVE = "https://terminal-api-live.adyen.com"; + public static final String ENDPOINT_TERMINAL_CLOUD_US_LIVE = "https://terminal-api-live-us.adyen.com"; + public static final String ENDPOINT_TERMINAL_CLOUD_AU_LIVE = "https://terminal-api-live-au.adyen.com"; + public static final String ENDPOINT_TERMINAL_CLOUD_APSE_LIVE = "https://terminal-api-live-apse.adyen.com"; public Client() { this.config = new Config(); diff --git a/src/main/java/com/adyen/enums/Region.java b/src/main/java/com/adyen/enums/Region.java index 3ff5e7843..fe6249e79 100644 --- a/src/main/java/com/adyen/enums/Region.java +++ b/src/main/java/com/adyen/enums/Region.java @@ -1,8 +1,59 @@ 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 enum Region { - EU, - AU, - US, - IN -} +public final class Region { + + // Prevent instantiation + private Region() { + throw new UnsupportedOperationException("Utility class"); + } + + /** + * European Union region + */ + public static final String EU = "eu"; + + /** + * United States region + */ + public static final String US = "us"; + + /** + * Australia region + */ + public static final String AU = "au"; + + /** + * India region + */ + public static final String IN = "in"; + + /** + * Asia-Pacific, South East region + */ + public static final String APSE = "apse"; + + /** + * List of all valid regions + */ + public static final List VALID_REGIONS = + Collections.unmodifiableList(List.of(EU, US, AU, IN, APSE)); + + /** + * Maps regions to their respective Terminal API endpoints. + */ + public static final Map TERMINAL_API_ENDPOINTS_MAPPING; + + static { + Map endpointsMapping = new HashMap<>(); + endpointsMapping.put(EU, Client.ENDPOINT_TERMINAL_CLOUD_LIVE); + endpointsMapping.put(US, Client.ENDPOINT_TERMINAL_CLOUD_US_LIVE); + endpointsMapping.put(AU, Client.ENDPOINT_TERMINAL_CLOUD_AU_LIVE); + endpointsMapping.put(APSE, Client.ENDPOINT_TERMINAL_CLOUD_APSE_LIVE); + TERMINAL_API_ENDPOINTS_MAPPING = Collections.unmodifiableMap(endpointsMapping); + } +} \ No newline at end of file From 5c26c9d11c516e478fa3d162bb29cd3b103de08d Mon Sep 17 00:00:00 2001 From: Djoyke Reijans <115019123+DjoykeAbyah@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:17:11 +0000 Subject: [PATCH 2/6] added tests for Region --- src/main/java/com/adyen/enums/Region.java | 4 +- src/test/java/com/adyen/RegionTest.java | 54 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/adyen/RegionTest.java diff --git a/src/main/java/com/adyen/enums/Region.java b/src/main/java/com/adyen/enums/Region.java index fe6249e79..0faeb05f4 100644 --- a/src/main/java/com/adyen/enums/Region.java +++ b/src/main/java/com/adyen/enums/Region.java @@ -27,9 +27,9 @@ private Region() { */ public static final String AU = "au"; - /** + /** * India region - */ + */ public static final String IN = "in"; /** diff --git a/src/test/java/com/adyen/RegionTest.java b/src/test/java/com/adyen/RegionTest.java new file mode 100644 index 000000000..ad210c18e --- /dev/null +++ b/src/test/java/com/adyen/RegionTest.java @@ -0,0 +1,54 @@ +/** + * Adyen Java API Library + * Copyright (c) 2022 Adyen N.V. + * This file is open source and available under the MIT license. + * See the LICENSE file for more info. + */ +package com.adyen; + +import com.adyen.enums.Region; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class RegionTest { + @Test + public void testValidRegions() { + // Define the expected list of valid regions + List 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, + Region.VALID_REGIONS + ); + } + + @Test + public void testTerminalApiEndpointsMapping() { + // Define the expected map of region to endpoint mappings + Map 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, + Region.TERMINAL_API_ENDPOINTS_MAPPING + ); + } +} \ No newline at end of file From 3172679a3e1d10f16408a6ebc168cb6962c7ed8c Mon Sep 17 00:00:00 2001 From: Djoyke Reijans <115019123+DjoykeAbyah@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:10:45 +0000 Subject: [PATCH 3/6] refactored client.java and region.java --- src/main/java/com/adyen/Client.java | 7 +++---- src/main/java/com/adyen/enums/Region.java | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/adyen/Client.java b/src/main/java/com/adyen/Client.java index d39314998..3390c880e 100644 --- a/src/main/java/com/adyen/Client.java +++ b/src/main/java/com/adyen/Client.java @@ -13,10 +13,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"; - public static final String ENDPOINT_TERMINAL_CLOUD_LIVE = "https://terminal-api-live.adyen.com"; - public static final String ENDPOINT_TERMINAL_CLOUD_US_LIVE = "https://terminal-api-live-us.adyen.com"; - public static final String ENDPOINT_TERMINAL_CLOUD_AU_LIVE = "https://terminal-api-live-au.adyen.com"; - public static final String ENDPOINT_TERMINAL_CLOUD_APSE_LIVE = "https://terminal-api-live-apse.adyen.com"; + 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(); diff --git a/src/main/java/com/adyen/enums/Region.java b/src/main/java/com/adyen/enums/Region.java index 0faeb05f4..bfea8dded 100644 --- a/src/main/java/com/adyen/enums/Region.java +++ b/src/main/java/com/adyen/enums/Region.java @@ -50,10 +50,10 @@ private Region() { static { Map endpointsMapping = new HashMap<>(); - endpointsMapping.put(EU, Client.ENDPOINT_TERMINAL_CLOUD_LIVE); - endpointsMapping.put(US, Client.ENDPOINT_TERMINAL_CLOUD_US_LIVE); - endpointsMapping.put(AU, Client.ENDPOINT_TERMINAL_CLOUD_AU_LIVE); - endpointsMapping.put(APSE, Client.ENDPOINT_TERMINAL_CLOUD_APSE_LIVE); + endpointsMapping.put(EU, Client.TERMINAL_API_ENDPOINT_LIVE); + endpointsMapping.put(US, Client.TERMINAL_API_ENDPOINT_US); + endpointsMapping.put(AU, Client.TERMINAL_API_ENDPOINT_AU); + endpointsMapping.put(APSE, Client.TERMINAL_API_ENDPOINT_APSE); TERMINAL_API_ENDPOINTS_MAPPING = Collections.unmodifiableMap(endpointsMapping); } } \ No newline at end of file From 080cb20b92d5d1b57f6d34299dfc73ceb2bf6dc5 Mon Sep 17 00:00:00 2001 From: Djoyke Reijans <115019123+DjoykeAbyah@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:17:27 +0000 Subject: [PATCH 4/6] adjusted regions.java to contain enums and regiontest.java --- src/main/java/com/adyen/enums/Region.java | 59 -------------------- src/main/java/com/adyen/enums/Regions.java | 62 ++++++++++++++++++++++ src/test/java/com/adyen/ClientTest.java | 1 - src/test/java/com/adyen/RegionTest.java | 30 +++++++---- 4 files changed, 82 insertions(+), 70 deletions(-) delete mode 100644 src/main/java/com/adyen/enums/Region.java create mode 100644 src/main/java/com/adyen/enums/Regions.java diff --git a/src/main/java/com/adyen/enums/Region.java b/src/main/java/com/adyen/enums/Region.java deleted file mode 100644 index bfea8dded..000000000 --- a/src/main/java/com/adyen/enums/Region.java +++ /dev/null @@ -1,59 +0,0 @@ -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 Region { - - // Prevent instantiation - private Region() { - throw new UnsupportedOperationException("Utility class"); - } - - /** - * European Union region - */ - public static final String EU = "eu"; - - /** - * United States region - */ - public static final String US = "us"; - - /** - * Australia region - */ - public static final String AU = "au"; - - /** - * India region - */ - public static final String IN = "in"; - - /** - * Asia-Pacific, South East region - */ - public static final String APSE = "apse"; - - /** - * List of all valid regions - */ - public static final List VALID_REGIONS = - Collections.unmodifiableList(List.of(EU, US, AU, IN, APSE)); - - /** - * Maps regions to their respective Terminal API endpoints. - */ - public static final Map TERMINAL_API_ENDPOINTS_MAPPING; - - static { - Map endpointsMapping = new HashMap<>(); - endpointsMapping.put(EU, Client.TERMINAL_API_ENDPOINT_LIVE); - endpointsMapping.put(US, Client.TERMINAL_API_ENDPOINT_US); - endpointsMapping.put(AU, Client.TERMINAL_API_ENDPOINT_AU); - endpointsMapping.put(APSE, Client.TERMINAL_API_ENDPOINT_APSE); - TERMINAL_API_ENDPOINTS_MAPPING = Collections.unmodifiableMap(endpointsMapping); - } -} \ No newline at end of file diff --git a/src/main/java/com/adyen/enums/Regions.java b/src/main/java/com/adyen/enums/Regions.java new file mode 100644 index 000000000..d2a72d445 --- /dev/null +++ b/src/main/java/com/adyen/enums/Regions.java @@ -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 VALID_REGIONS = + Collections.unmodifiableList(List.of(Region.values())); + + /** + * Maps regions to their respective Terminal API endpoints. + */ + public static final Map TERMINAL_API_ENDPOINTS_MAPPING; + + static { + Map 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); + } +} \ No newline at end of file diff --git a/src/test/java/com/adyen/ClientTest.java b/src/test/java/com/adyen/ClientTest.java index accc9a461..24612da13 100644 --- a/src/test/java/com/adyen/ClientTest.java +++ b/src/test/java/com/adyen/ClientTest.java @@ -1,7 +1,6 @@ package com.adyen; import com.adyen.enums.Environment; -import com.adyen.enums.Region; import com.adyen.model.RequestOptions; import org.junit.Assert; import org.junit.Test; diff --git a/src/test/java/com/adyen/RegionTest.java b/src/test/java/com/adyen/RegionTest.java index ad210c18e..7dd9b3fb8 100644 --- a/src/test/java/com/adyen/RegionTest.java +++ b/src/test/java/com/adyen/RegionTest.java @@ -1,14 +1,9 @@ -/** - * Adyen Java API Library - * Copyright (c) 2022 Adyen N.V. - * This file is open source and available under the MIT license. - * See the LICENSE file for more info. - */ package com.adyen; -import com.adyen.enums.Region; +import com.adyen.enums.Regions.Region; +import com.adyen.enums.Regions; +import java.util.stream.Collectors; import org.junit.Test; -import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -17,6 +12,12 @@ public class RegionTest { @Test public void testValidRegions() { + + // Convert Regions.VALID_REGIONS to lowercase strings for comparison + List actual = Regions.VALID_REGIONS.stream() + .map(region -> region.name().toLowerCase()) + .collect(Collectors.toList()); + // Define the expected list of valid regions List expected = List.of( "eu", @@ -30,12 +31,21 @@ public void testValidRegions() { assertEquals( "VALID_REGIONS should match the expected regions.", expected, - Region.VALID_REGIONS + actual ); } @Test public void testTerminalApiEndpointsMapping() { + + // Convert TERMINAL_API_ENDPOINTS_MAPPING keys to lowercase strings for comparison + Map actual = Regions.TERMINAL_API_ENDPOINTS_MAPPING.entrySet() + .stream() + .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 expected = Map.of( "eu", "https://terminal-api-live.adyen.com", @@ -48,7 +58,7 @@ public void testTerminalApiEndpointsMapping() { assertEquals( "TERMINAL_API_ENDPOINTS_MAPPING should match the expected mappings.", expected, - Region.TERMINAL_API_ENDPOINTS_MAPPING + actual ); } } \ No newline at end of file From e50066317617eff6cfc73fdab20c2f16f5fe2ded Mon Sep 17 00:00:00 2001 From: Djoyke Reijans <115019123+DjoykeAbyah@users.noreply.github.com> Date: Tue, 17 Dec 2024 15:36:45 +0000 Subject: [PATCH 5/6] added getCloudEndpoint method --- src/main/java/com/adyen/Client.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/com/adyen/Client.java b/src/main/java/com/adyen/Client.java index 3390c880e..3d1d3a9fe 100644 --- a/src/main/java/com/adyen/Client.java +++ b/src/main/java/com/adyen/Client.java @@ -1,6 +1,7 @@ package com.adyen; import com.adyen.enums.Environment; +import com.adyen.enums.Regions; import com.adyen.httpclient.AdyenHttpClient; import com.adyen.httpclient.ClientInterface; @@ -142,6 +143,19 @@ public void setEnvironment(Environment environment, String liveEndpointUrlPrefix } } + public String getCloudEndpoint() { + // Return a custom endpoint if it has already been set + if (config.getTerminalApiCloudEndpoint() != null) { + return config.getTerminalApiCloudEndpoint(); + } + // Check the environment and get the endpoint + if (Environment.TEST.equals(this.config.getEnvironment())){ + return Client.TERMINAL_API_ENDPOINT_TEST; + } + // For LIVE environment, lookup the endpoint using the map + return Regions.TERMINAL_API_ENDPOINTS_MAPPING.get(region != null ? region : Region.EU); + } + @Override public String toString() { return "Client [webServiceUser=" + this.config.username + ", environment=" + this.config.environment + "]"; From ad0c80d2eae219830ed26b7311f0bdd4705978cb Mon Sep 17 00:00:00 2001 From: Djoyke Reijans <115019123+DjoykeAbyah@users.noreply.github.com> Date: Tue, 17 Dec 2024 16:02:39 +0000 Subject: [PATCH 6/6] adjust ClientTest --- src/main/java/com/adyen/Client.java | 5 +++-- src/main/java/com/adyen/Config.java | 11 ++++++++++- src/test/java/com/adyen/ClientTest.java | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/adyen/Client.java b/src/main/java/com/adyen/Client.java index 3d1d3a9fe..bc491bdbe 100644 --- a/src/main/java/com/adyen/Client.java +++ b/src/main/java/com/adyen/Client.java @@ -2,6 +2,7 @@ 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; @@ -143,13 +144,13 @@ public void setEnvironment(Environment environment, String liveEndpointUrlPrefix } } - public String getCloudEndpoint() { + public String getCloudEndpoint(Region region) { // Return a custom endpoint if it has already been set if (config.getTerminalApiCloudEndpoint() != null) { return config.getTerminalApiCloudEndpoint(); } // Check the environment and get the endpoint - if (Environment.TEST.equals(this.config.getEnvironment())){ + if (Environment.TEST.equals(this.config.getEnvironment())) { return Client.TERMINAL_API_ENDPOINT_TEST; } // For LIVE environment, lookup the endpoint using the map diff --git a/src/main/java/com/adyen/Config.java b/src/main/java/com/adyen/Config.java index dae821953..61b301ad1 100644 --- a/src/main/java/com/adyen/Config.java +++ b/src/main/java/com/adyen/Config.java @@ -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; @@ -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; @@ -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; } diff --git a/src/test/java/com/adyen/ClientTest.java b/src/test/java/com/adyen/ClientTest.java index 24612da13..290fe3500 100644 --- a/src/test/java/com/adyen/ClientTest.java +++ b/src/test/java/com/adyen/ClientTest.java @@ -35,6 +35,7 @@ public void testConfigLiveClient() { config.setApiKey(apiKey); Client client = new Client(config); Assert.assertEquals(Environment.LIVE, client.getConfig().getEnvironment()); + // Assert.assertEquals("https://terminal-api-live.adyen.com", client.getTerminalApiRegion()); } @Test