Skip to content

Commit

Permalink
Improve 'editoffer' opt parsing, fix test pkg name
Browse files Browse the repository at this point in the history
  • Loading branch information
ghubstan committed Jun 15, 2021
1 parent e2a205a commit 4da64b9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 45 deletions.
65 changes: 41 additions & 24 deletions cli/src/main/java/bisq/cli/opts/EditOfferOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@

public class EditOfferOptionParser extends AbstractMethodOptionParser implements MethodOpts {

static int OPT_ENABLE_ON = 1;
static int OPT_ENABLE_OFF = 0;
static int OPT_ENABLE_IGNORED = -1;

final OptionSpec<String> offerIdOpt = parser.accepts(OPT_OFFER_ID, "id of offer to cancel")
.withRequiredArg();

Expand Down Expand Up @@ -106,11 +110,7 @@ public EditOfferOptionParser parse() {
throw new IllegalArgumentException("no fixed price specified");

String fixedPriceAsString = options.valueOf(fixedPriceOpt);
try {
Double.valueOf(fixedPriceAsString);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(format("%s is not a number", fixedPriceAsString));
}
verifyStringIsValidDouble(fixedPriceAsString);

boolean fixedPriceOptIsOnlyOpt = !options.has(mktPriceMarginOpt)
&& !options.has(triggerPriceOpt)
Expand All @@ -137,11 +137,7 @@ public EditOfferOptionParser parse() {
if (priceMarginAsString.isEmpty())
throw new IllegalArgumentException("no market price margin specified");

try {
Double.valueOf(priceMarginAsString);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(format("%s is not a number", priceMarginAsString));
}
verifyStringIsValidDouble(priceMarginAsString);

boolean mktPriceMarginOptIsOnlyOpt = !options.has(triggerPriceOpt)
&& !options.has(fixedPriceOpt)
Expand All @@ -167,11 +163,7 @@ public EditOfferOptionParser parse() {
if (triggerPriceAsString.isEmpty())
throw new IllegalArgumentException("trigger price not specified");

try {
Double.valueOf(triggerPriceAsString);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(format("%s is not a number", triggerPriceAsString));
}
verifyStringIsValidDouble(triggerPriceAsString);

boolean triggerPriceOptIsOnlyOpt = !options.has(mktPriceMarginOpt)
&& !options.has(fixedPriceOpt)
Expand Down Expand Up @@ -214,29 +206,46 @@ public String getOfferId() {
}

public String getFixedPrice() {
return options.has(fixedPriceOpt) ? options.valueOf(fixedPriceOpt) : "0";
if (offerEditType.equals(FIXED_PRICE_ONLY) || offerEditType.equals(FIXED_PRICE_AND_ACTIVATION_STATE)) {
return options.has(fixedPriceOpt) ? options.valueOf(fixedPriceOpt) : "0";
} else {
return "0";
}
}

public String getTriggerPrice() {
return options.has(triggerPriceOpt) ? options.valueOf(triggerPriceOpt) : "0";
if (offerEditType.equals(TRIGGER_PRICE_ONLY)
|| offerEditType.equals(TRIGGER_PRICE_AND_ACTIVATION_STATE)
|| offerEditType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE)
|| offerEditType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE)) {
return options.has(triggerPriceOpt) ? options.valueOf(triggerPriceOpt) : "0";
} else {
return "0";
}
}

public BigDecimal getTriggerPriceAsBigDecimal() {
return new BigDecimal(getTriggerPrice());
}

public String getMktPriceMargin() {
return isUsingMktPriceMargin() ? options.valueOf(mktPriceMarginOpt) : "0.00";
if (offerEditType.equals(MKT_PRICE_MARGIN_ONLY)
|| offerEditType.equals(MKT_PRICE_MARGIN_AND_ACTIVATION_STATE)
|| offerEditType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE)
|| offerEditType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE)) {
return isUsingMktPriceMargin() ? options.valueOf(mktPriceMarginOpt) : "0.00";
} else {
return "0.00";
}
}

