Skip to content

Commit 3923a52

Browse files
authored
Merge branch 'develop' into 11-proxy
2 parents 8e02c99 + bc331c2 commit 3923a52

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,5 @@ gradle-app.setting
6969
# bunq-specific
7070
bunq.conf
7171
context-save-restore-test.conf
72-
tmp/attachment_out.jpg
7372
/tmp
7473
config.properties

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.bunq.sdk.model.generated.Session;
99
import com.bunq.sdk.security.SecurityUtils;
1010
import com.google.gson.Gson;
11-
import com.google.gson.JsonObject;
1211
import com.google.gson.annotations.Expose;
1312
import com.google.gson.annotations.SerializedName;
1413
import java.io.File;
@@ -145,14 +144,20 @@ public static ApiContext restore(String fileName) {
145144
try {
146145
File file = new File(fileName);
147146
String json = FileUtils.readFileToString(file, ENCODING_BUNQ_CONF);
148-
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
149147

150-
return gson.fromJson(jsonObject, ApiContext.class);
148+
return fromJson(json);
151149
} catch (IOException exception) {
152150
throw new BunqException(ERROR_COULD_NOT_RESTORE_API_CONTEXT, exception);
153151
}
154152
}
155153

154+
/**
155+
* Restores a context from a given JSON string.
156+
*/
157+
public static ApiContext fromJson(String json) {
158+
return gson.fromJson(json, ApiContext.class);
159+
}
160+
156161
private void initialize(String deviceDescription, List<String> permittedIps) {
157162
/* The calls below are order-sensitive: to initialize a Device Registration, we need an
158163
* Installation, and to initialize a Session we need a Device Registration. */
@@ -254,13 +259,19 @@ public void save() {
254259
public void save(String fileName) {
255260
try {
256261
File file = new File(fileName);
257-
String json = gson.toJson(this);
258-
FileUtils.writeStringToFile(file, json, ENCODING_BUNQ_CONF);
262+
FileUtils.writeStringToFile(file, toJson(), ENCODING_BUNQ_CONF);
259263
} catch (IOException exception) {
260264
throw new BunqException(ERROR_COULD_NOT_SAVE_API_CONTEXT, exception);
261265
}
262266
}
263267

268+
/**
269+
* Serializes the context to JSON.
270+
*/
271+
public String toJson() {
272+
return gson.toJson(this);
273+
}
274+
264275
/**
265276
* @return The base URI of the current environment.
266277
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.bunq.sdk.context;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import com.bunq.sdk.BunqSdkTestBase;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
/**
10+
* Tests:
11+
* ApiContext
12+
*/
13+
public class ApiContextTest extends BunqSdkTestBase {
14+
15+
/**
16+
* Path to a temporary context file.
17+
*/
18+
private static final String CONTEXT_FILENAME_TEST = "context-save-restore-test.conf";
19+
20+
private static ApiContext apiContext;
21+
22+
@BeforeClass
23+
public static void setUpBeforeClass() {
24+
apiContext = getApiContext();
25+
}
26+
27+
@Test
28+
public void apiContextSerializeDeserializeTest() {
29+
String apiContextJson = apiContext.toJson();
30+
ApiContext apiContextDeSerialised = ApiContext.fromJson(apiContextJson);
31+
32+
assertEquals(apiContextJson, apiContextDeSerialised.toJson());
33+
}
34+
35+
@Test
36+
public void apiContextSaveRestoreTest() {
37+
String apiContextJson = apiContext.toJson();
38+
apiContext.save(CONTEXT_FILENAME_TEST);
39+
ApiContext apiContextRestored = ApiContext.restore(CONTEXT_FILENAME_TEST);
40+
41+
assertEquals(apiContextJson, apiContextRestored.toJson());
42+
}
43+
44+
}

0 commit comments

Comments
 (0)