BAEL 3320 JCommander (#7971)

* init jcommander

* add model layer jcommander app

* service scaffolding

* init jcommander cli layer

* wire up services and commands

* splitter impl; validator impl; tests and cleanup

* cleanup pom

* integration tests

* fix uuid validator example

* optimise uuid regex; if-else to switch

* review comments

* fix builder formatting

* change list assertion in fetch charges tests

* missing minor edit

* move to new module libraries-3

* rm unwanted files
This commit is contained in:
Priyank Srivastava
2019-11-06 03:22:34 +05:30
committed by ashleyfrieze
parent f2d9829b91
commit e16bd73565
21 changed files with 823 additions and 0 deletions
@@ -0,0 +1,28 @@
package com.baeldung.jcommander.helloworld;
import com.beust.jcommander.JCommander;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HelloWorldAppUnitTest {
@Test
public void whenJCommanderInvokedWithArgs_thenArgsParsed() {
HelloWorldArgs jArgs = new HelloWorldArgs();
JCommander helloCmd = JCommander
.newBuilder()
.addObject(jArgs)
.build();
// when
String[] argv = new String[] {
"--name", "JavaWorld"
};
helloCmd.parse(argv);
// then
assertEquals("JavaWorld", jArgs.getName());
}
}
@@ -0,0 +1,61 @@
package com.baeldung.jcommander.usagebilling.cli;
import com.beust.jcommander.JCommander;
import org.junit.Test;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.Assert.assertThat;
public class FetchCurrentChargesCommandUnitTest {
private JCommander jc = JCommander.newBuilder()
.addObject(new FetchCurrentChargesCommand())
.build();
@Test
public void whenParsedMultipleSubscriptionsParameter_thenParameterSubscriptionsIsPopulated() {
FetchCurrentChargesCommand cmd = (FetchCurrentChargesCommand) jc
.getObjects()
.get(0);
jc.parse(new String[] {
"-C", "cb898e7a-f2a0-46d2-9a09-531f1cee1839",
"-S", "subscriptionA001",
"-S", "subscriptionA002",
"-S", "subscriptionA003",
});
assertThat(cmd.getSubscriptionIds(),
contains("subscriptionA001", "subscriptionA002", "subscriptionA003"));
}
@Test
public void whenParsedSubscriptionsColonSeparatedParameter_thenParameterSubscriptionsIsPopulated() {
FetchCurrentChargesCommand cmd = (FetchCurrentChargesCommand) jc
.getObjects()
.get(0);
jc.parse(new String[] {
"-C", "cb898e7a-f2a0-46d2-9a09-531f1cee1839",
"-S", "subscriptionA001:subscriptionA002:subscriptionA003",
});
assertThat(cmd.getSubscriptionIds(),
contains("subscriptionA001", "subscriptionA002", "subscriptionA003"));
}
@Test
public void whenParsedSubscriptionsWithVariableArity_thenParameterSubscriptionsIsPopulated() {
FetchCurrentChargesCommand cmd = (FetchCurrentChargesCommand) jc
.getObjects()
.get(0);
jc.parse(new String[] {
"-C", "cb898e7a-f2a0-46d2-9a09-531f1cee1839",
"-S", "subscriptionA001", "subscriptionA002", "subscriptionA003",
});
assertThat(cmd.getSubscriptionIds(),
contains("subscriptionA001", "subscriptionA002", "subscriptionA003"));
}
}
@@ -0,0 +1,64 @@
package com.baeldung.jcommander.usagebilling.cli;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SubmitUsageCommandUnitTest {
private JCommander jc = JCommander.newBuilder()
.addObject(new SubmitUsageCommand())
.build();
@Test
public void whenParsedCustomerParameter_thenParameterOfTypeStringIsPopulated() {
jc.parse(new String[] {
"--customer", "cb898e7a-f2a0-46d2-9a09-531f1cee1839",
"--subscription", "subscriptionPQRMN001",
"--pricing-type", "PRE_RATED",
"--timestamp", "2019-10-03T10:58:00",
"--quantity", "7",
"--price", "24.56"
});
SubmitUsageCommand cmd = (SubmitUsageCommand) jc
.getObjects()
.get(0);
assertEquals("cb898e7a-f2a0-46d2-9a09-531f1cee1839", cmd.getCustomerId());
}
@Test
public void whenParsedTimestampParameter_thenParameterOfTypeInstantIsPopulated() {
jc.parse(new String[] {
"--customer", "cb898e7a-f2a0-46d2-9a09-531f1cee1839",
"--subscription", "subscriptionPQRMN001",
"--pricing-type", "PRE_RATED",
"--timestamp", "2019-10-03T10:58:00",
"--quantity", "7",
"--price", "24.56"
});
SubmitUsageCommand cmd = (SubmitUsageCommand) jc
.getObjects()
.get(0);
assertEquals("2019-10-03T10:58:00Z", cmd
.getTimestamp()
.toString());
}
@Test(expected = ParameterException.class)
public void whenParsedCustomerIdNotUUID_thenParameterException() {
jc.parse(new String[] {
"--customer", "customer001",
"--subscription", "subscriptionPQRMN001",
"--pricing-type", "PRE_RATED",
"--timestamp", "2019-10-03T10:58:00",
"--quantity", "7",
"--price", "24.56"
});
}
}