Skip to content

Commit fde6590

Browse files
committed
Merge branch 'develop' of https://github.com/bunq/sdk_java into develop
2 parents a9068f7 + 29e6d0d commit fde6590

File tree

11 files changed

+103
-44
lines changed

11 files changed

+103
-44
lines changed

.github/ISSUE_TEMPLATE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[//]: # (If there is a traceback please share it in a quote! You can do this by pasting the traceback text, highlighting it and pressing the quote button.)
1212

1313
## SDK version and environment
14-
- Tested on [0.12.4](https://github.com/bunq/sdk_java/releases/tag/0.12.4)
14+
- Tested on [1.14.1](https://github.com/bunq/sdk_java/releases/tag/1.14.1)
1515
- [ ] Sandbox
1616
- [ ] Production
1717

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version '1.14.1'
33

44
apply plugin: 'java'
55
apply plugin: 'maven'
6-
sourceCompatibility = 1.7
6+
sourceCompatibility = 1.8
77

88

99
sourceSets {

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,31 @@ public static ApiContext create(
117117
/**
118118
* Create and initialize an API Context with given permitted ips and no proxy.
119119
*/
120-
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
121-
String deviceDescription, List<String> permittedIps) {
120+
public static ApiContext create(ApiEnvironmentType environmentType,
121+
String apiKey,
122+
String deviceDescription,
123+
List<String> permittedIps) {
122124
return create(environmentType, apiKey, deviceDescription, permittedIps, null);
123125
}
124126

125127
/**
126128
* Create and initialize an API Context with current IP as permitted and a proxy.
127129
*/
128-
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
129-
String deviceDescription, String proxy) {
130+
public static ApiContext create(ApiEnvironmentType environmentType,
131+
String apiKey,
132+
String deviceDescription,
133+
String proxy) {
130134
return create(environmentType, apiKey, deviceDescription, new ArrayList<String>(), proxy);
131135
}
132136

133137
/**
134138
* Create and initialize an API Context.
135139
*/
136-
public static ApiContext create(ApiEnvironmentType environmentType, String apiKey,
137-
String deviceDescription, List<String> permittedIps, String proxy) {
140+
public static ApiContext create(ApiEnvironmentType environmentType,
141+
String apiKey,
142+
String deviceDescription,
143+
List<String> permittedIps,
144+
String proxy) {
138145
ApiContext apiContext = new ApiContext(environmentType, apiKey);
139146
apiContext.proxy = proxy;
140147
apiContext.initialize(deviceDescription, permittedIps);

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bunq.sdk.context;
22

33
import com.bunq.sdk.exception.BunqException;
4+
import com.bunq.sdk.model.core.UserContextHelper;
45

56
public final class BunqContext {
67

@@ -16,6 +17,7 @@ public final class BunqContext {
1617
public static void loadApiContext(ApiContext apiContext) {
1718
BunqContext.apiContext = apiContext;
1819
BunqContext.userContext = new UserContext(apiContext);
20+
BunqContext.userContext.initMainMonetaryAccount(new UserContextHelper(apiContext));
1921
}
2022

2123
public static ApiContext getApiContext() {

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

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.bunq.sdk.model.generated.endpoint.UserCompany;
88
import com.bunq.sdk.model.generated.endpoint.UserPaymentServiceProvider;
99
import com.bunq.sdk.model.generated.endpoint.UserPerson;
10+
import com.bunq.sdk.util.BunqUtil;
1011
import com.google.gson.annotations.Expose;
1112
import com.google.gson.annotations.SerializedName;
1213

@@ -45,13 +46,33 @@ public class SessionContext implements java.io.Serializable {
4546
@SerializedName("user_id")
4647
private final Integer userId;
4748

49+
@Expose
50+
@SerializedName("user_person")
51+
private final UserPerson userPerson;
52+
53+
@Expose
54+
@SerializedName("user_company")
55+
private final UserCompany userCompany;
56+
57+
@Expose
58+
@SerializedName("user_api_key")
59+
private final UserApiKey userApiKey;
60+
61+
@Expose
62+
@SerializedName("user_payment_service_provider")
63+
private final UserPaymentServiceProvider userPaymentServiceProvider;
64+
4865
/**
4966
* @param sessionServer Object containing the session info.
5067
*/
5168
SessionContext(SessionServer sessionServer) {
5269
this.token = sessionServer.getSessionToken().getToken();
5370
this.expiryTime = calculateExpiryTime(sessionServer);
5471
this.userId = getUserId(sessionServer.getReferencedUser());
72+
this.userPerson = sessionServer.getUserPersonOrNull();
73+
this.userCompany = sessionServer.getUserCompanyOrNull();
74+
this.userApiKey = sessionServer.getUserApiKeyOrNull();
75+
this.userPaymentServiceProvider = sessionServer.getUserPaymentServiceProviderOrNull();
5576
}
5677

5778
private int getUserId(BunqModel user) {
@@ -111,4 +132,8 @@ Date getExpiryTime() {
111132
public Integer getUserId() {
112133
return userId;
113134
}
135+
136+
public BunqModel getUserReference() {
137+
return BunqUtil.getReferencedUser(userPerson, userCompany, userApiKey, userPaymentServiceProvider);
138+
}
114139
}

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.bunq.sdk.model.generated.endpoint.*;
77

88
public class UserContext {
9-
109
/**
1110
* Error constants.
1211
*/
@@ -22,7 +21,7 @@ public class UserContext {
2221

2322
public UserContext(ApiContext apiContext) {
2423
this.apiContext = apiContext;
25-
refreshContext();
24+
initUser(this.apiContext.getSessionContext().getUserReference());
2625
}
2726

2827
private void initUser(BunqModel user) {
@@ -39,19 +38,18 @@ private void initUser(BunqModel user) {
3938
}
4039
}
4140

42-
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) {
43-
this.primaryMonetaryAccountBank = monetaryAccountBank;
41+
public void initMainMonetaryAccount(UserContextHelper helper) {
42+
if (this.userPaymentServiceProvider != null) {
43+
return;
44+
}
45+
46+
this.primaryMonetaryAccountBank = helper.getFirstActiveMonetaryAccountBankByUserId(getUserId());
4447
}
4548

4649
public void refreshContext() {
4750
UserContextHelper helper = new UserContextHelper(this.apiContext);
4851
this.initUser(helper.getFirstUser().getReferencedObject());
49-
50-
if (this.userPaymentServiceProvider != null) {
51-
return;
52-
}
53-
54-
this.initMainMonetaryAccount(helper.getFirstActiveMonetaryAccountBankByUserId(getUserId()));
52+
this.initMainMonetaryAccount(helper);
5553
}
5654

5755
public Integer getUserId() {

src/main/java/com/bunq/sdk/model/core/SessionServer.java

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package com.bunq.sdk.model.core;
22

33
import com.bunq.sdk.context.ApiContext;
4-
import com.bunq.sdk.exception.BunqException;
54
import com.bunq.sdk.http.ApiClient;
65
import com.bunq.sdk.http.BunqResponse;
76
import com.bunq.sdk.http.BunqResponseRaw;
87
import com.bunq.sdk.model.generated.endpoint.UserApiKey;
98
import com.bunq.sdk.model.generated.endpoint.UserCompany;
109
import com.bunq.sdk.model.generated.endpoint.UserPaymentServiceProvider;
1110
import com.bunq.sdk.model.generated.endpoint.UserPerson;
11+
import com.bunq.sdk.util.BunqUtil;
1212

1313
import java.util.HashMap;
1414

1515
public class SessionServer extends BunqModel {
16-
/**
17-
* Error constants.
18-
*/
19-
private static final String ERROR_ALL_FIELD_NULL = "All fields of an extended model or object are null.";
20-
2116
/**
2217
* Endpoint name.
2318
*/
@@ -94,12 +89,20 @@ public SessionToken getSessionToken() {
9489
return sessionToken;
9590
}
9691

97-
public UserCompany getUserCompany() {
92+
public UserPerson getUserPersonOrNull() {
93+
return userPerson;
94+
}
95+
96+
public UserCompany getUserCompanyOrNull() {
9897
return userCompany;
9998
}
10099

101-
public UserPerson getUserPerson() {
102-
return userPerson;
100+
public UserApiKey getUserApiKeyOrNull() {
101+
return userApiKey;
102+
}
103+
104+
public UserPaymentServiceProvider getUserPaymentServiceProviderOrNull() {
105+
return userPaymentServiceProvider;
103106
}
104107

105108
@Override
@@ -124,16 +127,6 @@ public boolean isAllFieldNull() {
124127
}
125128

126129
public BunqModel getReferencedUser() {
127-
if (this.userCompany == null && this.userApiKey == null && this.userPerson != null && this.userPaymentServiceProvider == null) {
128-
return this.userPerson;
129-
} else if (this.userPerson == null && this.userApiKey == null && this.userCompany != null && this.userPaymentServiceProvider == null) {
130-
return this.userCompany;
131-
} else if (this.userPerson == null && this.userCompany == null && this.userApiKey != null && this.userPaymentServiceProvider == null) {
132-
return this.userApiKey;
133-
} else if (this.userPerson == null && this.userCompany == null && this.userApiKey == null && this.userPaymentServiceProvider != null) {
134-
return this.userPaymentServiceProvider;
135-
} else {
136-
throw new BunqException(ERROR_ALL_FIELD_NULL);
137-
}
130+
return BunqUtil.getReferencedUser(userPerson, userCompany, userApiKey, userPaymentServiceProvider);
138131
}
139132
}

src/main/java/com/bunq/sdk/security/SecurityUtils.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private static String getPrivateKeyFormattedString(PrivateKey privateKey) {
171171
}
172172

173173
/**
174-
* @param publicKeyString X509 Public Key string
174+
* @param publicKeyString X509 Public Key string
175175
* @param privateKeyString PKCS8 Private Key string
176176
*/
177177
public static KeyPair createKeyPairFromFormattedStrings(String publicKeyString,
@@ -538,10 +538,11 @@ public static Certificate getCertificateFromFile(String path) throws IOException
538538
File certificateFile = new File(path);
539539
if (certificateFile.exists()) {
540540
Certificate certificate = new Certificate();
541-
certificate.setCertificate(FileUtils.readFileToString(certificateFile));
541+
certificate.setCertificate(FileUtils.readFileToString(certificateFile, "UTF-8"));
542542
return certificate;
543+
} else {
544+
throw new FileNotFoundException();
543545
}
544-
throw new FileNotFoundException();
545546
}
546547

547548
private static byte[] getResponseHeadBytes(int code, List<BunqBasicHeader> headers) {

src/main/java/com/bunq/sdk/util/BunqUtil.java

+31
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package com.bunq.sdk.util;
22

3+
import com.bunq.sdk.exception.BunqException;
4+
import com.bunq.sdk.model.core.BunqModel;
5+
import com.bunq.sdk.model.generated.endpoint.UserApiKey;
6+
import com.bunq.sdk.model.generated.endpoint.UserCompany;
7+
import com.bunq.sdk.model.generated.endpoint.UserPaymentServiceProvider;
8+
import com.bunq.sdk.model.generated.endpoint.UserPerson;
9+
310
import java.util.ArrayList;
411
import java.util.List;
512
import java.util.Map;
613

714
public class BunqUtil {
15+
/**
16+
* Error constants.
17+
*/
18+
private static final String ERROR_ALL_FIELD_NULL = "All fields of an extended model or object are null.";
19+
820
public static <T> T getValueFromMapOrDefault(Map<String, T> map, String key, T defaultValue) {
921
return map.getOrDefault(key, defaultValue);
1022
}
@@ -23,4 +35,23 @@ public static List<String> getChunksFromString(String stringToSplit, int chunkSi
2335

2436
return chunkList;
2537
}
38+
39+
public static BunqModel getReferencedUser(
40+
UserPerson userPerson,
41+
UserCompany userCompany,
42+
UserApiKey userApiKey,
43+
UserPaymentServiceProvider userPaymentServiceProvider
44+
) {
45+
if (userCompany == null && userApiKey == null && userPerson != null && userPaymentServiceProvider == null) {
46+
return userPerson;
47+
} else if (userPerson == null && userApiKey == null && userCompany != null && userPaymentServiceProvider == null) {
48+
return userCompany;
49+
} else if (userPerson == null && userCompany == null && userApiKey != null && userPaymentServiceProvider == null) {
50+
return userApiKey;
51+
} else if (userPerson == null && userCompany == null && userApiKey == null && userPaymentServiceProvider != null) {
52+
return userPaymentServiceProvider;
53+
} else {
54+
throw new BunqException(ERROR_ALL_FIELD_NULL);
55+
}
56+
}
2657
}

src/test/java/com/bunq/sdk/BunqSdkTestBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class BunqSdkTestBase {
4848
protected static final String ATTACHMENT_PATH_IN = "assets/vader.png";
4949
protected static final String SCHEME_HTTPS = "https://";
5050
protected static final String URL_PATH_SEPARATOR = "/";
51-
protected static final String URL_PATH_SANDBOX_USER = "/sandbox-user";
51+
protected static final String URL_PATH_SANDBOX_USER = "/sandbox-user-person";
5252
protected static final String CURRENCY_EUR = "EUR";
5353
protected static final String ACCOUNT_DESCRIPTION = "test account java";
5454
protected static final String SPENDING_MONEY_AMOUNT = "500";

src/test/java/com/bunq/sdk/context/UserContextTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bunq.sdk.context;
22

33
import com.bunq.sdk.BunqSdkTestBase;
4+
import com.bunq.sdk.model.core.UserContextHelper;
45
import org.junit.Test;
56

67
import static org.junit.Assert.assertNotNull;
@@ -9,9 +10,10 @@ public class UserContextTest extends BunqSdkTestBase {
910

1011
@Test
1112
public void buildUserContext() {
12-
ApiContext context = getApiContext();
13+
ApiContext apiContext = getApiContext();
1314

14-
UserContext userContext = new UserContext(context);
15+
UserContext userContext = new UserContext(apiContext);
16+
userContext.initMainMonetaryAccount(new UserContextHelper(apiContext));
1517

1618
assertNotNull(userContext.getUserId());
1719
assertNotNull(userContext.getMainMonetaryAccountId());

0 commit comments

Comments
 (0)