Skip to content

Commit

Permalink
build UserContext non-static (#93)
Browse files Browse the repository at this point in the history
UserContext building without static references, backwards compatible
  • Loading branch information
tubbynl committed Jun 17, 2018
1 parent 7ed15ad commit 714d486
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 33 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
1 change: 0 additions & 1 deletion src/main/java/com/bunq/sdk/context/SessionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,4 @@ Date getExpiryTime() {
public Integer getUserId() {
return userId;
}

}
48 changes: 18 additions & 30 deletions src/main/java/com/bunq/sdk/context/UserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand All @@ -18,54 +19,41 @@ public class UserContext {
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 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;
}

private BunqModel getUserObject() {
return User.list().getValue().get(INDEX_FIRST).getReferencedObject();
public UserContext(ApiContext apiContext) {
this.apiContext = apiContext;
refreshContext();
}

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!=null && user.getUserPerson()!=null) {
this.userPerson = user.getUserPerson();
} else if (user!=null && 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;
}
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) {
if(monetaryAccountBank==null) {
throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND);
}

throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND);
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.getFirstActiveMonetaryAccountBank(getUserId()));
}

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

public boolean isOnlyUserPersonSet() {
Expand Down
47 changes: 47 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,47 @@
package com.bunq.sdk.model.core;

import com.bunq.sdk.context.ApiContext;
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 {
private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE";
private static final int INDEX_FIRST = 0;

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("user");
BunqResponse<List<User>> response = fromJsonList(User.class, responseRaw);
return response.getValue().get(INDEX_FIRST);
}

public MonetaryAccountBank getFirstActiveMonetaryAccountBank(Integer userId) {
BunqResponseRaw responseRaw = getRawResponse(String.format("user/%s/monetary-account-bank", userId));
BunqResponse<List<MonetaryAccountBank>> response = fromJsonList(MonetaryAccountBank.class, responseRaw, MonetaryAccountBank.class.getSimpleName());
for (MonetaryAccountBank monetaryAccountBank : response.getValue()) {
if (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) {
return monetaryAccountBank;
}
}
return null;
}

@Override
public boolean isAllFieldNull() {
return true;
}
}
19 changes: 19 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,19 @@
package com.bunq.sdk.context;

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

import static org.junit.Assert.*;

public class UserContextTest extends BunqSdkTestBase {

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

UserContext sut = new UserContext(context);

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

0 comments on commit 714d486

Please sign in to comment.