Wrote tests for adding and deleting recipe stars.
This commit is contained in:
parent
7c7e29ac27
commit
b173d86f23
@ -1,6 +1,7 @@
|
||||
package app.mealsmadeeasy.api.recipe;
|
||||
|
||||
import app.mealsmadeeasy.api.matchers.Matchers;
|
||||
import app.mealsmadeeasy.api.recipe.star.RecipeStar;
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
import app.mealsmadeeasy.api.user.UserEntity;
|
||||
import app.mealsmadeeasy.api.user.UserRepository;
|
||||
@ -11,8 +12,9 @@ import org.springframework.test.annotation.DirtiesContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static app.mealsmadeeasy.api.matchers.Matchers.isUser;
|
||||
import static app.mealsmadeeasy.api.matchers.Matchers.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
@SpringBootTest
|
||||
@ -32,7 +34,7 @@ public class RecipeServiceTests {
|
||||
return this.userRepository.save(draft);
|
||||
}
|
||||
|
||||
private Recipe createTestRecipe(User owner) throws RecipeException {
|
||||
private Recipe createTestRecipe(User owner) {
|
||||
return this.recipeService.create(owner, "My Recipe" , "Hello!");
|
||||
}
|
||||
|
||||
@ -48,7 +50,7 @@ public class RecipeServiceTests {
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void createViaUser() throws RecipeException {
|
||||
public void createViaUser() {
|
||||
final User user = this.createTestUser("recipeOwner");
|
||||
final Recipe recipe = this.recipeService.create(user, "My Recipe", "Hello!");
|
||||
assertThat(recipe.getOwner().getUsername(), is(user.getUsername()));
|
||||
@ -66,6 +68,16 @@ public class RecipeServiceTests {
|
||||
assertThat(byId.getRawText(), is("Hello!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void getByIdWithStars() throws RecipeException {
|
||||
final User owner = this.createTestUser("recipeOwner");
|
||||
final Recipe recipe = this.createTestRecipe(owner);
|
||||
final RecipeStar star = this.recipeService.addStar(recipe, owner);
|
||||
final Recipe byIdWithStars = this.recipeService.getByIdWithStars(recipe.getId());
|
||||
assertThat(byIdWithStars.getStars(), containsStars(star));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void getByMinimumStars() throws RecipeException {
|
||||
@ -181,4 +193,29 @@ public class RecipeServiceTests {
|
||||
assertThat(recipe.getOwner(), isUser(secondOwner));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void addStar() throws RecipeException {
|
||||
final User owner = this.createTestUser("recipeOwner");
|
||||
final User starer = this.createTestUser("starer");
|
||||
Recipe recipe = this.createTestRecipe(owner);
|
||||
recipe = this.recipeService.addViewer(recipe, starer);
|
||||
final RecipeStar star = this.recipeService.addStar(recipe, starer);
|
||||
assertThat(star.getRecipe(), isRecipe(recipe));
|
||||
assertThat(star.getOwner(), isUser(starer));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void deleteStar() throws RecipeException {
|
||||
final User owner = this.createTestUser("recipeOwner");
|
||||
final User starer = this.createTestUser("starer");
|
||||
Recipe recipe = this.createTestRecipe(owner);
|
||||
recipe = this.recipeService.addViewer(recipe, starer);
|
||||
final RecipeStar star = this.recipeService.addStar(recipe, starer);
|
||||
this.recipeService.deleteStar(star);
|
||||
recipe = this.recipeService.getByIdWithStars(recipe.getId());
|
||||
assertThat(recipe.getStars(), is(empty()));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,8 +6,10 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface RecipeRepository extends JpaRepository<RecipeEntity, Long> {
|
||||
|
||||
List<RecipeEntity> findAllByIsPublicIsTrue();
|
||||
List<RecipeEntity> findAllByViewersContaining(UserEntity viewer);
|
||||
List<RecipeEntity> findAllByOwner(UserEntity owner);
|
||||
@ -18,4 +20,9 @@ public interface RecipeRepository extends JpaRepository<RecipeEntity, Long> {
|
||||
@Query("SELECT r FROM Recipe r WHERE r.id = ?1")
|
||||
@EntityGraph(attributePaths = { "viewers" })
|
||||
RecipeEntity getByIdWithViewers(long id);
|
||||
|
||||
@Query("SELECT r FROM Recipe r WHERE r.id = ?1")
|
||||
@EntityGraph(attributePaths = { "stars" })
|
||||
Optional<RecipeEntity> findByIdWithStars(long id);
|
||||
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ public interface RecipeService {
|
||||
Recipe create(User user, String title, String rawText);
|
||||
|
||||
Recipe getById(long id) throws RecipeException;
|
||||
Recipe getByIdWithStars(long id) throws RecipeException;
|
||||
|
||||
List<Recipe> getByMinimumStars(long minimumStars);
|
||||
List<Recipe> getPublicRecipes();
|
||||
List<Recipe> getRecipesViewableBy(User user);
|
||||
|
@ -17,6 +17,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
@ -78,6 +79,14 @@ public final class RecipeServiceImpl implements RecipeService {
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Recipe getByIdWithStars(long id) throws RecipeException {
|
||||
return this.recipeRepository.findByIdWithStars(id).orElseThrow(() -> new RecipeException(
|
||||
RecipeException.Type.INVALID_ID,
|
||||
"No such recipe for id " + id
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Recipe> getByMinimumStars(long minimumStars) {
|
||||
return List.copyOf(this.recipeRepository.findAllByStarsGreaterThanEqual(minimumStars));
|
||||
@ -138,7 +147,7 @@ public final class RecipeServiceImpl implements RecipeService {
|
||||
@Override
|
||||
public RecipeStar addStar(Recipe recipe, User giver) throws RecipeException {
|
||||
boolean viewable = false;
|
||||
if (recipe.isPublic()) {
|
||||
if (recipe.isPublic() || Objects.equals(recipe.getOwner().getId(), giver.getId())) {
|
||||
viewable = true;
|
||||
} else {
|
||||
final RecipeEntity withViewers = this.recipeRepository.getByIdWithViewers(recipe.getId());
|
||||
|
@ -6,6 +6,7 @@ import app.mealsmadeeasy.api.user.User;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public interface RecipeStar {
|
||||
Long getId();
|
||||
User getOwner();
|
||||
LocalDateTime getDate();
|
||||
Recipe getRecipe();
|
||||
|
@ -25,7 +25,8 @@ public final class RecipeStarEntity implements RecipeStar {
|
||||
@JoinColumn(name = "recipe_id", nullable = false, updatable = false)
|
||||
private RecipeEntity recipe;
|
||||
|
||||
public long getId() {
|
||||
@Override
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@ -60,4 +61,9 @@ public final class RecipeStarEntity implements RecipeStar {
|
||||
this.recipe = recipe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RecipeStarEntity(" + this.id + ")";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
package app.mealsmadeeasy.api.matchers;
|
||||
|
||||
import app.mealsmadeeasy.api.recipe.star.RecipeStar;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public class ContainsStarsMatcher extends BaseMatcher<Set<RecipeStar>> {
|
||||
|
||||
private final RecipeStar[] allExpected;
|
||||
|
||||
public ContainsStarsMatcher(RecipeStar[] allExpected) {
|
||||
this.allExpected = allExpected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object actual) {
|
||||
if (actual instanceof Set<?> set) {
|
||||
checkExpected:
|
||||
for (final RecipeStar expected : allExpected) {
|
||||
for (final Object item : set) {
|
||||
if (item instanceof RecipeStar o && Objects.equals(o.getId(), expected.getId())) {
|
||||
continue checkExpected;
|
||||
}
|
||||
}
|
||||
// Did not find the expected in the set
|
||||
return false;
|
||||
}
|
||||
// Found all expected in set
|
||||
return true;
|
||||
}
|
||||
// actual is not a Set
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("Expected ").appendValue(Set.of(this.allExpected));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package app.mealsmadeeasy.api.matchers;
|
||||
|
||||
import app.mealsmadeeasy.api.recipe.Recipe;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class IsRecipeMatcher extends BaseMatcher<Recipe> {
|
||||
|
||||
private final Recipe expected;
|
||||
|
||||
public IsRecipeMatcher(Recipe expected) {
|
||||
this.expected = expected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object actual) {
|
||||
return actual instanceof Recipe o && Objects.equals(this.expected.getId(), o.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("Expected ").appendValue(this.expected);
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package app.mealsmadeeasy.api.matchers;
|
||||
|
||||
import app.mealsmadeeasy.api.recipe.Recipe;
|
||||
import app.mealsmadeeasy.api.recipe.star.RecipeStar;
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
|
||||
public final class Matchers {
|
||||
@ -9,6 +10,14 @@ public final class Matchers {
|
||||
return new ContainsRecipesMatcher(expected);
|
||||
}
|
||||
|
||||
public static ContainsStarsMatcher containsStars(RecipeStar... expected) {
|
||||
return new ContainsStarsMatcher(expected);
|
||||
}
|
||||
|
||||
public static IsRecipeMatcher isRecipe(Recipe expected) {
|
||||
return new IsRecipeMatcher(expected);
|
||||
}
|
||||
|
||||
public static IsUserMatcher isUser(User expected) {
|
||||
return new IsUserMatcher(expected);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user