-
Notifications
You must be signed in to change notification settings - Fork 24
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
Optimise test framework (bunq/sdk_java#78) #85
Changes from all commits
812c235
451deed
dd8ae32
268e2e6
e826eb9
8467210
c12a285
58adb6c
b9a1b66
b075dec
38b52f0
255d46d
d0f9ed0
86acab5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,4 @@ context-save-restore-test.conf | |
config.properties | ||
bunq-test.conf | ||
.idea/codeStyles/ | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,51 +4,195 @@ | |
import com.bunq.sdk.context.ApiEnvironmentType; | ||
import com.bunq.sdk.context.BunqContext; | ||
import com.bunq.sdk.exception.BunqException; | ||
import com.bunq.sdk.http.ApiClient; | ||
import com.bunq.sdk.http.BunqResponse; | ||
import com.bunq.sdk.model.generated.endpoint.CashRegister; | ||
import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank; | ||
import com.bunq.sdk.model.generated.endpoint.RequestInquiry; | ||
import com.bunq.sdk.model.generated.endpoint.SandboxUser; | ||
import com.bunq.sdk.model.generated.object.Amount; | ||
import com.bunq.sdk.model.generated.object.Pointer; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.stream.JsonReader; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import org.junit.BeforeClass; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Base class for the Bunq SDK tests. | ||
*/ | ||
public class BunqSdkTestBase { | ||
|
||
/** | ||
* Description of the test device for Java SDK. | ||
* Error constants. | ||
*/ | ||
private static final String ERROR_COULD_NOT_GENERATE_NEW_API_KEY = "Encountered error while retrieving new sandbox ApiKey.\nError message %s"; | ||
|
||
private static final String DEVICE_DESCRIPTION = "Java test device"; | ||
private static final int HTTP_STATUS_OK = 200; | ||
private static final String FIELD_RESPONSE = "Response"; | ||
private static final String FIELD_API_KEY = "ApiKey"; | ||
private static final int INDEX_FIRST = 0; | ||
|
||
protected static final String TEST_CONFIG_PATH = "bunq-test.conf"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. protected under private? |
||
protected static final String CONTENT_TYPE = "image/png"; | ||
protected static final String ATTACHMENT_DESCRIPTION = "TEST PNG JAVA"; | ||
protected static final String ATTACHMENT_PATH_IN = "assets/[email protected]"; | ||
protected static final String SCHEME_HTTPS = "https://"; | ||
protected static final String URL_PATH_SEPARATOR = "/"; | ||
protected static final String URL_PATH_SANDBOX_USER = "/sandbox-user"; | ||
protected static final String CURRENCY_EUR = "EUR"; | ||
protected static final String ACCOUNT_DESCRIPTION = "test account java"; | ||
protected static final String SPENDING_MONEY_AMOUNT = "500"; | ||
protected static final String POINTER_TYPE_EMAIL = "EMAIL"; | ||
protected static final String SUGAR_DADDY_EMAIL = "[email protected]"; | ||
protected static final String SUGAR_DADDY_REQUESTS_DESCRIPTION = "sdk java test, thanks daddy <3"; | ||
protected static final String EMAIL_BRAVO = "[email protected]"; | ||
protected static final String CASH_REGISTER_DESCRIPTION = "java cash register test"; | ||
|
||
|
||
/** | ||
* Individual properties. | ||
*/ | ||
private static String apiKey = Config.getApiKey(); | ||
private static String[] permittedIps = Config.getPermittedIps(); | ||
private static String apiConfigPath = Config.getApiConfigPath(); | ||
protected static MonetaryAccountBank secondMonetaryAccountBank; | ||
protected static CashRegister cashRegister; | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
public static void setUpBeforeClass() { | ||
BunqContext.loadApiContext(getApiContext()); | ||
setSecondMonetaryAccountBank(); | ||
requestSpendingMoney(); | ||
|
||
try { | ||
Thread.sleep(500); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this sleep? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure the spending money request has been accepted before refreshing. The refresh part will be added with #79 |
||
BunqContext.getUserContext().refreshContext(); | ||
} catch (InterruptedException exception) { | ||
throw new BunqException(exception.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Based on the result of isSessionActive will create a new ApiContext or restore an old conf | ||
* file | ||
*/ | ||
protected static ApiContext getApiContext() { | ||
ApiContext apiContext; | ||
if (doesTestConfFileExist()) { | ||
ApiContext apiContext = ApiContext.restore(TEST_CONFIG_PATH); | ||
apiContext.ensureSessionActive(); | ||
apiContext.save(TEST_CONFIG_PATH); | ||
|
||
return apiContext; | ||
} else { | ||
SandboxUser sandboxUser = generateNewSandboxUser(); | ||
ApiContext apiContext = ApiContext.create( | ||
ApiEnvironmentType.SANDBOX, | ||
sandboxUser.getApiKey(), | ||
DEVICE_DESCRIPTION | ||
); | ||
apiContext.save(TEST_CONFIG_PATH); | ||
|
||
return apiContext; | ||
} | ||
} | ||
|
||
private static boolean doesTestConfFileExist() { | ||
File confFile = new File(TEST_CONFIG_PATH); | ||
|
||
return confFile.exists() && !confFile.isDirectory(); | ||
} | ||
|
||
private static SandboxUser generateNewSandboxUser() { | ||
OkHttpClient client = new OkHttpClient(); | ||
|
||
Request request = new Request.Builder() | ||
.url( | ||
SCHEME_HTTPS + | ||
ApiEnvironmentType.SANDBOX.getBaseUri() + | ||
URL_PATH_SEPARATOR + | ||
ApiEnvironmentType.SANDBOX.getApiVersion() + | ||
URL_PATH_SANDBOX_USER | ||
) | ||
.post(RequestBody.create(null, new byte[INDEX_FIRST])) | ||
.addHeader(ApiClient.HEADER_REQUEST_ID, UUID.randomUUID().toString()) | ||
.addHeader(ApiClient.HEADER_CACHE_CONTROL, ApiClient.CACHE_CONTROL_NONE) | ||
.addHeader(ApiClient.HEADER_GEOLOCATION, ApiClient.GEOLOCATION_ZERO) | ||
.addHeader(ApiClient.HEADER_LANGUAGE, ApiClient.LANGUAGE_EN_US) | ||
.addHeader(ApiClient.HEADER_REGION, ApiClient.REGION_NL_NL) | ||
.build(); | ||
|
||
try { | ||
apiContext = ApiContext.restore(apiConfigPath); | ||
apiContext.ensureSessionActive(); | ||
} catch (BunqException exception) { | ||
List<String> ips = Arrays.asList(permittedIps); | ||
apiContext = ApiContext.create(ApiEnvironmentType.SANDBOX, apiKey, DEVICE_DESCRIPTION, ips); | ||
Response response = client.newCall(request).execute(); | ||
if (response.code() == HTTP_STATUS_OK) { | ||
String responseString = response.body().string(); | ||
JsonObject jsonObject = new Gson().fromJson(responseString, JsonObject.class); | ||
JsonObject apiKEy =jsonObject.getAsJsonArray(FIELD_RESPONSE) | ||
.get(INDEX_FIRST) | ||
.getAsJsonObject() | ||
.get(FIELD_API_KEY) | ||
.getAsJsonObject(); | ||
|
||
return SandboxUser.fromJsonReader(new JsonReader(new StringReader(apiKEy.toString()))); | ||
} else { | ||
throw new BunqException(String.format(ERROR_COULD_NOT_GENERATE_NEW_API_KEY, response.body().string())); | ||
} | ||
} catch (IOException e) { | ||
throw new BunqException(e.getMessage()); | ||
} | ||
} | ||
|
||
private static void setSecondMonetaryAccountBank() { | ||
BunqResponse<Integer> response = MonetaryAccountBank.create(CURRENCY_EUR, ACCOUNT_DESCRIPTION); | ||
|
||
secondMonetaryAccountBank = MonetaryAccountBank.get(response.getValue()).getValue(); | ||
} | ||
|
||
/** | ||
* To ensure that our test user has enough money on the account, we sent a request to [email protected] | ||
* to top-up the account. | ||
*/ | ||
private static void requestSpendingMoney() { | ||
if (shouldMoneyBeRequested(BunqContext.getUserContext().getPrimaryMonetaryAccountBank())) { | ||
RequestInquiry.create( | ||
new Amount(SPENDING_MONEY_AMOUNT, CURRENCY_EUR), | ||
new Pointer(POINTER_TYPE_EMAIL, SUGAR_DADDY_EMAIL), | ||
SUGAR_DADDY_REQUESTS_DESCRIPTION, | ||
false | ||
); | ||
} | ||
|
||
if (shouldMoneyBeRequested(secondMonetaryAccountBank)) { | ||
RequestInquiry.create( | ||
new Amount(SPENDING_MONEY_AMOUNT, CURRENCY_EUR), | ||
new Pointer(POINTER_TYPE_EMAIL, SUGAR_DADDY_EMAIL), | ||
SUGAR_DADDY_REQUESTS_DESCRIPTION, | ||
false, | ||
secondMonetaryAccountBank.getId() | ||
); | ||
} | ||
} | ||
|
||
apiContext.save(apiConfigPath); | ||
protected static Pointer getPointerBravo() { | ||
return new Pointer(POINTER_TYPE_EMAIL, EMAIL_BRAVO); | ||
} | ||
|
||
protected static CashRegister getCashRegister() { | ||
if (cashRegister == null) { | ||
BunqResponse<Integer> response = CashRegister.create(CASH_REGISTER_DESCRIPTION); | ||
cashRegister = CashRegister.get(response.getValue()).getValue(); | ||
} | ||
|
||
return apiContext; | ||
return cashRegister; | ||
} | ||
|
||
} | ||
private static boolean shouldMoneyBeRequested(MonetaryAccountBank monetaryAccountBank) { | ||
return Float.parseFloat(monetaryAccountBank.getBalance().getValue()) < 10; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public under private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm., i normally group constants in groups, not on visibility. Error constants on top, ten the first used group in the class. So there could indeed be a mixture of visibility in a specific group.