Skip to content

Commit 4b1d5b4

Browse files
DjoykeAbyahkwokhe
and
kwokhe
authored
Adjust Region class with valid regions plus endpoint mappings for terminal api live (#1407)
Co-authored-by: kwokhe <[email protected]>
1 parent cc52b02 commit 4b1d5b4

File tree

7 files changed

+211
-21
lines changed

7 files changed

+211
-21
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ import com.adyen.model.terminal.*;
304304
// Step 2: Initialize the client object
305305
Client client = new Client("Your YOUR_API_KEY", Environment.TEST);
306306

307+
// for LIVE environment use
308+
// Config config = new Config();
309+
// config.setEnvironment(Environment.LIVE);
310+
// config.setTerminalApiRegion(Region.EU);
311+
// Client client = new Client(config);
312+
307313
// Step 3: Initialize the API object
308314
TerminalCloudAPI terminalCloudApi = new TerminalCloudAPI(client);
309315

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -270,5 +270,11 @@
270270
<version>${swagger-core-version}</version>
271271
<scope>compile</scope>
272272
</dependency>
273+
<dependency>
274+
<groupId>org.junit.jupiter</groupId>
275+
<artifactId>junit-jupiter-params</artifactId>
276+
<version>5.10.0</version>
277+
<scope>test</scope>
278+
</dependency>
273279
</dependencies>
274280
</project>

src/main/java/com/adyen/Client.java

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.adyen;
22

33
import com.adyen.enums.Environment;
4+
import com.adyen.enums.Region;
45
import com.adyen.httpclient.AdyenHttpClient;
56
import com.adyen.httpclient.ClientInterface;
67

@@ -13,6 +14,9 @@ public class Client {
1314
public static final String LIB_VERSION = "32.1.0";
1415
public static final String TERMINAL_API_ENDPOINT_TEST = "https://terminal-api-test.adyen.com";
1516
public static final String TERMINAL_API_ENDPOINT_LIVE = "https://terminal-api-live.adyen.com";
17+
public static final String TERMINAL_API_ENDPOINT_US = "https://terminal-api-live-us.adyen.com";
18+
public static final String TERMINAL_API_ENDPOINT_AU = "https://terminal-api-live-au.adyen.com";
19+
public static final String TERMINAL_API_ENDPOINT_APSE = "https://terminal-api-live-apse.adyen.com";
1620

1721
public Client() {
1822
this.config = new Config();
@@ -127,16 +131,35 @@ public void setEnvironment(Environment environment) {
127131
* @param liveEndpointUrlPrefix Provide the unique live url prefix from the "API URLs and Response" menu in the Adyen Customer Area
128132
*/
129133
public void setEnvironment(Environment environment, String liveEndpointUrlPrefix) {
130-
if (liveEndpointUrlPrefix != null) {
131-
config.setLiveEndpointUrlPrefix(liveEndpointUrlPrefix);
134+
config.setEnvironment(environment);
135+
config.setLiveEndpointUrlPrefix(liveEndpointUrlPrefix);
136+
137+
String endpoint = retrieveCloudEndpoint(config.getTerminalApiRegion(), environment);
138+
config.setTerminalApiCloudEndpoint(endpoint);
139+
}
140+
141+
/**
142+
* @param region The region for which the endpoint is requested. If null or the region is not found, defaults to default EU endpoint.
143+
*/
144+
public String retrieveCloudEndpoint(Region region, Environment environment) {
145+
// Check the environment for TEST and get the endpoint
146+
if (environment.equals(Environment.TEST)) {
147+
return Client.TERMINAL_API_ENDPOINT_TEST;
132148
}
133-
if (Environment.TEST.equals(environment)) {
134-
this.config.setEnvironment(environment);
135-
this.config.setTerminalApiCloudEndpoint(TERMINAL_API_ENDPOINT_TEST);
136-
} else if (Environment.LIVE.equals(environment)) {
137-
this.config.setEnvironment(environment);
138-
this.config.setTerminalApiCloudEndpoint(TERMINAL_API_ENDPOINT_LIVE);
149+
150+
// For LIVE environment, lookup the endpoint using the map
151+
if (environment.equals(Environment.LIVE)) {
152+
if (region == null) {
153+
return Region.TERMINAL_API_ENDPOINTS_MAPPING.get(Region.EU);
154+
}
155+
if (!Region.TERMINAL_API_ENDPOINTS_MAPPING.containsKey(region)) {
156+
throw new IllegalArgumentException("TerminalAPI endpoint for " + region + " is not supported yet");
157+
}
158+
return Region.TERMINAL_API_ENDPOINTS_MAPPING.getOrDefault(region, TERMINAL_API_ENDPOINT_LIVE);
139159
}
160+
161+
// Default to TEST if no environment or region is specified
162+
return Client.TERMINAL_API_ENDPOINT_TEST;
140163
}
141164

142165
@Override

src/main/java/com/adyen/Config.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.adyen;
22

33
import com.adyen.enums.Environment;
4+
import com.adyen.enums.Region;
45

56
import javax.net.ssl.HostnameVerifier;
67
import javax.net.ssl.SSLContext;
@@ -22,6 +23,7 @@ public class Config {
2223
protected String terminalApiCloudEndpoint;
2324
protected String terminalApiLocalEndpoint;
2425
protected String liveEndpointUrlPrefix;
26+
protected Region terminalApiRegion;
2527
protected SSLContext sslContext;
2628
protected HostnameVerifier hostnameVerifier;
2729

@@ -85,6 +87,14 @@ public void setTerminalApiLocalEndpoint(String terminalApiLocalEndpoint) {
8587
this.terminalApiLocalEndpoint = terminalApiLocalEndpoint;
8688
}
8789

90+
public Region getTerminalApiRegion() {
91+
return terminalApiRegion;
92+
}
93+
94+
public void setTerminalApiRegion(Region terminalApiRegion) {
95+
this.terminalApiRegion = terminalApiRegion;
96+
}
97+
8898
public int getConnectionTimeoutMillis() {
8999
return connectionTimeoutMillis;
90100
}
+39-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
package com.adyen.enums;
22

3+
import java.util.Map;
4+
import com.adyen.Client;
5+
36
public enum Region {
7+
8+
/**
9+
* European Union region
10+
*/
411
EU,
12+
13+
/**
14+
* Australia region
15+
*/
516
AU,
17+
18+
/**
19+
* United States region
20+
*/
621
US,
7-
IN
22+
23+
/**
24+
* India region
25+
*/
26+
IN,
27+
28+
/**
29+
* Asia-Pacific, South East region
30+
*/
31+
APSE;
32+
33+
/**
34+
* Maps regions to their respective Terminal API endpoints.
35+
*/
36+
public static final Map<Region, String> TERMINAL_API_ENDPOINTS_MAPPING;
37+
38+
static {
39+
TERMINAL_API_ENDPOINTS_MAPPING = Map.of(
40+
Region.EU, Client.TERMINAL_API_ENDPOINT_LIVE,
41+
Region.AU, Client.TERMINAL_API_ENDPOINT_AU,
42+
Region.US, Client.TERMINAL_API_ENDPOINT_US,
43+
Region.APSE, Client.TERMINAL_API_ENDPOINT_APSE
44+
);
45+
}
846
}

src/test/java/com/adyen/ClientTest.java

+64-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package com.adyen;
22

3-
import com.adyen.enums.Environment;
4-
import com.adyen.enums.Region;
5-
import com.adyen.model.RequestOptions;
3+
import java.util.HashMap;
4+
import java.util.stream.Stream;
5+
6+
import javax.net.ssl.SSLContext;
7+
68
import org.junit.Assert;
79
import org.junit.Test;
810
import org.mockito.Mock;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.Arguments;
13+
import org.junit.jupiter.params.provider.MethodSource;
914

10-
import javax.net.ssl.SSLContext;
11-
import java.util.HashMap;
15+
import com.adyen.enums.Environment;
16+
import com.adyen.enums.Region;
17+
import com.adyen.model.RequestOptions;
1218

1319
public class ClientTest {
1420

@@ -18,16 +24,16 @@ public class ClientTest {
1824
@Mock
1925
private String apiKey;
2026

27+
2128
@Test
2229
public void testConfigTestClient() {
2330
Config config = new Config();
2431
config.setEnvironment(Environment.TEST);
2532
config.setApiKey(apiKey);
2633
Client client = new Client(config);
27-
2834
Assert.assertEquals(Environment.TEST, client.getConfig().getEnvironment());
2935
}
30-
36+
3137
@Test
3238
public void testConfigLiveClient() {
3339
Config config = new Config();
@@ -38,6 +44,56 @@ public void testConfigLiveClient() {
3844
Assert.assertEquals(Environment.LIVE, client.getConfig().getEnvironment());
3945
}
4046

47+
private static Stream<Arguments> provideCloudTestEndpointTestCases() {
48+
return Stream.of(
49+
Arguments.of(null, Environment.TEST, "https://terminal-api-test.adyen.com"),
50+
Arguments.of(Region.EU, Environment.TEST, "https://terminal-api-test.adyen.com"),
51+
Arguments.of(Region.AU, Environment.TEST, "https://terminal-api-test.adyen.com"),
52+
Arguments.of(Region.US, Environment.TEST, "https://terminal-api-test.adyen.com"),
53+
Arguments.of(Region.APSE, Environment.TEST, "https://terminal-api-test.adyen.com")
54+
);
55+
}
56+
57+
@ParameterizedTest
58+
@MethodSource("provideCloudTestEndpointTestCases")
59+
public void testGetCloudEndpointForTestEnvironment(Region region, Environment environment, String expectedEndpoint) {
60+
Config testConfig = new Config();
61+
testConfig.setEnvironment(Environment.TEST);
62+
testConfig.setTerminalApiRegion(region);
63+
Client testClient = new Client(testConfig);
64+
Assert.assertEquals(expectedEndpoint, testConfig.getTerminalApiCloudEndpoint());
65+
}
66+
67+
private static Stream<Arguments> provideCloudLiveEndpointTestCases() {
68+
return Stream.of(
69+
Arguments.of(null, Environment.LIVE, "https://terminal-api-live.adyen.com"),
70+
Arguments.of(Region.EU, Environment.LIVE, "https://terminal-api-live.adyen.com"),
71+
Arguments.of(Region.AU, Environment.LIVE, "https://terminal-api-live-au.adyen.com"),
72+
Arguments.of(Region.US, Environment.LIVE, "https://terminal-api-live-us.adyen.com"),
73+
Arguments.of(Region.APSE, Environment.LIVE, "https://terminal-api-live-apse.adyen.com")
74+
);
75+
}
76+
77+
@ParameterizedTest
78+
@MethodSource("provideCloudLiveEndpointTestCases")
79+
public void testGetCloudEndpointForLiveEnvironment(Region region, Environment environment, String expectedEndpoint) {
80+
Config liveConfig = new Config();
81+
liveConfig.setEnvironment(Environment.LIVE);
82+
liveConfig.setTerminalApiRegion(region);
83+
Client liveClient = new Client(liveConfig);
84+
Assert.assertEquals(expectedEndpoint, liveConfig.getTerminalApiCloudEndpoint());
85+
}
86+
87+
@Test
88+
public void testUnmappedIndiaRegionThrowsException() {
89+
Config config = new Config();
90+
config.setEnvironment(Environment.LIVE);
91+
config.setTerminalApiRegion(Region.IN);
92+
93+
Assert.assertThrows(IllegalArgumentException.class,
94+
() -> new Client(config));
95+
}
96+
4197
@Test
4298
public void testClientCertificateAuth() {
4399
Client client = new Client(clientCertificateAuthSSLContext, apiKey);
@@ -54,8 +110,4 @@ public void testRequestOptionsBuilderPattern() {
54110
.additionalServiceHeaders(map);
55111
Assert.assertEquals(requestOptions.getAdditionalServiceHeaders(), map);
56112
}
57-
58-
private void assertCommonEndpoints(Config config) {
59-
Assert.assertEquals(Client.TERMINAL_API_ENDPOINT_LIVE, config.getTerminalApiCloudEndpoint());
60-
}
61-
}
113+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.adyen;
2+
3+
import com.adyen.enums.Region;
4+
import java.util.stream.Collectors;
5+
import org.junit.Test;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class RegionTest {
12+
@Test
13+
public void testValidRegions() {
14+
List<Region> VALID_REGIONS = List.of(Region.values());
15+
16+
// Convert Regions.VALID_REGIONS to lowercase strings for comparison
17+
List<String> actual = VALID_REGIONS.stream()
18+
.map(region -> region.name().toLowerCase())
19+
.collect(Collectors.toList());
20+
21+
// Define the expected list of valid regions
22+
List<String> expected = List.of(
23+
"eu",
24+
"au",
25+
"us",
26+
"in",
27+
"apse"
28+
);
29+
30+
// Assert that the VALID_REGIONS in Region matches the expected list
31+
assertEquals(expected, actual);
32+
}
33+
34+
@Test
35+
public void testTerminalApiEndpointsMapping() {
36+
// Convert TERMINAL_API_ENDPOINTS_MAPPING keys to lowercase strings for comparison
37+
Map<String, String> actual = Region.TERMINAL_API_ENDPOINTS_MAPPING.entrySet()
38+
.stream()
39+
.collect(Collectors.toMap(
40+
entry -> entry.getKey().name().toLowerCase(), // Convert key (Region enum) to lowercase
41+
Map.Entry::getValue
42+
));
43+
44+
// Define the expected map of region to endpoint mappings
45+
Map<String, String> expected = Map.of(
46+
"eu", "https://terminal-api-live.adyen.com",
47+
"au", "https://terminal-api-live-au.adyen.com",
48+
"us", "https://terminal-api-live-us.adyen.com",
49+
"apse", "https://terminal-api-live-apse.adyen.com"
50+
);
51+
52+
// Assert that the TERMINAL_API_ENDPOINTS_MAPPING in Region matches the expected map
53+
assertEquals(expected, actual);
54+
}
55+
}

0 commit comments

Comments
 (0)