Skip to content

Commit aa4736b

Browse files
authored
Merge pull request #99 from tubbynl/develop-non-breaking-refactor
UserContext building without static references. (#93)
2 parents 6880709 + c416a86 commit aa4736b

File tree

4 files changed

+118
-38
lines changed

4 files changed

+118
-38
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public final class BunqContext {
1515

1616
public static void loadApiContext(ApiContext apiContext) {
1717
BunqContext.apiContext = apiContext;
18-
BunqContext.userContext = new UserContext(apiContext.getSessionContext().getUserId());
19-
BunqContext.userContext.initMainMonetaryAccount();
18+
BunqContext.userContext = new UserContext(apiContext);
2019
}
2120

2221
public static ApiContext getApiContext() {
Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,52 @@
11
package com.bunq.sdk.context;
22

33
import com.bunq.sdk.exception.BunqException;
4-
import com.bunq.sdk.model.core.BunqModel;
4+
import com.bunq.sdk.model.core.UserContextHelper;
55
import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank;
66
import com.bunq.sdk.model.generated.endpoint.User;
77
import com.bunq.sdk.model.generated.endpoint.UserCompany;
88
import com.bunq.sdk.model.generated.endpoint.UserPerson;
99

10-
import java.util.List;
11-
1210
public class UserContext {
1311

1412
/**
1513
* Error constants.
1614
*/
1715
private static final String ERROR_UNEXPECTED_USER_INSTANCE = "\"%s\" is unexpected user instance.";
18-
private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND = "No active monetary account found.";
19-
private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set.";
20-
21-
private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE";
22-
private static final int INDEX_FIRST = 0;
16+
private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set";
2317

18+
private final ApiContext apiContext;
2419
private UserCompany userCompany;
2520
private UserPerson userPerson;
2621
private MonetaryAccountBank primaryMonetaryAccountBank;
27-
private Integer userId;
2822

29-
public UserContext(Integer userId) {
30-
this.setUser(this.getUserObject());
31-
this.userId = userId;
23+
public UserContext(ApiContext apiContext) {
24+
this.apiContext = apiContext;
25+
refreshContext();
3226
}
3327

34-
private BunqModel getUserObject() {
35-
return User.list().getValue().get(INDEX_FIRST).getReferencedObject();
36-
}
37-
38-
private void setUser(BunqModel user) {
39-
if (user instanceof UserPerson) {
40-
this.userPerson = (UserPerson) user;
41-
} else if (user instanceof UserCompany) {
42-
this.userCompany = (UserCompany) user;
28+
private void initUser(User user) {
29+
if (user.getUserPerson() != null) {
30+
this.userPerson = user.getUserPerson();
31+
} else if (user.getUserCompany() != null) {
32+
this.userCompany = user.getUserCompany();
4333
} else {
4434
throw new BunqException(ERROR_UNEXPECTED_USER_INSTANCE);
4535
}
4636
}
4737

48-
public void initMainMonetaryAccount() {
49-
List<MonetaryAccountBank> allMonetaryAccount = MonetaryAccountBank.list().getValue();
50-
51-
for (MonetaryAccountBank monetaryAccountBank : allMonetaryAccount) {
52-
if (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) {
53-
this.primaryMonetaryAccountBank = monetaryAccountBank;
54-
55-
return;
56-
}
57-
}
58-
59-
throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND);
38+
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) {
39+
this.primaryMonetaryAccountBank = monetaryAccountBank;
6040
}
6141

6242
public void refreshContext() {
63-
this.setUser(this.getUserObject());
64-
this.initMainMonetaryAccount();
43+
UserContextHelper helper = new UserContextHelper(this.apiContext);
44+
this.initUser(helper.getFirstUser());
45+
this.initMainMonetaryAccount(helper.getFirstActiveMonetaryAccountBankByUserId(getUserId()));
6546
}
6647

6748
public Integer getUserId() {
68-
return this.userId;
49+
return this.apiContext.getSessionContext().getUserId();
6950
}
7051

7152
public boolean isOnlyUserPersonSet() {
@@ -99,4 +80,5 @@ public UserCompany getUserCompany() {
9980
public MonetaryAccountBank getPrimaryMonetaryAccountBank() {
10081
return this.primaryMonetaryAccountBank;
10182
}
83+
10284
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.bunq.sdk.model.core;
2+
3+
import com.bunq.sdk.context.ApiContext;
4+
import com.bunq.sdk.exception.BunqException;
5+
import com.bunq.sdk.http.ApiClient;
6+
import com.bunq.sdk.http.BunqResponse;
7+
import com.bunq.sdk.http.BunqResponseRaw;
8+
import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank;
9+
import com.bunq.sdk.model.generated.endpoint.User;
10+
11+
import java.util.List;
12+
13+
public class UserContextHelper extends BunqModel {
14+
15+
/**
16+
* Error constants.
17+
*/
18+
private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK =
19+
"No active monetary account found.";
20+
21+
/**
22+
* Endpoint constants.
23+
*/
24+
private static final String ENDPOINT_USER = "user";
25+
private static final String ENDPOINT_MONETARY_ACCOUNT_BANK = "user/%s/monetary-account-bank";
26+
27+
/**
28+
* The index of the first item in an array.
29+
*/
30+
private static final Integer INDEX_FIRST = 0;
31+
32+
/**
33+
* Status constants.
34+
*/
35+
private static final String STATUS_ACTIVE = "ACTIVE";
36+
37+
private final ApiClient apiClient;
38+
39+
public UserContextHelper(ApiContext apiContext) {
40+
this.apiClient = new ApiClient(apiContext);
41+
}
42+
43+
private BunqResponseRaw getRawResponse(String url) {
44+
return this.apiClient.get(url, null, null);
45+
}
46+
47+
public User getFirstUser() {
48+
BunqResponseRaw responseRaw = getRawResponse(ENDPOINT_USER);
49+
BunqResponse<List<User>> response = fromJsonList(User.class, responseRaw);
50+
51+
return response.getValue().get(INDEX_FIRST);
52+
}
53+
54+
public MonetaryAccountBank getFirstActiveMonetaryAccountBankByUserId(Integer userId) {
55+
BunqResponseRaw responseRaw = getRawResponse(
56+
String.format(ENDPOINT_MONETARY_ACCOUNT_BANK, userId)
57+
);
58+
String wrapper = MonetaryAccountBank.class.getSimpleName();
59+
BunqResponse<List<MonetaryAccountBank>> response = fromJsonList(
60+
MonetaryAccountBank.class,
61+
responseRaw,
62+
wrapper
63+
);
64+
65+
for (MonetaryAccountBank monetaryAccountBank : response.getValue()) {
66+
if (STATUS_ACTIVE.equals(monetaryAccountBank.getStatus())) {
67+
return monetaryAccountBank;
68+
}
69+
}
70+
71+
throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK);
72+
}
73+
74+
@Override
75+
public boolean isAllFieldNull() {
76+
return true;
77+
}
78+
79+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bunq.sdk.context;
2+
3+
import com.bunq.sdk.BunqSdkTestBase;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertNotNull;
7+
8+
public class UserContextTest extends BunqSdkTestBase {
9+
10+
@Test
11+
public void buildUserContext() {
12+
ApiContext context = getApiContext();
13+
14+
UserContext userContext = new UserContext(context);
15+
16+
assertNotNull(userContext.getUserId());
17+
assertNotNull(userContext.getMainMonetaryAccountId());
18+
}
19+
20+
}

0 commit comments

Comments
 (0)