mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:35:50 +00:00
Add /api/greetings endpoint with optional name
This commit is contained in:
parent
c5af32d5a1
commit
7cce657d3f
3 changed files with 52 additions and 0 deletions
10
README.md
10
README.md
|
@ -163,3 +163,13 @@ For additional details, please refer to the blog post [Hello DCO, Goodbye CLA: S
|
||||||
## License
|
## License
|
||||||
|
|
||||||
The Spring PetClinic sample application is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
|
The Spring PetClinic sample application is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
|
||||||
|
|
||||||
|
### User Story
|
||||||
|
|
||||||
|
As a pet clinic administrator, I want to add a `/greetings` API endpoint that returns a personalized welcome message so that I can display it on the homepage.
|
||||||
|
|
||||||
|
Acceptance Criteria:
|
||||||
|
- Endpoint: `/api/greetings`
|
||||||
|
- Method: GET
|
||||||
|
- Optional query param: `name`
|
||||||
|
- Returns: "Welcome to the PetClinic, <name>!" or "Welcome to the PetClinic!" if no name is passed.
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api")
|
||||||
|
public class GreetingController {
|
||||||
|
|
||||||
|
@GetMapping("/greetings")
|
||||||
|
public String greet(@RequestParam(required = false) String name) {
|
||||||
|
if (name == null || name.isEmpty()) {
|
||||||
|
return "Welcome to the PetClinic!";
|
||||||
|
} else {
|
||||||
|
return "Welcome to the PetClinic, " + name + "!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
|
||||||
|
@WebMvcTest(GreetingController.class)
|
||||||
|
class GreetingControllerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnDefaultGreetingWhenNameIsNotProvided() throws Exception {
|
||||||
|
mockMvc.perform(get("/api/greetings"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string("Welcome to the PetClinic!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnPersonalizedGreetingWhenNameIsProvided() throws Exception {
|
||||||
|
mockMvc.perform(get("/api/greetings").param("name", "John"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string("Welcome to the PetClinic, John!"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue