Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions src/main/java/com/adyen/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.adyen;

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

/**
* A generic service that provides shared functionality for all API services. It handles client and
Expand Down Expand Up @@ -139,6 +140,22 @@ protected String createBaseURL(String url) {
}
}

if (url.contains("device-api-")) {
if (config.getTerminalApiRegion() == null
|| config.getTerminalApiRegion().equals(Region.EU)) {
Comment thread
gcatanese marked this conversation as resolved.
url =
url.replaceFirst(
"https://device-api-test.adyen.com", "https://device-api-live.adyen.com");
} else {
url =
url.replaceFirst(
"https://device-api-test.adyen.com",
String.format(
"https://device-api-live-%s.adyen.com",
Comment thread
gcatanese marked this conversation as resolved.
config.getTerminalApiRegion().name().toLowerCase()));
}
}
Comment thread
gcatanese marked this conversation as resolved.

return url.replaceFirst("-test", "-live");
}
}
42 changes: 42 additions & 0 deletions src/test/java/com/adyen/ServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import static org.junit.jupiter.api.Assertions.*;

import com.adyen.enums.Environment;
import com.adyen.enums.Region;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/** Tests for {@link Service#createBaseURL(String)}. */
public class ServiceTest extends BaseTest {
Expand Down Expand Up @@ -122,4 +127,41 @@ public void testLiveRecurringUrlWithoutPrefix() {
assertThrows(IllegalArgumentException.class, () -> service.createBaseURL(testUrl));
assertEquals("please provide a live url prefix in the client", e.getMessage());
}

@Test
public void testDeviceApiTestEnvironment() {
config.setEnvironment(Environment.TEST);
String testUrl = "https://device-api-test.adyen.com/device/v1";

String actualUrl = service.createBaseURL(testUrl);
assertEquals(testUrl, actualUrl);
}

static Stream<Arguments> deviceApiRegionToLiveUrl() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👏

return Stream.of(
Arguments.of(null, "https://device-api-live.adyen.com/device/v1"),
Arguments.of(Region.EU, "https://device-api-live.adyen.com/device/v1"),
Arguments.of(Region.AU, "https://device-api-live-au.adyen.com/device/v1"),
Arguments.of(Region.US, "https://device-api-live-us.adyen.com/device/v1"),
Arguments.of(Region.APSE, "https://device-api-live-apse.adyen.com/device/v1"));
}

@ParameterizedTest
@MethodSource("deviceApiRegionToLiveUrl")
public void testDeviceApiLiveUrlPerRegion(Region region, String expectedUrl) {
config.setTerminalApiRegion(region);
String testUrl = "https://device-api-test.adyen.com/device/v1";

String actualUrl = service.createBaseURL(testUrl);
assertEquals(expectedUrl, actualUrl);
}

@Test
public void testDeviceApiCustomDomainFallsBackToGenericReplacement() {
String testUrl = "https://my-proxy-test.example.com/device-api-v1/path";
String expectedUrl = "https://my-proxy-live.example.com/device-api-v1/path";

String actualUrl = service.createBaseURL(testUrl);
assertEquals(expectedUrl, actualUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.adyen.service.clouddevice;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;

import com.adyen.BaseTest;
import com.adyen.Client;
import com.adyen.Config;
import com.adyen.constants.ApiConstants;
import com.adyen.enums.Environment;
import com.adyen.enums.Region;
import com.adyen.model.clouddevice.*;
import com.adyen.model.tapi.*;
import java.math.BigDecimal;
Expand All @@ -16,6 +20,60 @@

public class CloudDeviceApiTest extends BaseTest {

@Test
public void baseUrlOnTest() {
Client client = new Client(new Config().apiKey("test").environment(Environment.TEST));

CloudDeviceApi cloudDeviceApi = new CloudDeviceApi(client);
assertEquals(
String.format("https://device-api-test.adyen.com/v%s", CloudDeviceApi.API_VERSION),
cloudDeviceApi.baseURL);
}

@Test
public void baseUrlOnLive() {
Client client = new Client(new Config().apiKey("test").environment(Environment.LIVE));

CloudDeviceApi cloudDeviceApi = new CloudDeviceApi(client);
assertEquals(
String.format("https://device-api-live.adyen.com/v%s", CloudDeviceApi.API_VERSION),
cloudDeviceApi.baseURL);
}

@Test
public void baseUrlOnLivePreservesVersionPath() {
Client client = new Client(new Config().apiKey("test").environment(Environment.LIVE));

CloudDeviceApi cloudDeviceApi = new CloudDeviceApi(client);
assertEquals(
String.format("https://device-api-live.adyen.com/v%s", CloudDeviceApi.API_VERSION),
cloudDeviceApi.baseURL);
}

@Test
public void baseUrlOnLiveWithEuRegion() {
Client client =
new Client(
new Config().apiKey("test").environment(Environment.LIVE).terminalApiRegion(Region.EU));

CloudDeviceApi cloudDeviceApi = new CloudDeviceApi(client);
assertEquals(
String.format("https://device-api-live.adyen.com/v%s", CloudDeviceApi.API_VERSION),
cloudDeviceApi.baseURL);
}

@Test
public void baseUrlOnLiveWithRegion() {
Client client =
new Client(
new Config().apiKey("test").environment(Environment.LIVE).terminalApiRegion(Region.US));

CloudDeviceApi cloudDeviceApi = new CloudDeviceApi(client);
assertEquals(
String.format("https://device-api-live-us.adyen.com/v%s", CloudDeviceApi.API_VERSION),
cloudDeviceApi.baseURL);
}

@Test
public void sendSync() throws Exception {
Client client = createMockClientFromFile("mocks/clouddevice/payment-sync-success.json");
Expand Down
Loading