mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-24 11:22:48 +00:00
Add /api/greetings endpoint with optional name
This commit is contained in:
parent
0c88f916db
commit
8af91504f0
3 changed files with 44 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
|
||||
|
||||
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 "Hello, World!";
|
||||
} else {
|
||||
return "Hello, " + name + "!";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class GreetingControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void testGreetWithName() throws Exception {
|
||||
mockMvc.perform(get("/api/greetings?name=Anu"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("Welcome to the PetClinic, Anu!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGreetWithoutName() throws Exception {
|
||||
mockMvc.perform(get("/api/greetings"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("Welcome to the PetClinic!"));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue