diff --git a/src/main/java/com/bunq/sdk/context/UserContext.java b/src/main/java/com/bunq/sdk/context/UserContext.java index 087297bc..98084e2d 100644 --- a/src/main/java/com/bunq/sdk/context/UserContext.java +++ b/src/main/java/com/bunq/sdk/context/UserContext.java @@ -40,7 +40,7 @@ private void initUser(User user) { } private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) { - if(monetaryAccountBank==null) { + if(monetaryAccountBank == null) { throw new BunqException(ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND); } this.primaryMonetaryAccountBank = monetaryAccountBank; diff --git a/src/main/java/com/bunq/sdk/model/core/UserContextHelper.java b/src/main/java/com/bunq/sdk/model/core/UserContextHelper.java index 83221c3f..abd6a273 100644 --- a/src/main/java/com/bunq/sdk/model/core/UserContextHelper.java +++ b/src/main/java/com/bunq/sdk/model/core/UserContextHelper.java @@ -11,7 +11,8 @@ public class UserContextHelper extends BunqModel { private static final String MONETARY_ACCOUNT_STATUS_ACTIVE = "ACTIVE"; - private static final int INDEX_FIRST = 0; + private static final String USER_ENDPOINT_URL_LISTING = "user"; + private static final String MONETARY_ENDPOINT_URL_READ = "user/%s/monetary-account-bank/%s"; private final ApiClient apiClient; @@ -24,20 +25,21 @@ private BunqResponseRaw getRawResponse(String url) { } public User getFirstUser() { - BunqResponseRaw responseRaw = getRawResponse("user"); + BunqResponseRaw responseRaw = getRawResponse(USER_ENDPOINT_URL_LISTING); BunqResponse> response = fromJsonList(User.class, responseRaw); - return response.getValue().get(INDEX_FIRST); + + + return response.getValue().stream().findFirst().orElse(null); } public MonetaryAccountBank getFirstActiveMonetaryAccountBank(Integer userId) { - BunqResponseRaw responseRaw = getRawResponse(String.format("user/%s/monetary-account-bank", userId)); + BunqResponseRaw responseRaw = getRawResponse(String.format(MONETARY_ENDPOINT_URL_READ, userId)); BunqResponse> 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; + + return response.getValue().stream() + .filter(monetaryAccountBank->monetaryAccountBank.getStatus().equals(MONETARY_ACCOUNT_STATUS_ACTIVE)) + .findFirst() + .orElse(null); } @Override diff --git a/src/test/java/com/bunq/sdk/context/UserContextTest.java b/src/test/java/com/bunq/sdk/context/UserContextTest.java index 2c631976..2304d9e9 100644 --- a/src/test/java/com/bunq/sdk/context/UserContextTest.java +++ b/src/test/java/com/bunq/sdk/context/UserContextTest.java @@ -3,7 +3,7 @@ import com.bunq.sdk.BunqSdkTestBase; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; public class UserContextTest extends BunqSdkTestBase { @@ -16,4 +16,4 @@ public void testConstruct() { assertNotNull(sut.getUserId()); assertNotNull(sut.getMainMonetaryAccountId()); } -} \ No newline at end of file +}