-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
344 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
eeos/resources/test-develop-environment/mysql-init.d/00_init.sql
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
eeos/src/test/java/com/blackcompany/eeos/common/DataClearExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.blackcompany.eeos.common; | ||
|
||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
@Component | ||
public class DataClearExtension implements BeforeEachCallback { | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) { | ||
DatabaseCleaner dataCleaner = getDataCleaner(context); | ||
dataCleaner.clear(); | ||
} | ||
|
||
private DatabaseCleaner getDataCleaner(final ExtensionContext extensionContext) { | ||
return SpringExtension.getApplicationContext(extensionContext).getBean(DatabaseCleaner.class); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
eeos/src/test/java/com/blackcompany/eeos/common/DatabaseCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.blackcompany.eeos.common; | ||
|
||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
public class DatabaseCleaner { | ||
private static final String FOREIGN_KEY_CHECK_FORMAT = "SET FOREIGN_KEY_CHECKS = %d"; | ||
private static final String TRUNCATE_FORMAT = "TRUNCATE TABLE %s"; | ||
private static final String AUTO_INCREMENT_FORMAT = "ALTER TABLE %s AUTO_INCREMENT = 1"; | ||
|
||
@PersistenceContext private EntityManager entityManager; | ||
|
||
@Transactional | ||
public void clear() { | ||
disableForeignKeyChecks(); | ||
List<String> tableNames = getTableNames(); | ||
truncate(tableNames); | ||
enableForeignKeyChecks(); | ||
} | ||
|
||
private void disableForeignKeyChecks() { | ||
entityManager.createNativeQuery(String.format(FOREIGN_KEY_CHECK_FORMAT, 0)).executeUpdate(); | ||
} | ||
|
||
private List<String> getTableNames() { | ||
String query = | ||
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE()"; | ||
return entityManager.createNativeQuery(query).getResultList(); | ||
} | ||
|
||
private void enableForeignKeyChecks() { | ||
entityManager.createNativeQuery(String.format(FOREIGN_KEY_CHECK_FORMAT, 1)).executeUpdate(); | ||
} | ||
|
||
private void truncate(List<String> tableNames) { | ||
for (String tableName : tableNames) { | ||
entityManager.createNativeQuery(String.format(TRUNCATE_FORMAT, tableName)).executeUpdate(); | ||
entityManager | ||
.createNativeQuery(String.format(AUTO_INCREMENT_FORMAT, tableName)) | ||
.executeUpdate(); | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...rc/test/java/com/blackcompany/eeos/member/application/service/AdminMemberServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.blackcompany.eeos.member.application.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import com.blackcompany.eeos.auth.persistence.OAuthMemberRepository; | ||
import com.blackcompany.eeos.common.DataClearExtension; | ||
import com.blackcompany.eeos.member.application.dto.ChangeActiveStatusRequest; | ||
import com.blackcompany.eeos.member.application.dto.CommandMemberResponse; | ||
import com.blackcompany.eeos.member.application.exception.DeniedMemberEditException; | ||
import com.blackcompany.eeos.member.application.model.ActiveStatus; | ||
import com.blackcompany.eeos.member.fixture.MemberFixture; | ||
import com.blackcompany.eeos.member.persistence.MemberEntity; | ||
import com.blackcompany.eeos.member.persistence.MemberRepository; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
|
||
@SpringBootTest | ||
@ExtendWith(DataClearExtension.class) | ||
class AdminMemberServiceTest { | ||
|
||
@Autowired private AdminMemberService adminMemberService; | ||
|
||
@SpyBean private MemberRepository memberRepository; | ||
|
||
@MockBean private OAuthMemberRepository oAuthMemberRepository; | ||
|
||
Long adminId = 1L; | ||
Long memberId1 = 2L; | ||
Long memberId2 = 3L; | ||
|
||
@Test | ||
@DisplayName("관리자는 회원의 상태를 변경할 수 있다.") | ||
void admin_change_status() { | ||
// given | ||
MemberEntity adminEntity = MemberFixture.어드민_엔티티(adminId); | ||
MemberEntity memberEntity = MemberFixture.멤버_엔티티(memberId1, ActiveStatus.AM); | ||
|
||
memberRepository.save(adminEntity); | ||
memberRepository.save(memberEntity); | ||
|
||
ChangeActiveStatusRequest request = | ||
ChangeActiveStatusRequest.builder().activeStatus("rm").build(); | ||
|
||
// when | ||
CommandMemberResponse result = | ||
adminMemberService.changeActiveStatus(adminEntity.getId(), memberEntity.getId(), request); | ||
|
||
// then | ||
assertEquals(ActiveStatus.RM.getStatus(), result.getActiveStatus()); | ||
assertEquals(memberEntity.getName(), result.getName()); | ||
} | ||
|
||
@Test | ||
@DisplayName("관리자가 아닌 사용자가 회원 상태를 변경하려고 하면 예외가 발생한다.") | ||
void non_admin_change_status() { | ||
// given | ||
MemberEntity nonAdminEntity = MemberFixture.멤버_엔티티(memberId1, ActiveStatus.AM); | ||
MemberEntity memberEntity = MemberFixture.멤버_엔티티(memberId2, ActiveStatus.AM); | ||
|
||
memberRepository.save(nonAdminEntity); | ||
memberRepository.save(memberEntity); | ||
|
||
ChangeActiveStatusRequest request = | ||
ChangeActiveStatusRequest.builder().activeStatus("rm").build(); | ||
|
||
// when & then | ||
assertThrows( | ||
DeniedMemberEditException.class, | ||
() -> | ||
adminMemberService.changeActiveStatus( | ||
nonAdminEntity.getId(), memberEntity.getId(), request)); | ||
} | ||
|
||
@Test | ||
@DisplayName("관리자는 회원을 삭제할 수 있다.") | ||
void admin_delete_member() { | ||
// given | ||
MemberEntity adminEntity = MemberFixture.어드민_엔티티(adminId); | ||
MemberEntity memberEntity = MemberFixture.멤버_엔티티(memberId1, ActiveStatus.AM); | ||
|
||
memberRepository.save(adminEntity); | ||
memberRepository.save(memberEntity); | ||
|
||
// when | ||
adminMemberService.delete(adminEntity.getId(), memberEntity.getId()); | ||
|
||
// then | ||
assertFalse(memberRepository.existsById(memberEntity.getId())); | ||
assertFalse(oAuthMemberRepository.existsById(memberEntity.getId())); | ||
} | ||
|
||
@Test | ||
@DisplayName("관리자가 아닌 사용자가 회원을 삭제하려고하면 예외가 발생한다.") | ||
void non_admin_delete_member() { | ||
// given | ||
MemberEntity nonAdminEntity = MemberFixture.멤버_엔티티(memberId1, ActiveStatus.AM); | ||
MemberEntity memberEntity = MemberFixture.멤버_엔티티(memberId2, ActiveStatus.AM); | ||
|
||
memberRepository.save(nonAdminEntity); | ||
memberRepository.save(memberEntity); | ||
|
||
// when & then | ||
assertThrows( | ||
DeniedMemberEditException.class, | ||
() -> adminMemberService.delete(nonAdminEntity.getId(), memberEntity.getId())); | ||
} | ||
} |
Oops, something went wrong.