diff --git a/pom.xml b/pom.xml index 31b4f4e..0936a54 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ mysql mysql-connector-java - 8.0.18 + 8.0.19 diff --git a/src/main/java/com/github/perscholas/DatabaseConnection.java b/src/main/java/com/github/perscholas/DatabaseConnection.java index 9e33f41..da9811d 100644 --- a/src/main/java/com/github/perscholas/DatabaseConnection.java +++ b/src/main/java/com/github/perscholas/DatabaseConnection.java @@ -5,6 +5,7 @@ import java.sql.Connection; import java.sql.ResultSet; +import java.sql.SQLException; /** * Created by leon on 2/18/2020. @@ -20,59 +21,102 @@ public enum DatabaseConnection implements DatabaseConnectionInterface { this.connectionBuilder = connectionBuilder; } + //Added database details to connect with Database DatabaseConnection() { this(new ConnectionBuilder() .setUser("root") - .setPassword("") + .setPassword("DakshaHello") .setPort(3306) - .setDatabaseVendor("mariadb") + .setDatabaseVendor("mysql") .setHost("127.0.0.1")); } @Override - public String getDatabaseName() { + public String getDatabaseName() + { return name().toLowerCase(); } @Override - public Connection getDatabaseConnection() { + public Connection getDatabaseConnection() + { return connectionBuilder .setDatabaseName(getDatabaseName()) .build(); } @Override - public Connection getDatabaseEngineConnection() { + public Connection getDatabaseEngineConnection() + { return connectionBuilder.build(); } @Override + //creating Database public void create() { - String sqlStatement = null; // TODO - define statement + String sqlStatement = "CREATE DATABASE " + name().toLowerCase(); String info; try { - // TODO - execute statement - info = "Successfully executed statement `%s`."; - } catch (Exception sqlException) { - info = "Failed to executed statement `%s`."; + executeStatement(sqlStatement); + info = "Successfully executed CREATE statement `%s`."; + } + catch (Exception sqlException) { + info = "Failed to execute CREATE statement `%s`."; } console.println(info, sqlStatement); } @Override + //Dropping Database if already exists public void drop() { + System.out.println("Database Name : " + name().toLowerCase()); + String sqlStatement = "DROP DATABASE IF EXISTS " +name().toLowerCase(); + String info; + try { + executeStatement(sqlStatement); + info = "Successfully executed DROP statement '%s'."; + } + catch (Exception sqlException){ + info = "Failed to execute DROP statement '%s'."; + } + console.println(info, sqlStatement); } @Override + //Use Main Schema public void use() { + String sqlStatement = "USE " + name().toLowerCase(); + String info; + try { + executeStatement(sqlStatement); + info = "Successfully executed USE statement '%s'."; + } + catch (Exception sqlException) { + info = "Failed to execute USE statement '%s'."; + } + console.println(info, sqlStatement); } @Override public void executeStatement(String sqlStatement) { + //Executing SQL Statement + try { + //console.println( sqlStatement); + getDatabaseEngineConnection().createStatement().execute(sqlStatement); + } + catch (SQLException se) { + throw new RuntimeException(se); + } } @Override public ResultSet executeQuery(String sqlQuery) { - return null; + //Executing SQL Query + try { + return getDatabaseConnection().createStatement().executeQuery(sqlQuery); + } + catch (SQLException se){ + throw new RuntimeException(se); + } } } \ No newline at end of file diff --git a/src/main/java/com/github/perscholas/JdbcConfigurator.java b/src/main/java/com/github/perscholas/JdbcConfigurator.java index cdee602..3af083e 100644 --- a/src/main/java/com/github/perscholas/JdbcConfigurator.java +++ b/src/main/java/com/github/perscholas/JdbcConfigurator.java @@ -2,28 +2,34 @@ import com.github.perscholas.utils.DirectoryReference; import com.github.perscholas.utils.FileReader; +import com.mysql.cj.jdbc.Driver; import java.io.File; +import java.sql.DriverManager; public class JdbcConfigurator { static { try { // TODO - Attempt to register JDBC Driver + DriverManager.registerDriver(Driver.class.newInstance()); } catch (Exception e) { throw new Error(e); } } - private static final DatabaseConnection dbc = DatabaseConnection.MANAGEMENT_SYSTEM; + private static final DatabaseConnection dc = DatabaseConnection.MANAGEMENT_SYSTEM; public static void initialize() { - dbc.drop(); - dbc.create(); - dbc.use(); + dc.drop(); + dc.create(); + dc.use(); executeSqlFile("courses.create-table.sql"); executeSqlFile("courses.populate-table.sql"); executeSqlFile("students.create-table.sql"); executeSqlFile("students.populate-table.sql"); + //created Student course table and added data into the table + executeSqlFile("student_course.create-table.sql"); + executeSqlFile("student_course.populate-table.sql"); } private static void executeSqlFile(String fileName) { @@ -32,7 +38,7 @@ private static void executeSqlFile(String fileName) { String[] statements = fileReader.toString().split(";"); for (int i = 0; i < statements.length; i++) { String statement = statements[i]; - dbc.executeStatement(statement); + dc.executeStatement(statement); } } } diff --git a/src/main/java/com/github/perscholas/SchoolManagementSystem.java b/src/main/java/com/github/perscholas/SchoolManagementSystem.java index 79b55a3..244a4ef 100644 --- a/src/main/java/com/github/perscholas/SchoolManagementSystem.java +++ b/src/main/java/com/github/perscholas/SchoolManagementSystem.java @@ -2,9 +2,12 @@ import com.github.perscholas.dao.StudentDao; import com.github.perscholas.model.CourseInterface; +import com.github.perscholas.service.CourseService; +import com.github.perscholas.service.StudentService; import com.github.perscholas.utils.IOConsole; import java.util.List; +import java.util.stream.Collectors; public class SchoolManagementSystem implements Runnable { private static final IOConsole console = new IOConsole(); @@ -15,24 +18,36 @@ public void run() { do { smsDashboardInput = getSchoolManagementSystemDashboardInput(); if ("login".equals(smsDashboardInput)) { - StudentDao studentService = null; // TODO - Instantiate `StudentDao` child + StudentDao studentService = new StudentService(); String studentEmail = console.getStringInput("Enter your email:"); String studentPassword = console.getStringInput("Enter your password:"); Boolean isValidLogin = studentService.validateStudent(studentEmail, studentPassword); + //Handled the invalid case if (isValidLogin) { - String studentDashboardInput = getStudentDashboardInput(); + String studentDashboardInput = getStudentDashboardInput(studentEmail); if ("register".equals(studentDashboardInput)) { Integer courseId = getCourseRegistryInput(); studentService.registerStudentToCourse(studentEmail, courseId); String studentCourseViewInput = getCourseViewInput(); + //Formatted the output based on the example workflow if ("view".equals(studentCourseViewInput)) { - List courses = null; // TODO - Instantiate and populate `courses`; + List courses = new StudentService().getStudentCourses(studentEmail) + .stream() + .map(course -> String.format("%-5s %-15s %-10s", course.getId().toString() , course.getName() , course.getInstructor().toString())) + .collect(Collectors.toList()); console.println(new StringBuilder() - .append("[ %s ] is registered to the following courses:") - .append("\n\t" + courses) + .append( studentEmail +" is registered to the following courses:") + .append("\n\t" + String.format("%-5s %-15s %-10s", "ID", "Course Name", "Instructor Name")) + .append("\n\t" + courses + .toString() + .replaceAll("\\[", "") + .replaceAll("\\]", "") + .replaceAll(", ", "\n\t")) .toString(), studentEmail); } } + } else { + console.println("Invalid Login Details, please try again: \n"); } } } while (!"logout".equals(smsDashboardInput)); @@ -54,20 +69,37 @@ private String getSchoolManagementSystemDashboardInput() { .toString()); } - private String getStudentDashboardInput() { + //Formatted the output based on the example workflow + private String getStudentDashboardInput(String studentEmail) { + List listOfStudentClass = new StudentService().getStudentCourses(studentEmail) + .stream() + .map(classes -> String.format("%-5s %-15s %-10s", classes.getId().toString() , classes.getName() , classes.getInstructor().toString())) + .collect(Collectors.toList()); return console.getStringInput(new StringBuilder() - .append("Welcome to the Course Registration Dashboard!") - .append("\nFrom here, you can select any of the following options:") + .append("My Classes: ") + .append("\n\t" + String.format("%-5s %-15s %-10s", "ID", "Course Name", "Instructor Name")) + .append("\n\t" + listOfStudentClass + .toString() + .replaceAll("\\[", "") + .replaceAll("\\]", "") + .replaceAll(", ", "\n\t")) + .append("\n\nWelcome to the Course Registration Dashboard!") + .append("\nFrom here, you can select any of the following options: ") .append("\n\t[ register ], [ logout]") .toString()); } - + //Formatted the output based on the example workflow private Integer getCourseRegistryInput() { - List listOfCoursesIds = null; // TODO - instantiate and populate `listOfCourseIds` + List listOfCoursesIds = new CourseService().getAllCourses() + .stream() + .map(course -> String.format("%-10s %-30s %-20s",course.getId().toString() , course.getName().toString() , course.getInstructor().toString())) + .collect(Collectors.toList()); return console.getIntegerInput(new StringBuilder() .append("Welcome to the Course Registration Dashboard!") .append("\nFrom here, you can select any of the following options:") + .append("\nAll Courses: \n") + .append(String.format("%-5s %-15s %-10s", "ID", "Course Name", "Instructor Name")) .append("\n\t" + listOfCoursesIds .toString() .replaceAll("\\[", "") diff --git a/src/main/java/com/github/perscholas/model/Course.java b/src/main/java/com/github/perscholas/model/Course.java index 930aece..d3bd31d 100644 --- a/src/main/java/com/github/perscholas/model/Course.java +++ b/src/main/java/com/github/perscholas/model/Course.java @@ -1,5 +1,68 @@ package com.github.perscholas.model; -// TODO - Annotate and Implement respective interface and define behaviors -public class Course { +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Objects; + + +@Entity +public class Course implements CourseInterface { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Integer id; + private String name; + private String instructor; + + @Override + public Integer getId() { + return id; + } + + @Override + public void setId(Integer id) { + this.id = id; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public String getInstructor() { + return instructor; + } + + @Override + public void setInstructor(String instructor) { + this.instructor = instructor; + } + + @Override + public String toString() { + return "Course{" + + "id=" + id + + ", name='" + name + '\'' + + ", instructor='" + instructor + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Course)) return false; + Course course = (Course) o; + return Objects.equals(id, course.id) && + Objects.equals(name, course.name) && + Objects.equals(instructor, course.instructor); + } + } diff --git a/src/main/java/com/github/perscholas/model/Student.java b/src/main/java/com/github/perscholas/model/Student.java index 94db67f..db30fc6 100644 --- a/src/main/java/com/github/perscholas/model/Student.java +++ b/src/main/java/com/github/perscholas/model/Student.java @@ -1,5 +1,79 @@ package com.github.perscholas.model; -// TODO - Annotate and Implement respective interface and define behaviors -public class Student { +import javax.persistence.Entity; +import javax.persistence.Id; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Entity +public class Student implements StudentInterface { + + private String email; + private String name; + private String password; + + private List studentCourses; + + public Student() { + studentCourses = new ArrayList<>(); + } + + @Override + public String getEmail() { + return email; + } + + @Override + public void setEmail(String email) { + this.email = email; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public void setPassword(String password) { + this.password = password; + } + + public List getStudentCourses() { + return studentCourses; + } + + public void setStudentCourses(List studentCourses) { + this.studentCourses = studentCourses; + } + + @Override + public String toString() { + return "Student{" + + "email='" + email + '\'' + + ", name='" + name + '\'' + + ", password='" + password + '\'' + + ", studentCourses=" + studentCourses + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Student student = (Student) o; + return Objects.equals(email, student.email) && + Objects.equals(name, student.name) && + Objects.equals(password, student.password); + } } diff --git a/src/main/java/com/github/perscholas/service/CourseService.java b/src/main/java/com/github/perscholas/service/CourseService.java index 86e33e2..8a4d2e8 100644 --- a/src/main/java/com/github/perscholas/service/CourseService.java +++ b/src/main/java/com/github/perscholas/service/CourseService.java @@ -1,5 +1,46 @@ package com.github.perscholas.service; +import com.github.perscholas.DatabaseConnection; +import com.github.perscholas.dao.CourseDao; +import com.github.perscholas.model.Course; +import com.github.perscholas.model.CourseInterface; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + // TODO - Implement respective DAO interface -public class CourseService { +public class CourseService implements CourseDao { + private DatabaseConnection dc; + + public CourseService(DatabaseConnection dc) { + this.dc = dc; + } + + public CourseService() { + this(DatabaseConnection.MANAGEMENT_SYSTEM); + } + + @Override + //This method will return all the courses available in course table + public List getAllCourses() { + List courses = new ArrayList<>(); + try { + String query = "SELECT * FROM course"; + ResultSet resultSet = dc.executeQuery(query); + + while(resultSet.next()) { + Course course = new Course(); + course.setId(resultSet.getInt("id")); + course.setName(resultSet.getString("name")); + course.setInstructor(resultSet.getString("instructor")); + courses.add(course); + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } + return courses; + } } diff --git a/src/main/java/com/github/perscholas/service/StudentService.java b/src/main/java/com/github/perscholas/service/StudentService.java index 0751a9c..97ae076 100644 --- a/src/main/java/com/github/perscholas/service/StudentService.java +++ b/src/main/java/com/github/perscholas/service/StudentService.java @@ -6,50 +6,109 @@ import com.github.perscholas.model.Student; import com.github.perscholas.model.StudentInterface; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Optional; // TODO - Implement respective DAO interface public class StudentService implements StudentDao { - private final DatabaseConnection dbc; + private final DatabaseConnection dc; - public StudentService(DatabaseConnection dbc) { - this.dbc = dbc; + public StudentService(DatabaseConnection dc) { + this.dc = dc; } public StudentService() { - this(DatabaseConnection.UAT); + this(DatabaseConnection.MANAGEMENT_SYSTEM); } @Override + //This method will return all students available in Students table public List getAllStudents() { - ResultSet resultSet = dbc.executeQuery("SELECT * FROM students"); + ResultSet resultSet = dc.executeQuery("SELECT * FROM student"); try { - return null; // TODO - Parse `List` from `resultSet` + List students = new ArrayList<>(); + while(resultSet.next()) { + Student student = new Student(); + student.setEmail(resultSet.getString("email")); + student.setName(resultSet.getString("name")); + student.setPassword(resultSet.getString("password")); + students.add(student); + } + return students; } catch(Exception e) { throw new Error(e); } } @Override + //This method will return all students available in Students table using email id public StudentInterface getStudentByEmail(String studentEmail) { - return null; + + return getAllStudents().stream() + .filter(student -> student.getEmail().equals(studentEmail)) + .findFirst() + .get(); } @Override + //This method will validate student using email and password public Boolean validateStudent(String studentEmail, String password) { - return null; + + return getAllStudents().stream() + .filter(student -> student.getEmail().equals(studentEmail) && student.getPassword().equals(password)) + .count() == 1 ? true : false; } @Override + //This method will register the course to Student public void registerStudentToCourse(String studentEmail, int courseId) { + List studentCourses = getStudentCourses(studentEmail); + Optional course = studentCourses.stream() + .filter(crs -> crs.getId() == courseId) + .findFirst(); + if(course.isPresent()) { + course.ifPresent(crs -> System.out.println("Already registered to this Course!!")); + return; + } + String query = "INSERT INTO Student_Course values (?, ?)"; + try { + PreparedStatement preparedStatement = dc.getDatabaseConnection().prepareStatement(query); + preparedStatement.setString(1, studentEmail); + preparedStatement.setInt(2, courseId); + preparedStatement.execute(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } @Override + //This method will return all courses mapped to Student public List getStudentCourses(String studentEmail) { - return null; + + List studentCourses = new ArrayList<>(); + String query = "SELECT * FROM Student_Course WHERE Student_email=?"; + try { + PreparedStatement preparedStatement = dc.getDatabaseConnection().prepareStatement(query); + preparedStatement.setString(1, studentEmail); + ResultSet resultSet = preparedStatement.executeQuery(); + + while(resultSet.next()) { + int courseId = resultSet.getInt("Course_id"); + studentCourses.add(new CourseService().getAllCourses() + .stream() + .filter(course -> course.getId() == courseId) + .findFirst() + .get()); + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } + return studentCourses; } } diff --git a/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java b/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java index 745e5a9..4b8edc2 100644 --- a/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java +++ b/src/main/java/com/github/perscholas/utils/ConnectionBuilder.java @@ -73,10 +73,11 @@ public String toString() { .append("://") .append(isHostNull ? "localhost" : "") .append(!isHostNull ? this.hostName : "") - .append(hasPortBeenSet ? ":" : "") - .append(hasPortBeenSet ? portNumber : "") + .append(!hasPortBeenSet ? ":" : "") + .append(!hasPortBeenSet ? portNumber : "") .append("/") .append(databaseName != null ? databaseName : "") + .append("?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC") .toString(); return jdbcUrl; } diff --git a/src/main/resources/courses.create-table.sql b/src/main/resources/courses.create-table.sql index e69de29..9574153 100644 --- a/src/main/resources/courses.create-table.sql +++ b/src/main/resources/courses.create-table.sql @@ -0,0 +1,6 @@ +CREATE TABLE management_system.Course ( +id int NOT NULL, +name varchar(50) NOT NULL, +instructor varchar(50) NOT NULL, +PRIMARY KEY (id) +); \ No newline at end of file diff --git a/src/main/resources/courses.populate-table.sql b/src/main/resources/courses.populate-table.sql index 0708967..a61b2e4 100644 --- a/src/main/resources/courses.populate-table.sql +++ b/src/main/resources/courses.populate-table.sql @@ -1,10 +1,10 @@ -insert into Course (id, name, instructor) values (1, 'English', 'Anderea Scamaden'); -insert into Course (id, name, instructor) values (2, 'Mathematics', 'Eustace Niemetz'); -insert into Course (id, name, instructor) values (3, 'Anatomy', 'Reynolds Pastor'); -insert into Course (id, name, instructor) values (4, 'Organic Chemistry', 'Odessa Belcher'); -insert into Course (id, name, instructor) values (5, 'Physics', 'Dani Swallow'); -insert into Course (id, name, instructor) values (6, 'Digital Logic', 'Glenden Reilingen'); -insert into Course (id, name, instructor) values (7, 'Object Oriented Programming','Giselle Ardy'); -insert into Course (id, name, instructor) values (8, 'Data Structures', 'Carolan Stoller'); -insert into Course (id, name, instructor) values (9, 'Politics', 'Carmita De Maine'); -insert into Course (id, name, instructor) values (10, 'Art', 'Kingsly Doxsey'); \ No newline at end of file +insert into management_system.Course (id, name, instructor) values (1, 'English', 'Anderea Scamaden'); +insert into management_system.Course (id, name, instructor) values (2, 'Mathematics', 'Eustace Niemetz'); +insert into management_system.Course (id, name, instructor) values (3, 'Anatomy', 'Reynolds Pastor'); +insert into management_system.Course (id, name, instructor) values (4, 'Organic Chemistry', 'Odessa Belcher'); +insert into management_system.Course (id, name, instructor) values (5, 'Physics', 'Dani Swallow'); +insert into management_system.Course (id, name, instructor) values (6, 'Digital Logic', 'Glenden Reilingen'); +insert into management_system.Course (id, name, instructor) values (7, 'Object Oriented Programming','Giselle Ardy'); +insert into management_system.Course (id, name, instructor) values (8, 'Data Structures', 'Carolan Stoller'); +insert into management_system.Course (id, name, instructor) values (9, 'Politics', 'Carmita De Maine'); +insert into management_system.Course (id, name, instructor) values (10, 'Art', 'Kingsly Doxsey'); \ No newline at end of file diff --git a/src/main/resources/student_course.create-table.sql b/src/main/resources/student_course.create-table.sql new file mode 100644 index 0000000..adc0886 --- /dev/null +++ b/src/main/resources/student_course.create-table.sql @@ -0,0 +1,7 @@ +CREATE TABLE management_system.Student_Course ( + Student_email varchar(50) NOT NULL, + Course_id int not null, + FOREIGN KEY (Student_email) REFERENCES Student(email), + FOREIGN KEY (Course_id) REFERENCES Course(id), + UNIQUE (Student_email, Course_id) +); \ No newline at end of file diff --git a/src/main/resources/student_course.populate-table.sql b/src/main/resources/student_course.populate-table.sql new file mode 100644 index 0000000..06f32bd --- /dev/null +++ b/src/main/resources/student_course.populate-table.sql @@ -0,0 +1,2 @@ +INSERT INTO management_system.Student_Course (student_email, course_id) values ('deepti.c@gmail.com', 1); +INSERT INTO management_system.Student_Course (student_email, course_id) values ('deepti.c@gmail.com', 2); \ No newline at end of file diff --git a/src/main/resources/students.create-table.sql b/src/main/resources/students.create-table.sql index e69de29..141f179 100644 --- a/src/main/resources/students.create-table.sql +++ b/src/main/resources/students.create-table.sql @@ -0,0 +1,6 @@ +CREATE TABLE management_system.Student ( + email varchar(50) NOT NULL, + name varchar(50) NOT NULL, + password varchar(50) NOT NULL, + PRIMARY KEY (email) +); \ No newline at end of file diff --git a/src/main/resources/students.populate-table.sql b/src/main/resources/students.populate-table.sql index e352727..759b765 100644 --- a/src/main/resources/students.populate-table.sql +++ b/src/main/resources/students.populate-table.sql @@ -1,10 +1,11 @@ -insert into Student (email, name, password) values ('hluckham0@google.ru', 'Hazel Luckham', 'X1uZcoIh0dj'); -insert into Student (email, name, password) values ('sbowden1@yellowbook.com', 'Sonnnie Bowden', 'SJc4aWSU'); -insert into Student (email, name, password) values ('qllorens2@howstuffworks.com', 'Quillan Llorens', 'W6rJuxd'); -insert into Student (email, name, password) values ('cstartin3@flickr.com', 'Clem Startin', 'XYHzJ1S'); -insert into Student (email, name, password) values ('tattwool4@biglobe.ne.jp', 'Thornie Attwool', 'Hjt0SoVmuBz'); -insert into Student (email, name, password) values ('hguerre5@deviantart.com', 'Harcourt Guerre', 'OzcxzD1PGs'); -insert into Student (email, name, password) values ('htaffley6@columbia.edu', 'Holmes Taffley', 'xowtOQ'); -insert into Student (email, name, password) values ('aiannitti7@is.gd', 'Alexandra Iannitti', 'TWP4hf5j'); -insert into Student (email, name, password) values ('ljiroudek8@sitemeter.com', 'Laryssa Jiroudek', 'bXRoLUP'); -insert into Student (email, name, password) values ('cjaulme9@bing.com', 'Cahra Jaulme', 'FnVklVgC6r6'); \ No newline at end of file +insert into management_system.Student (email, name, password) values ('hluckham0@google.ru', 'Hazel Luckham', 'X1uZcoIh0dj'); +insert into management_system.Student (email, name, password) values ('sbowden1@yellowbook.com', 'Sonnnie Bowden', 'SJc4aWSU'); +insert into management_system.Student (email, name, password) values ('qllorens2@howstuffworks.com', 'Quillan Llorens', 'W6rJuxd'); +insert into management_system.Student (email, name, password) values ('cstartin3@flickr.com', 'Clem Startin', 'XYHzJ1S'); +insert into management_system.Student (email, name, password) values ('tattwool4@biglobe.ne.jp', 'Thornie Attwool', 'Hjt0SoVmuBz'); +insert into management_system.Student (email, name, password) values ('hguerre5@deviantart.com', 'Harcourt Guerre', 'OzcxzD1PGs'); +insert into management_system.Student (email, name, password) values ('htaffley6@columbia.edu', 'Holmes Taffley', 'xowtOQ'); +insert into management_system.Student (email, name, password) values ('aiannitti7@is.gd', 'Alexandra Iannitti', 'TWP4hf5j'); +insert into management_system.Student (email, name, password) values ('ljiroudek8@sitemeter.com', 'Laryssa Jiroudek', 'bXRoLUP'); +insert into management_system.Student (email, name, password) values ('cjaulme9@bing.com', 'Cahra Jaulme', 'FnVklVgC6r6'); +insert into management_system.Student (email, name, password) values ('deepti.c@gmail.com', 'Deepti C', 'dak'); \ No newline at end of file diff --git a/src/test/java/com/github/perscholas/service/courseservice/GetAllCoursesTest.java b/src/test/java/com/github/perscholas/service/courseservice/GetAllCoursesTest.java index fdf4686..f461fa7 100644 --- a/src/test/java/com/github/perscholas/service/courseservice/GetAllCoursesTest.java +++ b/src/test/java/com/github/perscholas/service/courseservice/GetAllCoursesTest.java @@ -1,10 +1,20 @@ package com.github.perscholas.service.courseservice; +import com.github.perscholas.DatabaseConnection; +import com.github.perscholas.dao.CourseDao; +import com.github.perscholas.model.Course; +import com.github.perscholas.model.CourseInterface; +import com.github.perscholas.service.CourseService; import com.github.perscholas.JdbcConfigurator; import com.github.perscholas.utils.DirectoryReference; import org.junit.Before; - +import org.junit.Assert; +import org.junit.Test; import java.io.File; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; /** * @author leonhunter @@ -27,14 +37,29 @@ public void setup() { } // given - private void test() { + @Test + public void test() { JdbcConfigurator.initialize(); + CourseDao courseService = new CourseService(); // when - // TODO - define `when` clause - + List expectedCourses = courseService.getAllCourses(); // then - // TODO - define `then` clause + ResultSet resultSet = DatabaseConnection.MANAGEMENT_SYSTEM.executeQuery("SELECT * FROM course"); + List actualCourses = new ArrayList<>(); + + try { + while(resultSet.next()) { + Course course = new Course(); + course.setId(resultSet.getInt("id")); + course.setName(resultSet.getString("name")); + course.setInstructor(resultSet.getString("instructor")); + actualCourses.add(course); + } + } catch (SQLException ex) { + throw new RuntimeException(ex); + } + Assert.assertArrayEquals(expectedCourses.toArray(), actualCourses.toArray()); } } diff --git a/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java b/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java index f7e80b4..34b6472 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java +++ b/src/test/java/com/github/perscholas/service/studentservice/GetAllStudentsTest.java @@ -5,17 +5,22 @@ import com.github.perscholas.model.StudentInterface; import com.github.perscholas.service.StudentService; import com.github.perscholas.utils.DirectoryReference; +import com.github.perscholas.DatabaseConnection; +import com.github.perscholas.model.Student; import org.junit.Before; - +import org.junit.Test; +import org.junit.Assert; import java.io.File; import java.util.List; +import java.sql.ResultSet; +import java.util.ArrayList; /** * @author leonhunter * @created 02/12/2020 - 8:22 PM */ public class GetAllStudentsTest { - @Before // TODO (OPTIONAL) - Use files to execute SQL commands + @Before public void setup() { DirectoryReference directoryReference = DirectoryReference.RESOURCE_DIRECTORY; File coursesSchemaFile = directoryReference.getFileFromDirectory("courses.create-table.sql"); @@ -31,15 +36,28 @@ public void setup() { } // given - // TODO - Add `@Test` annotation + @Test public void test() { JdbcConfigurator.initialize(); StudentDao service = (StudentDao) new StudentService(); // when - List studentList = service.getAllStudents(); + List expectedstudentList = service.getAllStudents(); // then - // TODO - define _then_ clause + ResultSet resultSet = DatabaseConnection.MANAGEMENT_SYSTEM.executeQuery("SELECT * FROM student"); + List actualStudents = new ArrayList<>(); + try { + while(resultSet.next()) { + Student student = new Student(); + student.setEmail(resultSet.getString("email")); + student.setName(resultSet.getString("name")); + student.setPassword(resultSet.getString("password")); + actualStudents.add(student); + } + } catch(Exception ex) { + throw new Error(ex); + } + Assert.assertArrayEquals(expectedstudentList.toArray(), actualStudents.toArray()); } } diff --git a/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java b/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java index 3f0e7e4..ae01930 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java +++ b/src/test/java/com/github/perscholas/service/studentservice/GetStudentByEmailTest.java @@ -3,8 +3,13 @@ import com.github.perscholas.JdbcConfigurator; import com.github.perscholas.utils.DirectoryReference; import org.junit.Before; - +import com.github.perscholas.dao.StudentDao; +import com.github.perscholas.model.StudentInterface; +import com.github.perscholas.service.StudentService; import java.io.File; +import org.junit.Assert; +import org.junit.Test; +import java.util.List; /** * @author leonhunter @@ -27,15 +32,16 @@ public void setup() { } // given - // TODO - Add `@Test` annotation + @Test public void test() { JdbcConfigurator.initialize(); + StudentDao studentService = new StudentService(); - // when - // TODO - define `when` clause + // when + List expectedstudentList = studentService.getAllStudents(); // then - // TODO - define `then` clause + expectedstudentList.forEach(expectedStudent -> Assert.assertEquals(expectedStudent, studentService.getStudentByEmail(expectedStudent.getEmail()))); } } diff --git a/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java b/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java index a037077..d777f69 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java +++ b/src/test/java/com/github/perscholas/service/studentservice/GetStudentCoursesTest.java @@ -3,8 +3,17 @@ import com.github.perscholas.JdbcConfigurator; import com.github.perscholas.utils.DirectoryReference; import org.junit.Before; - import java.io.File; +import com.github.perscholas.dao.CourseDao; +import com.github.perscholas.dao.StudentDao; +import com.github.perscholas.model.Course; +import com.github.perscholas.model.CourseInterface; +import com.github.perscholas.model.StudentInterface; +import com.github.perscholas.service.CourseService; +import com.github.perscholas.service.StudentService; +import org.junit.Test; +import org.junit.Assert; +import java.util.*; /** * @author leonhunter @@ -27,15 +36,16 @@ public void setup() { } // given - // TODO - Add `@Test` annotation + @Test public void test() { JdbcConfigurator.initialize(); + StudentDao studentService = new StudentService(); // when - // TODO - define `when` clause - + List courses = studentService.getStudentCourses("deepti.c@gmail.com"); // then - // TODO - define `then` clause + Assert.assertEquals(courses.get(0).getName(),"English"); + Assert.assertEquals(courses.get(1).getName(),"Mathematics"); } } diff --git a/src/test/java/com/github/perscholas/service/studentservice/RegisterStudentToCourseTest.java b/src/test/java/com/github/perscholas/service/studentservice/RegisterStudentToCourseTest.java index 559170e..af67458 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/RegisterStudentToCourseTest.java +++ b/src/test/java/com/github/perscholas/service/studentservice/RegisterStudentToCourseTest.java @@ -3,6 +3,10 @@ import com.github.perscholas.JdbcConfigurator; import com.github.perscholas.utils.DirectoryReference; import org.junit.Before; +import com.github.perscholas.dao.StudentDao; +import com.github.perscholas.service.StudentService; +import org.junit.Assert; +import org.junit.Test; import java.io.File; @@ -27,15 +31,15 @@ public void setup() { } // given - // TODO - Add `@Test` annotation + @Test public void test() { JdbcConfigurator.initialize(); // when - // TODO - define `when` clause - + StudentDao service = new StudentService(); + service.registerStudentToCourse("deepti.c@gmail.com",5); // then - // TODO - define `then` clause + Assert.assertEquals(service.getStudentCourses("deepti.c@gmail.com").get(2).getName(),"Physics"); } } diff --git a/src/test/java/com/github/perscholas/service/studentservice/TestConstructor.java b/src/test/java/com/github/perscholas/service/studentservice/TestConstructor.java index bb34242..30e24fc 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/TestConstructor.java +++ b/src/test/java/com/github/perscholas/service/studentservice/TestConstructor.java @@ -1,8 +1,30 @@ package com.github.perscholas.service.studentservice; +import com.github.perscholas.model.Course; +import com.github.perscholas.model.CourseInterface; +import com.github.perscholas.model.Student; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; /** * @author leonhunter * @created 02/12/2020 - 8:47 PM - */ // TODO - Define tests + */ + + public class TestConstructor { + + @Test + public void courseConstructorTest() { + Course course = new Course(); + Assert.assertTrue(course instanceof CourseInterface); + } + + @Test + public void studentConstructorTest() { + Student student = new Student(); + Assert.assertTrue(student instanceof Student); + Assert.assertNotNull(student.getStudentCourses()); + } } diff --git a/src/test/java/com/github/perscholas/service/studentservice/ValidateStudentTest.java b/src/test/java/com/github/perscholas/service/studentservice/ValidateStudentTest.java index 831528e..9afdbfc 100644 --- a/src/test/java/com/github/perscholas/service/studentservice/ValidateStudentTest.java +++ b/src/test/java/com/github/perscholas/service/studentservice/ValidateStudentTest.java @@ -1,8 +1,23 @@ package com.github.perscholas.service.studentservice; +import com.github.perscholas.dao.StudentDao; +import com.github.perscholas.model.StudentInterface; +import com.github.perscholas.service.StudentService; +import org.junit.Assert; +import org.junit.Test; + + /** * @author leonhunter * @created 02/12/2020 - 8:24 PM */ // TODO - Define tests public class ValidateStudentTest { + @Test + public void testValidateStudent(){ + //when + StudentDao service = new StudentService(); + //then + Assert.assertFalse(service.validateStudent("deepti.c@gmail.com","")); + Assert.assertTrue(service.validateStudent("deepti.c@gmail.com","dak")); + } }