From 97bbab3cf0f4c71b6c1d7de4be45df5418045752 Mon Sep 17 00:00:00 2001 From: JesseBrault0709 <62299747+JesseBrault0709@users.noreply.github.com> Date: Tue, 9 Jul 2024 09:59:48 +0200 Subject: [PATCH] Basic RecipeController and tests. --- .../api/recipe/RecipeControllerTests.java | 52 +++++++++++++++++++ .../api/recipe/RecipeController.java | 40 ++++++++++++++ .../api/recipe/view/RecipeExceptionView.java | 21 ++++++++ .../api/recipe/view/RecipeGetView.java | 21 ++++++++ .../api/security/SecurityConfiguration.java | 2 +- 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java create mode 100644 src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java create mode 100644 src/main/java/app/mealsmadeeasy/api/recipe/view/RecipeExceptionView.java create mode 100644 src/main/java/app/mealsmadeeasy/api/recipe/view/RecipeGetView.java diff --git a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java new file mode 100644 index 0000000..214502f --- /dev/null +++ b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java @@ -0,0 +1,52 @@ +package app.mealsmadeeasy.api.recipe; + +import app.mealsmadeeasy.api.user.User; +import app.mealsmadeeasy.api.user.UserCreateException; +import app.mealsmadeeasy.api.user.UserService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +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.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class RecipeControllerTests { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private RecipeService recipeService; + + @Autowired + private UserService userService; + + private User createTestUser(String username) { + try { + return this.userService.createUser(username, username + "@test.com", "test"); + } catch (UserCreateException e) { + throw new RuntimeException(e); + } + } + + private Recipe createTestRecipe(User owner) { + return this.recipeService.create(owner, "Test Recipe", "Hello, World!"); + } + + @Test + public void getByIdPublicRecipeNoPrincipal() throws Exception { + final User owner = this.createTestUser("owner"); + final Recipe recipe = this.createTestRecipe(owner); + this.recipeService.setPublic(recipe, owner, true); + this.mockMvc.perform(get("/recipe/{id}", recipe.getId())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(1)) + .andExpect(jsonPath("$.title").value("Test Recipe")); + } + +} diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java new file mode 100644 index 0000000..b70800a --- /dev/null +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java @@ -0,0 +1,40 @@ +package app.mealsmadeeasy.api.recipe; + +import app.mealsmadeeasy.api.recipe.view.RecipeExceptionView; +import app.mealsmadeeasy.api.recipe.view.RecipeGetView; +import app.mealsmadeeasy.api.user.User; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/recipe") +public class RecipeController { + + private final RecipeService recipeService; + + public RecipeController(RecipeService recipeService) { + this.recipeService = recipeService; + } + + @ExceptionHandler + public ResponseEntity onRecipeException(RecipeException recipeException) { + return ResponseEntity.badRequest().body(new RecipeExceptionView( + recipeException.getType().toString(), + recipeException.getMessage() + )); + } + + @GetMapping("/{id}") + public ResponseEntity getById(@PathVariable long id, @AuthenticationPrincipal User user) + throws RecipeException { + final Recipe recipe; + if (user != null) { + recipe = this.recipeService.getById(id, user); + } else { + recipe = this.recipeService.getById(id); + } + return ResponseEntity.ok(new RecipeGetView(recipe.getId(), recipe.getTitle())); + } + +} diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/view/RecipeExceptionView.java b/src/main/java/app/mealsmadeeasy/api/recipe/view/RecipeExceptionView.java new file mode 100644 index 0000000..dc7f5b3 --- /dev/null +++ b/src/main/java/app/mealsmadeeasy/api/recipe/view/RecipeExceptionView.java @@ -0,0 +1,21 @@ +package app.mealsmadeeasy.api.recipe.view; + +public final class RecipeExceptionView { + + private final String type; + private final String message; + + public RecipeExceptionView(String type, String message) { + this.type = type; + this.message = message; + } + + public String getType() { + return this.type; + } + + public String getMessage() { + return this.message; + } + +} diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/view/RecipeGetView.java b/src/main/java/app/mealsmadeeasy/api/recipe/view/RecipeGetView.java new file mode 100644 index 0000000..24d6a43 --- /dev/null +++ b/src/main/java/app/mealsmadeeasy/api/recipe/view/RecipeGetView.java @@ -0,0 +1,21 @@ +package app.mealsmadeeasy.api.recipe.view; + +public final class RecipeGetView { + + private final long id; + private final String title; + + public RecipeGetView(long id, String title) { + this.id = id; + this.title = title; + } + + public long getId() { + return this.id; + } + + public String getTitle() { + return this.title; + } + +} diff --git a/src/main/java/app/mealsmadeeasy/api/security/SecurityConfiguration.java b/src/main/java/app/mealsmadeeasy/api/security/SecurityConfiguration.java index 29d0403..e9917ab 100644 --- a/src/main/java/app/mealsmadeeasy/api/security/SecurityConfiguration.java +++ b/src/main/java/app/mealsmadeeasy/api/security/SecurityConfiguration.java @@ -34,7 +34,7 @@ public class SecurityConfiguration { @Bean public WebSecurityCustomizer webSecurityCustomizer() { - return web -> web.ignoring().requestMatchers("/greeting", "/auth/**", "/sign-up/**"); + return web -> web.ignoring().requestMatchers("/greeting", "/auth/**", "/sign-up/**", "/recipe/**"); } @Bean