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

Conversation

tubbynl
Copy link
Contributor

@tubbynl tubbynl commented Jun 14, 2018

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

@tubbynl
Copy link
Contributor Author

tubbynl commented Jun 14, 2018

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

@bunq bunq deleted a comment Jun 14, 2018
@tubbynl
Copy link
Contributor Author

tubbynl commented Jun 15, 2018

test fail on
com.bunq.sdk.model.generated.endpoint.TabUsageSingleTest > classMethod FAILED
com.bunq.sdk.exception.TooManyRequestsException at TabUsageSingleTest.java:30

perhaps the API-key used for testing should be bumped

@OGKevin
Copy link
Contributor

OGKevin commented Jun 17, 2018

@tubbynl please rebase your commits to follow the following pattern.

- '([A-Za-z0-9 ]+)\. (\(bunq\/sdk_java#[0-9]+\))'

@OGKevin OGKevin self-requested a review June 17, 2018 06:35
UserContext building without static references, backwards compatible
@tubbynl tubbynl force-pushed the develop-non-breaking-refactor branch from 2895b15 to 714d486 Compare June 17, 2018 15:01
@bunq bunq deleted a comment Jun 17, 2018
@@ -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

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

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

}
private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) {
if(monetaryAccountBank==null) {
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

}

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.

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

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.

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()) {
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.

}

public MonetaryAccountBank getFirstActiveMonetaryAccountBank(Integer userId) {
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.

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.

@bunq bunq deleted a comment Jun 17, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
} 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.

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

Copy link
Contributor

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);
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.

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 after if

BunqResponseRaw responseRaw = getRawResponse(USER_ENDPOINT_URL_LISTING);
BunqResponse<List<User>> response = fromJsonList(User.class, responseRaw);


Copy link
Contributor

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

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

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.

Copy link
Contributor Author

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

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.

Copy link
Contributor

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.

Copy link
Contributor

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

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

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
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 18, 2018
@bunq bunq deleted a comment Jun 20, 2018
@bunq bunq deleted a comment Jun 20, 2018
@bunq bunq deleted a comment Jun 20, 2018
@bunq bunq deleted a comment Jun 20, 2018
@OGKevin
Copy link
Contributor

OGKevin commented Jun 20, 2018

@tubbynl be aware i did a small push to your branch be sure to pull, ill do a review cycle now.

Copy link
Contributor

@OGKevin OGKevin left a 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);
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

@OGKevin
Copy link
Contributor

OGKevin commented Jun 20, 2018

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.

@bunq bunq deleted a comment Jun 20, 2018
@OGKevin
Copy link
Contributor

OGKevin commented Jun 20, 2018

@patrickdw1991 please 👀

OGKevin
OGKevin previously approved these changes Jun 20, 2018
@OGKevin OGKevin added this to the 1.0.0 milestone Jun 20, 2018
);
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 :)

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

@tubbynl
Copy link
Contributor Author

tubbynl commented Jun 20, 2018

see #100 close as-you-wish

@OGKevin
Copy link
Contributor

OGKevin commented Jun 22, 2018

@patrickdw1991 👁

@OGKevin OGKevin changed the title #93 UserContext building without static references, backwards compatible UserContext building without static references. (bunq/sdk_java#93) Jun 22, 2018
@bunq bunq deleted a comment Jun 22, 2018
@OGKevin OGKevin merged commit aa4736b into bunq:develop Jun 22, 2018
@OGKevin
Copy link
Contributor

OGKevin commented Jun 22, 2018

@andrederoos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants