diff --git a/src/main/java/io/zipcoder/persistenceapp/controllers/DepartmentController.java b/src/main/java/io/zipcoder/persistenceapp/controllers/DepartmentController.java new file mode 100644 index 0000000..523a5f5 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/controllers/DepartmentController.java @@ -0,0 +1,52 @@ +package io.zipcoder.persistenceapp.controllers; + +import io.zipcoder.persistenceapp.entities.Department; +import io.zipcoder.persistenceapp.services.DepartmentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/department/") +public class DepartmentController { + + DepartmentService departmentService; + + @Autowired + public DepartmentController(DepartmentService departmentService){ + this.departmentService = departmentService; + } + + @GetMapping + public ResponseEntity> getAllDepartments(){ + return new ResponseEntity<>(departmentService.getAllDepartments(), HttpStatus.OK); + } + + @GetMapping("{id}/name") + public String getDepartmentName (@PathVariable Long id){ + return departmentService.getDepartmentName(id); + } + + @GetMapping("{id}") + public ResponseEntity getDepartmentById(@PathVariable Long id){ + return new ResponseEntity(departmentService.getDepartmentById(id),HttpStatus.OK); + } + + @PutMapping("{id}") + public ResponseEntity updateDepartmentManager(@RequestBody Department newData, @PathVariable Long id){ + return new ResponseEntity(departmentService.updateDepartmentManager(newData, id),HttpStatus.OK); + } + + @PutMapping("{id}/name") + public ResponseEntity updateDepartmentName(@RequestBody Department newData, @PathVariable Long id){ + return new ResponseEntity(departmentService.updateDepartmentName(newData, id),HttpStatus.OK); + } + + @PostMapping + public ResponseEntity createDepartment(@RequestBody Department newDepartment){ + return new ResponseEntity(departmentService.createDepartment(newDepartment),HttpStatus.CREATED); + } + + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/controllers/EmployeeController.java b/src/main/java/io/zipcoder/persistenceapp/controllers/EmployeeController.java new file mode 100644 index 0000000..94fe571 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/controllers/EmployeeController.java @@ -0,0 +1,80 @@ +package io.zipcoder.persistenceapp.controllers; + +import io.zipcoder.persistenceapp.entities.Employee; +import io.zipcoder.persistenceapp.services.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +@RestController +@RequestMapping("/api/employee/") +public class EmployeeController { + + EmployeeService employeeService; + + @Autowired + public EmployeeController(EmployeeService employeeService) { + this.employeeService = employeeService; + } + + @GetMapping("allundermanager/{id}") + public ResponseEntity> getAllEmployeesUnderManager(@PathVariable Long id) { + return new ResponseEntity<>(employeeService.getAllEmployeesUnderManager(id), HttpStatus.OK); + } + + @GetMapping("allindepartment/{id}") + public ResponseEntity> getAllEmployeesInDepartment(@PathVariable Long id) { + return new ResponseEntity<>(employeeService.getAllEmployeesInDepartment(id), HttpStatus.OK); + } + + @GetMapping("allnomanager") + public ResponseEntity> getAllEmployeesNoManager() { + return new ResponseEntity<>(employeeService.getAllEmployeesNoManager(), HttpStatus.OK); + } + + @GetMapping("hierarchy/{id}") + public ResponseEntity> getHierarchy(@PathVariable Long id) { + return new ResponseEntity<>(employeeService.getHierarchy(id), HttpStatus.OK); + } + + @PostMapping + public ResponseEntity createEmployee (@RequestBody Employee newEmployee){ + return new ResponseEntity<>(employeeService.createEmployee(newEmployee),HttpStatus.CREATED); + } + + @PutMapping("{id}") + public ResponseEntity updateManager (@RequestBody Employee updatedData, @PathVariable Long id){ + return new ResponseEntity<>(employeeService.updatedData(updatedData, id),HttpStatus.OK); + } + + @DeleteMapping("{id}") + public ResponseEntity deleteEmployee (@PathVariable Long id){ + return new ResponseEntity<>(employeeService.deleteEmployee(id),HttpStatus.OK); + } + + @DeleteMapping("manager/{id}") + public ResponseEntity deleteEmployeeByManager(@PathVariable Long id){ + return new ResponseEntity<>(employeeService.deleteEmployeeByManager(id),HttpStatus.OK); + } + + @DeleteMapping("department/{id}") + public ResponseEntity deleteEmployeesByDepartment(@PathVariable Long id){ + return new ResponseEntity<>(employeeService.deleteEmployeesByDepartment(id), HttpStatus.OK); + } + + @PutMapping("mergedepartment/{deptfrom}/{deptto}") + public ResponseEntity> mergeDepartments(@PathVariable Long deptfrom, @PathVariable Long deptto){ + return new ResponseEntity<>(employeeService.mergeDepartments(deptfrom,deptto),HttpStatus.OK); + } + + // TODO + /* + + * remove all direct reports to a manager. Any employees previously managed by the deleted employees + should now be managed by the next manager up the hierarchy. + + */ +} diff --git a/src/main/java/io/zipcoder/persistenceapp/entities/Department.java b/src/main/java/io/zipcoder/persistenceapp/entities/Department.java new file mode 100644 index 0000000..49bdc27 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/entities/Department.java @@ -0,0 +1,39 @@ +package io.zipcoder.persistenceapp.entities; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Department { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long DEPARTMENT_ID; + + private String DEPARTMENT_NAME; + private Long DEPARTMENT_MANAGER; + + + public Long getDEPARTMENT_ID() { + return DEPARTMENT_ID; + } + public void setDEPARTMENT_ID (Long id){ this.DEPARTMENT_ID = id;} + + public String getDEPARTMENT_NAME() { + return DEPARTMENT_NAME; + } + + public void setDEPARTMENT_NAME(String DEPARTMENT_NAME) { + this.DEPARTMENT_NAME = DEPARTMENT_NAME; + } + + public Long getDEPARTMENT_MANAGER() { + return DEPARTMENT_MANAGER; + } + + public void setDEPARTMENT_MANAGER(Long DEPARTMENT_MANAGER) { + this.DEPARTMENT_MANAGER = DEPARTMENT_MANAGER; + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/entities/Employee.java b/src/main/java/io/zipcoder/persistenceapp/entities/Employee.java new file mode 100644 index 0000000..cb1e9d0 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/entities/Employee.java @@ -0,0 +1,84 @@ +package io.zipcoder.persistenceapp.entities; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Date; + +@Entity +public class Employee { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long EMPLOYEE_ID; + + private String FIRST_NAME; + private String LAST_NAME; + private String TITLE; + private String PHONE; + private Date HIRE_DATE; + private Long MANAGER_ID; + private Long DEPARTMENT_ID; + + + public Long getEMPLOYEE_ID() { + return EMPLOYEE_ID; + } + + public String getFIRST_NAME() { + return FIRST_NAME; + } + + public void setFIRST_NAME(String FIRST_NAME) { + this.FIRST_NAME = FIRST_NAME; + } + + public String getLAST_NAME() { + return LAST_NAME; + } + + public void setLAST_NAME(String LAST_NAME) { + this.LAST_NAME = LAST_NAME; + } + + public String getTITLE() { + return TITLE; + } + + public void setTITLE(String TITLE) { + this.TITLE = TITLE; + } + + public String getPHONE() { + return PHONE; + } + + public void setPHONE(String PHONE) { + this.PHONE = PHONE; + } + + public Date getHIRE_DATE() { + return HIRE_DATE; + } + + public void setHIRE_DATE(Date HIRE_DATE) { + this.HIRE_DATE = HIRE_DATE; + } + + public Long getMANAGER_ID() { + return MANAGER_ID; + } + + public void setMANAGER_ID(Long MANAGER_ID) { + this.MANAGER_ID = MANAGER_ID; + } + + public Long getDEPARTMENT_ID() { + return DEPARTMENT_ID; + } + + public void setDEPARTMENT_ID(Long DEPARTMENT_ID) { + this.DEPARTMENT_ID = DEPARTMENT_ID; + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/exceptions/ResourceNotFoundException.java b/src/main/java/io/zipcoder/persistenceapp/exceptions/ResourceNotFoundException.java new file mode 100644 index 0000000..eafaf21 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/exceptions/ResourceNotFoundException.java @@ -0,0 +1,15 @@ +package io.zipcoder.persistenceapp.exceptions; + +public class ResourceNotFoundException extends RuntimeException{ + public ResourceNotFoundException() { + } + + public ResourceNotFoundException(String message) { + super(message); + } + + public ResourceNotFoundException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/repositories/DepartmentRepository.java b/src/main/java/io/zipcoder/persistenceapp/repositories/DepartmentRepository.java new file mode 100644 index 0000000..a571bb7 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/repositories/DepartmentRepository.java @@ -0,0 +1,11 @@ +package io.zipcoder.persistenceapp.repositories; + +import io.zipcoder.persistenceapp.entities.Department; +import io.zipcoder.persistenceapp.entities.Employee; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface DepartmentRepository extends CrudRepository { + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/repositories/EmployeeRepository.java b/src/main/java/io/zipcoder/persistenceapp/repositories/EmployeeRepository.java new file mode 100644 index 0000000..d2997bb --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/repositories/EmployeeRepository.java @@ -0,0 +1,12 @@ +package io.zipcoder.persistenceapp.repositories; + +import io.zipcoder.persistenceapp.entities.Employee; +import org.hibernate.annotations.SQLDelete; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface EmployeeRepository extends CrudRepository { + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/services/DepartmentService.java b/src/main/java/io/zipcoder/persistenceapp/services/DepartmentService.java new file mode 100644 index 0000000..1f283a2 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/services/DepartmentService.java @@ -0,0 +1,63 @@ +package io.zipcoder.persistenceapp.services; + +import io.zipcoder.persistenceapp.entities.Department; +import io.zipcoder.persistenceapp.exceptions.ResourceNotFoundException; +import io.zipcoder.persistenceapp.repositories.DepartmentRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class DepartmentService { + + DepartmentRepository departmentRepository; + + @Autowired + public DepartmentService(DepartmentRepository departmentRepository){ + this.departmentRepository = departmentRepository; + } + + public Iterable getAllDepartments(){ + return departmentRepository.findAll(); + } + + public Department getDepartmentById(Long id){ + verifyDepartment(id); + return departmentRepository.findOne(id); + } + + public String getDepartmentName(Long id){ + verifyDepartment(id); + return getDepartmentById(id).getDEPARTMENT_NAME(); + } + + public Department updateDepartmentManager(Department newData, Long ID){ + + verifyDepartment(ID); + + Department existingData = departmentRepository.findOne(ID); + existingData.setDEPARTMENT_MANAGER(newData.getDEPARTMENT_MANAGER()); + return departmentRepository.save(existingData); + } + + public Department updateDepartmentName(Department newData, Long id){ + + verifyDepartment(id); + + Department existingData = departmentRepository.findOne(id); + existingData.setDEPARTMENT_NAME(newData.getDEPARTMENT_NAME()); + return departmentRepository.save(existingData); + } + + public Department createDepartment (Department newDepartment){ + return departmentRepository.save(newDepartment); + } + + private void verifyDepartment(Long departmentId){ + if (departmentRepository.exists(departmentId)){ + + } else { + throw new ResourceNotFoundException("Department not found!"); + } + } + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/services/EmployeeService.java b/src/main/java/io/zipcoder/persistenceapp/services/EmployeeService.java new file mode 100644 index 0000000..293a779 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/services/EmployeeService.java @@ -0,0 +1,149 @@ +package io.zipcoder.persistenceapp.services; + +import io.zipcoder.persistenceapp.entities.Employee; +import io.zipcoder.persistenceapp.exceptions.ResourceNotFoundException; +import io.zipcoder.persistenceapp.repositories.DepartmentRepository; +import io.zipcoder.persistenceapp.repositories.EmployeeRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class EmployeeService { + + EmployeeRepository employeeRepository; + + @Autowired + DepartmentRepository departmentRepository; + + @Autowired + public EmployeeService(EmployeeRepository employeeRepository) { + this.employeeRepository = employeeRepository; + } + + public Iterable getAllEmployeesUnderManager(Long id) { + List allEmployees = new ArrayList<>(); + if (verifyEmployeeExists(id)) { + for (Employee each : employeeRepository.findAll()) { + if (each.getMANAGER_ID() == id) { + allEmployees.add(each); + } + } + } + return allEmployees; + } + + public Iterable getAllEmployeesInDepartment(Long id) { + List allEmployees = new ArrayList<>(); + + if (verifyEmployeeExists(id)) { + for (Employee each : employeeRepository.findAll()) { + if (each.getDEPARTMENT_ID() == id) { + allEmployees.add(each); + } + } + } + return allEmployees; + } + + public Iterable getAllEmployeesNoManager() { + List allEmployees = new ArrayList<>(); + for (Employee each : employeeRepository.findAll()) { + if (each.getMANAGER_ID() == null) { + allEmployees.add(each); + } + } + return allEmployees; + } + + public Iterable getHierarchy(Long id) { + List result = new ArrayList<>(); + Employee one = employeeRepository.findOne(id); + result.add(one); + Boolean topManager = false; + + if (verifyEmployeeExists(id)) { + while (!topManager) { + if (one.getMANAGER_ID() == null) { + topManager = true; + } else { + one = employeeRepository.findOne(one.getMANAGER_ID()); + if (one != null) { + result.add(one); // needs logic if we delete the dept manager + } else topManager = true; + + } + } + } + return result; + } + + public Employee createEmployee(Employee newEmployee) { + return employeeRepository.save(newEmployee); + } + + public Employee updatedData(Employee newData, Long id) { + Employee existingData = null; + + if (verifyEmployeeExists(id)) { + existingData = employeeRepository.findOne(id); + existingData.setMANAGER_ID(newData.getMANAGER_ID()); + } + return employeeRepository.save(existingData); + } + + public Boolean deleteEmployee(Long id) { + if (verifyEmployeeExists(id)) { + employeeRepository.delete(id); + return true; + } + return false; + } + + public Boolean deleteEmployeeByManager (Long id){ + Iterable employees = getAllEmployeesUnderManager(id); + employeeRepository.delete(employees); + return true; + } + + public Boolean deleteEmployeesByDepartment (Long id){ + if (verifyDepartmentExists(id)) { + Iterable employees = getAllEmployeesInDepartment(id); + employeeRepository.delete(employees); + return true; + } + return false; + } + + public Iterable mergeDepartments(Long fromDepartment, Long toDepartment){ + if (verifyDepartmentExists(fromDepartment) && verifyDepartmentExists(toDepartment)){ + Iterable fromdepartment = getAllEmployeesInDepartment(fromDepartment); + if (fromdepartment != null){ + for (Employee each : fromdepartment){ + each.setDEPARTMENT_ID(toDepartment); + } + return employeeRepository.save(fromdepartment); + } + } + return null; + } + + + private Boolean verifyEmployeeExists(Long id) { + if (employeeRepository.exists(id)) { + return true; + } else { + throw new ResourceNotFoundException("Employee not found!"); + } + } + + private Boolean verifyDepartmentExists(Long id){ + if (departmentRepository.exists(id)){ + return true; + } else { + throw new ResourceNotFoundException("Department not found"); + } + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7d4dc6f..a3aa50a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,6 @@ spring.profiles.active=h2 logging.level.org.springframework.boot.context.embedded=INFO -spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect \ No newline at end of file +spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect + +server.port=8081 +spring.jpa.properties.hibernate.id.new_generator_mappings=true \ No newline at end of file diff --git a/src/main/resources/data-h2.sql b/src/main/resources/data-h2.sql new file mode 100644 index 0000000..ef15915 --- /dev/null +++ b/src/main/resources/data-h2.sql @@ -0,0 +1,73 @@ + + +INSERT INTO DEPARTMENT ( +DEPARTMENT_NAME, DEPARTMENT_MANAGER +) VALUES ( +'HUMAN RESOURCES', 14 +); + +INSERT INTO DEPARTMENT ( +DEPARTMENT_NAME, DEPARTMENT_MANAGER +) VALUES ( +'MARKETING', 10 +); + +INSERT INTO DEPARTMENT ( +DEPARTMENT_NAME, DEPARTMENT_MANAGER +) VALUES ( +'IT', 2 +); + + + + +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Ailee', 'Perin', 'Services', '215-333-4444', '2010-08-28', 14, 1); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Horace', 'Vaud', 'IT Dept Manager', '215-333-4444', '2008-08-28', null, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Obed', 'Fussie', 'Services', '215-333-4444', '2010-08-28', 14, 1); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Hale', 'Tubritt', 'Services', '215-333-4444', '2010-08-28', 14, 1); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Lenci', 'Robbie', 'IT Manager', '215-333-4444', '2010-08-28', 2, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Daria', 'Vautin', 'Legal', '215-333-4444', '2010-08-28', 14, 1); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Sidonnie', 'Chicken', 'Sales', '215-333-4444', '2010-08-28', 14, 1); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Delila', 'Seary', 'Accounting', '215-333-4444', '2010-08-28', 14, 1); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Anita', 'McKaile', 'Engineering', '215-333-4444', '2010-08-28', 14, 1); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Stephanie', 'O''Gormally', 'Marketing Dept Manager', '215-333-4444', '2007-08-28', null, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Lorenza', 'Torrecilla', 'Marketing Manager', '215-333-4444', '2010-08-28', 10, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Kingsley', 'Heeron', 'Support', '215-333-4444', '2010-08-28', 11, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Neysa', 'Klamp', 'Product Management', '215-333-4444', '2010-08-28', 11, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Janot', 'Nyssen', 'HR Dept Manager', '215-333-4444', '2008-08-28', null, 1); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Juan', 'Wedgwood', 'Engineering', '215-333-4444', '2010-08-28', 11, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Mellie', 'Diloway', 'Marketing', '215-333-4444', '2010-08-28', 11, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Rosamund', 'Bessell', 'Services', '215-333-4444', '2010-08-28', 11, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Harbert', 'Rogers', 'IT Manager', '215-333-4444', '2010-08-28', 2, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Alix', 'McGow', 'Sales', '215-333-4444', '2010-08-28', 11, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Seth', 'Booton', 'Product Management', '215-333-4444', '2010-08-28', 11, 2); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Kayne', 'Reyner', 'Research and Development', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Mauricio', 'Saiger', 'Sales', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Cymbre', 'Fairman', 'Sales', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Alex', 'Dunckley', 'Engineering', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Esta', 'Gersam', 'Sales', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Hillary', 'Touson', 'Services', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Magdalene', 'Molesworth', 'Marketing', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Orren', 'Boame', 'Business Development', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Erik', 'Lipprose', 'Human Resources', '215-333-4444', '2010-08-28', 5, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Ilyse', 'Aherne', 'Accounting', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Evyn', 'McNickle', 'Accounting', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Kamilah', 'Avrahamof', 'Marketing', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Stanwood', 'Seniour', 'Marketing', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Rosene', 'Portress', 'Support', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Kristien', 'Enticott', 'Sales', '215-333-4444', '2010-08-28', null, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Noach', 'Gittins', 'Business Development', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Leda', 'Kindley', 'Services', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Norrie', 'Garrard', 'Sales', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Virgilio', 'Draisey', 'Accounting', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Jane', 'Edgeson', 'Marketing', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Jarrod', 'Murley', 'Business Development', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Vallie', 'Firks', 'Engineering', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Renell', 'Okenden', 'Legal', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Jefferey', 'Yallowley', 'Engineering', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Emanuel', 'Hamblin', 'Training', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Anderson', 'Striker', 'Support', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Maryrose', 'Waring', 'Accounting', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Horatia', 'Parrin', 'Sales', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Rosita', 'Egdal', 'Business Development', '215-333-4444', '2010-08-28', 18, 3); +insert into EMPLOYEE (FIRST_NAME, LAST_NAME, TITLE, PHONE, HIRE_DATE, MANAGER_ID, DEPARTMENT_ID) values ('Khalil', 'Proven', 'Marketing', '215-333-4444', '2010-08-28', 18, 3); diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql index 030913b..dae8e93 100644 --- a/src/main/resources/schema-h2.sql +++ b/src/main/resources/schema-h2.sql @@ -1,33 +1,27 @@ -DROP TABLE PERSON; - -CREATE TABLE PERSON ( - ID NUMBER(10,0) NOT NULL AUTO_INCREMENT, - FIRST_NAME VARCHAR2(255) NOT NULL DEFAULT '', - LAST_NAME VARCHAR2(255) NOT NULL DEFAULT '', - MOBILE VARCHAR2(20) NOT NULL DEFAULT '', - BIRTHDAY DATE DEFAULT NULL, - PRIMARY KEY (ID)); - -DROP TABLE HOME; - -CREATE TABLE HOME ( - ID NUMBER(10,0) NOT NULL AUTO_INCREMENT, - ADDRESS VARCHAR2(255) not null default '', - HOMENUMBER varchar2(255) NOT NULL DEFAULT '', - PRIMARY KEY (ID) +DROP TABLE IF EXISTS EMPLOYEE; + +CREATE TABLE EMPLOYEE ( + EMPLOYEE_ID NUMBER(10,0) NOT NULL AUTO_INCREMENT, + FIRST_NAME VARCHAR(255) NOT NULL DEFAULT '', + LAST_NAME VARCHAR(255) NOT NULL DEFAULT '', + TITLE VARCHAR(255) NOT NULL DEFAULT 'EMPLOYEE', + PHONE VARCHAR(12) NOT NULL DEFAULT '', + HIRE_DATE DATE DEFAULT NULL, + MANAGER_ID NUMBER(10,0) NULL, + DEPARTMENT_ID NUMBER(10,0) NOT NULL, + PRIMARY KEY (EMPLOYEE_ID) + ); + +DROP TABLE IF EXISTS DEPARTMENT; + +CREATE TABLE DEPARTMENT ( + DEPARTMENT_ID NUMBER(10,0) NOT NULL AUTO_INCREMENT, + DEPARTMENT_NAME VARCHAR (255) NOT NULL DEFAULT '', + DEPARTMENT_MANAGER NUMBER (10,0) NULL, + PRIMARY KEY (DEPARTMENT_ID) ); -DROP TABLE CAR; - -CREATE TABLE CAR ( - ID NUMBER(10,0) NOT NULL AUTO_INCREMENT, - MAKE VARCHAR2(255) not null default '', - MODEL varchar2(255) NOT NULL DEFAULT '', - YEAR VARCHAR2(5) NOT NULL DEFAULT '01907', - PRIMARY KEY (ID) -); - -DROP SEQUENCE hibernate_sequence; +DROP IF EXISTS SEQUENCE hibernate_sequence; CREATE SEQUENCE hibernate_sequence; \ No newline at end of file diff --git a/src/test/java/io/zipcoder/PersistenceStarterApplicationTests.java b/src/test/java/io/zipcoder/PersistenceStarterApplicationTests.java index 3e5dd20..0121733 100644 --- a/src/test/java/io/zipcoder/PersistenceStarterApplicationTests.java +++ b/src/test/java/io/zipcoder/PersistenceStarterApplicationTests.java @@ -1,12 +1,13 @@ package io.zipcoder; +import io.zipcoder.persistenceapp.PersistenceStarterApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest(classes = PersistenceStarterApplication.class) public class PersistenceStarterApplicationTests { @Test diff --git a/src/test/java/io/zipcoder/persistenceapp/controllers/DepartmentControllerTest.java b/src/test/java/io/zipcoder/persistenceapp/controllers/DepartmentControllerTest.java new file mode 100644 index 0000000..11c808c --- /dev/null +++ b/src/test/java/io/zipcoder/persistenceapp/controllers/DepartmentControllerTest.java @@ -0,0 +1,102 @@ +package io.zipcoder.persistenceapp.controllers; + +import io.zipcoder.persistenceapp.entities.Department; +import io.zipcoder.persistenceapp.repositories.DepartmentRepository; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.mockito.BDDMockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.util.ArrayList; +import java.util.List; + + +@SpringBootTest +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +public class DepartmentControllerTest { + + + @Autowired + private MockMvc mvc; + + @MockBean + private DepartmentRepository repository; + + + @Test + public void testGetAllDepartments() throws Exception { + List allDepts = new ArrayList<>(); + allDepts.add(new Department()); + allDepts.add(new Department()); + + // given + BDDMockito + .given(repository.findAll()) + .willReturn(allDepts); + + // when + this.mvc.perform(MockMvcRequestBuilders + .get("/api/department/") + ).andExpect(MockMvcResultMatchers.status().isOk()); + + + this.mvc.perform(MockMvcRequestBuilders + .get("/api/departments/") + ).andExpect(MockMvcResultMatchers.status().is4xxClientError()); + } + + @Test + public void testGetDepartmentName() throws Exception { + Department dept = new Department(); + dept.setDEPARTMENT_ID(5L); + dept.setDEPARTMENT_NAME("Test"); + + BDDMockito + .given(repository.findOne(5L)) + .willReturn(dept); + + BDDMockito + .given(repository.exists(5L)) + .willReturn(true); + + + this.mvc.perform(MockMvcRequestBuilders + .get("/api/department/5/name") + ).andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().string("Test")); + } + + @Test + public void testGetDepartmentById() throws Exception { + Department dept = new Department(); + dept.setDEPARTMENT_ID(5L); + dept.setDEPARTMENT_NAME("Test"); + dept.setDEPARTMENT_MANAGER(10L); + + BDDMockito + .given(repository.findOne(5L)) + .willReturn(dept); + + BDDMockito + .given(repository.exists(5L)) + .willReturn(true); + + String expected = "{\"department_NAME\":\"Test\",\"department_MANAGER\":10,\"department_ID\":5}"; + this.mvc.perform(MockMvcRequestBuilders + .get("/api/department/5") + .contentType(MediaType.APPLICATION_JSON) + ).andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().string(expected)); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/persistenceapp/services/DepartmentServiceTest.java b/src/test/java/io/zipcoder/persistenceapp/services/DepartmentServiceTest.java new file mode 100644 index 0000000..efbb431 --- /dev/null +++ b/src/test/java/io/zipcoder/persistenceapp/services/DepartmentServiceTest.java @@ -0,0 +1,92 @@ +package io.zipcoder.persistenceapp.services; + +import io.zipcoder.persistenceapp.entities.Department; +import io.zipcoder.persistenceapp.repositories.DepartmentRepository; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.List; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class DepartmentServiceTest { + + @MockBean + private DepartmentRepository repository; + + @Autowired + private DepartmentService service; + + + @Test + public void getDepartmentById() throws Exception{ + Long givenId = 1L; + Department departmentTest = new Department(); + departmentTest.setDEPARTMENT_NAME("Marketing"); + departmentTest.setDEPARTMENT_ID(givenId); + + BDDMockito + .given(repository.findOne(givenId)) + .willReturn(departmentTest); + + BDDMockito + .given(repository.exists(givenId)) + .willReturn(true); + + Department response = service.getDepartmentById(givenId); + + Assert.assertEquals(departmentTest,response); + } + + @Test + public void getDepartmentName() throws Exception { + Long givenId = 1L; + String expected = "MARKETING"; + Department departmentTest = new Department(); + departmentTest.setDEPARTMENT_NAME(expected); + departmentTest.setDEPARTMENT_ID(givenId); + + + BDDMockito + .given(repository.findOne(givenId)) + .willReturn(departmentTest); + + BDDMockito + .given(repository.exists(givenId)) + .willReturn(true); + + String response = service.getDepartmentName(givenId); + + Assert.assertEquals(expected,response); + } + + @Test + public void testGetAllDepartments() throws Exception{ + Department departmentTest = new Department(); + departmentTest.setDEPARTMENT_NAME("Test1"); + departmentTest.setDEPARTMENT_ID(1L); + Department departmentTest2 = new Department(); + departmentTest2.setDEPARTMENT_NAME("Test1"); + departmentTest2.setDEPARTMENT_ID(2L); + + List allDepartments = new ArrayList<>(); + allDepartments.add(departmentTest); + allDepartments.add(departmentTest2); + + BDDMockito + .given(repository.findAll()) + .willReturn(allDepartments); + + Integer expected = 2; + Integer actual = allDepartments.size(); + + Assert.assertEquals(expected,actual); + } +} \ No newline at end of file