FavoriteService { public List<Favorite> search(String userId, String appId, Pageable page) { boolean isUserIdEmpty = StringUtils.isEmpty(userId); boolean isAppIdEmpty = StringUtils.isEmpty(appId); if (isAppIdEmpty && isUserIdEmpty) { throw new BadRequestException("user id and app id can't be empty at the same time"); } if (!isUserIdEmpty) { UserInfo loginUser = userInfoHolder.getUser(); if (!Objects.equals(loginUser.getUserId(), userId)) { userId = loginUser.getUserId(); } } if (isAppIdEmpty && !isUserIdEmpty) { return favoriteRepository.findByUserIdOrderByPositionAscDataChangeCreatedTimeAsc(userId, page); } if (!isAppIdEmpty && isUserIdEmpty) { return favoriteRepository.findByAppIdOrderByPositionAscDataChangeCreatedTimeAsc(appId, page); } return Collections.singletonList(favoriteRepository.findByUserIdAndAppId(userId, appId)); } FavoriteService(
      final UserInfoHolder userInfoHolder,
      final FavoriteRepository favoriteRepository,
      final UserService userService); Favorite addFavorite(Favorite favorite); List<Favorite> search(String userId, String appId, Pageable page); void deleteFavorite(long favoriteId); void adjustFavoriteToFirst(long favoriteId); void batchDeleteByAppId(String appId, String operator); static final long POSITION_DEFAULT; }
@Test @Sql(scripts = "/sql/favorites/favorites.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) public void testSearchByUserId() { List<Favorite> favorites = favoriteService.search(testUser, null, PageRequest.of(0, 10)); Assert.assertEquals(4, favorites.size()); }
@Test @Sql(scripts = "/sql/favorites/favorites.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) public void testSearchByAppId() { List<Favorite> favorites = favoriteService.search(null, "test0621-04", PageRequest.of(0, 10)); Assert.assertEquals(3, favorites.size()); }
@Test @Sql(scripts = "/sql/favorites/favorites.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) public void testSearchByAppIdAndUserId() { List<Favorite> favorites = favoriteService.search(testUser, "test0621-04", PageRequest.of(0, 10)); Assert.assertEquals(1, favorites.size()); }
@Test(expected = BadRequestException.class) @Sql(scripts = "/sql/favorites/favorites.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) public void testSearchWithErrorParams() { favoriteService.search(null, null, PageRequest.of(0, 10)); }