Skip to content

Commit

Permalink
Merge pull request #234 from /issues/232
Browse files Browse the repository at this point in the history
Issues/232 - List e2e Cucumber Feature
  • Loading branch information
cnorburn authored Nov 3, 2023
2 parents ada9d95 + f4d35a4 commit c2be644
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 4 deletions.
176 changes: 176 additions & 0 deletions src/test/java/com/casper/sdk/e2e/steps/NestedListStepDefinitions.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down
10 changes: 8 additions & 2 deletions src/test/java/com/casper/sdk/e2e/utils/CLValueFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -108,10 +114,10 @@ private Map buildMap(final List<AbstractCLValue<?, ?>> innerValues) throws Excep
final Map<AbstractCLValue, AbstractCLValue> 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;
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/casper/sdk/e2e/utils/DeployUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static Deploy buildStandardTransferDeploy(final List<NamedArg<?>> namedAr
);
}

public static AbstractCLValue<?, ?> getNamedArgValue(final String type, final List<NamedArg<?>> namedArgs) {
public static AbstractCLValue<?, ?> getNamedArgValue(final List<NamedArg<?>> namedArgs, final String type) {
return namedArgs.stream()
.filter(namedArg -> type.equals(namedArg.getType()))
.findFirst()
Expand Down

0 comments on commit c2be644

Please sign in to comment.