diff --git a/README.md b/README.md index c865c3b51..971d9b6d4 100644 --- a/README.md +++ b/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, !" or "Welcome to the PetClinic!" if no name is passed. diff --git a/src/main/java/org/springframework/samples/petclinic/owner/GreetingController.java b/src/main/java/org/springframework/samples/petclinic/owner/GreetingController.java new file mode 100644 index 000000000..4cb8cfb88 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/GreetingController.java @@ -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 + "!"; + } + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/owner/GreetingControllerTests.java b/src/test/java/org/springframework/samples/petclinic/owner/GreetingControllerTests.java new file mode 100644 index 000000000..2b599333e --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/owner/GreetingControllerTests.java @@ -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!")); + } +}