Simulating a Cronjob with .Net Core and xUnit.
The solution contains a console application and a unit test project.
After running the app, it will be required for the user to enter a cron string composed by 5 fields separated by empty spaces: minute, hour, day of month, month and day of week. The output will display values as numbers for each field:
- minute: 1 to 60.
- hour: 0 to 23.
- day of month: 1 to 31.
- month: 1 to 12.
- day of week: 0 to 6.
Day of week is the only field in which you must enter characters instead of numbers. Its options are: SUN, MON, TUE, WED, THU, FRI and SAT.
The characters "?" and "*" will display all the fields' values.
The character "-" is a range between two values. For example, 2-5 will display: 2 3 4 5.
The character "," can be used between multiple entries from any field to bring their respective values to output. For example: SUN,WED,FRI would display: 0 3 5.
The console application contains this structure:
Inside Program.cs, the CronJob class will be instatiated after the user enter the cron string:
The Cronjob's CheckLine method is responsible for checking the user's input. The first validation occurs at the number of required fields. Then each field receives inner validations from the GetValue method:
Each field has its own validation class:
All of them implements the BaseValidation class, whose GeneralValidation method is responsible for validating the special characters and treating the output accordingly.
Checking "?" and "*":
Checking "-":
Checking ",":
If none of them are used, the field's respective callback function will be executed. For example:
When running the app the positive output will be displayed this way:
And a negative output will be like this:
The xUnit project contains this structure:
The TheoryTest.cs will test multiple entries and expect for specific warnings to return. For example:
Each warning has its own error code to be identified during unit tests.
LineTest method will wait for the warning containing "000" because it belongs to a validation inside Cronjob.cs.
The GetOutput method is the one responsible for checking the desired output:
The FactTest.cs will test for positive results by sending fixed values and expecting all validations to be flawless:
The Visual Studio 2019's Test Manager will show the unit tests this way after running successfully:
You can see the the output of any unit test, both Fact or Theory:
This is an example of a Fact after a successful execution:
And this one is a Theory example with its desired warning: