Skip to content
Open
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
18 changes: 16 additions & 2 deletions src/main/java/com/currencycloud/client/CurrencyCloudClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -119,7 +122,18 @@ public CurrencyCloudClient(Environment environment, String loginId, String apiKe
this(environment.url, loginId, apiKey, HttpClientConfiguration.builder().build());
}

CurrencyCloudClient(String url, String loginId, String apiKey, HttpClientConfiguration httpClientConfiguration) {
public CurrencyCloudClient(String url, String loginId, String apiKey) {
this(url, loginId, apiKey, HttpClientConfiguration.builder().build());
}

public CurrencyCloudClient(String url, String loginId, String apiKey, HttpClientConfiguration httpClientConfiguration) {
String wellFormedUrl;
try {
wellFormedUrl = new URI(url).toURL().toString();
} catch (URISyntaxException | MalformedURLException e) {
throw new IllegalArgumentException(e);
}

this.loginId = loginId;
this.apiKey = apiKey;
ClientConfig config = new ClientConfig();
Expand All @@ -144,7 +158,7 @@ public void configureObjectMapper(ObjectMapper objectMapper) {
config.setHttpReadTimeout(httpClientConfiguration.getHttpReadTimeout());

api = RestProxyFactory.createProxy(
CurrencyCloud.class, url, config,
CurrencyCloud.class, wellFormedUrl, config,
new AutoAuthenticator(this), new ExceptionTransformer(), new Reauthenticator(this)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,45 @@
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

public class CurrencyCloudClientSessionTest {

@Test
public void testRaisesAnErrorIfTheEnvironmentIsNotSet() throws Exception {
try {
CurrencyCloudClient client = new CurrencyCloudClient((CurrencyCloudClient.Environment) null, null, null);
client.authenticate();
throw new AssertionError("Should have failed.");
} catch (NullPointerException ignored) { }
public void testRaisesAnErrorIfTheEnvironmentIsNotSet() {
assertThrows(NullPointerException.class, () -> new CurrencyCloudClient((CurrencyCloudClient.Environment) null, null, null));
}

@Test
public void testRaisesAnErrorIfTheLoginIdIsNotSet() throws Exception {
public void testRaisesAnErrorIfTheLoginIdIsNotSet() {
CurrencyCloudClient client = new CurrencyCloudClient(CurrencyCloudClient.Environment.demo, null, null);
try {
client.authenticate();
throw new AssertionError("Should have failed.");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("apiKey must be set"));
}
Exception exception = assertThrows(IllegalArgumentException.class, client::authenticate);
assertThat(exception.getMessage(), containsString("apiKey must be set"));
}

@Test
public void testRaisesAnErrorIfTheApiKeyIsNotSet() throws Exception {
public void testRaisesAnErrorIfTheApiKeyIsNotSet() {
CurrencyCloudClient client = new CurrencyCloudClient(CurrencyCloudClient.Environment.demo, "development@currencycloud.com", null);
try {
client.authenticate();
throw new AssertionError("Should have failed.");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("apiKey"));
assertThat(e.getMessage(), containsString("must be set"));
}
Exception exception = assertThrows(IllegalArgumentException.class, client::authenticate);
assertThat(exception.getMessage(), containsString("apiKey must be set"));
}

@Test
public void testRaisesAnErrorIfTheUrlNoAbsolute() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> new CurrencyCloudClient("example.com", "development@currencycloud.com", "key"));
assertThat(exception.getMessage(), equalTo("URI is not absolute"));
}

@Test
public void testRaisesAnErrorIfTheUrlIncorrect() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> new CurrencyCloudClient("example com", "development@currencycloud.com", "key"));
assertThat(exception.getMessage(), containsString("Illegal character in path at index 7: example com"));
}

@Test(expected = Test.None.class)
public void testValidUrlAndPortAccepted() {
new CurrencyCloudClient("http://localhost:8080", "development@currencycloud.com", "key");
}
}