-
Notifications
You must be signed in to change notification settings - Fork 24
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
Conversation
i noticed that the actual user contained in the UserContext is the first User accessible for within the ApiContext. I would expect that the userId contained in the SessionContext would specify the User |
test fail on perhaps the API-key used for testing should be bumped |
UserContext building without static references, backwards compatible
2895b15
to
714d486
Compare
@@ -101,5 +101,4 @@ Date getExpiryTime() { | |||
public Integer getUserId() { | |||
return userId; | |||
} | |||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bump
There was a problem hiding this comment.
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
} else if (user instanceof UserCompany) { | ||
this.userCompany = (UserCompany) user; | ||
private void initUser(User user) { | ||
if (user!=null && user.getUserPerson()!=null) { |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 theinstanceof
statement as some form of technical debt, it is not best practice and is generally avoidable (and is a code-smell)
return; | ||
} | ||
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) { | ||
if(monetaryAccountBank==null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing space around operators
} | ||
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) { | ||
if(monetaryAccountBank==null) { | ||
throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
} | ||
|
||
public User getFirstUser() { | ||
BunqResponseRaw responseRaw = getRawResponse("user"); |
There was a problem hiding this comment.
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.
assertNotNull(sut.getUserId()); | ||
assertNotNull(sut.getMainMonetaryAccountId()); | ||
} | ||
} |
There was a problem hiding this comment.
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
return monetaryAccountBank; | ||
} | ||
} | ||
return null; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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()) { |
There was a problem hiding this comment.
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.
} | ||
|
||
public MonetaryAccountBank getFirstActiveMonetaryAccountBank(Integer userId) { | ||
BunqResponseRaw responseRaw = getRawResponse(String.format("user/%s/monetary-account-bank", userId)); |
There was a problem hiding this comment.
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.
import com.bunq.sdk.BunqSdkTestBase; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now wild imports please.
} else if (user instanceof UserCompany) { | ||
this.userCompany = (UserCompany) user; | ||
private void initUser(User user) { | ||
if (user!=null && user.getUserPerson()!=null) { |
There was a problem hiding this comment.
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
@@ -101,5 +101,4 @@ Date getExpiryTime() { | |||
public Integer getUserId() { | |||
return userId; | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bump
} | ||
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) { | ||
if(monetaryAccountBank == null) { | ||
throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND); |
There was a problem hiding this comment.
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.
return; | ||
} | ||
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) { | ||
if(monetaryAccountBank == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing space after if
BunqResponseRaw responseRaw = getRawResponse(USER_ENDPOINT_URL_LISTING); | ||
BunqResponse<List<User>> response = fromJsonList(User.class, responseRaw); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new line can go.
return response.getValue().stream().findFirst().orElse(null); | ||
} | ||
|
||
public MonetaryAccountBank getFirstActiveMonetaryAccountBank(Integer userId) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...byUserId
BunqResponse<List<MonetaryAccountBank>> response = fromJsonList(MonetaryAccountBank.class, responseRaw, MonetaryAccountBank.class.getSimpleName()); | ||
|
||
return response.getValue().stream() | ||
.filter(monetaryAccountBank->monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not java 7 compatible, please refactor to make it java 7 compatible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that is then also applicable for the use of Optional (UserContextHelper#getFirstUser)
return response.getValue().stream() | ||
.filter(monetaryAccountBank->monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) | ||
.findFirst() | ||
.orElse(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should throw exception instead of returning null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the mehtod name does not indicate that it will return null if not found which could cause unwanted null pointer exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So please throw the no active MA found
exception here.
public class UserContextTest extends BunqSdkTestBase { | ||
|
||
@Test | ||
public void testConstruct() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bump
public void testConstruct() { | ||
ApiContext context = getApiContext(); | ||
|
||
UserContext sut = new UserContext(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bump
less complexity, risk for nullpointers is the same as before
@tubbynl be aware i did a small push to your branch be sure to pull, ill do a review cycle now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have one more, after this LGTM.
} | ||
} | ||
|
||
throw new BunqException(ERROR_NO_ACTIVE_MONETARY); |
There was a problem hiding this comment.
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
The 2 failing test are alright. rate limit and a 500 which has nothing to do with the SDK it self. The 429 in the tests should be fixed with the throttle. |
@patrickdw1991 please 👀 |
); | ||
String wrapper = MonetaryAccountBank.class.getSimpleName(); | ||
BunqResponse<List<MonetaryAccountBank>> response = fromJsonList( | ||
MonetaryAccountBank.class, responseRaw, wrapper |
There was a problem hiding this comment.
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 :)
import java.util.List; | ||
|
||
public class UserContextHelper extends BunqModel { | ||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing new line
see #100 close as-you-wish |
for prior art see #98
i'm attempting some minor (non-breaking) changes to enable creation of the UserContext without referencing the static BunqContext
References #93