Skip to content

Commit 7d6bf29

Browse files
sanaulla123maibin
authored andcommitted
sample code for update to BAEL-743 (eugenp#1669)
1 parent 874f3a0 commit 7d6bf29

File tree

4 files changed

+165
-1
lines changed

4 files changed

+165
-1
lines changed

spring-rest/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@
188188
<artifactId>kryo</artifactId>
189189
<version>${kryo.version}</version>
190190
</dependency>
191+
192+
<dependency>
193+
<groupId>com.jayway.jsonpath</groupId>
194+
<artifactId>json-path</artifactId>
195+
</dependency>
196+
191197
</dependencies>
192198

193199
<build>
@@ -358,7 +364,8 @@
358364

359365
<!-- okhttp -->
360366
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version>
361-
367+
368+
<json.path.version>2.2.0</json.path.version>
362369
</properties>
363370

364371
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.baeldung.web.controller;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.baeldung.web.dto.Bazz;
7+
import org.springframework.http.HttpStatus;
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.RequestMapping;
15+
import org.springframework.web.bind.annotation.RequestParam;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
import com.fasterxml.jackson.core.JsonProcessingException;
19+
20+
@RestController
21+
@RequestMapping("/bazz")
22+
public class BazzNewMappingsExampleController {
23+
24+
@GetMapping
25+
public ResponseEntity<?> getBazzs() throws JsonProcessingException{
26+
List<Bazz> data = Arrays.asList(
27+
new Bazz("1", "Bazz1"),
28+
new Bazz("2", "Bazz2"),
29+
new Bazz("3", "Bazz3"),
30+
new Bazz("4", "Bazz4"));
31+
return new ResponseEntity<>(data, HttpStatus.OK);
32+
}
33+
34+
@GetMapping("/{id}")
35+
public ResponseEntity<?> getBazz(@PathVariable String id){
36+
return new ResponseEntity<>(new Bazz(id, "Bazz"+id), HttpStatus.OK);
37+
}
38+
39+
@PostMapping
40+
public ResponseEntity<?> newBazz(@RequestParam("name") String name){
41+
return new ResponseEntity<>(new Bazz("5", name), HttpStatus.OK);
42+
}
43+
44+
@PutMapping("/{id}")
45+
public ResponseEntity<?> updateBazz(@PathVariable String id,
46+
@RequestParam("name") String name){
47+
return new ResponseEntity<>(new Bazz(id, name), HttpStatus.OK);
48+
}
49+
50+
@DeleteMapping("/{id}")
51+
public ResponseEntity<?> deleteBazz(@PathVariable String id){
52+
return new ResponseEntity<>(new Bazz(id), HttpStatus.OK);
53+
}
54+
55+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.web.dto;
2+
3+
public class Bazz {
4+
5+
6+
public String id;
7+
public String name;
8+
9+
public Bazz(String id){
10+
this.id = id;
11+
}
12+
public Bazz(String id, String name) {
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "Bazz [id=" + id + ", name=" + name + "]";
20+
}
21+
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
package org.baeldung.web.test;
3+
4+
import static org.hamcrest.Matchers.hasSize;
5+
import static org.hamcrest.Matchers.is;
6+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
9+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
12+
13+
import org.baeldung.config.WebConfig;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.test.context.ContextConfiguration;
19+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
20+
import org.springframework.test.context.web.WebAppConfiguration;
21+
import org.springframework.test.web.servlet.MockMvc;
22+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
23+
import org.springframework.web.context.WebApplicationContext;
24+
25+
26+
@RunWith(SpringJUnit4ClassRunner.class)
27+
@ContextConfiguration(classes = WebConfig.class)
28+
@WebAppConfiguration
29+
public class BazzNewMappingsExampleControllerTest {
30+
31+
private MockMvc mockMvc;
32+
33+
@Autowired
34+
private WebApplicationContext webApplicationContext;
35+
36+
@Before
37+
public void setUp() {
38+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
39+
}
40+
41+
@Test
42+
public void whenGettingAllBazz_thenSuccess() throws Exception{
43+
mockMvc.perform(get("/bazz"))
44+
.andExpect(status().isOk())
45+
.andExpect(jsonPath("$", hasSize(4)))
46+
.andExpect(jsonPath("$[1].id", is("2")))
47+
.andExpect(jsonPath("$[1].name", is("Bazz2")));
48+
}
49+
50+
@Test
51+
public void whenGettingABazz_thenSuccess() throws Exception{
52+
mockMvc.perform(get("/bazz/1"))
53+
.andExpect(status().isOk())
54+
.andExpect(jsonPath("$.id", is("1")))
55+
.andExpect(jsonPath("$.name", is("Bazz1")));
56+
}
57+
58+
@Test
59+
public void whenAddingABazz_thenSuccess() throws Exception{
60+
mockMvc.perform(post("/bazz").param("name", "Bazz5"))
61+
.andExpect(status().isOk())
62+
.andExpect(jsonPath("$.id", is("5")))
63+
.andExpect(jsonPath("$.name", is("Bazz5")));
64+
}
65+
66+
@Test
67+
public void whenUpdatingABazz_thenSuccess() throws Exception{
68+
mockMvc.perform(put("/bazz/5").param("name", "Bazz6"))
69+
.andExpect(status().isOk())
70+
.andExpect(jsonPath("$.id", is("5")))
71+
.andExpect(jsonPath("$.name", is("Bazz6")));
72+
}
73+
74+
@Test
75+
public void whenDeletingABazz_thenSuccess() throws Exception{
76+
mockMvc.perform(delete("/bazz/5"))
77+
.andExpect(status().isOk())
78+
.andExpect(jsonPath("$.id", is("5")));
79+
}
80+
}

0 commit comments

Comments
 (0)