Skip to content

Commit 88ca903

Browse files
enpy303maibin
authored andcommitted
BAEL 2730 (eugenp#6524)
* BAEL 2730 * jackson-dataformat-xml uncommented
1 parent 39a0eda commit 88ca903

4 files changed

Lines changed: 253 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.web.controller.students;
2+
3+
public class Student {
4+
5+
private long id;
6+
private String firstName;
7+
private String lastName;
8+
9+
public Student() {}
10+
11+
public Student(String firstName, String lastName) {
12+
super();
13+
this.firstName = firstName;
14+
this.lastName = lastName;
15+
}
16+
17+
public Student(long id, String firstName, String lastName) {
18+
super();
19+
this.id = id;
20+
this.firstName = firstName;
21+
this.lastName = lastName;
22+
}
23+
24+
public long getId() {
25+
return id;
26+
}
27+
28+
public void setId(long id) {
29+
this.id = id;
30+
}
31+
32+
public String getFirstName() {
33+
return firstName;
34+
}
35+
36+
public void setFirstName(String firstName) {
37+
this.firstName = firstName;
38+
}
39+
40+
public String getLastName() {
41+
return lastName;
42+
}
43+
44+
public void setLastName(String lastName) {
45+
this.lastName = lastName;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
51+
}
52+
53+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.web.controller.students;
2+
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
5+
import java.util.List;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.DeleteMapping;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.PutMapping;
14+
import org.springframework.web.bind.annotation.RequestBody;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RestController;
17+
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
18+
import com.baeldung.web.controller.students.StudentService;
19+
20+
@RestController
21+
@RequestMapping("/students")
22+
public class StudentController {
23+
24+
@Autowired
25+
private StudentService service;
26+
27+
@GetMapping("/")
28+
public List<Student> read() {
29+
return service.readAll();
30+
}
31+
32+
@GetMapping("/{id}")
33+
public ResponseEntity<Student> read(@PathVariable("id") Long id) {
34+
Student foundStudent = service.read(id);
35+
if (foundStudent == null) {
36+
return ResponseEntity.notFound().build();
37+
} else {
38+
return ResponseEntity.ok(foundStudent);
39+
}
40+
}
41+
42+
@PostMapping("/")
43+
public ResponseEntity<Student> create(@RequestBody Student student) throws URISyntaxException {
44+
Student createdStudent = service.create(student);
45+
46+
URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
47+
.path("/{id}")
48+
.buildAndExpand(createdStudent.getId())
49+
.toUri();
50+
51+
return ResponseEntity.created(uri)
52+
.body(createdStudent);
53+
54+
}
55+
56+
@PutMapping("/{id}")
57+
public ResponseEntity<Student> update(@RequestBody Student student, @PathVariable Long id) {
58+
Student updatedStudent = service.update(id, student);
59+
if (updatedStudent == null) {
60+
return ResponseEntity.notFound().build();
61+
} else {
62+
return ResponseEntity.ok(updatedStudent);
63+
}
64+
}
65+
66+
@DeleteMapping("/{id}")
67+
public ResponseEntity<Object> deleteStudent(@PathVariable Long id) {
68+
service.delete(id);
69+
70+
return ResponseEntity.noContent().build();
71+
}
72+
73+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.web.controller.students;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.concurrent.atomic.AtomicLong;
7+
import java.util.function.Function;
8+
import java.util.stream.Collectors;
9+
10+
import org.springframework.stereotype.Service;
11+
12+
@Service
13+
public class StudentService {
14+
15+
// DB repository mock
16+
private Map<Long, Student> repository = Arrays.asList(
17+
new Student[]{
18+
new Student(1, "Alan","Turing"),
19+
new Student(2, "Sebastian","Bach"),
20+
new Student(3, "Pablo","Picasso"),
21+
}).stream()
22+
.collect(Collectors.toConcurrentMap(s -> s.getId(), Function.identity()));
23+
24+
// DB id sequence mock
25+
private AtomicLong sequence = new AtomicLong(3);
26+
27+
public List<Student> readAll() {
28+
return repository.values().stream().collect(Collectors.toList());
29+
}
30+
31+
public Student read(Long id) {
32+
return repository.get(id);
33+
}
34+
35+
public Student create(Student student) {
36+
long key = sequence.incrementAndGet();
37+
student.setId(key);
38+
repository.put(key, student);
39+
return student;
40+
}
41+
42+
public Student update(Long id, Student student) {
43+
student.setId(id);
44+
Student oldStudent = repository.replace(id, student);
45+
return oldStudent == null ? null : student;
46+
}
47+
48+
public void delete(Long id) {
49+
repository.remove(id);
50+
}
51+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.web;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.web.server.MediaTypeNotSupportedStatusException;
12+
13+
import com.baeldung.web.controller.students.Student;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
15+
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
19+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
22+
23+
@RunWith(SpringRunner.class)
24+
@SpringBootTest
25+
@AutoConfigureMockMvc
26+
public class StudentControllerIntegrationTest {
27+
28+
private static final String STUDENTS_PATH = "/students/";
29+
30+
@Autowired
31+
private MockMvc mockMvc;
32+
33+
@Test
34+
public void whenReadAll_thenStatusIsOk() throws Exception {
35+
this.mockMvc.perform(get(STUDENTS_PATH))
36+
.andExpect(status().isOk());
37+
}
38+
39+
@Test
40+
public void whenReadOne_thenStatusIsOk() throws Exception {
41+
this.mockMvc.perform(get(STUDENTS_PATH + 1))
42+
.andExpect(status().isOk());
43+
}
44+
45+
@Test
46+
public void whenCreate_thenStatusIsCreated() throws Exception {
47+
Student student = new Student(10, "Albert", "Einstein");
48+
this.mockMvc.perform(post(STUDENTS_PATH).content(asJsonString(student))
49+
.contentType(MediaType.APPLICATION_JSON_VALUE))
50+
.andExpect(status().isCreated());
51+
}
52+
53+
@Test
54+
public void whenUpdate_thenStatusIsOk() throws Exception {
55+
Student student = new Student(1, "Nikola", "Tesla");
56+
this.mockMvc.perform(put(STUDENTS_PATH + 1)
57+
.content(asJsonString(student))
58+
.contentType(MediaType.APPLICATION_JSON_VALUE))
59+
.andExpect(status().isOk());
60+
}
61+
62+
@Test
63+
public void whenDelete_thenStatusIsNoContent() throws Exception {
64+
this.mockMvc.perform(delete(STUDENTS_PATH + 3))
65+
.andExpect(status().isNoContent());
66+
}
67+
68+
private String asJsonString(final Object obj) {
69+
try {
70+
return new ObjectMapper().writeValueAsString(obj);
71+
} catch (Exception e) {
72+
throw new RuntimeException(e);
73+
}
74+
}
75+
76+
}

0 commit comments

Comments
 (0)