Skip to content

Commit b95a014

Browse files
tomekl007pedja4
authored andcommitted
BAEL-738 (eugenp#1456) PUT and PATCH
* BAEL-724 code for put/patch article * BAEL-724 fix typo
1 parent b4b2ad3 commit b95a014

5 files changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.repository;
2+
3+
import org.baeldung.web.dto.HeavyResource;
4+
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
5+
6+
public class HeavyResourceRepository {
7+
8+
public void save(HeavyResource heavyResource) {
9+
}
10+
11+
public void save(HeavyResourceAddressPartialUpdate partialUpdate) {
12+
13+
}
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.web.controller;
2+
3+
4+
import org.baeldung.repository.HeavyResourceRepository;
5+
import org.baeldung.web.dto.HeavyResource;
6+
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@RestController
15+
public class HeavyResourceController {
16+
17+
private HeavyResourceRepository heavyResourceRepository = new HeavyResourceRepository();
18+
19+
@RequestMapping(value = "/heavy", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
20+
public ResponseEntity<?> saveResource(@RequestBody HeavyResource heavyResource) {
21+
heavyResourceRepository.save(heavyResource);
22+
return ResponseEntity.ok("resource saved");
23+
}
24+
25+
@RequestMapping(value = "/heavy", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
26+
public ResponseEntity<?> partialUpdateName(@RequestBody HeavyResourceAddressPartialUpdate partialUpdate) {
27+
heavyResourceRepository.save(partialUpdate);
28+
return ResponseEntity.ok("resource address updated");
29+
}
30+
31+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.baeldung.web.dto;
2+
3+
4+
public class HeavyResource {
5+
private Integer id;
6+
private String name;
7+
private String surname;
8+
private Integer age;
9+
private String address;
10+
11+
12+
public HeavyResource() {
13+
}
14+
15+
public HeavyResource(Integer id, String name, String surname, Integer age, String address) {
16+
this.id = id;
17+
this.name = name;
18+
this.surname = surname;
19+
this.age = age;
20+
this.address = address;
21+
}
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public String getSurname() {
40+
return surname;
41+
}
42+
43+
public void setSurname(String surname) {
44+
this.surname = surname;
45+
}
46+
47+
public Integer getAge() {
48+
return age;
49+
}
50+
51+
public void setAge(Integer age) {
52+
this.age = age;
53+
}
54+
55+
public String getAddress() {
56+
return address;
57+
}
58+
59+
public void setAddress(String address) {
60+
this.address = address;
61+
}
62+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.web.dto;
2+
3+
4+
public class HeavyResourceAddressPartialUpdate {
5+
private Integer id;
6+
private String address;
7+
8+
public HeavyResourceAddressPartialUpdate() {
9+
}
10+
11+
public HeavyResourceAddressPartialUpdate(Integer id, String address) {
12+
this.id = id;
13+
this.address = address;
14+
}
15+
16+
public Integer getId() {
17+
return id;
18+
}
19+
20+
public void setId(Integer id) {
21+
this.id = id;
22+
}
23+
24+
public String getAddress() {
25+
return address;
26+
}
27+
28+
public void setAddress(String address) {
29+
this.address = address;
30+
}
31+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.baeldung.web.controller;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.baeldung.config.WebConfig;
5+
import org.baeldung.web.dto.HeavyResource;
6+
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.http.MediaType;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
import org.springframework.test.context.web.WebAppConfiguration;
15+
import org.springframework.test.web.servlet.MockMvc;
16+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
17+
import org.springframework.web.context.WebApplicationContext;
18+
19+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22+
23+
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@ContextConfiguration(classes = WebConfig.class)
26+
@WebAppConfiguration
27+
public class HeavyResourceControllerTest {
28+
29+
private MockMvc mockMvc;
30+
31+
@Autowired
32+
private WebApplicationContext webApplicationContext;
33+
34+
private final ObjectMapper objectMapper = new ObjectMapper();
35+
36+
@Before
37+
public void setUp() {
38+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
39+
}
40+
41+
@Test
42+
public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Exception {
43+
mockMvc.perform(put("/heavy")
44+
.contentType(MediaType.APPLICATION_JSON_VALUE)
45+
.content(objectMapper.writeValueAsString(new HeavyResource(1, "Tom", "Jackson", 12, "heaven street")))
46+
).andExpect(status().isOk());
47+
}
48+
49+
@Test
50+
public void givenNewAddressOfResource_whenExecutePutRequest_thenUpdateResourcePartially() throws Exception {
51+
mockMvc.perform(patch("/heavy")
52+
.contentType(MediaType.APPLICATION_JSON_VALUE)
53+
.content(objectMapper.writeValueAsString(new HeavyResourceAddressPartialUpdate(1, "5th avenue")))
54+
).andExpect(status().isOk());
55+
}
56+
57+
}

0 commit comments

Comments
 (0)