public BigDecimal getMktPriceMarginAsBigDecimal() {
return isUsingMktPriceMargin()
? new BigDecimal(options.valueOf(mktPriceMarginOpt))
: BigDecimal.ZERO;
return new BigDecimal(options.valueOf(mktPriceMarginOpt));
}

public boolean isUsingMktPriceMargin() {
return options.has(mktPriceMarginOpt);
return !offerEditType.equals(FIXED_PRICE_ONLY)
&& !offerEditType.equals(FIXED_PRICE_AND_ACTIVATION_STATE);
}

public int getEnableAsSignedInt() {
Expand All @@ -247,8 +256,8 @@ public int getEnableAsSignedInt() {
@Nullable
Boolean input = isEnable();
return input == null
? -1
: input ? 1 : 0;
? OPT_ENABLE_IGNORED
: input ? OPT_ENABLE_ON : OPT_ENABLE_OFF;
}

@Nullable
Expand All @@ -261,4 +270,12 @@ public Boolean isEnable() {
public EditOfferRequest.EditType getOfferEditType() {
return offerEditType;
}

private void verifyStringIsValidDouble(String string) {
try {
Double.valueOf(string);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(format("%s is not a number", string));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package bisq.cli.opt;
package bisq.cli.opts;

import org.junit.jupiter.api.Test;

import static bisq.cli.Method.editoffer;
import static bisq.cli.opts.EditOfferOptionParser.OPT_ENABLE_IGNORED;
import static bisq.cli.opts.EditOfferOptionParser.OPT_ENABLE_OFF;
import static bisq.cli.opts.EditOfferOptionParser.OPT_ENABLE_ON;
import static bisq.cli.opts.OptLabel.*;
import static bisq.proto.grpc.EditOfferRequest.EditType.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;



import bisq.cli.opts.EditOfferOptionParser;

// This opt parser test has the most thorough coverage,
// This opt parser test ahs the most thorough coverage,
// and is a reference for other opt parser tests.
public class EditOfferOptionParserTest {

Expand Down Expand Up @@ -124,7 +124,7 @@ public void testEditOfferActivationStateOnly() {
};
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(ACTIVATION_STATE_ONLY, parser.getOfferEditType());
assertEquals(1, parser.getEnableAsSignedInt());
assertEquals(OPT_ENABLE_ON, parser.getEnableAsSignedInt());
}

@Test
Expand Down Expand Up @@ -167,6 +167,9 @@ public void testEditOfferFixedPriceOnly() {
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(FIXED_PRICE_ONLY, parser.getOfferEditType());
assertEquals(fixedPriceAsString, parser.getFixedPrice());
assertFalse(parser.isUsingMktPriceMargin());
assertEquals("0.00", parser.getMktPriceMargin());
assertEquals(OPT_ENABLE_IGNORED, parser.getEnableAsSignedInt());
}

@Test
Expand All @@ -182,7 +185,9 @@ public void testEditOfferFixedPriceAndActivationStateOnly() {
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(FIXED_PRICE_AND_ACTIVATION_STATE, parser.getOfferEditType());
assertEquals(fixedPriceAsString, parser.getFixedPrice());
assertEquals(0, parser.getEnableAsSignedInt());
assertFalse(parser.isUsingMktPriceMargin());
assertEquals("0.00", parser.getMktPriceMargin());
assertEquals(OPT_ENABLE_OFF, parser.getEnableAsSignedInt());
}

@Test
Expand All @@ -196,7 +201,10 @@ public void testEditOfferMktPriceMarginOnly() {
};
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(MKT_PRICE_MARGIN_ONLY, parser.getOfferEditType());
assertTrue(parser.isUsingMktPriceMargin());
assertEquals(mktPriceMarginAsString, parser.getMktPriceMargin());
assertEquals("0", parser.getTriggerPrice());
assertEquals(OPT_ENABLE_IGNORED, parser.getEnableAsSignedInt());
}

@Test
Expand Down Expand Up @@ -225,8 +233,10 @@ public void testEditOfferMktPriceMarginAndActivationStateOnly() {
};
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(MKT_PRICE_MARGIN_AND_ACTIVATION_STATE, parser.getOfferEditType());
assertTrue(parser.isUsingMktPriceMargin());
assertEquals(mktPriceMarginAsString, parser.getMktPriceMargin());
assertEquals(0, parser.getEnableAsSignedInt());
assertEquals("0", parser.getTriggerPrice());
assertEquals(OPT_ENABLE_OFF, parser.getEnableAsSignedInt());
}

@Test
Expand All @@ -241,6 +251,9 @@ public void testEditTriggerPriceOnly() {
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(TRIGGER_PRICE_ONLY, parser.getOfferEditType());
assertEquals(triggerPriceAsString, parser.getTriggerPrice());
assertTrue(parser.isUsingMktPriceMargin());
assertEquals("0.00", parser.getMktPriceMargin());
assertEquals(OPT_ENABLE_IGNORED, parser.getEnableAsSignedInt());
}

@Test
Expand Down Expand Up @@ -284,7 +297,10 @@ public void testEditTriggerPriceAndActivationStateOnly() {
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(TRIGGER_PRICE_AND_ACTIVATION_STATE, parser.getOfferEditType());
assertEquals(triggerPriceAsString, parser.getTriggerPrice());
assertEquals(1, parser.getEnableAsSignedInt());
assertTrue(parser.isUsingMktPriceMargin());
assertEquals("0.00", parser.getMktPriceMargin());
assertEquals("0", parser.getFixedPrice());
assertEquals(OPT_ENABLE_ON, parser.getEnableAsSignedInt());
}

@Test
Expand All @@ -300,8 +316,11 @@ public void testEditMKtPriceMarginAndTriggerPrice() {
};
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE, parser.getOfferEditType());
assertEquals(mktPriceMarginAsString, parser.getMktPriceMargin());
assertEquals(triggerPriceAsString, parser.getTriggerPrice());
assertTrue(parser.isUsingMktPriceMargin());
assertEquals(mktPriceMarginAsString, parser.getMktPriceMargin());
assertEquals("0", parser.getFixedPrice());
assertEquals(OPT_ENABLE_IGNORED, parser.getEnableAsSignedInt());
}

