Skip to content

Commit

Permalink
test : 호스트 유저 테스트 코드 작성 (#356)
Browse files Browse the repository at this point in the history
* test : 호스트 테스트 최적화

* test : 호스트 유저 테스트 작성

* test : HostRole 테스트 작성

* test : Host vo 테스트 수정
  • Loading branch information
gengminy authored Feb 15, 2023
1 parent 01fa287 commit bf48c69
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import band.gosrock.domain.domains.host.domain.Host;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode
@Builder
public class HostInfoVo {
private final Long hostId;
Expand All @@ -20,7 +22,7 @@ public class HostInfoVo {

private final String contactNumber;

private final boolean partner;
private final Boolean partner;

public static HostInfoVo from(Host host) {
return HostInfoVo.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import band.gosrock.domain.domains.host.domain.Host;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode
@Builder
public class HostProfileVo {
private final Long hostId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package band.gosrock.domain.domains.host.domain;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

import band.gosrock.domain.common.aop.domainEvent.Events;
import band.gosrock.domain.common.events.host.HostRegisterSlackEvent;
Expand Down Expand Up @@ -33,7 +35,7 @@ public class Host extends BaseTimeEntity {

// 파트너 여부
// 정책상 초기값 false 로 고정입니다
private final Boolean partner = false;
private final Boolean partner = FALSE;

// 슬랙 웹훅 url
private String slackUrl;
Expand Down Expand Up @@ -68,11 +70,6 @@ public HostUser getHostUserByUserId(Long userId) {
.orElseThrow(() -> HostUserNotFoundException.EXCEPTION);
}

public String getSlackToken() {
if (this.slackUrl == null) return null;
return this.slackUrl.substring(this.slackUrl.indexOf("https://hooks.slack.com/services/"));
}

public void updateProfile(HostProfile hostProfile) {
this.profile.updateProfile(hostProfile);
}
Expand Down Expand Up @@ -100,9 +97,8 @@ public Boolean isActiveHostUserId(Long userId) {

public void setHostUserRole(Long userId, HostRole role) {
// 마스터의 역할은 수정할 수 없음
if (this.getMasterUserId().equals(userId)) {
if (this.getMasterUserId().equals(userId))
throw CannotModifyMasterHostRoleException.EXCEPTION;
}
this.hostUsers.stream()
.filter(hostUser -> hostUser.getUserId().equals(userId))
.findFirst()
Expand All @@ -125,17 +121,13 @@ public void validateHostUserExistence(HostUser hostUser) {

/** 해당 유저가 호스트에 속하는지 확인하는 검증 로직입니다 */
public void validateHostUser(Long userId) {
if (!this.hasHostUserId(userId)) {
throw ForbiddenHostException.EXCEPTION;
}
if (!this.hasHostUserId(userId)) throw ForbiddenHostException.EXCEPTION;
}

/** 해당 유저가 호스트에 속하며 가입 승인을 완료했는지 (활성상태) 확인하는 검증 로직입니다 */
public void validateActiveHostUser(Long userId) {
this.validateHostUser(userId);
if (!this.isActiveHostUserId(userId)) {
throw NotAcceptedHostException.EXCEPTION;
}
if (!this.isActiveHostUserId(userId)) throw NotAcceptedHostException.EXCEPTION;
}

/** 해당 유저가 매니저 이상인지 확인하는 검증 로직입니다 */
Expand All @@ -149,16 +141,12 @@ public void validateManagerHostUser(Long userId) {
/** 해당 유저가 호스트의 마스터(담당자, 방장)인지 확인하는 검증 로직입니다 */
public void validateMasterHostUser(Long userId) {
this.validateActiveHostUser(userId);
if (!this.getMasterUserId().equals(userId)) {
throw NotMasterHostException.EXCEPTION;
}
if (!this.getMasterUserId().equals(userId)) throw NotMasterHostException.EXCEPTION;
}

/** 해당 호스트가 파트너 인지 검증합니다. */
public void validatePartnerHost() {
if (!partner) {
throw NotPartnerHostException.EXCEPTION;
}
if (partner != TRUE) throw NotPartnerHostException.EXCEPTION;
}

public HostInfoVo toHostInfoVo() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package band.gosrock.domain.domains.host.domain;

import static band.gosrock.domain.domains.host.domain.HostRole.GUEST;
import static java.lang.Boolean.FALSE;

import band.gosrock.domain.common.aop.domainEvent.Events;
import band.gosrock.domain.common.events.host.HostUserJoinEvent;
Expand Down Expand Up @@ -32,11 +34,11 @@ public class HostUser extends BaseTimeEntity {
private Long userId;

// 초대 승락 여부
private Boolean active = false;
private Boolean active = FALSE;

// 유저의 권한
@Enumerated(EnumType.STRING)
private HostRole role = HostRole.GUEST;
private HostRole role = GUEST;

public void setHostRole(HostRole role) {
this.role = role;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package band.gosrock.domain.domains.host.domain;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class HostProfileTest {

@Mock Host host;

HostProfile hostProfile;

@BeforeEach
void setup() {
hostProfile = HostProfile.builder().build();
}

@Test
void 호스트_프로필_업데이트_테스트() {
// given
final HostProfile newHostProfile =
HostProfile.builder()
.name("테스트")
.contactEmail("[email protected]")
.contactNumber("010-0000-0000")
.introduce("123")
.profileImageKey("key")
.build();
// when
hostProfile.updateProfile(newHostProfile);
// then
assertEquals(hostProfile.getProfileImage(), newHostProfile.getProfileImage());
assertEquals(hostProfile.getContactEmail(), newHostProfile.getContactEmail());
assertEquals(hostProfile.getContactNumber(), newHostProfile.getContactNumber());
assertEquals(hostProfile.getIntroduce(), newHostProfile.getIntroduce());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package band.gosrock.domain.domains.host.domain;

import static band.gosrock.domain.domains.host.domain.HostRole.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class HostRoleTest {

@Test
void ENUM_값에_해당하지_않으면_NULL_반환해야한다() {
// given
// when
// then
assertEquals(HostRole.fromHostRole("MASTER"), MASTER);
assertEquals(HostRole.fromHostRole("MANAGER"), MANAGER);
assertEquals(HostRole.fromHostRole("GUEST"), GUEST);
assertNull(HostRole.fromHostRole("NOT_PROVIDED"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;

import band.gosrock.domain.common.vo.HostInfoVo;
import band.gosrock.domain.common.vo.HostProfileVo;
import band.gosrock.domain.domains.host.exception.*;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -19,6 +21,8 @@ public class HostTest {
@Mock HostUser managerHostUser;
@Mock HostUser guestHostUser;
@Mock HostProfile hostProfile;
@Mock HostInfoVo hostInfoVo;
@Mock HostProfileVo hostProfileVo;

Host host;
final Long masterUserId = 1L;
Expand Down Expand Up @@ -302,4 +306,13 @@ void setup() {
assertThrows(NotPartnerHostException.class, () -> host.validatePartnerHost());
assertDoesNotThrow(partnerHost::validatePartnerHost);
}

@Test
public void 호스트_vo_변환_테스트() {
// given
// when
// then
assertEquals(host.toHostInfoVo(), HostInfoVo.from(host));
assertEquals(host.toHostProfileVo(), HostProfileVo.from(host));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package band.gosrock.domain.domains.host.domain;

import static band.gosrock.domain.domains.host.domain.HostRole.MANAGER;
import static band.gosrock.domain.domains.host.domain.HostRole.MASTER;
import static java.lang.Boolean.TRUE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import band.gosrock.domain.domains.host.exception.AlreadyJoinedHostException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class HostUserTest {

@Mock Host host;
HostUser hostUser;

@BeforeEach
void setup() {
hostUser = HostUser.builder().host(host).role(MASTER).build();
}

@Test
void 호스트유저_권한변경_테스트() {
// given
final HostRole role = MANAGER;
// when
hostUser.setHostRole(role);
// then
assertEquals(hostUser.getRole(), role);
}

@Test
void 호스트유저_초대수락으로_활성화_테스트() {
// given
// when
hostUser.activate();
// then
assertEquals(hostUser.getActive(), TRUE);
}

@Test
void 호스트유저_초대_중복수락은_능하다() {
// given
// when
hostUser.activate();
// then
assertThrows(AlreadyJoinedHostException.class, () -> hostUser.activate());
}
}

0 comments on commit bf48c69

Please sign in to comment.