Skip to content

Commit

Permalink
test: refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
geneaky committed Sep 18, 2024
1 parent 8763648 commit 69b942e
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 210 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package toy.bookchat.bookchat.domain.chat.service;

import static org.assertj.core.api.Assertions.assertThat;
import static toy.bookchat.bookchat.domain.common.Status.ACTIVE;
import static toy.bookchat.bookchat.support.Status.ACTIVE;
import static toy.bookchat.bookchat.domain.participant.ParticipantStatus.HOST;

import java.time.LocalDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static toy.bookchat.bookchat.domain.common.Status.ACTIVE;
import static toy.bookchat.bookchat.support.Status.ACTIVE;

import java.util.Optional;
import javax.persistence.EntityManager;
Expand All @@ -17,144 +17,144 @@

class UserEntityRepositoryTest extends RepositoryTest {

@Autowired
private UserRepository userRepository;
@Autowired
private EntityManager em;
@Autowired
private UserRepository userRepository;
@Autowired
private EntityManager em;

@Test
void 사용자_이메일_OAuth2Provider_구분으로_조회_성공() throws Exception {

UserEntity userEntity1 = UserEntity.builder()
.name("user")
.email("[email protected]")
.provider(OAuth2Provider.KAKAO)
.build();

UserEntity userEntity2 = UserEntity.builder()
.name("user")
.email("[email protected]")
.provider(OAuth2Provider.GOOGLE)
.build();

userRepository.save(userEntity1);
userRepository.save(userEntity2);

Optional<UserEntity> kakaoUser = userRepository.findByEmailAndProvider(
"[email protected]", OAuth2Provider.KAKAO);

Optional<UserEntity> googleUser = userRepository.findByEmailAndProvider(
"[email protected]", OAuth2Provider.GOOGLE);

assertThat(kakaoUser).isNotEqualTo(googleUser);
}

@Test
void 사용자_nickname_존재시_true_반환_성공() throws Exception {
UserEntity userEntity = UserEntity.builder()
.name("user")
.email("[email protected]")
.provider(OAuth2Provider.KAKAO)
.nickname("nickname")
.build();

userRepository.save(userEntity);

boolean result = userRepository.existsByNickname("nickname");

assertThat(result).isTrue();
}

@Test
void 사용자_nickname_존재하지않을시_false_반환_성공() throws Exception {
boolean result = userRepository.existsByNickname("nickname");

assertThat(result).isFalse();
}

@Test
void 사용자_이름으로_조회_성공() throws Exception {
String userName = "KAKAO123456";
UserEntity userEntity = UserEntity.builder()
.name(userName)
.email("[email protected]")
.provider(OAuth2Provider.KAKAO)
.nickname("nickname")
.build();

userRepository.save(userEntity);

UserEntity findUserEntity = userRepository.findByName(userName).get();

assertThat(userEntity).isEqualTo(findUserEntity);
}

@Test
void 사용자_삭제_성공() throws Exception {
UserEntity userEntity = UserEntity.builder()
.email("[email protected]")
.name("testkakao")
.nickname("nickname")
.role(ROLE.USER)
.profileImageUrl("[email protected]")
.defaultProfileImageType(1)
.build();

userRepository.save(userEntity);

userRepository.delete(userEntity);

Optional<UserEntity> ou = userRepository.findById(userEntity.getId());
assertThat(ou).isNotPresent();
}

@Test
void 변경_요청한_닉네임_이미_있을시_예외발생() throws Exception {
UserEntity userEntity1 = UserEntity.builder()
.nickname("user1")
.build();

UserEntity userEntity2 = UserEntity.builder()
.nickname("user2")
.build();

userRepository.saveAndFlush(userEntity1);
userRepository.save(userEntity2);
userEntity2.changeUserNickname("user1");

assertThatThrownBy(() -> {
userRepository.flush();
}).isInstanceOf(DataIntegrityViolationException.class);
}

@Test
void 이미_존재하는_nickname_경우_생성_실패() throws Exception {
UserEntity userEntity1 = UserEntity.builder()
.nickname("test nickname")
.build();

userRepository.save(userEntity1);
@Test
void 사용자_이메일_OAuth2Provider_구분으로_조회_성공() throws Exception {

UserEntity userEntity1 = UserEntity.builder()
.name("user")
.email("[email protected]")
.provider(OAuth2Provider.KAKAO)
.build();

UserEntity userEntity2 = UserEntity.builder()
.name("user")
.email("[email protected]")
.provider(OAuth2Provider.GOOGLE)
.build();

userRepository.save(userEntity1);
userRepository.save(userEntity2);

Optional<UserEntity> kakaoUser = userRepository.findByEmailAndProvider(
"[email protected]", OAuth2Provider.KAKAO);

Optional<UserEntity> googleUser = userRepository.findByEmailAndProvider(
"[email protected]", OAuth2Provider.GOOGLE);

assertThat(kakaoUser).isNotEqualTo(googleUser);
}

@Test
void 사용자_nickname_존재시_true_반환_성공() throws Exception {
UserEntity userEntity = UserEntity.builder()
.name("user")
.email("[email protected]")
.provider(OAuth2Provider.KAKAO)
.nickname("nickname")
.build();

userRepository.save(userEntity);

boolean result = userRepository.existsByNickname("nickname");

assertThat(result).isTrue();
}

@Test
void 사용자_nickname_존재하지않을시_false_반환_성공() throws Exception {
boolean result = userRepository.existsByNickname("nickname");

assertThat(result).isFalse();
}

@Test
void 사용자_이름으로_조회_성공() throws Exception {
String userName = "KAKAO123456";
UserEntity userEntity = UserEntity.builder()
.name(userName)
.email("[email protected]")
.provider(OAuth2Provider.KAKAO)
.nickname("nickname")
.build();

userRepository.save(userEntity);

UserEntity findUserEntity = userRepository.findByName(userName).get();

assertThat(userEntity).isEqualTo(findUserEntity);
}

@Test
void 사용자_삭제_성공() throws Exception {
UserEntity userEntity = UserEntity.builder()
.email("[email protected]")
.name("testkakao")
.nickname("nickname")
.role(ROLE.USER)
.profileImageUrl("[email protected]")
.defaultProfileImageType(1)
.build();

userRepository.save(userEntity);

userRepository.delete(userEntity);

Optional<UserEntity> ou = userRepository.findById(userEntity.getId());
assertThat(ou).isNotPresent();
}

@Test
void 변경_요청한_닉네임_이미_있을시_예외발생() throws Exception {
UserEntity userEntity1 = UserEntity.builder()
.nickname("user1")
.build();

UserEntity userEntity2 = UserEntity.builder()
.nickname("user2")
.build();

userRepository.saveAndFlush(userEntity1);
userRepository.save(userEntity2);
userEntity2.changeUserNickname("user1");

assertThatThrownBy(() -> {
userRepository.flush();
}).isInstanceOf(DataIntegrityViolationException.class);
}

@Test
void 이미_존재하는_nickname_경우_생성_실패() throws Exception {
UserEntity userEntity1 = UserEntity.builder()
.nickname("test nickname")
.build();

userRepository.save(userEntity1);

UserEntity userEntity2 = UserEntity.builder()
.nickname("test nickname")
.build();
assertThatThrownBy(() -> userRepository.save(userEntity2))
.isInstanceOf(DataIntegrityViolationException.class);
}

@Test
void 사용자_계정생성시_기본_상태값_active_생성_성공() throws Exception {
UserEntity userEntity = UserEntity.builder()
.nickname("user1")
.build();

userRepository.save(userEntity);

em.flush();
em.clear();

UserEntity findUserEntity = userRepository.findById(userEntity.getId()).get();
UserEntity userEntity2 = UserEntity.builder()
.nickname("test nickname")
.build();
assertThatThrownBy(() -> userRepository.save(userEntity2))
.isInstanceOf(DataIntegrityViolationException.class);
}

@Test
void 사용자_계정생성시_기본_상태값_active_생성_성공() throws Exception {
UserEntity userEntity = UserEntity.builder()
.nickname("user1")
.build();

userRepository.save(userEntity);

em.flush();
em.clear();

UserEntity findUserEntity = userRepository.findById(userEntity.getId()).get();

assertThat(findUserEntity.getStatus()).isEqualTo(ACTIVE);
}
assertThat(findUserEntity.getStatus()).isEqualTo(ACTIVE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.internal.verification.VerificationModeFactory.noInteractions;
import static toy.bookchat.bookchat.domain.common.Status.ACTIVE;
import static toy.bookchat.bookchat.support.Status.ACTIVE;

import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -24,7 +24,7 @@
import toy.bookchat.bookchat.db_module.device.repository.DeviceRepository;
import toy.bookchat.bookchat.db_module.user.UserEntity;
import toy.bookchat.bookchat.db_module.user.repository.UserRepository;
import toy.bookchat.bookchat.domain.common.Status;
import toy.bookchat.bookchat.support.Status;
import toy.bookchat.bookchat.domain.storage.StorageService;
import toy.bookchat.bookchat.domain.user.UserProfile;
import toy.bookchat.bookchat.domain.user.api.v1.request.ChangeUserNicknameRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package toy.bookchat.bookchat.domain.common;
package toy.bookchat.bookchat.support;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import toy.bookchat.bookchat.support.RateLimit;

class RateLimiterAopTest {

Expand Down
Loading

0 comments on commit 69b942e

Please sign in to comment.