diff --git a/ScreenShot_final_project_finished/completed_POST_localhost8080employees1.jpg b/ScreenShot_final_project_finished/completed_POST_localhost8080employees1.jpg new file mode 100644 index 000000000..52982979e Binary files /dev/null and b/ScreenShot_final_project_finished/completed_POST_localhost8080employees1.jpg differ diff --git a/ScreenShot_final_project_finished/gs_rest_service_response b/ScreenShot_final_project_finished/gs_rest_service_response new file mode 100644 index 000000000..27335c576 --- /dev/null +++ b/ScreenShot_final_project_finished/gs_rest_service_response @@ -0,0 +1 @@ +{"employeeList":[{"employee_Id":1,"first_name":"James","last_name":"Smith","email":"smith_james@gmail.com","address":"1234 Main Street"},{"employee_Id":2,"first_name":"Mary","last_name":"Sue","email":"sue_mary@gmail.com","address":"1111 14th Grande Street"},{"employee_Id":3,"first_name":"John","last_name":"Gunther","email":"gunther_mg@gmail.com","address":"1456 15th West Street"}]} \ No newline at end of file diff --git a/ScreenShot_final_project_finished/photo_shot_of_localhost8080employees.jpg b/ScreenShot_final_project_finished/photo_shot_of_localhost8080employees.jpg new file mode 100644 index 000000000..92b2acefe Binary files /dev/null and b/ScreenShot_final_project_finished/photo_shot_of_localhost8080employees.jpg differ diff --git a/ScreenShot_final_project_finished/screenshot_POST_localhost8080employees1.jpg b/ScreenShot_final_project_finished/screenshot_POST_localhost8080employees1.jpg new file mode 100644 index 000000000..a3d03595a Binary files /dev/null and b/ScreenShot_final_project_finished/screenshot_POST_localhost8080employees1.jpg differ diff --git a/initial/pom.xml b/initial/pom.xml index 282ea1bb8..e3f9035ff 100644 --- a/initial/pom.xml +++ b/initial/pom.xml @@ -21,11 +21,29 @@ org.springframework.boot spring-boot-starter-web - + org.springframework.boot spring-boot-starter-test test + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-engine + test + + + + org.mockito + mockito-core + test diff --git a/initial/src/main/java/com/example/restservice/Employee.java b/initial/src/main/java/com/example/restservice/Employee.java new file mode 100644 index 000000000..fcf7cc999 --- /dev/null +++ b/initial/src/main/java/com/example/restservice/Employee.java @@ -0,0 +1,69 @@ +package com.example.restservice; + +public class Employee { + //Variables for employee + private Integer employee_Id; + private String first_name; + private String last_name; + private String email; + private String title; + + //Default Constructor for Employee + public Employee(){ + } + + //Parameterized Constructor + public Employee(Integer employee_Id, String first_name, String last_name, String email, String title) { + this.employee_Id = employee_Id; + this.first_name = first_name; + this.last_name = last_name; + this.email = email; + this.title = title; + } + + //Employee getters and setters + public Integer getEmployee_Id() { + return employee_Id; + } + + public void setEmployee_Id(Integer employee_Id) { + this.employee_Id = 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 getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @Override + public String toString() { + return "Employee [employee_Id=" + employee_Id + ", first_name=" + first_name + ", last_name=" + last_name + ", email=" + email + ", title=" + title + "]"; + } +} diff --git a/initial/src/main/java/com/example/restservice/EmployeeController.java b/initial/src/main/java/com/example/restservice/EmployeeController.java new file mode 100644 index 000000000..e575a8986 --- /dev/null +++ b/initial/src/main/java/com/example/restservice/EmployeeController.java @@ -0,0 +1,43 @@ +package com.example.restservice; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import java.net.URI; + +@RestController +public class EmployeeController { + + //inject the EmployeeManager + @Autowired + EmployeeManager employeeManager; + + //Get point to retrieve the employees from employeeManager + @GetMapping("/employees") + public Employees getAllEmployees() { + return employeeManager.getAllEmployees(); + } + + // POST endpoint to add a new employee + @PostMapping("/employees") + public ResponseEntity addEmployee(@RequestBody Employee employee) { + + // Generate ID for the new employee + Integer id = employeeManager.getAllEmployees().getEmployeeList().size() + 1; + employee.setEmployee_Id(id); + + // Add employee to the list + employeeManager.addEmployee(employee); + + // Build location URI for the new employee + URI location = ServletUriComponentsBuilder + .fromCurrentRequest() + .path("/{id}") + .buildAndExpand(employee.getEmployee_Id()) + .toUri(); + + return ResponseEntity.created(location).build(); + } +} diff --git a/initial/src/main/java/com/example/restservice/EmployeeManager.java b/initial/src/main/java/com/example/restservice/EmployeeManager.java new file mode 100644 index 000000000..105f06e4f --- /dev/null +++ b/initial/src/main/java/com/example/restservice/EmployeeManager.java @@ -0,0 +1,25 @@ +package com.example.restservice; + +import org.springframework.stereotype.Repository; + +@Repository +public class EmployeeManager { + private static final Employees employees = new Employees(); + + //sample employees + static { + employees.getEmployeeList().add(new Employee(1, "James", "Smith", "smith_james@gmail.com", "Manager" )); + employees.getEmployeeList().add(new Employee(2, "Mary", "Sue", "sue_mary@gmail.com", "Admin" )); + employees.getEmployeeList().add(new Employee(3, "John", "Gunther", "gunther_mg@gmail.com", "employee" )); + } + + //retrieve employees + public Employees getAllEmployees() { + return employees; + } + + //add employees + public void addEmployee(Employee employee) { + employees.getEmployeeList().add(employee); + } +} diff --git a/initial/src/main/java/com/example/restservice/Employees.java b/initial/src/main/java/com/example/restservice/Employees.java new file mode 100644 index 000000000..06a105d08 --- /dev/null +++ b/initial/src/main/java/com/example/restservice/Employees.java @@ -0,0 +1,20 @@ +package com.example.restservice; + +import java.util.ArrayList; +import java.util.List; + +public class Employees { + + private List employeeList; + + public List getEmployeeList() { + if (employeeList == null) { + employeeList = new ArrayList(); + } + return employeeList; + } + + public void setEmployeeList(List employeeList) { + this.employeeList = employeeList; + } +} diff --git a/initial/src/main/resources/application.properties b/initial/src/main/resources/application.properties index 8b1378917..9897b2cf6 100644 --- a/initial/src/main/resources/application.properties +++ b/initial/src/main/resources/application.properties @@ -1 +1,8 @@ +## Multipart config +spring.servlet.multipart.enabled=true +spring.servlet.multipart.file-size-threshold=2KB +spring.servlet.multipart.max-file-size=200MB +spring.servlet.multipart.max-request-size=215MB +## File upload custom properties +file.upload.location=/Users/no1sa diff --git a/initial/src/test/java/com/example/restservice/EmployeeControllerTest.java b/initial/src/test/java/com/example/restservice/EmployeeControllerTest.java new file mode 100644 index 000000000..4826b0d10 --- /dev/null +++ b/initial/src/test/java/com/example/restservice/EmployeeControllerTest.java @@ -0,0 +1,180 @@ +package com.example.restservice; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.Matchers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@WebMvcTest(EmployeeController.class) +public class EmployeeControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private EmployeeManager employeeManager; + + @Autowired + private ObjectMapper objectMapper; + + //test GET request delete or comment the other test for these + // commented out test to work +// @Test +// public void testGetAllEmployees() throws Exception { +// Employees employees = new Employees(); +// employees.setEmployeeList(Arrays.asList( +// new Employee(1, "James", "Smith", "smith_james@gmail.com", "Manager"), +// new Employee(2, "Mary", "Sue", "sue_mary@gmail.com", "Admin"), +// new Employee(3, "John", "Gunther", "gunther_mg@gmail.com", "employee") +// )); +// +// Mockito.when(employeeManager.getAllEmployees()).thenReturn(employees); +// +// mockMvc.perform(get("/employees") +// .contentType(MediaType.APPLICATION_JSON)) +// .andExpect(status().isOk()) +// .andExpect(jsonPath("$.employeeList", hasSize(3))) +// .andExpect(jsonPath("$.employeeList[0].first_name", is("James"))); +// } +// +// //test POST request +// @Test +// public void testAddEmployee() throws Exception { +// Employee newEmployee = new Employee(null, "John", "Doe", "john.doe@example.com", "Developer"); +// +// // Simulate existing list of employees to generate the next ID +// Employees employees = new Employees(); +// employees.setEmployeeList(Arrays.asList( +// new Employee(1, "James", "Smith", "smith_james@gmail.com", "Manager") +// )); +// +// Mockito.when(employeeManager.getAllEmployees()).thenReturn(employees); +// +// mockMvc.perform(post("/employees") +// .contentType(MediaType.APPLICATION_JSON) +// .content(objectMapper.writeValueAsString(newEmployee))) +// .andExpect(status().isCreated()) +// .andExpect(header().string("Location", containsString("/employees/2"))); +// } + + + // Helper function to get the number of current employees + int getEmployeeCount(EmployeeManager manager) + { + return manager.getAllEmployees().getEmployeeList().size(); + } + + @Test + // Ensure that employee list is populated on initialization + void createEmployeeManager() { + EmployeeManager newEmployeeManager = new EmployeeManager(); + assert(getEmployeeCount(newEmployeeManager) != 0); + } + + @Test + // Ensure that adding an employee increases the employee count by 1 + void addEmployee() { + EmployeeManager employeeManager = new EmployeeManager(); + int employeeCount = getEmployeeCount(employeeManager); + Employee employee = new Employee(1, "Daria", "Jones", "dariajones@gmail.com", "Software developer"); + employeeManager.addEmployee(employee); + assert(employeeCount + 1 == getEmployeeCount(employeeManager)); + } + + @ExtendWith(MockitoExtension.class) + @BeforeEach void setUp() + { + this.employeeManager = new EmployeeManager(); + Employee newEmployee = new Employee(1, "Daria", "Jones", "dariajones@gmail.com", "Software developer"); + this.employeeManager.addEmployee(newEmployee); + } + + @Test + // Check whether added employee ID is found in ID field + void employeeIdInList() { + List employees = this.employeeManager.getAllEmployees().getEmployeeList(); + for (int i=0; i employees = this.employeeManager.getAllEmployees().getEmployeeList(); + for (int i=0; i employees = this.employeeManager.getAllEmployees().getEmployeeList(); + for (int i=0; i employees = this.employeeManager.getAllEmployees().getEmployeeList(); + for (int i=0; i employees = this.employeeManager.getAllEmployees().getEmployeeList(); + for (int i=0; i