Skip to content

Commit

Permalink
Merge pull request #99 from tubbynl/develop-non-breaking-refactor
Browse files Browse the repository at this point in the history
UserContext building without static references. (#93)
  • Loading branch information
OGKevin authored Jun 22, 2018
2 parents 6880709 + c416a86 commit aa4736b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/bunq/sdk/context/BunqContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public final class BunqContext {

public static void loadApiContext(ApiContext apiContext) {
BunqContext.apiContext = apiContext;
BunqContext.userContext = new UserContext(apiContext.getSessionContext().getUserId());
BunqContext.userContext.initMainMonetaryAccount();
BunqContext.userContext = new UserContext(apiContext);
}

public static ApiContext getApiContext() {
Expand Down
54 changes: 18 additions & 36 deletions src/main/java/com/bunq/sdk/context/UserContext.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,52 @@
package com.bunq.sdk.context;

import com.bunq.sdk.exception.BunqException;
import com.bunq.sdk.model.core.BunqModel;
import com.bunq.sdk.model.core.UserContextHelper;
import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank;
import com.bunq.sdk.model.generated.endpoint.User;
import com.bunq.sdk.model.generated.endpoint.UserCompany;
import com.bunq.sdk.model.generated.endpoint.UserPerson;

import java.util.List;

public class UserContext {

/**
* Error constants.
*/
private static final String ERROR_UNEXPECTED_USER_INSTANCE = "\"%s\" is unexpected user instance.";
private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND = "No active monetary account found.";
private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set.";

private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE";
private static final int INDEX_FIRST = 0;
private static final String ERROR_PRIMARY_MONETARY_ACCOUNT_IS_NOT_SET = "Primary monetaryAccount is not set";

private final ApiContext apiContext;
private UserCompany userCompany;
private UserPerson userPerson;
private MonetaryAccountBank primaryMonetaryAccountBank;
private Integer userId;

public UserContext(Integer userId) {
this.setUser(this.getUserObject());
this.userId = userId;
public UserContext(ApiContext apiContext) {
this.apiContext = apiContext;
refreshContext();
}

private BunqModel getUserObject() {
return User.list().getValue().get(INDEX_FIRST).getReferencedObject();
}

private void setUser(BunqModel user) {
if (user instanceof UserPerson) {
this.userPerson = (UserPerson) user;
} else if (user instanceof UserCompany) {
this.userCompany = (UserCompany) user;
private void initUser(User user) {
if (user.getUserPerson() != null) {
this.userPerson = user.getUserPerson();
} else if (user.getUserCompany() != null) {
this.userCompany = user.getUserCompany();
} else {
throw new BunqException(ERROR_UNEXPECTED_USER_INSTANCE);
}
}

public void initMainMonetaryAccount() {
List<MonetaryAccountBank> allMonetaryAccount = MonetaryAccountBank.list().getValue();

for (MonetaryAccountBank monetaryAccountBank : allMonetaryAccount) {
if (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) {
this.primaryMonetaryAccountBank = monetaryAccountBank;

return;
}
}

throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND);
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) {
this.primaryMonetaryAccountBank = monetaryAccountBank;
}

public void refreshContext() {
this.setUser(this.getUserObject());
this.initMainMonetaryAccount();
UserContextHelper helper = new UserContextHelper(this.apiContext);
this.initUser(helper.getFirstUser());
this.initMainMonetaryAccount(helper.getFirstActiveMonetaryAccountBankByUserId(getUserId()));
}

public Integer getUserId() {
return this.userId;
return this.apiContext.getSessionContext().getUserId();
}

public boolean isOnlyUserPersonSet() {
Expand Down Expand Up @@ -99,4 +80,5 @@ public UserCompany getUserCompany() {
public MonetaryAccountBank getPrimaryMonetaryAccountBank() {
return this.primaryMonetaryAccountBank;
}

}
79 changes: 79 additions & 0 deletions src/main/java/com/bunq/sdk/model/core/UserContextHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.bunq.sdk.model.core;

import com.bunq.sdk.context.ApiContext;
import com.bunq.sdk.exception.BunqException;
import com.bunq.sdk.http.ApiClient;
import com.bunq.sdk.http.BunqResponse;
import com.bunq.sdk.http.BunqResponseRaw;
import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank;
import com.bunq.sdk.model.generated.endpoint.User;

import java.util.List;

public class UserContextHelper extends BunqModel {

/**
* Error constants.
*/
private static final String ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK =
"No active monetary account found.";

/**
* Endpoint constants.
*/
private static final String ENDPOINT_USER = "user";
private static final String ENDPOINT_MONETARY_ACCOUNT_BANK = "user/%s/monetary-account-bank";

/**
* The index of the first item in an array.
*/
private static final Integer INDEX_FIRST = 0;

/**
* Status constants.
*/
private static final String STATUS_ACTIVE = "ACTIVE";

private final ApiClient apiClient;

public UserContextHelper(ApiContext apiContext) {
this.apiClient = new ApiClient(apiContext);
}

private BunqResponseRaw getRawResponse(String url) {
return this.apiClient.get(url, null, null);
}

public User getFirstUser() {
BunqResponseRaw responseRaw = getRawResponse(ENDPOINT_USER);
BunqResponse<List<User>> response = fromJsonList(User.class, responseRaw);

return response.getValue().get(INDEX_FIRST);
}

public MonetaryAccountBank getFirstActiveMonetaryAccountBankByUserId(Integer userId) {
BunqResponseRaw responseRaw = getRawResponse(
String.format(ENDPOINT_MONETARY_ACCOUNT_BANK, userId)
);
String wrapper = MonetaryAccountBank.class.getSimpleName();
BunqResponse<List<MonetaryAccountBank>> response = fromJsonList(
MonetaryAccountBank.class,
responseRaw,
wrapper
);

for (MonetaryAccountBank monetaryAccountBank : response.getValue()) {
if (STATUS_ACTIVE.equals(monetaryAccountBank.getStatus())) {
return monetaryAccountBank;
}
}

throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK);
}

@Override
public boolean isAllFieldNull() {
return true;
}

}
20 changes: 20 additions & 0 deletions src/test/java/com/bunq/sdk/context/UserContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.bunq.sdk.context;

import com.bunq.sdk.BunqSdkTestBase;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class UserContextTest extends BunqSdkTestBase {

@Test
public void buildUserContext() {
ApiContext context = getApiContext();

UserContext userContext = new UserContext(context);

assertNotNull(userContext.getUserId());
assertNotNull(userContext.getMainMonetaryAccountId());
}

}

0 comments on commit aa4736b

Please sign in to comment.