From c4f5cc9582546bf1f26788346d6d725eeafcb25b Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 29 Oct 2023 14:13:12 +0200 Subject: [PATCH] Add a commmand validator. --- .../cli/validators/CommandValidator.java | 22 +++++++++++++++ .../validators/CommandValidatorSpec.groovy | 27 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/space/kklochko/jdbc_hospital_example/cli/validators/CommandValidator.java create mode 100644 src/test/groovy/space/kklochko/jdbc_hospital_example/cli/validators/CommandValidatorSpec.groovy diff --git a/src/main/java/space/kklochko/jdbc_hospital_example/cli/validators/CommandValidator.java b/src/main/java/space/kklochko/jdbc_hospital_example/cli/validators/CommandValidator.java new file mode 100644 index 0000000..0e91f4b --- /dev/null +++ b/src/main/java/space/kklochko/jdbc_hospital_example/cli/validators/CommandValidator.java @@ -0,0 +1,22 @@ +package space.kklochko.jdbc_hospital_example.cli.validators; + +import java.util.regex.Pattern; + +public class CommandValidator extends Validator { + String command; + + String formatRegEx = "(readAll|insert|update|remove)\\s(indicator|patient|department)\\(.*\\)"; + + public CommandValidator(String command) { + this.command = command; + } + + public boolean isValid() { + Pattern pattern = Pattern.compile(formatRegEx); + return pattern.matcher(command).matches(); + } + + public String getMessage() { + return "Please, check the format: `command item(arg1=value1, ...)`. Or check the `help`"; + } +} diff --git a/src/test/groovy/space/kklochko/jdbc_hospital_example/cli/validators/CommandValidatorSpec.groovy b/src/test/groovy/space/kklochko/jdbc_hospital_example/cli/validators/CommandValidatorSpec.groovy new file mode 100644 index 0000000..2a2867d --- /dev/null +++ b/src/test/groovy/space/kklochko/jdbc_hospital_example/cli/validators/CommandValidatorSpec.groovy @@ -0,0 +1,27 @@ +package space.kklochko.jdbc_hospital_example.cli.validators + +import spock.lang.Specification +import spock.lang.Subject + +class CommandValidatorSpec extends Specification { + def "Parser parse the commands"() { + given: "I have the validator" + @Subject + def validator = new CommandValidator(command) + + when: "check if valid" + boolean status = validator.isValid() + + then: "the result must be as expected" + status == expectedStatus + + where: + command || expectedStatus + "insert indicator(id='1', type='blood', values='Good samples.', timestamp='2023-10-17 14:30:00')" || true + "insert indicator(id='1', type='blood, blood', values='Good samples.', timestamp='2023-10-17 14:30:00')" || true + "update indicator(id='1', type='blood, blood', values='Good samples.', timestamp='2023-10-17 14:30:00')" || true + "remove indicator(id='1', type='blood, blood', values='Good samples.', timestamp='2023-10-17 14:30:00')" || true + "readAll indicator()" || true + } +} +