Remove the CLI.

main
KKlochko 1 year ago
parent af59bccc31
commit 4feca1319e

@ -1,136 +0,0 @@
package space.kklochko.jpa_hospital_example.cli;
import jakarta.persistence.EntityManager;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
import space.kklochko.jpa_hospital_example.cli.commands.CommandEntities;
import space.kklochko.jpa_hospital_example.cli.commands.CommandEntity;
import space.kklochko.jpa_hospital_example.cli.commands.factories.CommandFactory;
import space.kklochko.jpa_hospital_example.cli.commands.factories.ReadAllFactory;
import space.kklochko.jpa_hospital_example.cli.parsers.CommandParser;
import space.kklochko.jpa_hospital_example.cli.validators.CommandValidator;
import space.kklochko.jpa_hospital_example.cli.validators.InputStringFormatValidator;
import space.kklochko.jpa_hospital_example.cli.validators.Validator;
import space.kklochko.jpa_hospital_example.db.factories.DataBaseConnection;
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
import space.kklochko.jpa_hospital_example.db.repositories.DepartmentRepository;
import space.kklochko.jpa_hospital_example.db.repositories.IndicatorRepository;
import space.kklochko.jpa_hospital_example.db.repositories.PatientRepository;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
@Setter
@Getter
public class CLI {
private final String prompt = "> ";
private String state = "prompt";
private final String help = "Help (help or h)\n" +
"Command format: `command {item}(arg1=value1, ...`\n\n" +
"Commands: \n" +
"insert {item}({args}): \n" +
"update {item}({args}): \n" +
"readAll {item}: \n" +
"delete {item}(uuid='07c6e39e-727a-11ee-8e7b-c0e4349366ab'}).\n" +
"Examples:\n" +
"\nDepartment:\n" +
"\tinsert department(id='bcbbcdb4-702c-11ee-9113-c0e4349366cb', name='First department', location='Stepan Bandera Street', phone='380123451244')\n" +
"\tinsert department(name='First department', location='Stepan Bandera Street', phone='380123451244')\n" +
"\tupdate department(id='bcbbcdb4-702c-11ee-9113-c0e4349366cb', name='First department', location='Stepan Bandera Street', phone='380123451244')\n" +
"\treadAll department()\n" +
"\tremove department(id='bcbbcdb4-702c-11ee-9113-c0e4349366cb')\n" +
"\nPatient:\n" +
"\tinsert patient(id='bcbbcdb4-702c-11ee-9113-c0e4349366bb', name='Oleh', age='21', phone='380123451234')\n" +
"\tinsert patient(name='Oleh', age='21', phone='380123451234')\n" +
"\tupdate patient(id='bcbbcdb4-702c-11ee-9113-c0e4349366bb', name='Oleh', age='21', phone='380123451234')\n" +
"\treadAll patient()\n" +
"\tremove patient(id='bcbbcdb4-702c-11ee-9113-c0e4349366bb')\n" +
"\nIndicators:\n" +
"\tinsert indicator(id='07c6e39e-727a-11ee-8e7b-c0e4349366ab', type='blood245', values='well maybe', timestamp='2023-10-17 14:30:00')\n" +
"\tinsert indicator(type='blood246', values='well maybe', timestamp='2023-10-17 14:30:00')\n" +
"\tupdate indicator(id='07c6e39e-727a-11ee-8e7b-c0e4349366ab', type='blood247', values='well maybe', timestamp='2023-10-17 14:30:00')\n" +
"\treadAll indicator()\n" +
"\tremove indicator(id='07c6e39e-727a-11ee-8e7b-c0e4349366ab')\n";
EntityManager manager;
public CLI(EntityManager manager) {
this.manager = manager;
}
public void run() {
Scanner reader = new Scanner(System.in);
String line = "";
CommandData commandData;
while(true) {
if(getState() == "prompt") {
System.out.print(prompt);
setState("waitInput");
}else if(getState() == "waitInput") {
line = reader.nextLine();
setState("isHelp?");
}else if(getState() == "isHelp?") {
if(line.equals("h") || line.equals("help")) {
System.out.println(help);
setState("prompt");
}else {
setState("validate");
}
} else if(getState() == "validate") {
ArrayList<Validator> validators = new ArrayList<>();
validators.add(new CommandValidator(line));
validators.add(new InputStringFormatValidator(line));
setState("run");
for (Validator validator : validators) {
if(!validator.isValid()) {
System.err.println(validator.getMessage());
System.out.println();
setState("prompt");
}
}
} else if(getState() == "run") {
CommandParser commandParser = new CommandParser();
commandData = commandParser.parse(line);
runCommand(commandData);
setState("prompt");
}
}
}
public void runCommand(CommandData commandData) {
if(commandData.getName().equals("readAll")) {
ReadAllFactory factory = new ReadAllFactory();
CommandEntities command = factory.create(commandData, getRepository(commandData));
ArrayList items = command.run();
for(Iterator iter = command.run().iterator(); iter.hasNext(); ) {
System.out.println(iter.next());
}
} else {
CommandFactory factory = new CommandFactory();
CommandEntity command = factory.create(commandData, getRepository(commandData));
command.run();
}
}
public AbstractRepository getRepository(@NonNull CommandData commandData) {
if(commandData.getDatatype().equals("indicator")) {
return new IndicatorRepository(manager);
}else if(commandData.getDatatype().equals("patient")) {
return new PatientRepository(manager);
}else {
return new DepartmentRepository(manager);
}
}
}

