Skip to content

Commit 4a5f74f

Browse files
committed
cleanup tests and api client; fix PRODUCTION url
1 parent 8382489 commit 4a5f74f

20 files changed

+197
-164
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,20 @@ List<User> users = User.list(apiContext);
152152

153153
##### Example
154154
See [`UserListExample.java`](./src/main/java/com/bunq/sdk/example/UserListExample.java)
155+
156+
## Running Examples
157+
In order to make the experience of getting into bunq Java SDK smoother, we
158+
have bundled it with example use cases (located under `./src/main/java/com/bunq/sdk/example/`).
159+
160+
To run an example, please do the following:
161+
1. In your IDE, open the example you are interested in and adjust the constants,
162+
such as `API_KEY` or `USER_ID`, to hold your data.
163+
2. Since Java IDE's are typically advanced just run the example of your choice in your IDE.
164+
165+
In order for examples to run, you would need a valid context file (`bunq.conf`)
166+
to be present in the bunq SDK project root directory. The file can either copied
167+
from somewhere else (e.g. tests) or created by executing the `ApiContextSaveExample.java` in your
168+
IDE.
169+
170+
Please do not forget to set the `API_KEY` constant in `ApiContextSaveExample.java` to your actual
171+
API key before running the example!

src/main/java/com/bunq/sdk/context/ApiContext.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ public static ApiContext create(ApiEnvironmentType environmentType, String apiKe
108108
return apiContext;
109109
}
110110

111+
/**
112+
* Restores a context from a default location.
113+
*/
114+
public static ApiContext restore() {
115+
return restore(PATH_API_CONTEXT_DEFAULT);
116+
}
117+
118+
/**
119+
* Restores a context from a given file.
120+
*/
121+
public static ApiContext restore(String fileName) {
122+
try {
123+
File file = new File(fileName);
124+
String json = FileUtils.readFileToString(file, ENCODING_BUNQ_CONF);
125+
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
126+
127+
return gson.fromJson(jsonObject, ApiContext.class);
128+
} catch (IOException exception) {
129+
throw new BunqException(ERROR_COULD_NOT_RESTORE_API_CONTEXT, exception);
130+
}
131+
}
132+
111133
private void initialize(String deviceDescription, List<String> permittedIps) {
112134
/* The calls below are order-sensitive: to initialize a Device Registration, we need an
113135
* Installation, and to initialize a Session we need a Device Registration. */
@@ -204,28 +226,6 @@ public void save(String fileName) {
204226
}
205227
}
206228

207-
/**
208-
* Restores a context from a default location.
209-
*/
210-
public static ApiContext restore() {
211-
return restore(PATH_API_CONTEXT_DEFAULT);
212-
}
213-
214-
/**
215-
* Restores a context from a given file.
216-
*/
217-
public static ApiContext restore(String fileName) {
218-
try {
219-
File file = new File(fileName);
220-
String json = FileUtils.readFileToString(file, ENCODING_BUNQ_CONF);
221-
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
222-
223-
return gson.fromJson(jsonObject, ApiContext.class);
224-
} catch (IOException exception) {
225-
throw new BunqException(ERROR_COULD_NOT_RESTORE_API_CONTEXT, exception);
226-
}
227-
}
228-
229229
/**
230230
* @return The base URI of the current environment.
231231
*/

