Skip to content

Commit

Permalink
test: 프로필 서비스를 테스트한다
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorch9 committed Jan 1, 2024
1 parent 5cc2f5c commit 943e7c3
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public GetPresetBadgeService(final Users users, final ProfileSettings profileSet
@Transactional(readOnly = true)
public MultipleBadgesResponse invoke(final String username) {
final Long userId = users.getUserIdByUsername(username);
final List<Long> badgeIds = profileSettings.getById(userId).getBadgeIds();
final List<BadgeResponse> responses = badgeDao.findAllByIdIn(badgeIds)
final List<BadgeResponse> responses = badgeDao.findAllByIdIn(profileSettings.getBadgeIdsById(userId))
.stream().map(BadgeResponse::of).toList();
return new MultipleBadgesResponse(responses);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/daybyquest/profile/domain/ProfileSettings.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package daybyquest.profile.domain;

import daybyquest.global.error.exception.NotExistUserException;
import java.util.List;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class ProfileSettings {
Expand All @@ -19,4 +21,9 @@ public void save(final ProfileSetting profileSetting) {
public ProfileSetting getById(final Long userId) {
return profileSettingRepository.findByUserId(userId).orElseThrow(NotExistUserException::new);
}

@Transactional(readOnly = true)
public List<Long> getBadgeIdsById(final Long userId) {
return getById(userId).getBadgeIds();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package daybyquest.profile.application;

import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_2;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_3;
import static daybyquest.support.fixture.UserFixtures.ALICE;
import static org.assertj.core.api.Assertions.assertThat;

import daybyquest.badge.dto.response.BadgeResponse;
import daybyquest.badge.dto.response.MultipleBadgesResponse;
import daybyquest.profile.domain.ProfileSetting;
import daybyquest.support.test.ServiceTest;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetPresetBadgeServiceTest extends ServiceTest {

@Autowired
private GetPresetBadgeService getPresetBadgeService;

@Test
void 설정된_뱃지_목록을_조회한다() {
// given
final Long aliceId = ALICE를_저장한다();

final Long badge1Id = badges.save(BADGE_1.생성()).getId();
final Long badge2Id = badges.save(BADGE_2.생성()).getId();
badges.save(BADGE_3.생성());

ownings.saveByUserIdAndBadgeId(aliceId, badge1Id);
ownings.saveByUserIdAndBadgeId(aliceId, badge2Id);

final List<Long> expected = List.of(badge1Id, badge2Id);
final ProfileSetting profileSetting = new ProfileSetting(aliceId);
profileSetting.updateBadgeList(expected);
profileSettings.save(profileSetting);

// when
final MultipleBadgesResponse response = getPresetBadgeService.invoke(ALICE.username);
final List<Long> actual = response.badges().stream().map(BadgeResponse::id).toList();

// then
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package daybyquest.profile.application;

import static daybyquest.global.error.ExceptionCode.NOT_OWNING_BADGE;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_2;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_3;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import daybyquest.profile.dto.request.SaveBadgeListRequest;
import daybyquest.support.test.ServiceTest;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.util.ReflectionTestUtils;

public class SaveBadgeListServiceTest extends ServiceTest {

@Autowired
private SaveBadgeListService saveBadgeListService;

@Test
void 뱃지_목록을_저장한다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long badge1Id = badges.save(BADGE_1.생성()).getId();
final Long badge2Id = badges.save(BADGE_2.생성()).getId();
badges.save(BADGE_3.생성());

ownings.saveByUserIdAndBadgeId(aliceId, badge1Id);
ownings.saveByUserIdAndBadgeId(aliceId, badge2Id);

final List<Long> expected = List.of(badge1Id, badge2Id);
final SaveBadgeListRequest request = 뱃지_목록_수정_요청(expected);

// when
saveBadgeListService.invoke(aliceId, request);
final List<Long> actual = profileSettings.getBadgeIdsById(aliceId);

// then
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}

@Test
void 소유하지_않은_뱃지는_지정할_수_없다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long badgeId = badges.save(BADGE_1.생성()).getId();

final SaveBadgeListRequest request = 뱃지_목록_수정_요청(List.of(badgeId));

// when & then
assertThatThrownBy(() -> saveBadgeListService.invoke(aliceId, request))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_OWNING_BADGE.getMessage());
}

private SaveBadgeListRequest 뱃지_목록_수정_요청(final List<Long> badgeIds) {
final SaveBadgeListRequest request = new SaveBadgeListRequest();
ReflectionTestUtils.setField(request, "badgeIds", badgeIds);
return request;
}
}
11 changes: 9 additions & 2 deletions src/test/java/daybyquest/support/test/ServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import daybyquest.participant.domain.Participants;
import daybyquest.post.application.PostClient;
import daybyquest.post.domain.Posts;
import daybyquest.profile.domain.ProfileSetting;
import daybyquest.profile.domain.ProfileSettings;
import daybyquest.quest.application.QuestClient;
import daybyquest.quest.domain.Quests;
import daybyquest.relation.domain.Follows;
Expand Down Expand Up @@ -84,6 +86,9 @@ public class ServiceTest {
@Autowired
protected PostDislikes postDislikes;

@Autowired
protected ProfileSettings profileSettings;

@MockBean
protected DateTimeProvider dataTimeProvider;

Expand All @@ -105,7 +110,7 @@ void setUp() {
void cleanDatabase() {
cleaner.clean();
}

protected Long 중재자_권한으로_ALICE를_저장한다() {
final User user = ALICE.생성();
user.promote();
Expand All @@ -119,7 +124,9 @@ void cleanDatabase() {
}

protected Long ALICE를_저장한다() {
return users.save(ALICE.생성()).getId();
final Long id = users.save(ALICE.생성()).getId();
profileSettings.save(new ProfileSetting(id));
return id;
}

protected Long BOB을_저장한다() {
Expand Down

0 comments on commit 943e7c3

Please sign in to comment.