@ -1,14 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.commands;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Map;
@AllArgsConstructor
@Data
public class CommandData {
public String name;
public String datatype;
public Map<String, String> arguments;
}

@ -1,28 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.commands;
import lombok.AllArgsConstructor;
import lombok.Data;
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@AllArgsConstructor
@Data
public class CommandEntities<T> {
private AbstractRepository<T> repository;
private Method method;
public ArrayList<T> run() {
try {
return (ArrayList<T>) method.invoke(getRepository());
} catch (InvocationTargetException e) {
System.err.println(e.getMessage());
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
System.err.println(e.getMessage());
throw new RuntimeException(e);
}
};
}

@ -1,32 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.commands;
import lombok.Data;
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Data
public class CommandEntity <A, T> {
private AbstractRepository<T> repository;
private Method method;
private A argument;
public CommandEntity(AbstractRepository<T> repository, Method method, A argument) {
this.repository = repository;
this.method = method;
this.argument = argument;
}
public boolean run() {
try {
return (Boolean) method.invoke(getRepository(), getArgument());
} catch (InvocationTargetException e) {
System.err.println(e.getMessage());
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
System.err.println(e.getMessage());
throw new RuntimeException(e);
}
};
}

@ -1,58 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.commands.factories;
import lombok.NonNull;
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
import space.kklochko.jpa_hospital_example.cli.commands.CommandEntity;
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
import space.kklochko.jpa_hospital_example.models.Department;
import space.kklochko.jpa_hospital_example.models.Indicator;
import space.kklochko.jpa_hospital_example.models.Patient;
import java.lang.reflect.Method;
import java.util.UUID;
public class CommandFactory {
public <T> CommandEntity create(@NonNull CommandData commandData, @NonNull AbstractRepository<T> repository) {
try {
EntityFactory entityFactory = new EntityFactory();
if(commandData.getName().equals("insert")) {
if(commandData.getDatatype().equals("indicator")) {
Method method = repository.getClass().getMethod("create", Indicator.class);
Indicator indicator = (Indicator) entityFactory.create(commandData);
return new CommandEntity<Indicator, T>(repository, method, indicator);
}else if(commandData.getDatatype().equals("patient")) {
Method method = repository.getClass().getMethod("create", Patient.class);
Patient patient = (Patient) entityFactory.create(commandData);
return new CommandEntity<Patient, T>(repository, method, patient);
}else {
Method method = repository.getClass().getMethod("create", Department.class);
Department department = (Department) entityFactory.create(commandData);
return new CommandEntity<Department, T>(repository, method, department);
}
}else if(commandData.getName().equals("update")) {
if(commandData.getDatatype().equals("indicator")) {
Method method = repository.getClass().getMethod("update", Indicator.class);
Indicator indicator = (Indicator) entityFactory.create(commandData);
return new CommandEntity<Indicator, T>(repository, method, indicator);
}else if(commandData.getDatatype().equals("patient")) {
Method method = repository.getClass().getMethod("update", Patient.class);
Patient patient = (Patient) entityFactory.create(commandData);
return new CommandEntity<Patient, T>(repository, method, patient);
}else {
Method method = repository.getClass().getMethod("update", Department.class);
Department department = (Department) entityFactory.create(commandData);
return new CommandEntity<Department, T>(repository, method, department);
}
}else {
String uuidString = commandData.arguments.get("id");
UUID uuid = UUID.fromString(uuidString);
Method method = repository.getClass().getMethod("delete", UUID.class);
return new CommandEntity<UUID, T>(repository, method, uuid);
}
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}

@ -1,52 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.commands.factories;
import lombok.NonNull;
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
import space.kklochko.jpa_hospital_example.models.Department;
import space.kklochko.jpa_hospital_example.models.Indicator;
import space.kklochko.jpa_hospital_example.models.Patient;
import java.sql.Timestamp;
import java.util.Map;
import java.util.UUID;
public class EntityFactory {
public Object create(@NonNull CommandData commandData) {
if(commandData.getDatatype().equals("indicator")) {
Map<String, String> values = commandData.getArguments();
Timestamp timestamp = Timestamp.valueOf(values.get("timestamp"));
Indicator indicator = new Indicator(values.get("type"), values.get("values"), timestamp);
String uuidString = values.getOrDefault("id", null);
if(uuidString != null) {
UUID uuid = UUID.fromString(uuidString);
indicator.setId(uuid);
}
return (Object) indicator;
}else if(commandData.getDatatype().equals("patient")) {
Map<String, String> values = commandData.getArguments();
short age = Short.parseShort(values.get("age"));
Patient patient = new Patient(values.get("name"), age, values.get("phone"));
String uuidString = values.getOrDefault("id", null);
if(uuidString != null) {
UUID uuid = UUID.fromString(uuidString);
patient.setId(uuid);
}
return (Object) patient;
}else {
Map<String, String> values = commandData.getArguments();
Department department = new Department(values.get("name"), values.get("location"), values.get("phone"));
String uuidString = values.getOrDefault("id", null);
if(uuidString != null) {
UUID uuid = UUID.fromString(uuidString);
department.setId(uuid);
}
return (Object) department;
}
}
}

@ -1,19 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.commands.factories;
import lombok.NonNull;
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
import space.kklochko.jpa_hospital_example.cli.commands.CommandEntities;
import space.kklochko.jpa_hospital_example.db.repositories.AbstractRepository;
import java.lang.reflect.Method;
public class ReadAllFactory {
public <T> CommandEntities<T> create(@NonNull CommandData commandData, @NonNull AbstractRepository<T> repository) {
try {
Method method = repository.getClass().getMethod("readAll");
return new CommandEntities<T>(repository, method);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}

@ -1,30 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.parsers;
import lombok.NonNull;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class ArgumentParser {
public Map<String, String> parse(@NonNull String args) {
Map<String, String> parsed = new LinkedHashMap<String, String>();
if(args == "") return parsed;
// remove last '
args = args.substring(0, args.length() - 1);
for(String arg : args.split("',")) {
String[] parts = arg.split("=");
parsed.put(parts[0].trim(), removeQuotes(parts[1]));
}
return parsed;
}
public String removeQuotes(@NonNull String withQuotes) {
return withQuotes.substring(1, withQuotes.length());
}
}

@ -1,20 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.parsers;
import lombok.NonNull;
import lombok.ToString;
import space.kklochko.jpa_hospital_example.cli.commands.CommandData;
@ToString
public class CommandParser {
public CommandData parse(@NonNull String command) {
ArgumentParser parser = new ArgumentParser();
String[] command_args = command.split("[()]");
String[] command_parts = command_args[0].split(" ");
String args="";
if(command_args.length > 1)
args = command_args[1];
return new CommandData(command_parts[0], command_parts[1], parser.parse(args));
}
}

@ -1,22 +0,0 @@
package space.kklochko.jpa_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`";
}
}

@ -1,21 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.validators;
import java.util.regex.Pattern;
public class InputStringFormatValidator extends Validator {
String command;
String formatRegEx = "\\w+\\s\\w+\\([^)]*\\)";
public InputStringFormatValidator(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`";
}
}

@ -1,9 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.validators;
import java.util.regex.Pattern;
abstract public class Validator {
abstract public boolean isValid();
abstract public String getMessage();
}

@ -1,163 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.commands
import groovy.sql.Sql
import jakarta.persistence.EntityManager
import space.kklochko.jpa_hospital_example.cli.commands.factories.CommandFactory
import space.kklochko.jpa_hospital_example.config.factories.LoadDataBaseConfigFromEnvFile
import space.kklochko.jpa_hospital_example.config.models.DataBaseConfig
import space.kklochko.jpa_hospital_example.db.factories.DataBaseConnection
import space.kklochko.jpa_hospital_example.db.factories.EntityManagerConnection
import space.kklochko.jpa_hospital_example.db.repositories.DepartmentRepository
import space.kklochko.jpa_hospital_example.db.repositories.IndicatorRepository
import space.kklochko.jpa_hospital_example.db.repositories.PatientRepository
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Stepwise
import spock.lang.Subject
@Stepwise
class CommandEntitySpec extends Specification {
@Shared
Sql sql
@Shared
EntityManager manager
@Shared
DataBaseConfig db
@Shared
DataBaseConnection dbc
def setupSpec() {
db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing")
dbc = new DataBaseConnection(db)
manager = (new EntityManagerConnection("testing")).connect()
sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password"))
}
def "Test insert commands"() {
given: "I have a command data"
CommandData data = new CommandData("insert", datatype, values)
UUID id = UUID.fromString(data.arguments.get("id"))
and: "I have factory for the command"
CommandFactory factory = new CommandFactory()
@Subject
CommandEntity command = factory.create(data, repository)
when: "The command is executed"
def result = command.run()
then: "checking that the insert was successful"
result
id
and: "checking that the entry is added"
def isExist = sql.firstRow(verifyStatement, id)
isExist
cleanup: "remove the data"
sql.execute("delete from " + datatype + "s")
where:
datatype | values | repository || verifyStatement
"indicator" | ["id": "3b3f62f4-75b4-11ee-99cb-c0e4349366ad",
"type": "blood, blood",
"values": "Good samples.",
"timestamp": "2023-10-17 14:30:00"] | new IndicatorRepository(manager) || "SELECT type FROM indicators WHERE id = ?"
"patient" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366bd",
"name": "Oleh",
"age": "21",
"phone": "380123451234"] | new PatientRepository(manager) || "SELECT name FROM patients WHERE id = ?"
"department" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366cd",
"name": "First department",
"location": "Stepan Bandera Street",
"phone": "380123451244"] | new DepartmentRepository(manager) || "SELECT name FROM departments WHERE id = ?"
}
def "Test update commands"() {
given: "I have a command data"
CommandData data = new CommandData("update", datatype, values)
UUID id = UUID.fromString(data.arguments.get("id"))
and: "I have a fixture"
sql.execute(fixture)
and: "I have factory for the command"
CommandFactory factory = new CommandFactory()
@Subject
CommandEntity command = factory.create(data, repository)
when: "The command is executed"
def result = command.run()
then: "checking that the insert was successful"
result
id
and: "checking that the entry is added"
def isExist = sql.firstRow(verifyStatement, id)
isExist
cleanup: "remove the data"
sql.execute("delete from " + datatype + "s")
where:
datatype | values | fixture | repository || verifyStatement
"indicator" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366ab",
"type": "blood, blood",
"values": "Good samples.",
"timestamp": "2023-10-17 14:30:00"] | "insert into indicators (id, type, values, timestamp) values ('bcbbcdb4-702c-11ee-9113-c0e4349366ab', 'Samples', 'fine', '2023-10-17 16:30:00')"
| new IndicatorRepository(manager) || "SELECT type FROM indicators WHERE id = ? AND type='blood, blood'"
"patient" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366bb",
"name": "Oleh",
"age": "21",
"phone": "380123451234"] | "insert into patients (id, name, age, phone) values ('bcbbcdb4-702c-11ee-9113-c0e4349366bb', 'Maybe Oleh', 22, '380123451234')"
| new PatientRepository(manager) || "SELECT name FROM patients WHERE id = ? AND name='Oleh'"
"department" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366cb",
"name": "First department",
"location": "Stepan Bandera Street",
"phone": "380123451244"] | "insert into departments (id, name, location, phone) values ('bcbbcdb4-702c-11ee-9113-c0e4349366cb', 'Second department', 'Stepan Bandera Street', '380123451244')"
| new DepartmentRepository(manager) || "SELECT name FROM departments WHERE id = ? AND name='First department'"
}
def "Test delete commands"() {
given: "I have a command data"
CommandData data = new CommandData("remove", datatype, values)
UUID id = UUID.fromString(data.arguments.get("id"))
and: "I have a fixture"
sql.execute(fixture)
and: "I have factory for the command"
CommandFactory factory = new CommandFactory()
@Subject
CommandEntity command = factory.create(data, repository)
when: "The command is executed"
def result = command.run()
then: "checking that the insert was successful"
result
id
and: "checking that the entry is added"
def isExist = sql.firstRow(verifyStatement, id)
!isExist
cleanup: "remove the data"
sql.execute("delete from " + datatype + "s")
where:
datatype | values | fixture | repository || verifyStatement
"indicator" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366ab"] | "insert into indicators (id, type, values, timestamp) values ('bcbbcdb4-702c-11ee-9113-c0e4349366ab', 'Samples', 'fine', '2023-10-17 16:30:00')"
| new IndicatorRepository(manager) || "SELECT type FROM indicators WHERE id = ?"
"patient" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366bb"] | "insert into patients (id, name, age, phone) values ('bcbbcdb4-702c-11ee-9113-c0e4349366bb', 'Maybe Oleh', 22, '380123451234')"
| new PatientRepository(manager) || "SELECT name FROM patients WHERE id = ?"
"department" | ["id": "bcbbcdb4-702c-11ee-9113-c0e4349366cb"] | "insert into departments (id, name, location, phone) values ('bcbbcdb4-702c-11ee-9113-c0e4349366cb', 'Second department', 'Stepan Bandera Street', '380123451244')"
| new DepartmentRepository(manager) || "SELECT name FROM departments WHERE id = ?"
}
}

@ -1,27 +0,0 @@
package space.kklochko.jpa_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
}
}

@ -1,28 +0,0 @@
package space.kklochko.jpa_hospital_example.cli.validators
import space.kklochko.jpa_hospital_example.cli.commands.CommandData
import space.kklochko.jpa_hospital_example.cli.parsers.CommandParser
import spock.lang.Specification
import spock.lang.Subject
class InputStringFormatValidatorSpec extends Specification {
def "Parser parse the commands"() {
given: "I have the validator"
@Subject
def validator = new InputStringFormatValidator(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') " || false
"insert indicator(id='1', type='blood, blood', values='Good samples.', timestamp='2023-10-17 14:30:00')" || true
"readAll indicator()" || true
" readAll indicator() " || false
}
}
Loading…
Cancel
Save