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 39 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ import com.adyen.model.terminal.*;
// Step 2: Initialize the client object
Client client = new Client("Your YOUR_API_KEY", Environment.TEST);

// for LIVE environment use
// Config config = new Config();
// config.setEnvironment(Environment.LIVE);
// config.setTerminalApiRegion(Region.EU);
// Client client = new Client(config);

// Step 3: Initialize the API object
TerminalCloudAPI terminalCloudApi = new TerminalCloudAPI(client);

Expand Down
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>
40 changes: 32 additions & 8 deletions src/main/java/com/adyen/Client.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adyen;

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

Expand All @@ -13,6 +14,9 @@ public class Client {
public static final String LIB_VERSION = "32.1.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 @@ -127,16 +131,36 @@ public void setEnvironment(Environment environment) {
* @param liveEndpointUrlPrefix Provide the unique live url prefix from the "API URLs and Response" menu in the Adyen Customer Area
*/
public void setEnvironment(Environment environment, String liveEndpointUrlPrefix) {
if (liveEndpointUrlPrefix != null) {
config.setLiveEndpointUrlPrefix(liveEndpointUrlPrefix);
config.setEnvironment(environment);
config.setLiveEndpointUrlPrefix(liveEndpointUrlPrefix);

// Determine and set the endpoint using getCloudEndpoint
String endpoint = retrieveCloudEndpoint(config.getTerminalApiRegion(), environment);
config.setTerminalApiCloudEndpoint(endpoint);
}

/**
* @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 retrieveCloudEndpoint(Region region, Environment environment) {
// Check the environment for TEST and get the endpoint
if (environment.equals(Environment.TEST)) {
return Client.TERMINAL_API_ENDPOINT_TEST;
}
if (Environment.TEST.equals(environment)) {
this.config.setEnvironment(environment);
this.config.setTerminalApiCloudEndpoint(TERMINAL_API_ENDPOINT_TEST);
} else if (Environment.LIVE.equals(environment)) {
this.config.setEnvironment(environment);
this.config.setTerminalApiCloudEndpoint(TERMINAL_API_ENDPOINT_LIVE);

// For LIVE environment, lookup the endpoint using the map
if (environment.equals(Environment.LIVE)) {
if (region == null) {
return Region.TERMINAL_API_ENDPOINTS_MAPPING.get(Region.EU);
}
if (!Region.TERMINAL_API_ENDPOINTS_MAPPING.containsKey(region)) {
throw new IllegalArgumentException("TerminalAPI endpoint for " + region + " is not supported yet");
}
return Region.TERMINAL_API_ENDPOINTS_MAPPING.getOrDefault(region, TERMINAL_API_ENDPOINT_LIVE);
}

// Default to EU endpoint if no environment or region is specified
return Region.TERMINAL_API_ENDPOINTS_MAPPING.get(Region.EU);
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/adyen/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adyen;

import com.adyen.enums.Environment;
import com.adyen.enums.Region;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
Expand All @@ -22,6 +23,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 @@ -85,6 +87,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
40 changes: 39 additions & 1 deletion src/main/java/com/adyen/enums/Region.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
package com.adyen.enums;

import java.util.Map;
import com.adyen.Client;

public enum Region {
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved

/**
* European Union region
*/
EU,
DjoykeAbyah marked this conversation as resolved.
Show resolved Hide resolved

/**
* Australia region
*/
AU,

/**
* United States region
*/
US,
IN

/**
* India region
*/
IN,

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

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

static {
TERMINAL_API_ENDPOINTS_MAPPING = Map.of(
Region.EU, Client.TERMINAL_API_ENDPOINT_LIVE,
Region.AU, Client.TERMINAL_API_ENDPOINT_AU,
Region.US, Client.TERMINAL_API_ENDPOINT_US,
Region.APSE, Client.TERMINAL_API_ENDPOINT_APSE
);
}
}
76 changes: 64 additions & 12 deletions src/test/java/com/adyen/ClientTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.adyen;

import com.adyen.enums.Environment;
import com.adyen.enums.Region;
import com.adyen.model.RequestOptions;
import java.util.HashMap;
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.Region;
import com.adyen.model.RequestOptions;

public class ClientTest {

Expand All @@ -18,16 +24,16 @@ 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 +44,56 @@ 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);
testConfig.setTerminalApiRegion(region);
Client testClient = new Client(testConfig);
Assert.assertEquals(expectedEndpoint, testConfig.getTerminalApiCloudEndpoint());
}

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) {
Config liveConfig = new Config();
liveConfig.setEnvironment(Environment.LIVE);
liveConfig.setTerminalApiRegion(region);
Client liveClient = new Client(liveConfig);
Assert.assertEquals(expectedEndpoint, liveConfig.getTerminalApiCloudEndpoint());
}

@Test
public void testUnmappedIndiaRegionThrowsException() {
Config config = new Config();
config.setEnvironment(Environment.LIVE);
config.setTerminalApiRegion(Region.IN);

Assert.assertThrows(IllegalArgumentException.class,
() -> new Client(config));
}

@Test
public void testClientCertificateAuth() {
Client client = new Client(clientCertificateAuthSSLContext, apiKey);
Expand All @@ -54,8 +110,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());
}
}
}
55 changes: 55 additions & 0 deletions src/test/java/com/adyen/RegionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.adyen;

import com.adyen.enums.Region;
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() {
List<Region> VALID_REGIONS = List.of(Region.values());

// Convert Regions.VALID_REGIONS to lowercase strings for comparison
List<String> actual = VALID_REGIONS.stream()
.map(region -> region.name().toLowerCase())
.collect(Collectors.toList());

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

// Assert that the VALID_REGIONS in Region matches the expected list
assertEquals(expected, actual);
}

@Test
public void testTerminalApiEndpointsMapping() {
// Convert TERMINAL_API_ENDPOINTS_MAPPING keys to lowercase strings for comparison
Map<String, String> actual = Region.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<String, String> expected = Map.of(
"eu", "https://terminal-api-live.adyen.com",
"au", "https://terminal-api-live-au.adyen.com",
"us", "https://terminal-api-live-us.adyen.com",
"apse", "https://terminal-api-live-apse.adyen.com"
);

// Assert that the TERMINAL_API_ENDPOINTS_MAPPING in Region matches the expected map
assertEquals(expected, actual);
}
}
Loading