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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
@RequestMapping("student")
public class StudentController {

StudentService service = new StudentService();
private final StudentService service;

public StudentController(StudentService service) {
this.service = service;
}

@GetMapping
public List<Student> getAllStudents() {
Expand All @@ -30,7 +34,7 @@ public Student addStudent(@RequestBody Student student) {
}

@GetMapping("allStudents")
public List<Collection<Student>> listAllStudents() {
public List<Student> listAllStudents() {
return service.getAllStudents();
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/example/cgn221springstudent/model/Student.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.cgn221springstudent.model;

import java.util.Objects;

public class Student {

String name;
Expand All @@ -25,4 +27,25 @@ public String getId() {
public void setId(String id) {
this.id = id;
}

@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(name, student.name) && Objects.equals(id, student.id);
}

@Override
public int hashCode() {
return Objects.hash(name, id);
}

@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.example.cgn221springstudent.Repository;
package com.example.cgn221springstudent.repository;

import com.example.cgn221springstudent.model.Student;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@Service
public class StudentRepo {

Map<String, Student> students = new HashMap();

public StudentRepo() {
}
private final Map<String, Student> students = new HashMap<>();

public Student addStudent(Student student) {
students.put(student.getId(), student);
Expand All @@ -23,8 +20,8 @@ public Student getById(String id) {
return students.get(id);
}

public List<Collection<Student>> getAllStudents() {
return List.of(students.values());
public List<Student> getAllStudents() {
return new ArrayList<>(students.values());
}

public Student removeStudent(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.example.cgn221springstudent.service;

import com.example.cgn221springstudent.Repository.StudentRepo;
import com.example.cgn221springstudent.repository.StudentRepo;
import com.example.cgn221springstudent.model.Student;
import org.springframework.stereotype.Service;

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

@Service
public class StudentService {

StudentRepo repo = new StudentRepo();
private final StudentRepo repo;

public StudentService(StudentRepo repo) {
this.repo = repo;
}

public Student getStudentById(String id) {
return repo.getById(id);
Expand All @@ -20,7 +25,7 @@ public Student addStudent(Student student) {
return repo.addStudent(student);
}

public List<Collection<Student>> getAllStudents() {
public List<Student> getAllStudents() {
return repo.getAllStudents();
}

Expand All @@ -32,6 +37,19 @@ public Student updateStudent(Student student) {
return repo.updateStudent(student);
}

public List<Student> getStudentByName(String name) {
List<Student> allStudents = repo.getAllStudents();

List<Student> studentsWithName = new ArrayList<>();

for (Student currentStudent: allStudents) {
if (currentStudent.getName().equals(name)) {
studentsWithName.add(currentStudent);
}
}

return studentsWithName;
}

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.example.cgn221springstudent.service;

import com.example.cgn221springstudent.model.Student;
import com.example.cgn221springstudent.repository.StudentRepo;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class StudentServiceTest {

private StudentRepo studentRepo = mock(StudentRepo.class);
private StudentService studentService = new StudentService(studentRepo);

@Test
void getStudentById_whenId1_thenReturnStudentWithId1() {
//GIVEN
when(studentRepo.getById("1")).thenReturn(new Student("Florian", "1"));

//WHEN
Student actual = studentService.getStudentById("1");

//THEN
Student expected = new Student("Florian", "1");

verify(studentRepo).getById("1");
assertEquals(expected, actual);
}

@Test
void deleteStudent_whenId2_thenReturnDeletedStudent() {
//GIVEN
when(studentRepo.removeStudent("2")).thenReturn(new Student("Deleted Student", "2"));

//WHEN
Student actual = studentService.deleteStudent("2");

//THEN
Student expected = new Student("Deleted Student", "2");


verify(studentRepo).removeStudent("2");
assertEquals(expected, actual);
}

@Test
void getStudentByName() {
//GIVEN
when(studentRepo.getAllStudents()).thenReturn(List.of(new Student("Florian", "1"), new Student("Matthias", "2")));

//WHEN
List<Student> actual = studentService.getStudentByName("Matthias");

//THEN
List<Student> expected = List.of(new Student("Matthias", "2"));

verify(studentRepo).getAllStudents();
assertEquals(expected, actual);
}

@Test
void getStudentByName_whenFlorian_thenReturnFlorian() {
//GIVEN
when(studentRepo.getAllStudents()).thenReturn(List.of(new Student("Florian", "1"), new Student("Matthias", "2")));

//WHEN
List<Student> actual = studentService.getStudentByName("Florian");

//THEN
List<Student> expected = List.of(new Student("Florian", "1"));

verify(studentRepo).getAllStudents();
assertEquals(expected, actual);
}
}