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

Issues/235 - Option Values e2e test #238

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/test/java/com/casper/sdk/e2e/steps/OptionValuesStepDefinitin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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.CLValueOption;
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.DeployResult;
import com.casper.sdk.model.deploy.NamedArg;
import dev.oak3.sbs4j.exception.ValueSerializationException;
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.LinkedList;
import java.util.List;
import java.util.Optional;

import static com.casper.sdk.e2e.utils.DeployUtils.buildStandardTransferDeploy;
import static com.casper.sdk.e2e.utils.DeployUtils.getNamedArgValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;

/**
* Step definitions for the Option value feature.
*
* @author [email protected]
*/
public class OptionValuesStepDefinitin {

private CLValueOption clValueOption;
private final CLValueFactory clValueFactory = new CLValueFactory();
private DeployResult deployResult;
private DeployData deployData;

@Given("^that an Option value has an empty value$")
public void thatAnOptionValueHasAnEmptyValue() throws ValueSerializationException {
clValueOption = new CLValueOption(Optional.empty());
}

@Then("^the Option value is not present$")
public void theOptionValueShouldBeInvalid() {
assertThat(clValueOption.getValue().isPresent(), is(false));
}

@And("the Option value's bytes are {string}")
public void theOptionValueBytesAre(final String hexBytes) {
assertThat(clValueOption.getBytes(), is(hexBytes));
}

@Given("an Option value contains a {string} value of {string}")
public void thatAnOptionValuesHasAValueOf(final String typeName, final String strValue) throws Exception {
clValueOption = new CLValueOption(
Optional.of(clValueFactory.createValue(CLTypeData.getTypeByName(typeName), strValue))
);
}

@Then("the Option value is present")
public void theOptionValueIsPresent() {
assertThat(clValueOption.getValue().isPresent(), is(true));
}

@Given("that the Option value is deployed in a transfer as a named argument")
public void thatTheOptionValueIsDeployedInATransferAsANamedArgument() throws Exception {
final List<NamedArg<?>> transferArgs = new LinkedList<>();
transferArgs.add(new NamedArg<>("OPTION", clValueOption));

final Deploy deploy = buildStandardTransferDeploy(transferArgs);

clValueOption = null;

deployResult = CasperClientProvider.getInstance().getCasperService().putDeploy(deploy);
}

@And("the transfer containing the Option value is successfully executed")
public void theTransferContainingTheOptionValueIsSuccessfullyExecuted() {
deployData = DeployUtils.waitForDeploy(
deployResult.getDeployHash(),
300,
CasperClientProvider.getInstance().getCasperService()
);
}

@When("the Option is read from the deploy")
public void theOptionIsReadFromTheDeploy() {
clValueOption = (CLValueOption) getNamedArgValue(deployData.getDeploy().getSession().getArgs(), "OPTION");
assertThat(clValueOption, is(notNullValue()));
}

@And("the type of the Option is {string} with a value of {string}")
public void theTypeOfTheOptionIsWithAValueOf(final String typeName, final String strValue) throws Exception {
assertThat(clValueOption.getClType().getTypeName(), is("Option"));
assertThat(clValueOption.getValue(), is(notNullValue()));
//noinspection OptionalGetWithoutIsPresent
assertThat(
clValueOption.getValue().get().getValue(),
is(this.clValueFactory.createValue(CLTypeData.getTypeByName(typeName), strValue).getValue())
);
}
}
30 changes: 15 additions & 15 deletions src/test/java/com/casper/sdk/e2e/utils/CLValueFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ public class CLValueFactory {
case BOOL:
return new CLValueBool(Boolean.TRUE.toString().equalsIgnoreCase(strValue));

case BYTE_ARRAY:
return new CLValueByteArray(Hex.decode(strValue));

case I32:
return new CLValueI32(Integer.valueOf(strValue));

case I64:
return new CLValueI64(Long.valueOf(strValue));

case KEY:
return new CLValueKey(Key.fromTaggedHexString(strValue));

case PUBLIC_KEY:
return new CLValuePublicKey(PublicKey.fromTaggedHexString(strValue));

case U8:
return new CLValueU8(Byte.valueOf(strValue));

Expand All @@ -50,21 +65,6 @@ public class CLValueFactory {
case U512:
return new CLValueU512(new BigInteger(strValue));

case I32:
return new CLValueI32(Integer.valueOf(strValue));

case I64:
return new CLValueI64(Long.valueOf(strValue));

case BYTE_ARRAY:
return new CLValueByteArray(Hex.decode(strValue));

case KEY:
return new CLValueKey(Key.fromTaggedHexString(strValue));

case PUBLIC_KEY:
return new CLValuePublicKey(PublicKey.fromTaggedHexString(strValue));

case UREF:
return new CLValueURef(new URef(Hex.decode(strValue), URefAccessRight.READ_ADD_WRITE));

Expand Down