findAllByOrderByGpaDesc() {
37 | return studentRepository.findAllByOrderByGpaDesc();
38 | }
39 |
40 | @Override
41 | public Student saveOrUpdateStudent(Student student) {
42 | return studentRepository.save(student);
43 | }
44 |
45 | @Override
46 | public void deleteStudentById(String id) {
47 | studentRepository.deleteById(id);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/com/student/information/system/util/ObjectMapperUtils.java:
--------------------------------------------------------------------------------
1 | package com.student.information.system.util;
2 |
3 | import org.modelmapper.ModelMapper;
4 | import org.modelmapper.convention.MatchingStrategies;
5 |
6 | import java.util.Collection;
7 | import java.util.List;
8 | import java.util.stream.Collectors;
9 |
10 | /**
11 | * @author ragcrix
12 | */
13 | public class ObjectMapperUtils {
14 |
15 | private static ModelMapper modelMapper;
16 |
17 | /**
18 | * Model mapper property setting are specified in the following block.
19 | * Default property matching strategy is set to Strict see {@link MatchingStrategies}
20 | * Custom mappings are added using {@link ModelMapper#addMappings(PropertyMap)}
21 | */
22 | static {
23 | modelMapper = new ModelMapper();
24 | modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
25 | }
26 |
27 | /**
28 | * Hide from public usage.
29 | */
30 | private ObjectMapperUtils() {
31 | }
32 |
33 | /**
34 | * Note: outClass object must have default constructor with no arguments
35 | *
36 | * @param type of result object.
37 | * @param type of source object to map from.
38 | * @param entity entity that needs to be mapped.
39 | * @param outClass class of result object.
40 | * @return new object of outClass
type.
41 | */
42 | public static D map(final T entity, Class outClass) {
43 | return modelMapper.map(entity, outClass);
44 | }
45 |
46 | /**
47 | * Note: outClass object must have default constructor with no arguments
48 | *
49 | * @param entityList list of entities that needs to be mapped
50 | * @param outCLass class of result list element
51 | * @param type of objects in result list
52 | * @param type of entity in entityList
53 | * @return list of mapped object with
type.
54 | */
55 | public static List mapAll(final Collection entityList, Class outCLass) {
56 | return entityList.stream()
57 | .map(entity -> map(entity, outCLass))
58 | .collect(Collectors.toList());
59 | }
60 | }
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | #server
2 | server.port=8081
3 |
4 | #mongodb
5 | spring.data.mongodb.host=localhost
6 | spring.data.mongodb.port=27017
7 | spring.data.mongodb.database=test
8 |
9 | #logging
10 | logging.level.org.springframework.data=debug
11 | logging.level.=errors
--------------------------------------------------------------------------------
/src/test/java/com/student/information/system/controller/StudentRestControllerTest.java:
--------------------------------------------------------------------------------
1 | package com.student.information.system.controller;
2 |
3 | import com.fasterxml.jackson.databind.ObjectMapper;
4 | import com.student.information.system.model.Student;
5 | import com.student.information.system.service.StudentService;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.mockito.Mockito;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
12 | import org.springframework.boot.test.mock.mockito.MockBean;
13 | import org.springframework.http.MediaType;
14 | import org.springframework.test.context.junit4.SpringRunner;
15 | import org.springframework.test.web.servlet.MockMvc;
16 |
17 | import java.util.Arrays;
18 |
19 | import static org.hamcrest.Matchers.hasSize;
20 | import static org.hamcrest.Matchers.is;
21 | import static org.mockito.BDDMockito.given;
22 | import static org.mockito.Mockito.any;
23 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
24 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
25 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26 |
27 | /**
28 | * @author ragcrix
29 | */
30 | @RunWith(SpringRunner.class)
31 | @WebMvcTest
32 | public class StudentRestControllerTest {
33 |
34 | @Autowired
35 | private MockMvc mvc;
36 |
37 | @MockBean
38 | private StudentService studentService;
39 |
40 | private ObjectMapper objectMapper = new ObjectMapper();
41 |
42 | private Student ragcrix;
43 | private Student yigit;
44 |
45 | private final Long ragcrixStudentNumber = 23L;
46 | private final Long yigitStudentNumber = 91L;
47 |
48 | @Before
49 | public void setup() {
50 | ragcrix = new Student();
51 | ragcrix.setId("aBc123");
52 | ragcrix.setName("ragcrix");
53 | ragcrix.setEmail("ragcrix@rg.com");
54 | ragcrix.setStudentNumber(ragcrixStudentNumber);
55 | ragcrix.setCourseList(Arrays.asList("Math", "Science"));
56 | ragcrix.setGpa(3.37f);
57 |
58 | yigit = new Student();
59 | yigit.setId("dEf345");
60 | yigit.setName("yigit");
61 | yigit.setEmail("yigit@ygt.com");
62 | yigit.setStudentNumber(yigitStudentNumber);
63 | yigit.setCourseList(Arrays.asList("Turkish", "German"));
64 | yigit.setGpa(3.58f);
65 | }
66 |
67 | @Test
68 | public void givenStudents_whenGetAllStudents_thenReturnJsonArray() throws Exception {
69 | given(studentService.findAll()).willReturn(Arrays.asList(ragcrix));
70 |
71 | mvc.perform(get("/students/")
72 | .contentType(MediaType.APPLICATION_JSON))
73 | .andExpect(status().isOk())
74 | .andExpect(jsonPath("$", hasSize(1)))
75 | .andExpect(jsonPath("$[0].name", is(ragcrix.getName())));
76 | }
77 |
78 | @Test
79 | public void givenStudent_whenFindByStudentNumber_thenReturnJsonArray() throws Exception {
80 | given(studentService.findByStudentNumber(ragcrixStudentNumber)).willReturn(ragcrix);
81 |
82 | mvc.perform(get("/students/byStudentNumber/{studentNumber}", ragcrixStudentNumber)
83 | .contentType(MediaType.APPLICATION_JSON))
84 | .andExpect(status().isOk())
85 | .andExpect(jsonPath("$.name", is(ragcrix.getName())));
86 | }
87 |
88 | @Test
89 | public void givenStudent_whenFindAllByOrderByGpaDesc_thenReturnJsonArray() throws Exception {
90 | given(studentService.findAllByOrderByGpaDesc()).willReturn(Arrays.asList(yigit, ragcrix));
91 |
92 | mvc.perform(get("/students/orderByGpa/")
93 | .contentType(MediaType.APPLICATION_JSON))
94 | .andExpect(status().isOk())
95 | .andExpect(jsonPath("$", hasSize(2)))
96 | .andExpect(jsonPath("$[0].name", is(yigit.getName())));
97 | }
98 |
99 | @Test
100 | public void saveStudent_itShouldReturnStatusOk() throws Exception {
101 | given(studentService.saveOrUpdateStudent(any(Student.class))).willReturn(yigit);
102 |
103 | String jsonString = objectMapper.writeValueAsString(yigit);
104 |
105 | mvc.perform(post("/students/save/")
106 | .contentType(MediaType.APPLICATION_JSON).content(jsonString))
107 | .andExpect(status().isOk());
108 | }
109 |
110 | @Test
111 | public void deleteStudentByStudentNumber_itShouldReturnStatusOk() throws Exception {
112 | given(studentService.findByStudentNumber(ragcrixStudentNumber)).willReturn(ragcrix);
113 | Mockito.doNothing().when(studentService).deleteStudentById(any(String.class));
114 |
115 | mvc.perform(delete("/students/delete/{studentNumber}", ragcrixStudentNumber)
116 | .contentType(MediaType.APPLICATION_JSON))
117 | .andExpect(status().isOk());
118 | }
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/src/test/java/com/student/information/system/service/StudentServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.student.information.system.service;
2 |
3 | import com.student.information.system.model.Student;
4 | import com.student.information.system.repository.StudentRepository;
5 | import com.student.information.system.service.impl.StudentServiceImpl;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.mockito.Mockito;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.boot.test.context.TestConfiguration;
12 | import org.springframework.boot.test.mock.mockito.MockBean;
13 | import org.springframework.context.annotation.Bean;
14 | import org.springframework.test.context.junit4.SpringRunner;
15 |
16 | import java.util.ArrayList;
17 | import java.util.Arrays;
18 | import java.util.Comparator;
19 | import java.util.List;
20 | import java.util.stream.Collectors;
21 |
22 | import static org.junit.Assert.assertEquals;
23 | import static org.junit.Assert.assertNotNull;
24 |
25 | /**
26 | * @author ragcrix
27 | */
28 | @RunWith(SpringRunner.class)
29 | public class StudentServiceTest {
30 |
31 | @TestConfiguration
32 | static class StudentServiceImplTestContextConfiguration {
33 | @Bean
34 | public StudentService studentService() {
35 | return new StudentServiceImpl();
36 | }
37 | }
38 |
39 | @Autowired
40 | private StudentService studentService;
41 |
42 | @MockBean
43 | private StudentRepository studentRepository;
44 |
45 | private Student ragcrix;
46 | private Student yigit;
47 |
48 | private final Long ragcrixStudentNumber = 23L;
49 | private final Long yigitStudentNumber = 91L;
50 | private final String ragcrixEmail = "ragcrix@rg.com";
51 | private final String yigitEmail = "yigit@ygt.com";
52 | private final List students = new ArrayList<>();
53 |
54 | @Before
55 | public void setup() {
56 | ragcrix = new Student();
57 | ragcrix.setId("aBc123");
58 | ragcrix.setName("ragcrix");
59 | ragcrix.setEmail(ragcrixEmail);
60 | ragcrix.setStudentNumber(ragcrixStudentNumber);
61 | ragcrix.setCourseList(Arrays.asList("Math", "Science"));
62 | ragcrix.setGpa(3.37f);
63 |
64 | yigit = new Student();
65 | yigit.setId("dEf345");
66 | yigit.setName("yigit");
67 | yigit.setEmail(yigitEmail);
68 | yigit.setStudentNumber(yigitStudentNumber);
69 | yigit.setCourseList(Arrays.asList("Turkish", "German"));
70 | yigit.setGpa(3.58f);
71 |
72 | students.add(ragcrix);
73 | students.add(yigit);
74 |
75 | Mockito.when(studentRepository.findAll()).thenReturn(students);
76 |
77 | Mockito.when(studentRepository.findByStudentNumber(ragcrixStudentNumber))
78 | .thenReturn(ragcrix);
79 |
80 | Mockito.when(studentRepository.findByEmail(yigitEmail))
81 | .thenReturn(yigit);
82 |
83 | Mockito.when(studentRepository.findAllByOrderByGpaDesc())
84 | .thenReturn(students.stream().sorted(
85 | Comparator.comparing(Student::getGpa).reversed()).collect(Collectors.toList()));
86 |
87 | Mockito.when(studentRepository.save(ragcrix)).thenReturn(ragcrix);
88 | }
89 |
90 | @Test
91 | public void testFindAll_thenStudentListShouldBeReturned() {
92 | List foundStudents = studentService.findAll();
93 |
94 | assertNotNull(foundStudents);
95 | assertEquals(2, foundStudents.size());
96 | }
97 |
98 | @Test
99 | public void testFindByStudentNumber23_thenRagcrixShouldBeReturned() {
100 | Student found = studentService.findByStudentNumber(ragcrixStudentNumber);
101 |
102 | assertNotNull(found);
103 | assertEquals(ragcrix.getName(), found.getName());
104 | assertEquals(ragcrix.getId(), found.getId());
105 | }
106 |
107 | @Test
108 | public void testFindByEmail_thenYigitShouldBeReturned() {
109 | Student found = studentService.findByEmail(yigitEmail);
110 |
111 | assertNotNull(found);
112 | assertEquals(yigit.getName(), found.getName());
113 | assertEquals(yigit.getId(), found.getId());
114 | }
115 |
116 | @Test
117 | public void testFindAllByOrderByGpaDesc_thenStudentsShouldBeReturned_byGPADesc() {
118 | List foundStudents = studentService.findAllByOrderByGpaDesc();
119 |
120 | assertNotNull(foundStudents);
121 | assertEquals(2, foundStudents.size());
122 | assertEquals(yigit.getName(), foundStudents.get(0).getName());
123 | assertEquals(yigit.getId(), foundStudents.get(0).getId());
124 | }
125 |
126 | @Test
127 | public void testSaveOrUpdateStudent_thenStudentShouldBeReturned() {
128 | Student found = studentService.saveOrUpdateStudent(ragcrix);
129 |
130 | assertNotNull(found);
131 | assertEquals(ragcrix.getName(), found.getName());
132 | assertEquals(ragcrix.getId(), found.getId());
133 | }
134 |
135 | @Test
136 | public void testDeleteStudentById() {
137 | studentService.deleteStudentById(ragcrix.getId());
138 |
139 | Mockito.verify(studentRepository, Mockito.times(1))
140 | .deleteById(ragcrix.getId());
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------