Add FineractClientTest.testInvalidOperations (FINERACT-1221)

This commit is contained in:
Michael Vorburger
2020-10-19 17:58:43 +02:00
committed by Michael Vorburger ⛑️
parent c8324a90b9
commit db4d2ae006
2 changed files with 40 additions and 11 deletions
@@ -20,6 +20,7 @@ package org.apache.fineract.client.util;
import org.apache.fineract.client.ApiClient; import org.apache.fineract.client.ApiClient;
import org.apache.fineract.client.auth.ApiKeyAuth; import org.apache.fineract.client.auth.ApiKeyAuth;
import org.apache.fineract.client.auth.HttpBasicAuth;
import org.apache.fineract.client.services.*; import org.apache.fineract.client.services.*;
/** /**
@@ -243,48 +244,67 @@ public class FineractClient {
workingDays = apiClient.createService(WorkingDaysApi.class); workingDays = apiClient.createService(WorkingDaysApi.class);
} }
public static FineractClientBuilder builder() { public static Builder builder() {
return new FineractClientBuilder(); return new Builder();
} }
protected <S> S createService(Class<S> serviceClass) { protected <S> S createService(Class<S> serviceClass) {
return api.createService(serviceClass); return api.createService(serviceClass);
} }
public static class FineractClientBuilder { public static class Builder {
private final ApiClient apiClient = new ApiClient();
private String baseURL; private String baseURL;
private String tenant; private String tenant;
private String username; private String username;
private String password; private String password;
private FineractClientBuilder() {} private Builder() {}
public FineractClientBuilder baseURL(String baseURL) { public Builder baseURL(String baseURL) {
this.baseURL = baseURL; this.baseURL = baseURL;
return this; return this;
} }
public FineractClientBuilder tenant(String tenant) { public Builder tenant(String tenant) {
this.tenant = tenant; this.tenant = tenant;
return this; return this;
} }
public FineractClientBuilder basicAuth(String username, String password) { public Builder basicAuth(String username, String password) {
this.username = username; this.username = username;
this.password = password; this.password = password;
return this; return this;
} }
public FineractClient build() { public FineractClient build() {
ApiClient apiClient = new ApiClient("basicAuth", has("username", username), has("password", password)); // URL
apiClient.getAdapterBuilder().baseUrl(has("baseURL", baseURL)); apiClient.getAdapterBuilder().baseUrl(has("baseURL", baseURL));
ApiKeyAuth authorization = new ApiKeyAuth("header", "fineract-platform-tenantid");
authorization.setApiKey(has("tenant", tenant)); // Tenant
apiClient.addAuthorization("tenantid", authorization); ApiKeyAuth tenantAuth = new ApiKeyAuth("header", "fineract-platform-tenantid");
tenantAuth.setApiKey(has("tenant", tenant));
apiClient.addAuthorization("tenantid", tenantAuth);
// BASIC Auth
HttpBasicAuth basicAuth = new HttpBasicAuth();
basicAuth.setCredentials(has("username", username), has("password", password));
apiClient.addAuthorization("basicAuth", basicAuth);
return new FineractClient(apiClient); return new FineractClient(apiClient);
} }
/**
* Obtain the internal Retrofit ApiClient. This method is typically not required to be invoked for simple API
* usages, but can be a handy back door for non-trivial advanced customizations of the API client.
*
* @return the {@link ApiClient} which {@link #build()} will use.
*/
public ApiClient getApiClient() {
return apiClient;
}
private <T> T has(String propertyName, T value) throws IllegalStateException { private <T> T has(String propertyName, T value) throws IllegalStateException {
if (value == null) { if (value == null) {
throw new IllegalStateException("Must call " + propertyName + "(...) to create valid Builder"); throw new IllegalStateException("Must call " + propertyName + "(...) to create valid Builder");
@@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException; import java.io.IOException;
import org.apache.fineract.client.util.FineractClient; import org.apache.fineract.client.util.FineractClient;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@@ -41,6 +42,14 @@ public class FineractClientTest {
.getTotalFilteredRecords()).isAtLeast(3); .getTotalFilteredRecords()).isAtLeast(3);
} }
@Test
@Disabled // TODO remove Ignore once https://issues.apache.org/jira/browse/FINERACT-1221 is fixed
void testInvalidOperations() throws IOException {
FineractClient.Builder builder = FineractClient.builder().baseURL("http://test/").tenant("default").basicAuth("mifos", "password");
builder.getApiClient().getAdapterBuilder().validateEagerly(true); // see FINERACT-1221
builder.build();
}
@Test @Test
void testFineractClientBuilder() throws IOException { void testFineractClientBuilder() throws IOException {
assertThrows(IllegalStateException.class, () -> { assertThrows(IllegalStateException.class, () -> {