From b95a014c8ac1c1566d2725c2055a142bfc701b61 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 20 Mar 2017 17:54:02 +0100 Subject: [PATCH] BAEL-738 (#1456) PUT and PATCH * BAEL-724 code for put/patch article * BAEL-724 fix typo --- .../repository/HeavyResourceRepository.java | 14 +++++ .../controller/HeavyResourceController.java | 31 ++++++++++ .../org/baeldung/web/dto/HeavyResource.java | 62 +++++++++++++++++++ .../HeavyResourceAddressPartialUpdate.java | 31 ++++++++++ .../HeavyResourceControllerTest.java | 57 +++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/HeavyResource.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java create mode 100644 spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java diff --git a/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java new file mode 100644 index 0000000000..cff78442d0 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java @@ -0,0 +1,14 @@ +package org.baeldung.repository; + +import org.baeldung.web.dto.HeavyResource; +import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; + +public class HeavyResourceRepository { + + public void save(HeavyResource heavyResource) { + } + + public void save(HeavyResourceAddressPartialUpdate partialUpdate) { + + } +} diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java b/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java new file mode 100644 index 0000000000..a2d5cfbd7b --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/controller/HeavyResourceController.java @@ -0,0 +1,31 @@ +package org.baeldung.web.controller; + + +import org.baeldung.repository.HeavyResourceRepository; +import org.baeldung.web.dto.HeavyResource; +import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HeavyResourceController { + + private HeavyResourceRepository heavyResourceRepository = new HeavyResourceRepository(); + + @RequestMapping(value = "/heavy", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity saveResource(@RequestBody HeavyResource heavyResource) { + heavyResourceRepository.save(heavyResource); + return ResponseEntity.ok("resource saved"); + } + + @RequestMapping(value = "/heavy", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity partialUpdateName(@RequestBody HeavyResourceAddressPartialUpdate partialUpdate) { + heavyResourceRepository.save(partialUpdate); + return ResponseEntity.ok("resource address updated"); + } + +} diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResource.java b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResource.java new file mode 100644 index 0000000000..934e76606f --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResource.java @@ -0,0 +1,62 @@ +package org.baeldung.web.dto; + + +public class HeavyResource { + private Integer id; + private String name; + private String surname; + private Integer age; + private String address; + + + public HeavyResource() { + } + + public HeavyResource(Integer id, String name, String surname, Integer age, String address) { + this.id = id; + this.name = name; + this.surname = surname; + this.age = age; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java new file mode 100644 index 0000000000..f90f02ea08 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/HeavyResourceAddressPartialUpdate.java @@ -0,0 +1,31 @@ +package org.baeldung.web.dto; + + +public class HeavyResourceAddressPartialUpdate { + private Integer id; + private String address; + + public HeavyResourceAddressPartialUpdate() { + } + + public HeavyResourceAddressPartialUpdate(Integer id, String address) { + this.id = id; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java new file mode 100644 index 0000000000..e68506701d --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/web/controller/HeavyResourceControllerTest.java @@ -0,0 +1,57 @@ +package org.baeldung.web.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.baeldung.config.WebConfig; +import org.baeldung.web.dto.HeavyResource; +import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = WebConfig.class) +@WebAppConfiguration +public class HeavyResourceControllerTest { + + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext webApplicationContext; + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test + public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Exception { + mockMvc.perform(put("/heavy") + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(new HeavyResource(1, "Tom", "Jackson", 12, "heaven street"))) + ).andExpect(status().isOk()); + } + + @Test + public void givenNewAddressOfResource_whenExecutePutRequest_thenUpdateResourcePartially() throws Exception { + mockMvc.perform(patch("/heavy") + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(new HeavyResourceAddressPartialUpdate(1, "5th avenue"))) + ).andExpect(status().isOk()); + } + +} \ No newline at end of file