Skip to content

Commit 0950c53

Browse files
M-Abdelbasetmaibin
authored andcommitted
Bael-3395: Spring optional path variables (eugenp#8106)
* initial test cases * changes in @RequestMapping
1 parent 3622cd9 commit 0950c53

11 files changed

+435
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.model;
2+
3+
public class Article {
4+
5+
public static final Article DEFAULT_ARTICLE = new Article(12);
6+
7+
private Integer id;
8+
9+
public Article(Integer articleId) {
10+
this.id = articleId;
11+
}
12+
13+
public Integer getId() {
14+
return id;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "Article [id=" + id + "]";
20+
}
21+
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
4+
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import com.baeldung.model.Article;
10+
11+
@RestController
12+
public class ArticleViewerController {
13+
14+
@RequestMapping(value = {"/article", "/article/{id}"})
15+
public Article getArticle(@PathVariable(name = "id") Integer articleId) {
16+
17+
if (articleId != null) {
18+
return new Article(articleId);
19+
} else {
20+
return DEFAULT_ARTICLE;
21+
}
22+
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
4+
5+
import java.util.Map;
6+
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import com.baeldung.model.Article;
12+
13+
@RestController
14+
@RequestMapping(value = "/mapParam")
15+
public class ArticleViewerWithMapParamController {
16+
17+
@RequestMapping(value = {"/article", "/article/{id}"})
18+
public Article getArticle(@PathVariable Map<String, String> pathVarsMap) {
19+
20+
String articleId = pathVarsMap.get("id");
21+
22+
if (articleId != null) {
23+
return new Article(Integer.valueOf(articleId));
24+
} else {
25+
return DEFAULT_ARTICLE;
26+
}
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
4+
5+
import java.util.Optional;
6+
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import com.baeldung.model.Article;;
12+
13+
@RestController
14+
@RequestMapping("/optionalParam")
15+
public class ArticleViewerWithOptionalParamController {
16+
17+
@RequestMapping(value = {"/article", "/article/{id}"})
18+
public Article getArticle(@PathVariable(name = "id") Optional<Integer> optionalArticleId) {
19+
20+
if(optionalArticleId.isPresent()) {
21+
Integer articleId = optionalArticleId.get();
22+
return new Article(articleId);
23+
}else {
24+
return DEFAULT_ARTICLE;
25+
}
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
4+
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import com.baeldung.model.Article;;
10+
11+
@RestController
12+
@RequestMapping(value = "/requiredAttribute")
13+
public class ArticleViewerWithRequiredAttributeController {
14+
15+
@RequestMapping(value = {"/article", "/article/{id}"})
16+
public Article getArticle(@PathVariable(name = "id", required = false) Integer articleId) {
17+
18+
if (articleId != null) {
19+
return new Article(articleId);
20+
} else {
21+
return DEFAULT_ARTICLE;
22+
}
23+
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import static com.baeldung.model.Article.DEFAULT_ARTICLE;
4+
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import com.baeldung.model.Article;
10+
11+
@RestController
12+
@RequestMapping(value = "/seperateMethods")
13+
public class ArticleViewerWithTwoSeparateMethodsController {
14+
15+
@RequestMapping(value = "/article/{id}")
16+
public Article getArticle(@PathVariable(name = "id") Integer articleId) {
17+
18+
return new Article(articleId);
19+
}
20+
21+
@RequestMapping(value = "/article")
22+
public Article getDefaultArticle() {
23+
24+
return DEFAULT_ARTICLE;
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
import org.springframework.test.context.web.WebAppConfiguration;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
12+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
13+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14+
import org.springframework.web.context.WebApplicationContext;
15+
16+
import com.baeldung.spring.web.config.WebConfig;
17+
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@WebAppConfiguration
20+
@ContextConfiguration(classes = { WebConfig.class })
21+
public class ArticleViewerControllerIntegrationTest {
22+
23+
@Autowired
24+
private WebApplicationContext wac;
25+
26+
private MockMvc mockMvc;
27+
28+
@Before
29+
public void setup() throws Exception {
30+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
31+
}
32+
33+
@Test
34+
public void whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
35+
36+
int articleId = 5;
37+
38+
this.mockMvc
39+
.perform(MockMvcRequestBuilders.get("/article/{id}", articleId))
40+
.andExpect(MockMvcResultMatchers.status().isOk())
41+
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
42+
43+
}
44+
45+
@Test
46+
public void whenIdPathVariableIsNotPassed_thenResponse500() throws Exception {
47+
48+
this.mockMvc
49+
.perform(MockMvcRequestBuilders.get("/article"))
50+
.andExpect(MockMvcResultMatchers.status().isInternalServerError());
51+
52+
}
53+
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
import org.springframework.test.context.web.WebAppConfiguration;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
12+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
13+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14+
import org.springframework.web.context.WebApplicationContext;
15+
16+
import com.baeldung.model.Article;
17+
import com.baeldung.spring.web.config.WebConfig;
18+
19+
@RunWith(SpringJUnit4ClassRunner.class)
20+
@WebAppConfiguration
21+
@ContextConfiguration(classes = { WebConfig.class })
22+
public class ArticleViewerControllerWithOptionalParamIntegrationTest {
23+
24+
@Autowired
25+
private WebApplicationContext wac;
26+
27+
private MockMvc mockMvc;
28+
29+
@Before
30+
public void setup() throws Exception {
31+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
32+
}
33+
34+
@Test
35+
public void givenOPtionalParam_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
36+
37+
int articleId = 154;
38+
39+
this.mockMvc
40+
.perform(MockMvcRequestBuilders.get("/optionalParam/article/{id}", articleId))
41+
.andExpect(MockMvcResultMatchers.status().isOk())
42+
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
43+
44+
}
45+
46+
@Test
47+
public void givenOPtionalParam_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
48+
49+
this.mockMvc
50+
.perform(MockMvcRequestBuilders.get("/optionalParam/article"))
51+
.andExpect(MockMvcResultMatchers.status().isOk())
52+
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
53+
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
import org.springframework.test.context.web.WebAppConfiguration;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
12+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
13+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14+
import org.springframework.web.context.WebApplicationContext;
15+
16+
import com.baeldung.model.Article;
17+
import com.baeldung.spring.web.config.WebConfig;
18+
19+
@RunWith(SpringJUnit4ClassRunner.class)
20+
@WebAppConfiguration
21+
@ContextConfiguration(classes = { WebConfig.class })
22+
public class ArticleViewerControllerWithRequiredAttributeIntegrationTest {
23+
24+
@Autowired
25+
private WebApplicationContext wac;
26+
27+
private MockMvc mockMvc;
28+
29+
@Before
30+
public void setup() throws Exception {
31+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
32+
}
33+
34+
@Test
35+
public void givenRequiredAttributeIsFalse_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
36+
37+
int articleId = 154;
38+
39+
this.mockMvc
40+
.perform(MockMvcRequestBuilders.get("/requiredAttribute/article/{id}", articleId))
41+
.andExpect(MockMvcResultMatchers.status().isOk())
42+
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
43+
44+
}
45+
46+
@Test
47+
public void givenRequiredAttributeIsFalse_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
48+
49+
this.mockMvc
50+
.perform(MockMvcRequestBuilders.get("/requiredAttribute/article"))
51+
.andExpect(MockMvcResultMatchers.status().isOk())
52+
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
53+
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.web.controller.optionalpathvars;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
import org.springframework.test.context.web.WebAppConfiguration;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
12+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
13+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14+
import org.springframework.web.context.WebApplicationContext;
15+
16+
import com.baeldung.model.Article;
17+
import com.baeldung.spring.web.config.WebConfig;
18+
19+
@RunWith(SpringJUnit4ClassRunner.class)
20+
@WebAppConfiguration
21+
@ContextConfiguration(classes = { WebConfig.class })
22+
public class ArticleViewerWithMapParamIntegrationTest {
23+
24+
@Autowired
25+
private WebApplicationContext wac;
26+
27+
private MockMvc mockMvc;
28+
29+
@Before
30+
public void setup() throws Exception {
31+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
32+
}
33+
34+
@Test
35+
public void givenPathVarsMapParam_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
36+
37+
int articleId = 5;
38+
39+
this.mockMvc
40+
.perform(MockMvcRequestBuilders.get("/mapParam/article/{id}", articleId))
41+
.andExpect(MockMvcResultMatchers.status().isOk())
42+
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
43+
44+
}
45+
46+
@Test
47+
public void givenPathVarsMapParam_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
48+
49+
this.mockMvc
50+
.perform(MockMvcRequestBuilders.get("/mapParam/article"))
51+
.andExpect(MockMvcResultMatchers.status().isOk())
52+
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
53+
54+
}
55+
56+
57+
}

0 commit comments

Comments
 (0)