Skip to content

Commit

Permalink
[ACL-159] Add user_selected scheme selection (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
tl-luca-baggi authored Sep 18, 2024
1 parent a51d5b8 commit 42ffeab
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 41 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Main properties
group=com.truelayer
archivesBaseName=truelayer-java
version=14.0.0
version=14.1.0

# Artifacts properties
sonatype_repository_url=https://s01.oss.sonatype.org/service/local/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@JsonSubTypes({
@JsonSubTypes.Type(value = InstantOnlySchemeSelection.class, name = "instant_only"),
@JsonSubTypes.Type(value = InstantPreferredSchemeSelection.class, name = "instant_preferred"),
@JsonSubTypes.Type(value = UserSelectedSchemeSelection.class, name = "user_selected"),
@JsonSubTypes.Type(value = PreselectedSchemeSelection.class, name = "preselected"),
})
@Getter
Expand All @@ -32,6 +33,10 @@ public static InstantPreferredSchemeSelection.InstantPreferredSchemeSelectionBui
return InstantPreferredSchemeSelection.builder();
}

public static UserSelectedSchemeSelection.UserSelectedSchemeSelectionBuilder userSelected() {
return UserSelectedSchemeSelection.builder();
}

public static PreselectedSchemeSelection.PreselectedSchemeSelectionBuilder preselected() {
return PreselectedSchemeSelection.builder();
}
Expand All @@ -46,6 +51,11 @@ public boolean isInstantPreferred() {
return this instanceof InstantPreferredSchemeSelection;
}

@JsonIgnore
public boolean isUserSelected() {
return this instanceof UserSelectedSchemeSelection;
}

