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 1 commit
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
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;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

please revert the removal of this line.

Copy link
Contributor

Choose a reason for hiding this comment

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

bump

Copy link
Contributor

Choose a reason for hiding this comment

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

please revert the removal of this line. As described here: https://google.github.io/styleguide/javaguide.html#s4.6.1-vertical-whitespace

}
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you not using instaceof anymore ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original user var was based on User#getReferencedObject internally retrieved the BunqModel from either userPerson or userCompany, directly using the relevant accessors omitted the need to instanceof check

Copy link
Contributor

Choose a reason for hiding this comment

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

This increases the complexity of the if statement without any benefits. So please revert back to using instaceof

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the null-checks, the risk for nullpointers is the same as the original code, extra checking was merely defensive, it now has the same complexity

Copy link
Contributor Author

Choose a reason for hiding this comment

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

btw; your code review thingie should mark theinstanceofstatement as some form of technical debt, it is not best practice and is generally avoidable (and is a code-smell)

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

missing space around operators

throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND);
Copy link
Contributor

Choose a reason for hiding this comment

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

It does not make sense to throw this error here. It would made sense if this error says monetaryAccountBank can not be null. It being null will not automatically mean that no active MA has been found.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the error was kept for consistency; the helper now returns first active monetaryAccountBank or null, so if null it means no active monetaryAccountBank has been found

Copy link
Contributor

Choose a reason for hiding this comment

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

It does not make sense to throw an error here saying No active MA has been found The error should say that the parameter can not be null.

}

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

Choose a reason for hiding this comment

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

please extract magic value in constant.

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 getFirstActiveMonetaryAccountBank(Integer userId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

...byUserId

BunqResponseRaw responseRaw = getRawResponse(String.format("user/%s/monetary-account-bank", userId));
Copy link
Contributor

Choose a reason for hiding this comment

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

please extract magic value into constant.

BunqResponse<List<MonetaryAccountBank>> response = fromJsonList(MonetaryAccountBank.class, responseRaw, MonetaryAccountBank.class.getSimpleName());
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 (monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) {
return monetaryAccountBank;
}
}
return null;
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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Dont return null here, throw the exception here.

}

@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.

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

Choose a reason for hiding this comment

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

now wild imports please.


public class UserContextTest extends BunqSdkTestBase {

@Test
public void testConstruct() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Test construct of what ? Please rename method to reflect.

Copy link
Contributor

Choose a reason for hiding this comment

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

bump

ApiContext context = getApiContext();

UserContext sut = new UserContext(context);
Copy link
Contributor

Choose a reason for hiding this comment

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

what does sut stand for ? Why not just userContext ?

Copy link
Contributor

Choose a reason for hiding this comment

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

bump

Copy link
Contributor Author

@tubbynl tubbynl Jun 18, 2018

Choose a reason for hiding this comment

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

sut = system-under-test, mostly to specify the thing you are testing

but will rename


assertNotNull(sut.getUserId());
assertNotNull(sut.getMainMonetaryAccountId());
}
}
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 at EOF