MME-25 Test for spec to entity fields transfer.

This commit is contained in:
Jesse Brault 2026-02-10 18:00:12 -06:00
parent fe0f70cf7d
commit 2fbb9ee51e

View File

@ -1,6 +1,10 @@
package app.mealsmadeeasy.api.recipe; package app.mealsmadeeasy.api.recipe;
import app.mealsmadeeasy.api.PostgresTestsExtension; import app.mealsmadeeasy.api.PostgresTestsExtension;
import app.mealsmadeeasy.api.image.Image;
import app.mealsmadeeasy.api.image.ImageException;
import app.mealsmadeeasy.api.image.ImageService;
import app.mealsmadeeasy.api.image.spec.ImageCreateSpec;
import app.mealsmadeeasy.api.recipe.spec.RecipeCreateSpec; import app.mealsmadeeasy.api.recipe.spec.RecipeCreateSpec;
import app.mealsmadeeasy.api.recipe.spec.RecipeUpdateSpec; import app.mealsmadeeasy.api.recipe.spec.RecipeUpdateSpec;
import app.mealsmadeeasy.api.recipe.star.RecipeStar; import app.mealsmadeeasy.api.recipe.star.RecipeStar;
@ -17,13 +21,16 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice; import org.springframework.data.domain.Slice;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static app.mealsmadeeasy.api.recipe.ContainsRecipesMatcher.containsRecipes; import static app.mealsmadeeasy.api.recipe.ContainsRecipesMatcher.containsRecipes;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
// TODO: test mainImage included // TODO: test mainImage included
// TODO: test prep/cooking/total times included // TODO: test prep/cooking/total times included
@ -40,6 +47,9 @@ public class RecipeServiceTests {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
@Autowired
private ImageService imageService;
private User seedUser() { private User seedUser() {
final String uuid = UUID.randomUUID().toString(); final String uuid = UUID.randomUUID().toString();
final User draft = User.getDefaultDraft(); final User draft = User.getDefaultDraft();
@ -49,6 +59,24 @@ public class RecipeServiceTests {
return this.userRepository.save(draft); return this.userRepository.save(draft);
} }
private Image seedImage(User owner) throws ImageException, IOException {
final String uuid = UUID.randomUUID().toString();
final ImageCreateSpec spec = ImageCreateSpec
.builder()
.alt("Test alt")
.caption("Test caption")
.isPublic(true)
.build();
final InputStream inputStream = this.getClass().getResourceAsStream("/HAL9000.svg");
return this.imageService.create(
owner,
uuid + ".svg",
inputStream,
27881L,
spec
);
}
private Recipe createTestRecipe(@Nullable User owner) { private Recipe createTestRecipe(@Nullable User owner) {
return this.createTestRecipe(owner, false); return this.createTestRecipe(owner, false);
} }
@ -75,6 +103,33 @@ public class RecipeServiceTests {
assertThat(recipe.getRawText(), is("Hello!")); assertThat(recipe.getRawText(), is("Hello!"));
} }
@Test
public void whenCreate_allFieldsTransferredFromSpec() throws Exception {
final User owner = this.seedUser();
final Image image = this.seedImage(owner);
final RecipeCreateSpec spec = RecipeCreateSpec.builder()
.title("Recipe Title")
.slug("recipe-slug")
.preparationTime(15)
.cookingTime(30)
.totalTime(45)
.rawText("# Hello Recipe")
.isPublic(true)
.mainImage(image)
.build();
final Recipe recipe = this.recipeService.create(owner, spec, false);
assertThat(recipe.getId(), is(notNullValue()));
assertThat(recipe.getTitle(), is("Recipe Title"));
assertThat(recipe.getSlug(), is("recipe-slug"));
assertThat(recipe.getPreparationTime(), is(15));
assertThat(recipe.getCookingTime(), is(30));
assertThat(recipe.getTotalTime(), is(45));
assertThat(recipe.getRawText(), is("# Hello Recipe"));
assertThat(recipe.getMainImage(), is(notNullValue()));
assertThat(recipe.getMainImage().getId(), is(image.getId()));
assertThat(recipe.getIsPublic(), is(true));
}
@Test @Test
public void getByIdPublicNoViewerDoesNotThrow() { public void getByIdPublicNoViewerDoesNotThrow() {
final User owner = this.seedUser(); final User owner = this.seedUser();