diff --git a/src/test/java/com/casper/sdk/e2e/steps/NestedListStepDefinitions.java b/src/test/java/com/casper/sdk/e2e/steps/NestedListStepDefinitions.java new file mode 100644 index 000000000..3d632bf21 --- /dev/null +++ b/src/test/java/com/casper/sdk/e2e/steps/NestedListStepDefinitions.java @@ -0,0 +1,176 @@ +package com.casper.sdk.e2e.steps; + +import com.casper.sdk.e2e.utils.CLValueFactory; +import com.casper.sdk.e2e.utils.CasperClientProvider; +import com.casper.sdk.e2e.utils.DeployUtils; +import com.casper.sdk.model.clvalue.AbstractCLValue; +import com.casper.sdk.model.clvalue.CLValueList; +import com.casper.sdk.model.clvalue.cltype.CLTypeData; +import com.casper.sdk.model.deploy.Deploy; +import com.casper.sdk.model.deploy.DeployData; +import com.casper.sdk.model.deploy.NamedArg; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; + +import java.util.Arrays; +import java.util.Collections; + +import static com.casper.sdk.e2e.utils.DeployUtils.getNamedArgValue; +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.notNullValue; + +/** + * The step definitions for the nested list feature. + * + * @author ian@meywood.com + */ +public class NestedListStepDefinitions { + + private CLValueList clValueList; + private String deployHash; + private DeployData deployData; + private final CLValueFactory clValueFactory = new CLValueFactory(); + + @Given("^a list is created with U(\\d+) values of \\((\\d+), (\\d+), (\\d+)\\)$") + public void aListIsCreatedWithUValuesOf(final int numberLen, + final int val1, + final int val2, + final int val3) throws Exception { + clValueList = new CLValueList( + asList(createUnsignedValue(numberLen, val1), createUnsignedValue(numberLen, val2), createUnsignedValue(numberLen, val3)) + ); + } + + @Given("a list is created with I{int} values of \\({int}, {int}, {int})") + public void aListIsCreatedWithIValuesOf(final int numberLen, + final int val1, + final int val2, + final int val3) throws Exception { + clValueList = new CLValueList(asList( + createSignedValue(numberLen, val1), + createSignedValue(numberLen, val2), + createSignedValue(numberLen, val3) + )); + } + + @And("the list's {string} item is a CLValue with I{int} value of {int}") + public void theListSItemIsACLValueWithIValueOf(final String nth, final int type, final int value) throws Exception { + final AbstractCLValue clValue = getClValue(nth); + assertThat(clValue.getClType().getTypeName(), is("I" + type)); + assertThat(clValue.getValue(), is(createSignedValue(type, value).getValue())); + } + + @Then("^the list's bytes are \"([^\"]*)\"$") + public void theListSBytesAre(String hexBytes) { + assertThat(clValueList.getBytes(), is(hexBytes)); + } + + @And("^the list's length is (\\d+)$") + public void theListSLengthIs(int length) { + assertThat(clValueList.getValue().size(), is(length)); + } + + @And("^the list's \"([^\"]*)\" item is a CLValue with U(\\d+) value of (\\d+)$") + public void theListSItemIsACLValueWithUValueOf(final String index, final int numberLength, final int value) throws Throwable { + final AbstractCLValue clValue = getClValue(index); + assertThat(clValue.getClType().getTypeName(), is("U" + numberLength)); + assertThat(clValue.getValue(), is(createUnsignedValue(numberLength, value).getValue())); + } + + @Given("a list is created with {string} values of \\({string}, {string}, {string})") + public void aListIsCreatedWithValuesOf(final String type, final String val1, final String val2, final String val3) throws Exception { + clValueList = new CLValueList( + Arrays.asList(createValue(type, val1), createValue(type, val2), createValue(type, val3)) + ); + } + + @And("the list's {string} item is a CLValue with {string} value of {string}") + public void theListSItemIsACLValueWithValueOf(final String index, final String type, final String value) throws Throwable { + final AbstractCLValue clValue = getClValue(index); + assertThat(clValue.getClType().getTypeName(), is(type)); + assertThat(clValue.getValue(), is(createValue(type, value).getValue())); + } + + @Given("a nested list is created with U{int} values of \\(\\({int}, {int}, {int}),\\({int}, {int}, {int}))") + public void aNestedListIsCreatedWithValuesOf(final int type, + final int val1, + final int val2, + final int val3, + final int val4, + final int val5, + final int val6) throws Exception { + clValueList = new CLValueList(Arrays.asList( + new CLValueList( + Arrays.asList( + createUnsignedValue(type, val1), + createUnsignedValue(type, val2), + createUnsignedValue(type, val3) + ) + ), + new CLValueList( + Arrays.asList( + createUnsignedValue(type, val4), + createUnsignedValue(type, val5), + createUnsignedValue(type, val6) + ) + ) + )); + } + + @And("the {string} nested list's {string} item is a CLValue with U{int} value of {int}") + public void theNestedListSItemIsACLValueWithValueOf(String nth, String nestedNth, int type, int val) throws Exception { + CLValueList nestedList = (CLValueList) getClValue(nth); + AbstractCLValue clValue = getClValue(nestedList, nestedNth); + assertThat(clValue.getClType().getTypeName(), is("U" + type)); + assertThat(clValue.getValue(), is(createUnsignedValue(type, val).getValue())); + } + + @Given("that the list is deployed in a transfer") + public void thatTheListIsDeployedInATransfer() throws Exception { + + final Deploy deploy = DeployUtils.buildStandardTransferDeploy( + Collections.singletonList(new NamedArg<>("LIST", clValueList)) + ); + + deployHash = CasperClientProvider.getInstance().getCasperService().putDeploy(deploy).getDeployHash(); + } + + @And("the transfer containing the list is successfully executed") + public void theTransferContainingTheListIsSuccessfullyExecuted() { + deployData = DeployUtils.waitForDeploy( + deployHash, + 300, + CasperClientProvider.getInstance().getCasperService() + ); + } + + @When("the list is read from the deploy") + public void theListIsReadFromTheDeploy() { + clValueList = (CLValueList) getNamedArgValue(deployData.getDeploy().getSession().getArgs(), "LIST"); + assertThat(clValueList, is(notNullValue())); + } + + private AbstractCLValue getClValue(final String index) { + return getClValue(clValueList, index); + } + + private AbstractCLValue getClValue(final CLValueList list, final String index) { + return list.getValue().get(Integer.parseInt(index.substring(0, 1)) - 1); + } + + private AbstractCLValue createSignedValue(int numberLen, int val1) throws Exception { + return createValue("I" + numberLen, Integer.toString(val1)); + } + + private AbstractCLValue createUnsignedValue(final int numberLen, final int value) throws Exception { + return clValueFactory.createValue(CLTypeData.getTypeByName("U" + numberLen), Integer.toString(value)); + } + + private AbstractCLValue createValue(final String type, final String strValue) throws Exception { + return clValueFactory.createValue(CLTypeData.getTypeByName(type), strValue); + } +} diff --git a/src/test/java/com/casper/sdk/e2e/steps/NestedMapStepDefinitions.java b/src/test/java/com/casper/sdk/e2e/steps/NestedMapStepDefinitions.java index 3062dc34d..8d8842df1 100644 --- a/src/test/java/com/casper/sdk/e2e/steps/NestedMapStepDefinitions.java +++ b/src/test/java/com/casper/sdk/e2e/steps/NestedMapStepDefinitions.java @@ -161,7 +161,7 @@ public void aMapIsCreated(final int key1, @When("the map is read from the deploy") public void theMapIsReadFromTheDeploy() { - map = (CLValueMap) getNamedArgValue("MAP", deployData.getDeploy().getSession().getArgs()); + map = (CLValueMap) getNamedArgValue(deployData.getDeploy().getSession().getArgs(), "MAP"); assertThat(map, is(notNullValue())); } diff --git a/src/test/java/com/casper/sdk/e2e/utils/CLValueFactory.java b/src/test/java/com/casper/sdk/e2e/utils/CLValueFactory.java index 2ce9046c7..a221e6f75 100644 --- a/src/test/java/com/casper/sdk/e2e/utils/CLValueFactory.java +++ b/src/test/java/com/casper/sdk/e2e/utils/CLValueFactory.java @@ -38,9 +38,15 @@ public class CLValueFactory { case U64: return new CLValueU64(new BigInteger(strValue)); + case U128: + return new CLValueU128(new BigInteger(strValue)); + case U256: return new CLValueU256(new BigInteger(strValue)); + case U512: + return new CLValueU512(new BigInteger(strValue)); + case I32: return new CLValueI32(Integer.valueOf(strValue)); @@ -108,10 +114,10 @@ private Map buildMap(final List> innerValues) throws Excep final Map map = new LinkedHashMap<>(); int i = 0; - for (AbstractCLValue innerValue: innerValues) { + for (AbstractCLValue innerValue : innerValues) { AbstractCLValue key = new CLValueString(Integer.toString(i++)); map.put(key, innerValue); } - return map; + return map; } } diff --git a/src/test/java/com/casper/sdk/e2e/utils/DeployUtils.java b/src/test/java/com/casper/sdk/e2e/utils/DeployUtils.java index 119e47fa5..1c493d4e6 100644 --- a/src/test/java/com/casper/sdk/e2e/utils/DeployUtils.java +++ b/src/test/java/com/casper/sdk/e2e/utils/DeployUtils.java @@ -85,7 +85,7 @@ public static Deploy buildStandardTransferDeploy(final List> namedAr ); } - public static AbstractCLValue getNamedArgValue(final String type, final List> namedArgs) { + public static AbstractCLValue getNamedArgValue(final List> namedArgs, final String type) { return namedArgs.stream() .filter(namedArg -> type.equals(namedArg.getType())) .findFirst()