Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input validation #12

Merged
merged 2 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/com/rgsystem/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.rgsystem.input.CommandLineInputs;
import com.rgsystem.ui.CmdLineUI;
import com.rgsystem.ui.UI;
import com.rgsystem.validation.CMDInputValidations;
import com.rgsystem.validation.InputValidations;


public class Main {
Expand All @@ -20,7 +22,8 @@ public static void main(String[] args){
);

Database database = new SQLDatabase();
Inputs inputs = new CommandLineInputs(args);
InputValidations validations = new CMDInputValidations();
Inputs inputs = new CommandLineInputs(args, validations);
UI ui = new CmdLineUI();
ReportGeneratorApp app = new ReportGeneratorApp(connection, database, inputs, ui);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rgsystem.report;
package com.rgsystem.emails;

import java.io.File;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rgsystem.report;
package com.rgsystem.emails;

import java.io.File;

Expand Down
52 changes: 10 additions & 42 deletions src/main/java/com/rgsystem/input/CommandLineInputs.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package com.rgsystem.input;

import com.rgsystem.validation.InputValidations;

public class CommandLineInputs implements Inputs {

private final String[] args;
private final InputValidations validations;

public CommandLineInputs(String[] args) {
public CommandLineInputs(String[] args, InputValidations validations) {
this.args = args;
this.validations = validations;
}

@Override
public String getReportType() throws InvalidInputException {

String reportType = args[0];
//Make sure to validate the arguments before using...
if (reportType == null) {
throw new InvalidInputException("Please provide the report type as an argument");
}

if (!(reportType.equals("daily-sales") || reportType.equals("user-signups"))) {
throw new InvalidInputException
("Please provide daily-sales or user-signups as the report type for first argument");
}
validations.validateReportType(reportType);
return reportType;
}

Expand All @@ -30,14 +27,7 @@ public String getStartDate() throws InvalidInputException {
String startDate = args[1];

//Make sure to validate the arguments before using...
if (startDate == null) {
throw new InvalidInputException("Please provide the start date as the second argument");
}

/*if(!(isDateValid(startDate))){
throw new InvalidInputException("Please provide the start date in dd/mm/yyyy format");
} */

validations.validateDate(startDate);
return startDate;
}

Expand All @@ -47,14 +37,7 @@ public String getEndDate() throws InvalidInputException {
String endDate = args[2];

//Make sure to validate the arguments before using...
if (endDate == null) {
throw new InvalidInputException("Please provide the end date as the third argument");
}

/*if(!(isDateValid(endDate))){
throw new InvalidInputException("Please provide the end date in dd/mm/yyyy format");
}*/

validations.validateDate(endDate);
return endDate;
}

Expand All @@ -63,15 +46,7 @@ public String getOutputFormat() throws InvalidInputException {
String outputFormat = args[3];

//Make sure to validate the arguments before using...
if (outputFormat == null) {
throw new InvalidInputException("Please provide the report type as an argument");
}

if (!(outputFormat.equals("email") || outputFormat.equals("file"))) {
throw new InvalidInputException
("Please provide mail or file as the report output format for fourth argument");
}

validations.validateOutputFormat(outputFormat);
return outputFormat;
}

Expand All @@ -81,14 +56,7 @@ public String getUserEmail() throws InvalidInputException {
String userEmail = args[4];

//Make sure to validate the arguments before using...
if (userEmail == null) {
throw new InvalidInputException("Please provide the user email as the fifth argument");
}

/*if(!(isEmailValid(userEmail))){
throw new InvalidInputException("Please provide a valid user email as the fifth argument");
}*/

validations.validateEmail(userEmail);
return userEmail;

}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/rgsystem/output/outputs/EmailOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.rgsystem.output.ExcelFileOutput;
import com.rgsystem.output.Outputs;
import com.rgsystem.output.WorkBookWriter;
import com.rgsystem.report.EmailBody;
import com.rgsystem.report.EmailBodyGenerator;
import com.rgsystem.emails.EmailBody;
import com.rgsystem.emails.EmailBodyGenerator;
import com.rgsystem.report.Period;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/rgsystem/validation/CMDInputValidations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.rgsystem.validation;

import com.rgsystem.input.InvalidInputException;

public class CMDInputValidations implements InputValidations{

@Override
public void validateReportType(String reportType) throws InvalidInputException {

if (reportType == null) {
throw new InvalidInputException("Please provide the report type");

}else if (!(reportType.equals("daily-sales") || reportType.equals("user-signups"))){
throw new InvalidInputException
("Please provide daily-sales or user-signups as the report type for first argument");
}
}

@Override
public void validateDate(String date) throws InvalidInputException {

if (date == null) {
throw new InvalidInputException("Please provide the start and end dates");
}else if (!(date.matches("([0-9]{4})-([0-9]{2})-([0-9]{2})"))){
throw new InvalidInputException("Please provide the date in dd/mm/yyyy format");
}

}

@Override
public void validateOutputFormat(String outputFormat) throws InvalidInputException {
if (outputFormat == null) {
throw new InvalidInputException("Please provide the report type as an argument");
}else if(!(outputFormat.equals("email") || outputFormat.equals("file"))) {
throw new InvalidInputException
("Please provide mail or file as the report output format for fourth argument");
}
}

@Override
public void validateEmail(String email) throws InvalidInputException {

String regex = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";

if (email == null) {
throw new InvalidInputException("Please provide the user email as the fifth argument");
}else if(!(email.matches(regex))){
throw new InvalidInputException("Please provide a valid user email as the fifth argument");
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/rgsystem/validation/InputValidations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.rgsystem.validation;

import com.rgsystem.input.InvalidInputException;

public interface InputValidations {

void validateReportType(String reportType) throws InvalidInputException;
void validateDate(String date) throws InvalidInputException;
void validateOutputFormat(String outputFormat) throws InvalidInputException;
void validateEmail(String email) throws InvalidInputException;

}