Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserContext building without static references. (bunq/sdk_java#93) #99

Merged
merged 14 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}
75 changes: 75 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,75 @@
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 {
/**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new line

* Error constants.
*/
private static final String ERROR_NO_ACTIVE_MONETARY = "No active monetary account found.";
Copy link
Contributor

@OGKevin OGKevin Jun 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way we order constants is the following:

We group then by type/category

e.g.

/**
* Error constants.
*/
private static final String ERROR_AMI_ENVIRONMENT_NOT_EXPECTED = "ApiEnvironment type \"%s\" is unexpected";
private static final String ERROR_COULD_NOT_DETERMINE_RESPONSE_ID = "Could not determine response id.";
/**
* Endpoints not requiring active session for the request to succeed.
*/
private static final String DEVICE_SERVER_URL = "device-server";
private static final String INSTALLATION_URL = "installation";
private static final String SESSION_SERVER_URL = "session-server";
private static final List<String> URIS_NOT_REQUIRING_ACTIVE_SESSION = Arrays.asList(
DEVICE_SERVER_URL,
INSTALLATION_URL,
SESSION_SERVER_URL
);

Error constants are always first and then the groups that followed are on a first use bases. So the first constant that gets used, its group will go next and so on.


/**
* 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line before return statement.

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a new line after the ,s for readability :)

);

for (MonetaryAccountBank monetaryAccountBank : response.getValue()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line before loop.

if (STATUS_ACTIVE.equals(monetaryAccountBank.getStatus())) {
return monetaryAccountBank;
}
}

throw new BunqException(ERROR_NO_ACTIVE_MONETARY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line after for block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERROR_NO_ACTIVE_MONETARY --> ERROR_NO_ACTIVE_MONETARY_ACCOUNT_BANK

}

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

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line after class block.

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());
}

}