Skip to content
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ScreenShot_final_project_finished/gs_rest_service_response
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"employeeList":[{"employee_Id":1,"first_name":"James","last_name":"Smith","email":"[email protected]","address":"1234 Main Street"},{"employee_Id":2,"first_name":"Mary","last_name":"Sue","email":"[email protected]","address":"1111 14th Grande Street"},{"employee_Id":3,"first_name":"John","last_name":"Gunther","email":"[email protected]","address":"1456 15th West Street"}]}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion initial/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,29 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
69 changes: 69 additions & 0 deletions initial/src/main/java/com/example/restservice/Employee.java
Original file line number Diff line number Diff line change
@@ -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 + "]";
}
}
Original file line number Diff line number Diff line change
@@ -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<Object> 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();
}
}
25 changes: 25 additions & 0 deletions initial/src/main/java/com/example/restservice/EmployeeManager.java
Original file line number Diff line number Diff line change
@@ -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", "[email protected]", "Manager" ));
employees.getEmployeeList().add(new Employee(2, "Mary", "Sue", "[email protected]", "Admin" ));
employees.getEmployeeList().add(new Employee(3, "John", "Gunther", "[email protected]", "employee" ));
}

//retrieve employees
public Employees getAllEmployees() {
return employees;
}

//add employees
public void addEmployee(Employee employee) {
employees.getEmployeeList().add(employee);
}
}
20 changes: 20 additions & 0 deletions initial/src/main/java/com/example/restservice/Employees.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.restservice;

import java.util.ArrayList;
import java.util.List;

public class Employees {

private List<Employee> employeeList;

public List<Employee> getEmployeeList() {
if (employeeList == null) {
employeeList = new ArrayList<Employee>();
}
return employeeList;
}

public void setEmployeeList(List<Employee> employeeList) {
this.employeeList = employeeList;
}
}
7 changes: 7 additions & 0 deletions initial/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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", "[email protected]", "Manager"),
// new Employee(2, "Mary", "Sue", "[email protected]", "Admin"),
// new Employee(3, "John", "Gunther", "[email protected]", "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", "[email protected]", "Developer");
//
// // Simulate existing list of employees to generate the next ID
// Employees employees = new Employees();
// employees.setEmployeeList(Arrays.asList(
// new Employee(1, "James", "Smith", "[email protected]", "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", "[email protected]", "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", "[email protected]", "Software developer");
this.employeeManager.addEmployee(newEmployee);
}

@Test
// Check whether added employee ID is found in ID field
void employeeIdInList() {
List<Employee> employees = this.employeeManager.getAllEmployees().getEmployeeList();
for (int i=0; i<employees.size(); i++)
{
Employee employee = employees.get(i);
if (employee.getEmployee_Id() == 1)
{
return;
}
}
assert(false);
}

@Test
// Check whether added employee first name is found in first name field
void employeeFirstNameInList() {
List<Employee> employees = this.employeeManager.getAllEmployees().getEmployeeList();
for (int i=0; i<employees.size(); i++)
{
Employee employee = employees.get(i);
if (employee.getFirst_name() == "Daria")
{
return;
}
}
assert(false);
}

@Test
// Check whether added employee last name is found in last name field
void employeeLastNameInList() {
List<Employee> employees = this.employeeManager.getAllEmployees().getEmployeeList();
for (int i=0; i<employees.size(); i++)
{
Employee employee = employees.get(i);
if (employee.getLast_name() == "Jones")
{
return;
}
}
assert(false);
}

@Test
// Check whether added employee email is found in email field
void employeeEmailInList() {
List<Employee> employees = this.employeeManager.getAllEmployees().getEmployeeList();
for (int i=0; i<employees.size(); i++)
{
Employee employee = employees.get(i);
if (employee.getEmail() == "[email protected]")
{
return;
}
}
assert(false);
}

@Test
// Check whether added employee title is found in title field
void employeeTitleInList() {
List<Employee> employees = this.employeeManager.getAllEmployees().getEmployeeList();
for (int i=0; i<employees.size(); i++)
{
Employee employee = employees.get(i);
if (employee.getTitle() == "Software developer")
{
return;
}
}
assert(false);
}
}