Start a Gfsh Command
- Your command class needs to be stateless, no member variables are allowed.
- Your command class needs to extends GfshCommand, which implements spring shell's CommandMarker. We need this to know that this is a command class. GfshCommand also provides a lot of convenience methods for you to interact with the cluster.
- Avoid using static methods in your command. If the functionality is general enough, consider adding it to GfshCommand so that other commands can use it as well, and it provides easy mocking for testing.
A command usually needs to validate it's options. There are three ways to do validations. One is provided by the CliOption to mark an option as mandatory, one is to use Converters, and the other one is to use Interceptors. These are all done in the Gfsh client vm. It's best to do validation in these places other than putting the validation logic inside the command execution which is executed on the locator.
CliOption allows you to specify if an option is mandatory or not. If a parameter is marked as mandatory then the command will fail if this option is not specified.
Converters are the way for you to manipulate value input before you feed it into your command class. You can use them to convert and validate user input and provide hints for auto-completion. Gfsh provides commonly used converters that would convert strings to boolean, int, long, String (comma separated), enum, Date, File etc. These converters also serves validation purposes when converting the values. For example, below is an option specified in "test "command. If a command is "test --count=abc", then this command will fail the parser validation, because the parser can not convert "abc" into an integer.
Converters is a way to provide commonly-used validation that can be shared across multiple commands. E.g. a file type converter can validate if the filename is in the correct format and then converts it to File type. If we have such converters handy, all commands that needs a file option can use it and get the benefit of free validation. If you find yourself trying to validate a common type, consider using a converter.
Usually you can tell the parser to use a converter by just specifying your parameter type. If there is only one converter available for that type, then the parser will use that to convert your value, but if there are multiple converters available, then it's indeterministic as to which one parser will use. To bypass this ambiguity, you will need to use optionContext in @CliOption to narrow down which converter to use. e.g. below option specified an optionContext string:
Then in the converter implementation, you will need to use that string when determining if this converter can be used for that option or not:
Interceptors are generally used when your validate include multiple options, e.g validate mutual exclusivity. Here is an example of a command that has interceptors. You can see it's validating that user can not specify both options in this command.
Writing Tests for Option Validation
It's sufficient to write just a unit tests for your option validations. It's an overkill to fire up a cluster, only to issues commands that are going to fail validation. Use GfshParserRule to help you with these kind of tests. Below is an example:
When you are in the command execution code, please follow these guidelines:
- Use methods in GfshCommand as much as possbile to interact with the cluster, like getMember, findMember, getSecurityService, executeFunction etc. There are two sets of functions in GfshCommand: getXXX() and findXXX(). getXXX() will throw you and exception if nothing is found, while findXXX() will give you an empty set if nothing is found.
- Do not try to catch any exceptions unless you absolutely need to. All commands are executed by CommandExecutor which will turn exceptions into appropriate CommandResult.
- In your code, you can throw Exceptions. They are treated by CommandExecutors depending on what type of exceptions you throw:
- UserErrorException/IllegalArgumentException/IllegalStateExceptions are not logged by the executor (no stack trace in logs). The messages in these exception are reported back to gfsh as error messages. Commands are deemed to have failed.
- EntityNotFoundException can either be reported as error or info depending on whether the exception is created with statusOK flag. If your command requires you not to report "region not found" or "member not found" or any other "entity not found" back as error messages, you can throw an EntityNotFoundException with statusOK to be true. This exception is also not logged by the executor.
- NotAuthorizedException is logged and rethrown by the executor
- All other exceptions are caught by the executor and turned into a command result with "error" state, and statck trace logged in the logs.
- ResultBuilder.buildInfoResult() will build a result with OK status, while ResultBuilder.buildGemfireErrorResult or ResultBuilder.buildUserErrorResult will build a result with ERROR status.
Test Your Command
There are 4 level of tests for your command:
- There should be at least a unit test that would test the option validation and controll logic inside your command code. Use GfshParserRule as mentioned above for that purpose.
- Use LocatorStarterRule or ServerStarterRule in combination with GfshCommandRule to write an integration test for your command.
- Use LocatorServerStartupRule and GfshCommandRule to write DUnit test for your command
- Use GfshRule to write acceptance test for your command.
Unit tests are a must. Sometimes that alone would be sufficient to test your command if you are not testing the actual function of the command (like create the region, etc). But it would be nice to verify the behavior of the command using an integration test. If your command has complicated behavior with different VMs, a DUnit test would be needed. It's always a good idea to add an acceptance test for smoke test purposes.
When writing tests, please follow these guidelines:
- Use rules to share code between tests; DO NOT use abstract test classes.
- Each test class needs to be self contained, i.e., changing one test does not affect another one.
- Do NOT use DUnit/Integration tests to test option validation.
- We have tests that verified the behavior of all the functions in GfshCommand already; you do not need to test these functions in your command test again.