Skip to content

Commit

Permalink
improved FireboltAccountClient + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexradzin committed Nov 2, 2023
1 parent 97bfb3f commit d3db8d2
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import lombok.CustomLog;
import okhttp3.OkHttpClient;

import static java.lang.String.format;

@CustomLog
public class FireboltAccountClient extends FireboltClient {

Expand All @@ -42,7 +44,7 @@ public FireboltAccountClient(OkHttpClient httpClient, ObjectMapper objectMapper,
*/
public FireboltAccountResponse getAccount(String host, String account, String accessToken)
throws FireboltException, IOException {
String uri = String.format(GET_ACCOUNT_ID_URI, host, account);
String uri = format(GET_ACCOUNT_ID_URI, host, account);
return getResource(uri, host, accessToken, FireboltAccountResponse.class);
}

Expand All @@ -58,19 +60,22 @@ public FireboltAccountResponse getAccount(String host, String account, String ac
*/
public FireboltEngineResponse getEngine(String host, String accountId, String engineName, String engineId,
String accessToken) throws FireboltException, IOException {
try {
String uri = createAccountUri(accountId, host, URI_SUFFIX_ACCOUNT_ENGINE_INFO_BY_ENGINE_ID + engineId);
return getResource(uri, host, accessToken, FireboltEngineResponse.class);
} catch (FireboltException exception) {
if (exception.getType() == ExceptionType.RESOURCE_NOT_FOUND) {
throw new FireboltException(
String.format("The address of the engine with name %s and id %s could not be found", engineName,
engineId),
exception, ExceptionType.RESOURCE_NOT_FOUND);
} else {
throw exception;
}
}
String uri = createAccountUri(accountId, host, URI_SUFFIX_ACCOUNT_ENGINE_INFO_BY_ENGINE_ID + engineId);
return getResource(uri, host, accessToken, FireboltEngineResponse.class, format("The address of the engine with name %s and id %s could not be found", engineName, engineId));
// try {
// String uri = createAccountUri(accountId, host, URI_SUFFIX_ACCOUNT_ENGINE_INFO_BY_ENGINE_ID + engineId);
// getResource(uri, host, accessToken, FireboltEngineResponse.class, format("The address of the engine with name %s and id %s could not be found", engineName, engineId));
// return getResource(uri, host, accessToken, FireboltEngineResponse.class);
// } catch (FireboltException exception) {
// if (exception.getType() == ExceptionType.RESOURCE_NOT_FOUND) {
// throw new FireboltException(
// format("The address of the engine with name %s and id %s could not be found", engineName,
// engineId),
// exception, ExceptionType.RESOURCE_NOT_FOUND);
// } else {
// throw exception;
// }
// }
}

/**
Expand All @@ -85,17 +90,18 @@ public FireboltEngineResponse getEngine(String host, String accountId, String en
public FireboltDefaultDatabaseEngineResponse getDefaultEngineByDatabaseName(String host, String accountId, String dbName,
String accessToken) throws FireboltException, IOException {
String uri = createAccountUri(accountId, host, URI_SUFFIX_DATABASE_INFO_URL + dbName);
try {
return getResource(uri, host, accessToken, FireboltDefaultDatabaseEngineResponse.class);

} catch (FireboltException exception) {
if (exception.getType() == ExceptionType.RESOURCE_NOT_FOUND) {
throw new FireboltException(String.format("The database with the name %s could not be found", dbName),
exception, ExceptionType.RESOURCE_NOT_FOUND);
} else {
throw exception;
}
}
return getResource(uri, host, accessToken, FireboltDefaultDatabaseEngineResponse.class, format("The database with the name %s could not be found", dbName));
// try {
// return getResource(uri, host, accessToken, FireboltDefaultDatabaseEngineResponse.class);
//
// } catch (FireboltException exception) {
// if (exception.getType() == ExceptionType.RESOURCE_NOT_FOUND) {
// throw new FireboltException(format("The database with the name %s could not be found", dbName),
// exception, ExceptionType.RESOURCE_NOT_FOUND);
// } else {
// throw exception;
// }
// }
}

/**
Expand All @@ -109,25 +115,35 @@ public FireboltDefaultDatabaseEngineResponse getDefaultEngineByDatabaseName(Stri
*/
public FireboltEngineIdResponse getEngineId(String host, String accountId, String engineName, String accessToken)
throws FireboltException, IOException {
String uri = createAccountUri(accountId, host, URI_SUFFIX_ENGINE_AND_ACCOUNT_ID_BY_ENGINE_NAME + engineName);
return getResource(uri, host, accessToken, FireboltEngineIdResponse.class, format("The engine %s could not be found", engineName));
// try {
// String uri = createAccountUri(accountId, host, URI_SUFFIX_ENGINE_AND_ACCOUNT_ID_BY_ENGINE_NAME + engineName);
// return getResource(uri, host, accessToken, FireboltEngineIdResponse.class);
// } catch (FireboltException exception) {
// if (exception.getType() == ExceptionType.RESOURCE_NOT_FOUND) {
// throw new FireboltException(format("The engine %s could not be found", engineName), exception,
// ExceptionType.RESOURCE_NOT_FOUND);
// } else {
// throw exception;
// }
// }
}


private <R> R getResource(String uri, String host, String accessToken, Class<R> responseType, String notFoundErrorMessage) throws FireboltException, IOException {
try {
String uri = createAccountUri(accountId, host,
URI_SUFFIX_ENGINE_AND_ACCOUNT_ID_BY_ENGINE_NAME + engineName);
return getResource(uri, host, accessToken, FireboltEngineIdResponse.class);
return getResource(uri, host, accessToken, responseType);
} catch (FireboltException exception) {
if (exception.getType() == ExceptionType.RESOURCE_NOT_FOUND) {
throw new FireboltException(String.format("The engine %s could not be found", engineName), exception,
ExceptionType.RESOURCE_NOT_FOUND);
} else {
throw exception;
throw new FireboltException(notFoundErrorMessage, exception, ExceptionType.RESOURCE_NOT_FOUND);
}
throw exception;
}
}

private String createAccountUri(String account, String host, String suffix) {
if (StringUtils.isNotEmpty(account))
return String.format(URI_PREFIX_WITH_ACCOUNT_RESOURCE, host, account, suffix);
else
return String.format(URI_PREFIX_WITHOUT_ACCOUNT_RESOURCE, host, suffix);
return StringUtils.isEmpty(account) ? format(URI_PREFIX_WITHOUT_ACCOUNT_RESOURCE, host, suffix) : format(URI_PREFIX_WITH_ACCOUNT_RESOURCE, host, account, suffix);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;

@Value
@AllArgsConstructor
@Builder
public class FireboltAccountResponse {
@JsonProperty("account_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;

@Value
@AllArgsConstructor
@Builder
public class FireboltDefaultDatabaseEngineResponse {
@JsonProperty("engine_url")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.firebolt.jdbc.client.account;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.firebolt.jdbc.client.account.response.FireboltAccountResponse;
import com.firebolt.jdbc.client.account.response.FireboltDefaultDatabaseEngineResponse;
import com.firebolt.jdbc.client.account.response.FireboltEngineIdResponse;
import com.firebolt.jdbc.client.account.response.FireboltEngineResponse;
import com.firebolt.jdbc.client.gateway.GatewayUrlResponse;
import com.firebolt.jdbc.connection.FireboltConnection;
import com.firebolt.jdbc.exception.ExceptionType;
import com.firebolt.jdbc.exception.FireboltException;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.net.HttpURLConnection;

import static com.firebolt.jdbc.exception.ExceptionType.RESOURCE_NOT_FOUND;
import static com.firebolt.jdbc.exception.ExceptionType.TOO_MANY_REQUESTS;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class FireboltAccountClientTest {
private final ObjectMapper objectMapper = new ObjectMapper();
@Mock
private OkHttpClient httpClient;
@Mock
private FireboltConnection fireboltConnection;
@InjectMocks
private FireboltAccountClient client;

@Test
void getAccount() throws FireboltException, IOException {
String accountId = "123";
injectMockedResponse(httpClient, HTTP_OK, new FireboltAccountResponse(accountId));
assertEquals(accountId, client.getAccount("http://host", "account", "token").getAccountId());
}

@Test
void getEngine() throws IOException, FireboltException {
String endpoint = "http://engine/12345";
FireboltEngineResponse response = FireboltEngineResponse.builder().engine(FireboltEngineResponse.Engine.builder().currentStatus("running").endpoint(endpoint).build()).build();
injectMockedResponse(httpClient, HTTP_OK, response);
assertEquals(endpoint, client.getEngine("http://host", "account-id", "engine", "engine-id", "token").getEngine().getEndpoint());
}

@Test
void getDefaultEngineByDatabaseName() throws FireboltException, IOException {
String endpoint = "http://engine/12345";
FireboltDefaultDatabaseEngineResponse response = FireboltDefaultDatabaseEngineResponse.builder().engineUrl(endpoint).build();
injectMockedResponse(httpClient, HTTP_OK, response);
assertEquals(endpoint, client.getDefaultEngineByDatabaseName("http://host", "account-id", "db", "token").getEngineUrl());
}

@Test
void getEngineId() throws FireboltException, IOException {
String engineId = "456";
FireboltEngineIdResponse response = FireboltEngineIdResponse.builder().engine(FireboltEngineIdResponse.Engine.builder().engineId(engineId).build()).build();
injectMockedResponse(httpClient, HTTP_OK, response);
assertEquals(engineId, client.getEngineId("http://host", "account-id", "db", "token").getEngine().getEngineId());
}

@Test
void notFoundError() throws IOException {
injectMockedResponse(httpClient, HTTP_NOT_FOUND, null);
assertNotFoundException(assertThrows(FireboltException.class, () -> client.getEngine("http://host", "account-id", "engine", "engine-id", "token")));
assertNotFoundException(assertThrows(FireboltException.class, () -> client.getDefaultEngineByDatabaseName("http://host", "account-id", "db", "token")));
assertNotFoundException(assertThrows(FireboltException.class, () -> client.getEngineId("http://host", "account-id", "db", "token")));
}

@ParameterizedTest
@CsvSource(value = {
"404, RESOURCE_NOT_FOUND",
"400, INVALID_REQUEST",
"401, UNAUTHORIZED",
"429, TOO_MANY_REQUESTS",
"406, ERROR"
})
void httpFailures(int httpStatus, ExceptionType type) throws IOException {
injectMockedResponse(httpClient, httpStatus, null);
assertEquals(type, assertThrows(FireboltException.class, () -> client.getEngine("http://host", "account-id", "engine", "engine-id", "token")).getType());
assertEquals(type, assertThrows(FireboltException.class, () -> client.getDefaultEngineByDatabaseName("http://host", "account-id", "db", "token")).getType());
assertEquals(type, assertThrows(FireboltException.class, () -> client.getEngineId("http://host", "account-id", "db", "token")).getType());
}

private void assertNotFoundException(FireboltException e) {
assertEquals(RESOURCE_NOT_FOUND, e.getType());
assertTrue(e.getMessage().contains("could not be found"));
}

private void injectMockedResponse(OkHttpClient httpClient, int code, Object payload) throws IOException {
Response response = mock(Response.class);
Call call = mock(Call.class);
when(httpClient.newCall(any())).thenReturn(call);
when(call.execute()).thenReturn(response);
ResponseBody body = mock(ResponseBody.class);
when(response.body()).thenReturn(body);
when(response.code()).thenReturn(code);
String gatewayResponse = new ObjectMapper().writeValueAsString(payload);
lenient().when(body.string()).thenReturn(gatewayResponse);
}
}

0 comments on commit d3db8d2

Please sign in to comment.