Basic RecipeController and tests.

This commit is contained in:
JesseBrault0709 2024-07-09 09:59:48 +02:00
parent 35ef2aa039
commit 97bbab3cf0
5 changed files with 135 additions and 1 deletions

View File

@ -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"));
}
}

View File

@ -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<RecipeExceptionView> onRecipeException(RecipeException recipeException) {
return ResponseEntity.badRequest().body(new RecipeExceptionView(
recipeException.getType().toString(),
recipeException.getMessage()
));
}
@GetMapping("/{id}")
public ResponseEntity<RecipeGetView> 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()));
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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