@Test
Expand All @@ -318,8 +337,10 @@ public void testEditMKtPriceMarginAndTriggerPriceAndEnableState() {
};
EditOfferOptionParser parser = new EditOfferOptionParser(args).parse();
assertEquals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE, parser.getOfferEditType());
assertEquals(mktPriceMarginAsString, parser.getMktPriceMargin());
assertEquals(triggerPriceAsString, parser.getTriggerPrice());
assertFalse(parser.isEnable());
assertTrue(parser.isUsingMktPriceMargin());
assertEquals(mktPriceMarginAsString, parser.getMktPriceMargin());
assertEquals("0", parser.getFixedPrice());
assertEquals(OPT_ENABLE_OFF, parser.getEnableAsSignedInt());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bisq.cli.opt;
package bisq.cli.opts;

import org.junit.jupiter.api.Test;

Expand All @@ -11,13 +11,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;



import bisq.cli.opts.CancelOfferOptionParser;
import bisq.cli.opts.CreateCryptoCurrencyPaymentAcctOptionParser;
import bisq.cli.opts.CreateOfferOptionParser;
import bisq.cli.opts.CreatePaymentAcctOptionParser;


public class OptionParsersTest {

private static final String PASSWORD_OPT = "--" + OPT_PASSWORD + "=" + "xyz";
Expand Down

0 comments on commit 4da64b9

Please sign in to comment.