@JsonIgnore
public boolean isPreselected() {
return this instanceof PreselectedSchemeSelection;
Expand All @@ -67,6 +77,14 @@ public InstantPreferredSchemeSelection asInstantPreferred() {
return (InstantPreferredSchemeSelection) this;
}

@JsonIgnore
public UserSelectedSchemeSelection asUserSelected() {
if (!isUserSelected()) {
throw new TrueLayerException(buildErrorMessage());
}
return (UserSelectedSchemeSelection) this;
}

@JsonIgnore
public PreselectedSchemeSelection asPreselected() {
if (!isPreselected()) {
Expand All @@ -84,6 +102,7 @@ private String buildErrorMessage() {
public enum Type {
INSTANT_ONLY("instant_only"),
INSTANT_PREFERRED("instant_preferred"),
USER_SELECTED("user_selected"),
PRESELECTED("preselected");

@JsonValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.truelayer.java.payments.entities.schemeselection.preselected;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@Getter
@Builder
@EqualsAndHashCode(callSuper = false)
@ToString
public class UserSelectedSchemeSelection extends SchemeSelection {
private final Type type = Type.USER_SELECTED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = InstantOnlySchemeSelection.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = InstantOnlySchemeSelection.class, name = "instant_only"),
@JsonSubTypes.Type(value = InstantPreferredSchemeSelection.class, name = "instant_preferred")
@JsonSubTypes.Type(value = InstantPreferredSchemeSelection.class, name = "instant_preferred"),
@JsonSubTypes.Type(value = UserSelectedSchemeSelection.class, name = "user_selected"),
})
@Getter
@ToString
Expand All @@ -23,9 +24,6 @@ public abstract class SchemeSelection {
@JsonIgnore
public abstract Type getType();

@JsonIgnore
public abstract boolean allowRemitterFee();

public static InstantOnlySchemeSelection.InstantOnlySchemeSelectionBuilder instantOnly() {
return InstantOnlySchemeSelection.builder();
}
Expand All @@ -34,6 +32,10 @@ public static InstantPreferredSchemeSelection.InstantPreferredSchemeSelectionBui
return InstantPreferredSchemeSelection.builder();
}

public static UserSelectedSchemeSelection.UserSelectedSchemeSelectionBuilder userSelected() {
return UserSelectedSchemeSelection.builder();
}

@JsonIgnore
public boolean isInstantOnly() {
return this instanceof InstantOnlySchemeSelection;
Expand All @@ -44,6 +46,11 @@ public boolean isInstantPreferred() {
return this instanceof InstantPreferredSchemeSelection;
}

@JsonIgnore
public boolean isUserSelected() {
return this instanceof UserSelectedSchemeSelection;
}

@JsonIgnore
public InstantOnlySchemeSelection asInstantOnly() {
if (!isInstantOnly()) {
Expand All @@ -60,6 +67,14 @@ public InstantPreferredSchemeSelection asInstantPreferred() {
return (InstantPreferredSchemeSelection) this;
}

@JsonIgnore
public UserSelectedSchemeSelection asUserSelected() {
if (!isUserSelected()) {
throw new TrueLayerException(buildErrorMessage());
}
return (UserSelectedSchemeSelection) this;
}

private String buildErrorMessage() {
return String.format("Scheme selection is of type %s.", this.getClass().getSimpleName());
}
Expand All @@ -68,7 +83,8 @@ private String buildErrorMessage() {
@Getter
public enum Type {
INSTANT_ONLY("instant_only"),
INSTANT_PREFERRED("instant_preferred");
INSTANT_PREFERRED("instant_preferred"),
USER_SELECTED("user_selected");

@JsonValue
private final String type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.truelayer.java.payments.entities.schemeselection.userselected;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@Getter
@Builder
@EqualsAndHashCode(callSuper = false)
@ToString
public class UserSelectedSchemeSelection extends SchemeSelection {
private final Type type = Type.USER_SELECTED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ public void shouldNotConvertToInstantPreferred() {
String.format("Scheme selection is of type %s.", sut.getClass().getSimpleName()), thrown.getMessage());
}

@Test
@DisplayName("It should yield true if instance is of type UserSelectedSchemeSelection")
public void shouldYieldTrueIfUserSelected() {
SchemeSelection sut = new UserSelectedSchemeSelection();
assertTrue(sut.isUserSelected());
}

@Test
@DisplayName("It should convert to an instance of class UserSelectedSchemeSelection")
public void shouldConvertToUserSelected() {
SchemeSelection sut = new UserSelectedSchemeSelection();

assertDoesNotThrow(sut::asUserSelected);
}

@Test
@DisplayName("It should throw an error when converting to UserSelectedSchemeSelection")
public void shouldNotConvertToUserSelected() {
SchemeSelection sut = new InstantOnlySchemeSelection(true);

Throwable thrown = assertThrows(TrueLayerException.class, sut::asUserSelected);

assertEquals(
String.format("Scheme selection is of type %s.", sut.getClass().getSimpleName()), thrown.getMessage());
}

@Test
@DisplayName("It should yield true if instance is of type PreselectedSchemeSelection")
public void shouldYieldTrueIfPreselected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,11 @@
import static org.junit.jupiter.api.Assertions.*;

import com.truelayer.java.TrueLayerException;
import java.util.stream.Stream;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class SchemeSelectionTests {

@DisplayName("Scheme selection base class should yield the expected type and allow_remitter_fee")
@ParameterizedTest(name = "object={0}, expected type={1}, expected allow_remitter_fee={2}")
@MethodSource("provideSchemeSelectionTestParameters")
public void baseClassShouldYieldExpectedDetails(
SchemeSelection schemeSelection, SchemeSelection.Type expectedType, boolean expectAllowRemitterFee) {
assertEquals(schemeSelection.getType(), expectedType);
assertEquals(schemeSelection.allowRemitterFee(), expectAllowRemitterFee);
}

@Test
@DisplayName("It should yield true if instance is of type InstantOnlySchemeSelection")
public void shouldYieldTrueIfInstantOnly() {
Expand Down Expand Up @@ -73,27 +60,29 @@ public void shouldNotConvertToInstantPreferred() {
String.format("Scheme selection is of type %s.", sut.getClass().getSimpleName()), thrown.getMessage());
}

private static Stream<Arguments> provideSchemeSelectionTestParameters() {
return Stream.of(
Arguments.of(
SchemeSelection.instantOnly().allowRemitterFee(true).build(),
SchemeSelection.Type.INSTANT_ONLY,
true),
Arguments.of(
SchemeSelection.instantOnly().allowRemitterFee(false).build(),
SchemeSelection.Type.INSTANT_ONLY,
false),
Arguments.of(
SchemeSelection.instantPreferred()
.allowRemitterFee(true)
.build(),
SchemeSelection.Type.INSTANT_PREFERRED,
true),
Arguments.of(
SchemeSelection.instantPreferred()
.allowRemitterFee(false)
.build(),
SchemeSelection.Type.INSTANT_PREFERRED,
false));
@Test
@DisplayName("It should yield true if instance is of type UserSelectedSchemeSelection")
public void shouldYieldTrueIfUserSelected() {
SchemeSelection sut = new UserSelectedSchemeSelection();
assertTrue(sut.isUserSelected());
}

@Test
@DisplayName("It should convert to an instance of class UserSelectedSchemeSelection")
public void shouldConvertToUserSelected() {
SchemeSelection sut = new UserSelectedSchemeSelection();

assertDoesNotThrow(sut::asUserSelected);
}

@Test
@DisplayName("It should throw an error when converting to UserSelectedSchemeSelection")
public void shouldNotConvertToUserSelected() {
SchemeSelection sut = new InstantOnlySchemeSelection(true);

Throwable thrown = assertThrows(TrueLayerException.class, sut::asUserSelected);

assertEquals(
String.format("Scheme selection is of type %s.", sut.getClass().getSimpleName()), thrown.getMessage());
}
}

0 comments on commit 42ffeab

Please sign in to comment.