forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sample code for update to BAEL-743 (eugenp#1669)
- Loading branch information
1 parent
874f3a0
commit 7d6bf29
Showing
4 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
spring-rest/src/main/java/org/baeldung/web/controller/BazzNewMappingsExampleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.baeldung.web.controller; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.baeldung.web.dto.Bazz; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
||
@RestController | ||
@RequestMapping("/bazz") | ||
public class BazzNewMappingsExampleController { | ||
|
||
@GetMapping | ||
public ResponseEntity<?> getBazzs() throws JsonProcessingException{ | ||
List<Bazz> data = Arrays.asList( | ||
new Bazz("1", "Bazz1"), | ||
new Bazz("2", "Bazz2"), | ||
new Bazz("3", "Bazz3"), | ||
new Bazz("4", "Bazz4")); | ||
return new ResponseEntity<>(data, HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<?> getBazz(@PathVariable String id){ | ||
return new ResponseEntity<>(new Bazz(id, "Bazz"+id), HttpStatus.OK); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<?> newBazz(@RequestParam("name") String name){ | ||
return new ResponseEntity<>(new Bazz("5", name), HttpStatus.OK); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<?> updateBazz(@PathVariable String id, | ||
@RequestParam("name") String name){ | ||
return new ResponseEntity<>(new Bazz(id, name), HttpStatus.OK); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<?> deleteBazz(@PathVariable String id){ | ||
return new ResponseEntity<>(new Bazz(id), HttpStatus.OK); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.baeldung.web.dto; | ||
|
||
public class Bazz { | ||
|
||
|
||
public String id; | ||
public String name; | ||
|
||
public Bazz(String id){ | ||
this.id = id; | ||
} | ||
public Bazz(String id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Bazz [id=" + id + ", name=" + name + "]"; | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
spring-rest/src/test/java/org/baeldung/web/test/BazzNewMappingsExampleControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
|
||
package org.baeldung.web.test; | ||
|
||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.baeldung.config.WebConfig; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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; | ||
|
||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = WebConfig.class) | ||
@WebAppConfiguration | ||
public class BazzNewMappingsExampleControllerTest { | ||
|
||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private WebApplicationContext webApplicationContext; | ||
|
||
@Before | ||
public void setUp() { | ||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); | ||
} | ||
|
||
@Test | ||
public void whenGettingAllBazz_thenSuccess() throws Exception{ | ||
mockMvc.perform(get("/bazz")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", hasSize(4))) | ||
.andExpect(jsonPath("$[1].id", is("2"))) | ||
.andExpect(jsonPath("$[1].name", is("Bazz2"))); | ||
} | ||
|
||
@Test | ||
public void whenGettingABazz_thenSuccess() throws Exception{ | ||
mockMvc.perform(get("/bazz/1")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id", is("1"))) | ||
.andExpect(jsonPath("$.name", is("Bazz1"))); | ||
} | ||
|
||
@Test | ||
public void whenAddingABazz_thenSuccess() throws Exception{ | ||
mockMvc.perform(post("/bazz").param("name", "Bazz5")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id", is("5"))) | ||
.andExpect(jsonPath("$.name", is("Bazz5"))); | ||
} | ||
|
||
@Test | ||
public void whenUpdatingABazz_thenSuccess() throws Exception{ | ||
mockMvc.perform(put("/bazz/5").param("name", "Bazz6")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id", is("5"))) | ||
.andExpect(jsonPath("$.name", is("Bazz6"))); | ||
} | ||
|
||
@Test | ||
public void whenDeletingABazz_thenSuccess() throws Exception{ | ||
mockMvc.perform(delete("/bazz/5")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id", is("5"))); | ||
} | ||
} |