Skip to content

Commit

Permalink
Merge pull request #164 from DayByQuest/test/userApplication
Browse files Browse the repository at this point in the history
[Test] 사용자 서비스 테스트를 작성한다
  • Loading branch information
vectorch9 authored Dec 15, 2023
2 parents 50d4d76 + daee91e commit 33c0887
Show file tree
Hide file tree
Showing 16 changed files with 493 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/main/java/daybyquest/global/error/ExceptionCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum ExceptionCode {
INVALID_USER_EMAIL("USR-05", BAD_REQUEST, "이메일은 1~30자여야 하며, 형식을 지켜야합니다"),
INVALID_USER_INTEREST("USR-06", BAD_REQUEST, "사용자의 관심사는 5개 이하여야 합니다"),
NOT_UPDATABLE_USER("USR-07", BAD_REQUEST, "업데이트할 수 없는 사용자입니다"),
INVALID_VISIBILITY("USR-08", BAD_REQUEST, "올바르지 않은 가시성입니다"),

// Post
NOT_EXIST_POST("POT-00", BAD_REQUEST, "게시물이 존재하지 않습니다"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public UpdateVisibilityService(final Users users) {
@Transactional
public void invoke(final Long loginId, final UpdateUserVisibilityRequest visibility) {
final User user = users.getById(loginId);
user.updateVisibility(UserVisibility.valueOf(visibility.getVisibility()));
user.updateVisibility(UserVisibility.fromString(visibility.getVisibility()));
}
}

14 changes: 13 additions & 1 deletion src/main/java/daybyquest/user/domain/UserVisibility.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package daybyquest.user.domain;

import static daybyquest.global.error.ExceptionCode.INVALID_VISIBILITY;

import daybyquest.global.error.exception.InvalidDomainException;

public enum UserVisibility {

PUBLIC,
PRIVATE
PRIVATE;

public static UserVisibility fromString(final String name) {
try {
return valueOf(name);
} catch (final IllegalArgumentException e) {
throw new InvalidDomainException(INVALID_VISIBILITY);
}
}
}
27 changes: 2 additions & 25 deletions src/main/java/daybyquest/user/dto/response/MyProfileResponse.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
package daybyquest.user.dto.response;

import daybyquest.user.query.Profile;
import lombok.Getter;

@Getter
public class MyProfileResponse {

private final String username;

private final String name;

private final String imageIdentifier;

private final Long postCount;

private final Long followingCount;

private final Long followerCount;

private MyProfileResponse(final String username, final String name, final String imageIdentifier,
final Long postCount, final Long followingCount, final Long followerCount) {
this.username = username;
this.name = name;
this.imageIdentifier = imageIdentifier;
this.postCount = postCount;
this.followingCount = followingCount;
this.followerCount = followerCount;
}
public record MyProfileResponse(String username, String name, String imageIdentifier, Long postCount,
Long followingCount, Long followerCount) {

public static MyProfileResponse of(final Profile profile) {
return new MyProfileResponse(profile.getUsername(), profile.getName(), profile.getImageIdentifier(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package daybyquest.user.dto.response;

import daybyquest.user.domain.User;
import lombok.Getter;

@Getter
public class UserVisibilityResponse {

private final String visibility;

private UserVisibilityResponse(final String visibility) {
this.visibility = visibility;
}
public record UserVisibilityResponse(String visibility) {

public static UserVisibilityResponse of(final User user) {
return new UserVisibilityResponse(user.getVisibility().toString());
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/daybyquest/support/fixture/UserFixtures.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public enum UserFixtures {
ALICE("alice", "alice@email.com", "alice", "base.png"),
BOB("bob", "bob@email.com", "bob", "bob.png"),
CHARLIE("charlie", "charlie@email.com", "charlie", "charlie.png"),
DAVID("david", "david@email.com", "david", "david.png");
DAVID("david", "david@email.com", "david", "david.png"),
DARTH("darth", "darth@email.com", "darth", "darth.png");

public final String username;

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/daybyquest/support/test/ServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import static daybyquest.support.fixture.UserFixtures.ALICE;
import static daybyquest.support.fixture.UserFixtures.BOB;
import static daybyquest.support.fixture.UserFixtures.CHARLIE;
import static daybyquest.support.fixture.UserFixtures.DARTH;
import static daybyquest.support.fixture.UserFixtures.DAVID;

import daybyquest.global.query.NoOffsetIdPage;
import daybyquest.support.config.StubInfraConfig;
import daybyquest.support.util.DatabaseCleaner;
import daybyquest.user.domain.Users;
Expand Down Expand Up @@ -35,6 +39,22 @@ void cleanDatabase() {
return users.save(BOB.생성()).getId();
}

protected Long CHARLIE를_저장한다() {
return users.save(CHARLIE.생성()).getId();
}

protected Long DAVID를_저장한다() {
return users.save(DAVID.생성()).getId();
}

protected Long DARTH를_저장한다() {
return users.save(DARTH.생성()).getId();
}

protected NoOffsetIdPage 페이지() {
return new NoOffsetIdPage(null, 5);
}

protected MultipartFile 사진_파일() {
return new MockMultipartFile("image", "image.png",
MediaType.MULTIPART_FORM_DATA_VALUE, "file content".getBytes());
Expand Down
1 change: 1 addition & 0 deletions src/test/java/daybyquest/support/util/DatabaseCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void setUp() {
.filter(e -> e.getJavaType().getDeclaredAnnotation(Entity.class) != null)
.map(e -> convertToSnakeCase(e.getName()))
.collect(Collectors.toList());
this.tableNames.add("post_image");
}

private String convertToSnakeCase(final String original) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package daybyquest.user.application;

import static daybyquest.global.error.ExceptionCode.DUPLICATED_USERNAME;
import static daybyquest.support.fixture.UserFixtures.ALICE;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import daybyquest.support.test.ServiceTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class CheckUsernameServiceTest extends ServiceTest {

@Autowired
private CheckUsernameService checkUsernameService;

@Test
void 사용자_이름_중복을_검사한다() {
// given & when & then
assertThatCode(() -> checkUsernameService.invoke(ALICE.username))
.doesNotThrowAnyException();
}

@Test
void 사용자_이름이_이미_있다면_예외를_던진다() {
// given
ALICE를_저장한다();

// when & then
assertThatThrownBy(() -> checkUsernameService.invoke(ALICE.username))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(DUPLICATED_USERNAME.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package daybyquest.user.application;

import static daybyquest.support.fixture.UserFixtures.BOB;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.image.domain.BaseImageProperties;
import daybyquest.support.config.StubImages;
import daybyquest.support.test.ServiceTest;
import daybyquest.user.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class DeleteUserImageServiceTest extends ServiceTest {

@Autowired
private DeleteUserImageService deleteUserImageService;

@Autowired
private BaseImageProperties properties;

@Autowired
private StubImages images;

@Test
void 사용자_사진을_삭제한다() {
// given
final Long id = BOB을_저장한다();
images.upload(BOB.imageIdentifier, null);

// when
deleteUserImageService.invoke(id);

// then
final User user = users.getById(id);
assertAll(() -> {
assertThat(images.hasUploadImage(BOB.imageIdentifier)).isFalse();
assertThat(user.getImageIdentifier()).isEqualTo(properties.getUserIdentifier());
});
}

@Test
void 기본_사진이라면_삭제하지_않는다() {
// given
final Long id = ALICE를_저장한다();
images.upload(properties.getUserIdentifier(), null);

// when
deleteUserImageService.invoke(id);

// then
final User user = users.getById(id);
assertAll(() -> {
assertThat(images.hasUploadImage(properties.getUserIdentifier())).isTrue();
assertThat(user.getImageIdentifier()).isEqualTo(properties.getUserIdentifier());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package daybyquest.user.application;

import static daybyquest.support.fixture.PostFixtures.POST_1;
import static daybyquest.support.fixture.PostFixtures.POST_2;
import static daybyquest.support.fixture.PostFixtures.POST_3;
import static daybyquest.support.fixture.UserFixtures.ALICE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.post.domain.Posts;
import daybyquest.relation.domain.Follow;
import daybyquest.relation.domain.Follows;
import daybyquest.support.test.ServiceTest;
import daybyquest.user.dto.response.MyProfileResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetMyProfileServiceTest extends ServiceTest {

@Autowired
private GetMyProfileService getMyProfileService;

@Autowired
private Posts posts;

@Autowired
private Follows follows;

@Test
void 사용자_프로필을_조회한다() {
// given
final Long id = ALICE를_저장한다();

// when
final MyProfileResponse response = getMyProfileService.invoke(id);

// then
assertAll(() -> {
assertThat(response.username()).isEqualTo(ALICE.username);
assertThat(response.name()).isEqualTo(ALICE.name);
assertThat(response.imageIdentifier()).isEqualTo(ALICE.imageIdentifier);
});
}

@Test
void 게시물_수가_함께_조회된다() {
// given
final Long id = ALICE를_저장한다();
posts.save(POST_1.생성(id));
posts.save(POST_2.생성(id));
posts.save(POST_3.생성(id));

// when
final MyProfileResponse response = getMyProfileService.invoke(id);

// then
assertThat(response.postCount()).isEqualTo(3);
}

@Test
void 팔로워_수가_함께_조회된다() {
// given
final Long id = ALICE를_저장한다();
follows.save(new Follow(BOB을_저장한다(), id));
follows.save(new Follow(CHARLIE를_저장한다(), id));
follows.save(new Follow(DAVID를_저장한다(), id));

// when
final MyProfileResponse response = getMyProfileService.invoke(id);

// then
assertThat(response.followerCount()).isEqualTo(3);
}

@Test
void 팔로잉_수가_함께_조회된다() {
// given
final Long id = ALICE를_저장한다();
follows.save(new Follow(id, BOB을_저장한다()));
follows.save(new Follow(id, CHARLIE를_저장한다()));
follows.save(new Follow(id, DAVID를_저장한다()));

// when
final MyProfileResponse response = getMyProfileService.invoke(id);

// then
assertThat(response.followingCount()).isEqualTo(3);
}
}
Loading

0 comments on commit 33c0887

Please sign in to comment.