src/main/java/com/bunq/sdk/context/ApiEnvironmentType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public enum ApiEnvironmentType {
1111

12-
PRODUCTION("https://public.api.bunq.com/v1/"),
12+
PRODUCTION("https://api.bunq.com/v1/"),
1313
SANDBOX("https://sandbox.public.api.bunq.com/v1/");
1414

1515
/**

src/main/java/com/bunq/sdk/examples/ApiContextSaveExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.bunq.sdk.context.ApiEnvironmentType;
55
import com.bunq.sdk.json.BunqGsonBuilder;
66
import com.google.gson.Gson;
7-
import java.util.ArrayList;
87

98
/**
109
* Create an API context and save it to a file.

src/main/java/com/bunq/sdk/examples/UserListExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.bunq.sdk.context.ApiContext;
44
import com.bunq.sdk.model.generated.User;
5-
import com.bunq.sdk.model.generated.UserCompany;
65
import java.util.List;
76

87
/**

src/main/java/com/bunq/sdk/http/ApiClient.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,14 @@ private byte[] getBodyBytes(CloseableHttpResponse response) throws IOException {
261261
Integer responseCode = response.getStatusLine().getStatusCode();
262262
byte[] responseBodyBytes = EntityUtils.toByteArray(response.getEntity());
263263

264-
validateResponseSignature(responseCode, responseBodyBytes, response);
265264
assertResponseSuccess(responseCode, responseBodyBytes);
265+
validateResponseSignature(responseCode, responseBodyBytes, response);
266266

267267
return responseBodyBytes;
268268
}
269269

270270
private void validateResponseSignature(int responseCode, byte[] responseBodyBytes,
271-
HttpResponse response)
272-
{
271+
HttpResponse response) {
273272
InstallationContext installationContext = apiContext.getInstallationContext();
274273

275274
if (installationContext != null) {

src/main/java/com/bunq/sdk/json/SessionServerAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public class SessionServerAdapter implements JsonDeserializer<SessionServer> {
2929
private static final String FIELD_USER_COMPANY = "UserCompany";
3030
private static final String FIELD_USER_PERSON = "UserPerson";
3131

32+
private static JsonElement getByIndexAndFieldName(JsonArray values, int index, String fieldName) {
33+
return values.get(index).getAsJsonObject().get(fieldName);
34+
}
35+
3236
@Override
3337
public SessionServer deserialize(JsonElement json, Type typeOfT,
3438
JsonDeserializationContext context) throws JsonParseException {
@@ -61,8 +65,4 @@ public SessionServer deserialize(JsonElement json, Type typeOfT,
6165
}
6266
}
6367

64-
private static JsonElement getByIndexAndFieldName(JsonArray values, int index, String fieldName) {
65-
return values.get(index).getAsJsonObject().get(fieldName);
66-
}
67-
6868
}

src/test/config.example.properties

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# For all test
22
API_CONFIG_PATH=bunq-test.conf
3-
API_KEY = <API KEY>
3+
API_KEY=<API KEY>
44
PERMITTED_IPS=<IP ADDRESS>
55

66
#For UserTest
7-
USER_ID = xxxx
7+
USER_ID=xxxx
88

99
#For AttachmentPublicTest
10-
CONTENT_TYPE = image/png
11-
ATTACHMENT_DESCRIPTION = TEST PNG JAVA
12-
PATH_ATTACHMENT_IN = assets/[email protected]
10+
CONTENT_TYPE=image/png
11+
ATTACHMENT_DESCRIPTION=TEST PNG JAVA
12+
PATH_ATTACHMENT_IN=assets/[email protected]
1313

1414
#For MonetaryAccountTest
15-
MA_ID = xxxx
16-
MA_ID2 = xxxx
15+
MONETARY_ACCOUNT_ID=xxxx
16+
MONETARY_ACCOUNT_ID2=xxxx
1717
COUNTER_PARTY_ALIAS=<Email address>
1818
COUNTER_PARTY_TYPE=EMAIL
1919

2020
#For TabUsageSingleTest
21-
CR_ID = xxx
21+
CASH_REGISTER_ID=xxx
2222

2323
#For PaymentChatTest
2424
#Same user, alias from other account
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
1-
package com.bunq.sdk.model.generated;
1+
package com.bunq.sdk;
22

33
import com.bunq.sdk.context.ApiContext;
44
import com.bunq.sdk.context.ApiEnvironmentType;
55
import com.bunq.sdk.exception.ApiException;
66
import com.bunq.sdk.exception.BunqException;
7+
import com.bunq.sdk.model.generated.User;
78
import java.util.Arrays;
89
import java.util.List;
910
import java.util.Properties;
1011

1112
/**
12-
* Checks if the sessionToken stored in the conf file is still valid, if not it will generate a new
13-
* one
13+
* Base class for the Bunq SDK tests.
1414
*/
15-
public class ApiContextHandler {
15+
public class BunqSdkTestBase {
1616

1717
/**
18-
* Config fields
18+
* Description of the test device for Java SDK.
19+
*/
20+
private static final String DEVICE_DESCRIPTION = "Java test device";
21+
22+
/**
23+
* Config fields.
1924
*/
20-
private static final String DEVICE_DESCRIPTION = "Java test case";
2125
private static final String FIELD_API_KEY = "API_KEY";
2226
private static final String FIELD_PERMITTED_IPS = "PERMITTED_IPS";
2327
private static final String FIELD_API_CONFIG_PATH = "API_CONFIG_PATH";
28+
29+
/**
30+
* Delimiter between the IP addresses in the PERMITTED_IPS field.
31+
*/
2432
private static final String DELIMITER_IPS = ", ";
33+
34+
/**
35+
* Properties for the tests.
36+
*/
2537
private static Properties config = TestConfig.prop();
2638

39+
/**
40+
* Individual properties.
41+
*/
2742
private static String apiKey = config.getProperty(FIELD_API_KEY);
2843
private static String[] permittedIps = config.getProperty(FIELD_PERMITTED_IPS).split(
2944
DELIMITER_IPS);
@@ -33,32 +48,20 @@ public class ApiContextHandler {
3348
* Based on the result of isSessionActive will create a new ApiContext or restore an old conf
3449
* file
3550
*/
36-
public static ApiContext getApiContext() {
51+
protected static ApiContext getApiContext() {
3752
ApiContext apiContext;
3853

39-
if (isSessionActive()) {
54+
try {
4055
apiContext = ApiContext.restore(apiConfigPath);
41-
} else {
56+
User.list(apiContext);
57+
} catch (ApiException | BunqException exception) {
4258
List<String> ips = Arrays.asList(permittedIps);
4359
apiContext = ApiContext.create(ApiEnvironmentType.SANDBOX, apiKey, DEVICE_DESCRIPTION, ips);
44-
apiContext.save();
4560
}
4661

47-
return apiContext;
48-
}
49-
50-
/**
51-
* Checks if the session is active by making an API call and see if an Api/Bunq exception is
52-
* thrown
53-
*/
54-
private static boolean isSessionActive() {
55-
try {
56-
User.list(ApiContext.restore(apiConfigPath));
62+
apiContext.save();
5763

58-
return true;
59-
} catch (ApiException|BunqException exception) {
60-
return false;
61-
}
64+
return apiContext;
6265
}
6366

6467
}

src/test/java/com/bunq/sdk/model/generated/TestConfig.java renamed to src/test/java/com/bunq/sdk/TestConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.bunq.sdk.model.generated;
1+
package com.bunq.sdk;
22

33
import java.io.FileInputStream;
44
import java.io.IOException;

0 commit comments

Comments
 (0)