Skip to content

Commit

Permalink
fix: tests and format, also added e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darmalovan committed Jan 9, 2024
1 parent 8172d8a commit 3e4465f
Show file tree
Hide file tree
Showing 33 changed files with 167 additions and 172 deletions.
1 change: 1 addition & 0 deletions .framework/java/backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies {
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:2.2.2'
testImplementation 'com.h2database:h2:1.4.200'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.spring.api;

import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.NoAuthorizationException;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.CommentQueryService;
import io.spring.application.data.CommentData;
import io.spring.core.item.Item;
import io.spring.core.item.ItemRepository;
import io.spring.core.comment.Comment;
import io.spring.core.comment.CommentRepository;
import io.spring.core.service.AuthorizationService;
import io.spring.core.item.Item;
import io.spring.core.item.ItemRepository;
import io.spring.core.user.User;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -42,8 +40,7 @@ public ResponseEntity<?> createComment(
@PathVariable("slug") String slug,
@AuthenticationPrincipal User user,
@Valid @RequestBody NewCommentParam newCommentParam) {
Item item =
itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Item item = itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Comment comment = new Comment(newCommentParam.getBody(), user.getId(), item.getId());
commentRepository.save(comment);
return ResponseEntity.status(201)
Expand All @@ -53,8 +50,7 @@ public ResponseEntity<?> createComment(
@GetMapping
public ResponseEntity getComments(
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
Item item =
itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Item item = itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
List<CommentData> comments = commentQueryService.findByItemId(item.getId(), user);
return ResponseEntity.ok(
new HashMap<String, Object>() {
Expand All @@ -69,8 +65,7 @@ public ResponseEntity deleteComment(
@PathVariable("slug") String slug,
@PathVariable("id") String commentId,
@AuthenticationPrincipal User user) {
Item item =
itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Item item = itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
return commentRepository
.findById(item.getId(), commentId)
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import io.spring.api.exception.NoAuthorizationException;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.ItemQueryService;
import io.spring.application.data.ItemData;
import io.spring.application.item.ItemCommandService;
import io.spring.application.item.UpdateItemParam;
import io.spring.application.data.ItemData;
import io.spring.core.item.Item;
import io.spring.core.item.ItemRepository;
import io.spring.core.service.AuthorizationService;
Expand Down Expand Up @@ -53,11 +53,9 @@ public ResponseEntity<?> updateItem(
if (!AuthorizationService.canWriteItem(user, item)) {
throw new NoAuthorizationException();
}
Item updatedItem =
itemCommandService.updateItem(item, updateItemParam);
Item updatedItem = itemCommandService.updateItem(item, updateItemParam);
return ResponseEntity.ok(
itemResponse(
itemQueryService.findBySlug(updatedItem.getSlug(), user).get()));
itemResponse(itemQueryService.findBySlug(updatedItem.getSlug(), user).get()));
})
.orElseThrow(ResourceNotFoundException::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.ItemQueryService;
import io.spring.application.data.ItemData;
import io.spring.core.item.Item;
import io.spring.core.item.ItemRepository;
import io.spring.core.favorite.ItemFavorite;
import io.spring.core.favorite.ItemFavoriteRepository;
import io.spring.core.item.Item;
import io.spring.core.item.ItemRepository;
import io.spring.core.user.User;
import java.util.HashMap;
import lombok.AllArgsConstructor;
Expand All @@ -29,8 +29,7 @@ public class ItemFavoriteApi {
@PostMapping
public ResponseEntity favoriteItem(
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
Item item =
itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Item item = itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
ItemFavorite itemFavorite = new ItemFavorite(item.getId(), user.getId());
itemFavoriteRepository.save(itemFavorite);
return responseItemData(itemQueryService.findBySlug(slug, user).get());
Expand All @@ -39,8 +38,7 @@ public ResponseEntity favoriteItem(
@DeleteMapping
public ResponseEntity unfavoriteItem(
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
Item item =
itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Item item = itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
itemFavoriteRepository
.find(item.getId(), user.getId())
.ifPresent(
Expand All @@ -50,8 +48,7 @@ public ResponseEntity unfavoriteItem(
return responseItemData(itemQueryService.findBySlug(slug, user).get());
}

private ResponseEntity<HashMap<String, Object>> responseItemData(
final ItemData itemData) {
private ResponseEntity<HashMap<String, Object>> responseItemData(final ItemData itemData) {
return ResponseEntity.ok(
new HashMap<String, Object>() {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public ResponseEntity getItems(
@RequestParam(value = "seller", required = false) String seller,
@AuthenticationPrincipal User user) {
return ResponseEntity.ok(
itemQueryService.findRecentItems(
tag, seller, favoritedBy, new Page(offset, limit), user));
itemQueryService.findRecentItems(tag, seller, favoritedBy, new Page(offset, limit), user));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public CursorPager<ItemData> findRecentItemsWithCursor(
String favoritedBy,
CursorPageParameter<DateTime> page,
User currentUser) {
List<String> itemIds =
itemReadService.findItemsWithCursor(tag, seller, favoritedBy, page);
List<String> itemIds = itemReadService.findItemsWithCursor(tag, seller, favoritedBy, page);
if (itemIds.size() == 0) {
return new CursorPager<>(new ArrayList<>(), page.getDirection(), false);
} else {
Expand All @@ -83,8 +82,7 @@ public CursorPager<ItemData> findUserFeedWithCursor(
if (followedUsers.size() == 0) {
return new CursorPager<>(new ArrayList<>(), page.getDirection(), false);
} else {
List<ItemData> items =
itemReadService.findItemsOfSellersWithCursor(followedUsers, page);
List<ItemData> items = itemReadService.findItemsOfSellersWithCursor(followedUsers, page);
boolean hasExtra = items.size() > page.getLimit();
if (hasExtra) {
items.remove(page.getLimit());
Expand Down Expand Up @@ -134,9 +132,7 @@ private void setIsFollowingSeller(List<ItemData> items, User currentUser) {
Set<String> followingSellers =
userRelationshipQueryService.followingSellers(
currentUser.getId(),
items.stream()
.map(itemData1 -> itemData1.getProfileData().getId())
.collect(toList()));
items.stream().map(itemData1 -> itemData1.getProfileData().getId()).collect(toList()));
items.forEach(
itemData -> {
if (followingSellers.contains(itemData.getProfileData().getId())) {
Expand All @@ -154,15 +150,13 @@ private void setFavoriteCount(List<ItemData> items) {
item -> {
countMap.put(item.getId(), item.getCount());
});
items.forEach(
itemData -> itemData.setFavoritesCount(countMap.get(itemData.getId())));
items.forEach(itemData -> itemData.setFavoritesCount(countMap.get(itemData.getId())));
}

private void setIsFavorite(List<ItemData> items, User currentUser) {
Set<String> favoritedItems =
itemFavoritesReadService.userFavorites(
items.stream().map(itemData -> itemData.getId()).collect(toList()),
currentUser);
items.stream().map(itemData -> itemData.getId()).collect(toList()), currentUser);

items.forEach(
itemData -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import javax.validation.ConstraintValidatorContext;
import org.springframework.beans.factory.annotation.Autowired;

class DuplicatedItemValidator
implements ConstraintValidator<DuplicatedItemConstraint, String> {
class DuplicatedItemValidator implements ConstraintValidator<DuplicatedItemConstraint, String> {

@Autowired private ItemQueryService itemQueryService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public Item createItem(@Valid NewItemParam newItemParam, User creator) {

public Item updateItem(Item item, @Valid UpdateItemParam updateItemParam) {
item.update(
updateItemParam.getTitle(),
updateItemParam.getDescription(),
updateItemParam.getImage());
updateItemParam.getTitle(), updateItemParam.getDescription(), updateItemParam.getImage());
itemRepository.save(item);
return item;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public void update(String title, String description, String image) {
this.updatedAt = new DateTime();
}
if (!Util.isEmpty(image)) {
this.image = image;
this.updatedAt = new DateTime();
}
this.image = image;
this.updatedAt = new DateTime();
}
}

public static String toSlug(String title) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.spring.core.service;

import io.spring.core.item.Item;
import io.spring.core.comment.Comment;
import io.spring.core.item.Item;
import io.spring.core.user.User;

public class AuthorizationService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import io.spring.application.CursorPager;
import io.spring.application.CursorPager.Direction;
import io.spring.application.DateTimeCursor;
import io.spring.application.data.ItemData;
import io.spring.application.data.CommentData;
import io.spring.application.data.ItemData;
import io.spring.core.user.User;
import io.spring.graphql.DgsConstants.ITEM;
import io.spring.graphql.DgsConstants.COMMENTPAYLOAD;
import io.spring.graphql.types.Item;
import io.spring.graphql.DgsConstants.ITEM;
import io.spring.graphql.types.Comment;
import io.spring.graphql.types.CommentEdge;
import io.spring.graphql.types.CommentsConnection;
import io.spring.graphql.types.Item;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.CommentQueryService;
import io.spring.application.data.CommentData;
import io.spring.core.item.Item;
import io.spring.core.item.ItemRepository;
import io.spring.core.comment.Comment;
import io.spring.core.comment.CommentRepository;
import io.spring.core.item.Item;
import io.spring.core.item.ItemRepository;
import io.spring.core.service.AuthorizationService;
import io.spring.core.user.User;
import io.spring.graphql.DgsConstants.MUTATION;
Expand All @@ -32,8 +32,7 @@ public class CommentMutation {
public DataFetcherResult<CommentPayload> createComment(
@InputArgument("slug") String slug, @InputArgument("body") String body) {
User user = SecurityUtil.getCurrentUser().orElseThrow(AuthenticationException::new);
Item item =
itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Item item = itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Comment comment = new Comment(body, user.getId(), item.getId());
commentRepository.save(comment);
CommentData commentData =
Expand All @@ -51,8 +50,7 @@ public DeletionStatus removeComment(
@InputArgument("slug") String slug, @InputArgument("id") String commentId) {
User user = SecurityUtil.getCurrentUser().orElseThrow(AuthenticationException::new);

Item item =
itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
Item item = itemRepository.findBySlug(slug).orElseThrow(ResourceNotFoundException::new);
return commentRepository
.findById(item.getId(), commentId)
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
import graphql.relay.DefaultPageInfo;
import graphql.schema.DataFetchingEnvironment;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.ItemQueryService;
import io.spring.application.CursorPageParameter;
import io.spring.application.CursorPager;
import io.spring.application.CursorPager.Direction;
import io.spring.application.DateTimeCursor;
import io.spring.application.data.ItemData;
import io.spring.application.ItemQueryService;
import io.spring.application.data.CommentData;
import io.spring.application.data.ItemData;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.graphql.DgsConstants.ITEMPAYLOAD;
import io.spring.graphql.DgsConstants.COMMENT;
import io.spring.graphql.DgsConstants.ITEMPAYLOAD;
import io.spring.graphql.DgsConstants.PROFILE;
import io.spring.graphql.DgsConstants.QUERY;
import io.spring.graphql.types.Item;
Expand Down Expand Up @@ -80,8 +80,7 @@ public DataFetcherResult<ItemsConnection> getFeed(
.build();
return DataFetcherResult.<ItemsConnection>newResult()
.data(itemsConnection)
.localContext(
items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.localContext(items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.build();
}

Expand Down Expand Up @@ -130,8 +129,7 @@ public DataFetcherResult<ItemsConnection> userFeed(
.build();
return DataFetcherResult.<ItemsConnection>newResult()
.data(itemsConnection)
.localContext(
items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.localContext(items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.build();
}

Expand Down Expand Up @@ -184,8 +182,7 @@ public DataFetcherResult<ItemsConnection> userFavorites(
.build();
return DataFetcherResult.<ItemsConnection>newResult()
.data(itemsConnection)
.localContext(
items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.localContext(items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.build();
}

Expand Down Expand Up @@ -237,8 +234,7 @@ public DataFetcherResult<ItemsConnection> userItems(
.build();
return DataFetcherResult.<ItemsConnection>newResult()
.data(itemsConnection)
.localContext(
items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.localContext(items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.build();
}

Expand Down Expand Up @@ -292,8 +288,7 @@ public DataFetcherResult<ItemsConnection> getItems(
.build();
return DataFetcherResult.<ItemsConnection>newResult()
.data(itemsConnection)
.localContext(
items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.localContext(items.getData().stream().collect(Collectors.toMap(ItemData::getSlug, a -> a)))
.build();
}

Expand All @@ -319,8 +314,7 @@ public DataFetcherResult<Item> getItem(DataFetchingEnvironment dfe) {
}

@DgsData(parentType = COMMENT.TYPE_NAME, field = COMMENT.Item)
public DataFetcherResult<Item> getCommentItem(
DataFetchingEnvironment dataFetchingEnvironment) {
public DataFetcherResult<Item> getCommentItem(DataFetchingEnvironment dataFetchingEnvironment) {
CommentData comment = dataFetchingEnvironment.getLocalContext();
User current = SecurityUtil.getCurrentUser().orElse(null);
ItemData itemData =
Expand Down
Loading

0 comments on commit 3e4465f

Please sign in to comment.