diff --git a/.github/workflows/backend-ci.yml b/.github/workflows/backend-ci.yml index 32104770..7969837e 100644 --- a/.github/workflows/backend-ci.yml +++ b/.github/workflows/backend-ci.yml @@ -27,7 +27,7 @@ jobs: - name: Create application.properties run: | cd ./src/main/resources - echo "${{ secrets.PROPERTIES }}" > ./application.properties + echo "${{ secrets.PROPERTIES }}" > ./env.properties - name: Start DB run: | diff --git a/eeos/resources/test-develop-environment/mysql-init.d/00_init.sql b/eeos/resources/test-develop-environment/mysql-init.d/00_init.sql deleted file mode 100644 index 5c3dcc19..00000000 --- a/eeos/resources/test-develop-environment/mysql-init.d/00_init.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE - USER 'eeos-test'@'localhost' IDENTIFIED BY 'eeos-test'; -CREATE - USER 'eeos-test'@'%' IDENTIFIED BY 'eeos-test'; - -GRANT ALL PRIVILEGES ON *.* TO - 'eeos-test'@'localhost'; -GRANT ALL PRIVILEGES ON *.* TO - 'eeos-test'@'%'; - -CREATE - DATABASE eeos DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/eeos/src/main/java/com/blackcompany/eeos/member/application/model/MemberModel.java b/eeos/src/main/java/com/blackcompany/eeos/member/application/model/MemberModel.java index 9437f4c6..73fa012a 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/member/application/model/MemberModel.java +++ b/eeos/src/main/java/com/blackcompany/eeos/member/application/model/MemberModel.java @@ -37,14 +37,14 @@ public String getActiveStatus() { return activeStatus.getStatus(); } + @Override + public Long getMemberId() { + return id; + } + private void canEdit(ActiveStatus requestStatus) { if (requestStatus.isAll()) { throw new DeniedUpdateActiveException(requestStatus.getStatus()); } } - - @Override - public Long getMemberId() { - return id; - } } diff --git a/eeos/src/main/java/com/blackcompany/eeos/member/application/service/CommandMemberService.java b/eeos/src/main/java/com/blackcompany/eeos/member/application/service/AdminMemberService.java similarity index 84% rename from eeos/src/main/java/com/blackcompany/eeos/member/application/service/CommandMemberService.java rename to eeos/src/main/java/com/blackcompany/eeos/member/application/service/AdminMemberService.java index 51575998..2046daa9 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/member/application/service/CommandMemberService.java +++ b/eeos/src/main/java/com/blackcompany/eeos/member/application/service/AdminMemberService.java @@ -19,59 +19,56 @@ @Service @RequiredArgsConstructor -public class CommandMemberService implements ChangeActiveStatusUsecase { +public class AdminMemberService implements ChangeActiveStatusUsecase { private final MemberRepository memberRepository; private final OAuthMemberRepository oAuthMemberRepository; private final MemberEntityConverter memberConverter; private final CommandMemberResponseConverter responseConverter; private final ApplicationEventPublisher applicationEventPublisher; - private final QueryMemberService memberService; @Transactional @Override - public CommandMemberResponse adminChangeStatus( + public CommandMemberResponse changeActiveStatus( final Long adminMemberId, Long memberId, final ChangeActiveStatusRequest request) { - validateUser(adminMemberId); - MemberModel model = - memberRepository - .findById(memberId) - .map(memberConverter::from) - .orElseThrow(NotFoundMemberException::new); + validateAdminPermission(adminMemberId); + MemberModel model = findMember(memberId); MemberEntity updateMember = updateActiveStatus(model, request.getActiveStatus()); return responseConverter.from( updateMember.getName(), updateMember.getActiveStatus().getStatus()); } - private MemberEntity updateActiveStatus(final MemberModel model, final String status) { - MemberModel memberModel = model.updateActiveStatus(status); - return memberRepository.save(memberConverter.toEntity(memberModel)); - } + @Override + @Transactional + public void delete(final Long adminMemberId, final Long memberId) { + validateAdminPermission(adminMemberId); - private MemberModel findMember(final Long memberId) { - return memberRepository - .findById(memberId) - .map(memberConverter::from) - .orElseThrow(NotFoundMemberException::new); + MemberModel member = findMember(memberId); + memberRepository.deleteById(member.getId()); + oAuthMemberRepository.deleteById(member.getId()); + + applicationEventPublisher.publishEvent(DeletedMemberEvent.of(memberId)); } - private void validateUser(Long memberId) { - if (memberService.findMember(memberId).isAdmin()) { + private void validateAdminPermission(Long memberId) { + MemberModel member = findMember(memberId); + + if (member.isAdmin()) { return; } throw new DeniedMemberEditException(memberId); } - @Override - @Transactional - public void delete(final Long adminMemberId, final Long memberId) { - MemberModel member = findMember(memberId); - validateUser(adminMemberId); - - memberRepository.deleteById(member.getId()); - oAuthMemberRepository.deleteById(member.getId()); + private MemberModel findMember(final Long memberId) { + return memberRepository + .findById(memberId) + .map(memberConverter::from) + .orElseThrow(NotFoundMemberException::new); + } - applicationEventPublisher.publishEvent(DeletedMemberEvent.of(memberId)); + private MemberEntity updateActiveStatus(final MemberModel model, final String status) { + MemberModel memberModel = model.updateActiveStatus(status); + return memberRepository.save(memberConverter.toEntity(memberModel)); } } diff --git a/eeos/src/main/java/com/blackcompany/eeos/member/application/usecase/ChangeActiveStatusUsecase.java b/eeos/src/main/java/com/blackcompany/eeos/member/application/usecase/ChangeActiveStatusUsecase.java index 59f5f91e..de213975 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/member/application/usecase/ChangeActiveStatusUsecase.java +++ b/eeos/src/main/java/com/blackcompany/eeos/member/application/usecase/ChangeActiveStatusUsecase.java @@ -5,7 +5,7 @@ /** 멤버의 활동 상태를 변경한다. */ public interface ChangeActiveStatusUsecase { - CommandMemberResponse adminChangeStatus( + CommandMemberResponse changeActiveStatus( Long adminMemberId, Long memberId, ChangeActiveStatusRequest request); void delete(Long adminMemberId, Long memberId); diff --git a/eeos/src/main/java/com/blackcompany/eeos/member/presentation/controller/MemberController.java b/eeos/src/main/java/com/blackcompany/eeos/member/presentation/controller/MemberController.java index 01088001..b9c267bc 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/member/presentation/controller/MemberController.java +++ b/eeos/src/main/java/com/blackcompany/eeos/member/presentation/controller/MemberController.java @@ -10,14 +10,12 @@ import com.blackcompany.eeos.member.presentation.docs.MemberApi; import javax.validation.Valid; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor @RequestMapping("/api/members") -@Slf4j public class MemberController implements MemberApi { private final ChangeActiveStatusUsecase changeActiveStatusUsecase; private final GetMembersByActiveStatus getMembersByActiveStatus; @@ -30,7 +28,7 @@ public ApiResponse> adminChangeActiveStatus( @PathVariable("memberId") Long memberId, @RequestBody @Valid ChangeActiveStatusRequest request) { CommandMemberResponse response = - changeActiveStatusUsecase.adminChangeStatus(adminMemberId, memberId, request); + changeActiveStatusUsecase.changeActiveStatus(adminMemberId, memberId, request); return ApiResponseGenerator.success(response, HttpStatus.OK, MessageCode.UPDATE); } diff --git a/eeos/src/main/java/com/blackcompany/eeos/program/application/dto/ChangeAllAttendStatusRequest.java b/eeos/src/main/java/com/blackcompany/eeos/program/application/dto/ChangeAllAttendStatusRequest.java index 52118469..29d79c20 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/program/application/dto/ChangeAllAttendStatusRequest.java +++ b/eeos/src/main/java/com/blackcompany/eeos/program/application/dto/ChangeAllAttendStatusRequest.java @@ -12,6 +12,5 @@ @Builder(toBuilder = true) public class ChangeAllAttendStatusRequest implements AbstractRequestDto { private Long memberId; - private String beforeAttendStatus; private String afterAttendStatus; } diff --git a/eeos/src/main/java/com/blackcompany/eeos/program/application/model/ProgramModel.java b/eeos/src/main/java/com/blackcompany/eeos/program/application/model/ProgramModel.java index 811d2b2f..d4b1fbdb 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/program/application/model/ProgramModel.java +++ b/eeos/src/main/java/com/blackcompany/eeos/program/application/model/ProgramModel.java @@ -58,8 +58,6 @@ public void validateCreate() { if (!isGithubUrl()) { throw new IsNotGithubUrlException(); } - - return; } public void validateEditAttend(Long memberId) { diff --git a/eeos/src/main/java/com/blackcompany/eeos/target/application/model/AttendModel.java b/eeos/src/main/java/com/blackcompany/eeos/target/application/model/AttendModel.java index 6c051290..771e1b4c 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/target/application/model/AttendModel.java +++ b/eeos/src/main/java/com/blackcompany/eeos/target/application/model/AttendModel.java @@ -31,13 +31,6 @@ public AttendModel changeStatus(String afterStatus) { return this; } - public AttendModel changeStatusByManager(String beforeStatus, String afterStatus) { - validateChangeByManager(beforeStatus); - this.status = AttendStatus.find(afterStatus); - - return this; - } - public String getStatus() { return status.getStatus(); } diff --git a/eeos/src/main/java/com/blackcompany/eeos/target/application/service/SelectAttendCommandTargetMemberMemberService.java b/eeos/src/main/java/com/blackcompany/eeos/target/application/service/SelectAttendCommandTargetMemberMemberService.java index be193b1e..8f7598e0 100644 --- a/eeos/src/main/java/com/blackcompany/eeos/target/application/service/SelectAttendCommandTargetMemberMemberService.java +++ b/eeos/src/main/java/com/blackcompany/eeos/target/application/service/SelectAttendCommandTargetMemberMemberService.java @@ -98,7 +98,7 @@ private List findDifferent( private void updateAttendStatus( AttendModel model, List requests, AttendManager attendManager) { ChangeAllAttendStatusRequest request = findUpdateRequest(model.getMemberId(), requests); - model.changeStatusByManager(request.getBeforeAttendStatus(), request.getAfterAttendStatus()); + model.changeStatus(request.getAfterAttendStatus()); if (Objects.equals(request.getAfterAttendStatus(), AttendStatus.NONRELATED.getStatus())) { attendManager.addNonRelated(model); diff --git a/eeos/src/test/java/com/blackcompany/eeos/common/DataClearExtension.java b/eeos/src/test/java/com/blackcompany/eeos/common/DataClearExtension.java new file mode 100644 index 00000000..d657c828 --- /dev/null +++ b/eeos/src/test/java/com/blackcompany/eeos/common/DataClearExtension.java @@ -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); + } +} diff --git a/eeos/src/test/java/com/blackcompany/eeos/common/DatabaseCleaner.java b/eeos/src/test/java/com/blackcompany/eeos/common/DatabaseCleaner.java new file mode 100644 index 00000000..3c843297 --- /dev/null +++ b/eeos/src/test/java/com/blackcompany/eeos/common/DatabaseCleaner.java @@ -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 tableNames = getTableNames(); + truncate(tableNames); + enableForeignKeyChecks(); + } + + private void disableForeignKeyChecks() { + entityManager.createNativeQuery(String.format(FOREIGN_KEY_CHECK_FORMAT, 0)).executeUpdate(); + } + + private List 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 tableNames) { + for (String tableName : tableNames) { + entityManager.createNativeQuery(String.format(TRUNCATE_FORMAT, tableName)).executeUpdate(); + entityManager + .createNativeQuery(String.format(AUTO_INCREMENT_FORMAT, tableName)) + .executeUpdate(); + } + } +} diff --git a/eeos/src/test/java/com/blackcompany/eeos/member/application/service/AdminMemberServiceTest.java b/eeos/src/test/java/com/blackcompany/eeos/member/application/service/AdminMemberServiceTest.java new file mode 100644 index 00000000..a30d22ba --- /dev/null +++ b/eeos/src/test/java/com/blackcompany/eeos/member/application/service/AdminMemberServiceTest.java @@ -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())); + } +} diff --git a/eeos/src/test/java/com/blackcompany/eeos/member/application/service/CommandMemberServiceTest.java b/eeos/src/test/java/com/blackcompany/eeos/member/application/service/CommandMemberServiceTest.java deleted file mode 100644 index a0e5bd44..00000000 --- a/eeos/src/test/java/com/blackcompany/eeos/member/application/service/CommandMemberServiceTest.java +++ /dev/null @@ -1,43 +0,0 @@ -//package com.blackcompany.eeos.member.application.service; -// -//import static org.mockito.Mockito.when; -// -//import com.blackcompany.eeos.member.application.dto.ChangeActiveStatusRequest; -//import com.blackcompany.eeos.member.application.dto.converter.CommandMemberResponseConverter; -//import com.blackcompany.eeos.member.application.exception.NotFoundMemberException; -//import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter; -//import com.blackcompany.eeos.member.persistence.MemberRepository; -//import java.util.Optional; -//import org.junit.jupiter.api.Assertions; -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; -//import org.junit.jupiter.api.extension.ExtendWith; -//import org.mockito.InjectMocks; -//import org.mockito.Mock; -//import org.mockito.Mockito; -//import org.mockito.Spy; -//import org.mockito.junit.jupiter.MockitoExtension; -// -//@ExtendWith(MockitoExtension.class) -//class CommandMemberServiceTest { -// @Mock MemberRepository memberRepository; -// -// @Spy MemberEntityConverter entityConverter; -// -// @Spy CommandMemberResponseConverter responseConverter; -// -// @InjectMocks CommandMemberService commandMemberService; -// -// @Test -// @DisplayName("존재하지 않는 멤버일 경우 예외가 발생한다.") -// void not_found_member() { -// // given -// when(memberRepository.findById(Mockito.anyLong())).thenReturn(Optional.ofNullable(null)); -// ChangeActiveStatusRequest request = -// ChangeActiveStatusRequest.builder().activeStatus("am").build(); -// -// // when & then -// Assertions.assertThrows( -// NotFoundMemberException.class, () -> commandMemberService.adminChangeStatus(1L, request)); -// } -//} diff --git a/eeos/src/test/java/com/blackcompany/eeos/member/fixture/MemberFixture.java b/eeos/src/test/java/com/blackcompany/eeos/member/fixture/MemberFixture.java index 78ac9bbe..7b6676e2 100644 --- a/eeos/src/test/java/com/blackcompany/eeos/member/fixture/MemberFixture.java +++ b/eeos/src/test/java/com/blackcompany/eeos/member/fixture/MemberFixture.java @@ -12,6 +12,17 @@ public class MemberFixture { .name(String.valueOf(memberId)) .oauthServerType(OauthServerType.SLACK) .activeStatus(status) + .isAdmin(false) + .build(); + } + + public static MemberEntity 어드민_엔티티(Long memberId) { + return MemberEntity.builder() + .id(memberId) + .name(String.valueOf(memberId)) + .oauthServerType(OauthServerType.SLACK) + .activeStatus(ActiveStatus.AM) + .isAdmin(true) .build(); } @@ -21,6 +32,17 @@ public class MemberFixture { .name(String.valueOf(memberId)) .oauthServerType(OauthServerType.SLACK) .activeStatus(status) + .isAdmin(false) + .build(); + } + + public static MemberModel 어드민_모델(Long memberId) { + return MemberModel.builder() + .id(memberId) + .name(String.valueOf(memberId)) + .oauthServerType(OauthServerType.SLACK) + .activeStatus(ActiveStatus.AM) + .isAdmin(true) .build(); } } diff --git a/eeos/src/test/java/com/blackcompany/eeos/program/application/model/ProgramModelTest.java b/eeos/src/test/java/com/blackcompany/eeos/program/application/model/ProgramModelTest.java index 2bd3daa7..9f4a5ac6 100644 --- a/eeos/src/test/java/com/blackcompany/eeos/program/application/model/ProgramModelTest.java +++ b/eeos/src/test/java/com/blackcompany/eeos/program/application/model/ProgramModelTest.java @@ -2,12 +2,8 @@ import static org.junit.jupiter.api.Assertions.*; -import com.blackcompany.eeos.common.utils.DateConverter; -import com.blackcompany.eeos.program.application.exception.DeniedProgramEditException; -import com.blackcompany.eeos.program.application.exception.NotAllowedUpdatedProgramAttendException; -import com.blackcompany.eeos.program.application.exception.NotAllowedUpdatedProgramTypeException; -import com.blackcompany.eeos.program.application.exception.NotFoundProgramCategoryException; -import com.blackcompany.eeos.program.application.exception.OverDateException; +import com.blackcompany.eeos.program.application.exception.*; +import com.blackcompany.eeos.program.fixture.ProgramFixture; import com.blackcompany.eeos.program.persistence.ProgramCategory; import com.blackcompany.eeos.program.persistence.ProgramType; import java.time.LocalDate; @@ -20,10 +16,7 @@ class ProgramModelTest { @DisplayName("프로그램 날짜가 현 날짜의 이후라면 상태는 진행중(active)이다.") void calculateStatusAfterDay() { // given - LocalDate date = LocalDate.now().plusDays(1L); - - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now().plusDays(1L)); // when String programStatus = model.getProgramStatus(); @@ -36,26 +29,20 @@ void calculateStatusAfterDay() { @DisplayName("프로그램 날짜가 현 날짜와 같으면 상태는 진행중(active)이다.") void calculateStatusDay() { // given - LocalDate date = LocalDate.now(); - - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now()); // when - String prgramStatus = model.getProgramStatus(); + String programStatus = model.getProgramStatus(); // then - assertEquals(ProgramStatus.ACTIVE.getStatus(), prgramStatus); + assertEquals(ProgramStatus.ACTIVE.getStatus(), programStatus); } @Test @DisplayName("프로그램 날짜가 현 날짜의 이전이라면 상태는 완료(end)이다.") void calculateStatusBeforeDay() { // given - LocalDate date = LocalDate.now().minusDays(1L); - - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now().minusDays(1L)); // when String programStatus = model.getProgramStatus(); @@ -68,10 +55,7 @@ void calculateStatusBeforeDay() { @DisplayName("작성자 본인일 경우 프로그램 삭제가 가능하다.") void can_delete() { // given - LocalDate date = LocalDate.now().minusDays(1L); - - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).writer(1L).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now().minusDays(1L), 1L); // when & then assertDoesNotThrow(() -> model.validateDelete(1L)); @@ -81,10 +65,7 @@ void can_delete() { @DisplayName("작성자 본인이 아닐 경우 프로그램 삭제가 불가능하다.") void cannot_delete() { // given - LocalDate date = LocalDate.now().minusDays(1L); - - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).writer(2L).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now().minusDays(1L), 2L); // when & then assertThrows(DeniedProgramEditException.class, () -> model.validateDelete(1L)); @@ -94,10 +75,7 @@ void cannot_delete() { @DisplayName("완료(end)된 프로그램의 참석 대상자를 작성자는 수정할 수 있다.") void can_edit_when_end_program() { // given - LocalDate date = LocalDate.now().minusDays(1L); - - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).writer(2L).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now().minusDays(1L), 2L); // when & then assertDoesNotThrow(() -> model.validateEditAttend(2L)); @@ -107,10 +85,7 @@ void can_edit_when_end_program() { @DisplayName("진행중(active)인 프로그램의 참석 대상자는 수정할 수 없다") void cannot_edit_when_active_program() { // given - LocalDate date = LocalDate.now().plusDays(1L); - - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).writer(2L).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now().plusDays(1L), 2L); // when & then assertThrows(NotAllowedUpdatedProgramAttendException.class, () -> model.validateEditAttend(2L)); @@ -120,10 +95,7 @@ void cannot_edit_when_active_program() { @DisplayName("프로그램의 작성자가 아닐 시에 참석 대상자를 수정하지 못 한다.") void cannot_edit_when_not_writer() { // given - LocalDate date = LocalDate.now().plusDays(1L); - - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).writer(2L).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now().plusDays(1L), 2L); // when & then assertThrows(DeniedProgramEditException.class, () -> model.validateEditAttend(1L)); @@ -133,20 +105,10 @@ void cannot_edit_when_not_writer() { @DisplayName("프로그램 타입은 수정하지 못 한다.") void cannot_edit_program_type() { // given - LocalDate date = LocalDate.now().plusDays(1L); ProgramModel model = - ProgramModel.builder() - .programDate(DateConverter.toEpochSecond(date)) - .writer(2L) - .programType(ProgramType.NOTIFICATION) - .build(); - + ProgramFixture.프로그램_모델(LocalDate.now().plusDays(1L), ProgramType.NOTIFICATION, 2L); ProgramModel requestModel = - ProgramModel.builder() - .programDate(DateConverter.toEpochSecond(date)) - .writer(2L) - .programType(ProgramType.DEMAND) - .build(); + ProgramFixture.프로그램_모델(LocalDate.now().plusDays(1L), ProgramType.DEMAND, 2L); // when & then assertThrows(NotAllowedUpdatedProgramTypeException.class, () -> model.update(requestModel)); @@ -154,22 +116,14 @@ void cannot_edit_program_type() { @Test @DisplayName("all로 프로그램 카테고리를 수정하지 못 한다.") - // given void cannot_edit_program_category_all() { - LocalDate date = LocalDate.now().plusDays(1L); + // given ProgramModel model = - ProgramModel.builder() - .writer(1L) - .programType(ProgramType.DEMAND) - .programCategory(ProgramCategory.EVENT_TEAM) - .build(); - + ProgramFixture.프로그램_모델( + LocalDate.now().plusDays(1L), ProgramCategory.EVENT_TEAM, ProgramType.DEMAND, 1L); ProgramModel requestModel = - ProgramModel.builder() - .writer(1L) - .programType(ProgramType.DEMAND) - .programCategory(ProgramCategory.ALL) - .build(); + ProgramFixture.프로그램_모델( + LocalDate.now().plusDays(1L), ProgramCategory.ALL, ProgramType.DEMAND, 1L); // when & then assertThrows(NotFoundProgramCategoryException.class, () -> model.update(requestModel)); @@ -179,41 +133,23 @@ void cannot_edit_program_category_all() { @DisplayName("프로그램 작성자가 아니면 프로그램은 수정하지 못 한다.") void cannot_edit_program_when_not_writer() { // given - LocalDate date = LocalDate.now().plusDays(1L); - ProgramModel model = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).writer(2L).build(); - - ProgramModel requestModel = - ProgramModel.builder().programDate(DateConverter.toEpochSecond(date)).writer(1L).build(); + ProgramModel model = ProgramFixture.프로그램_모델(LocalDate.now().plusDays(1L), 2L); + ProgramModel requestModel = ProgramFixture.프로그램_모델(LocalDate.now().plusDays(1L), 1L); // when & then assertThrows(DeniedProgramEditException.class, () -> model.update(requestModel)); } @Test - @DisplayName("프로그램 작성자이며 프로그램 타입을 수정하지 않을 떄는 프로그램 수정이 가능하다.") - void cannot_edit_program() { + @DisplayName("프로그램 작성자이며 프로그램 타입을 수정하지 않을 때는 프로그램 수정이 가능하다.") + void can_edit_program() { // given - LocalDate date = LocalDate.now().plusDays(1L); ProgramModel model = - ProgramModel.builder() - .title("title") - .content("content") - .programDate(DateConverter.toEpochSecond(date)) - .programCategory(ProgramCategory.WEEKLY) - .programType(ProgramType.DEMAND) - .writer(2L) - .build(); - + ProgramFixture.프로그램_모델( + LocalDate.now().plusDays(1L), ProgramCategory.WEEKLY, ProgramType.DEMAND, 2L); ProgramModel requestModel = - ProgramModel.builder() - .title("title 변경") - .content("content 변경") - .programDate(DateConverter.toEpochSecond(date)) - .programCategory(ProgramCategory.EVENT_TEAM) - .programType(ProgramType.DEMAND) - .writer(2L) - .build(); + ProgramFixture.프로그램_모델( + LocalDate.now().plusDays(1L), ProgramCategory.EVENT_TEAM, ProgramType.DEMAND, 2L); // when ProgramModel update = model.update(requestModel); @@ -231,28 +167,12 @@ void cannot_edit_program() { @DisplayName("프로그램 수정은 수정 기준 이전 날짜도 가능하다.") void can_edit_program_before_date() { // given - LocalDate date = LocalDate.now().plusDays(1L); ProgramModel model = - ProgramModel.builder() - .title("title") - .content("content") - .programDate(DateConverter.toEpochSecond(date)) - .programCategory(ProgramCategory.WEEKLY) - .programType(ProgramType.DEMAND) - .writer(2L) - .build(); - - LocalDate beforeDate = LocalDate.now().minusDays(1L); - + ProgramFixture.프로그램_모델( + LocalDate.now().plusDays(1L), ProgramCategory.WEEKLY, ProgramType.DEMAND, 2L); ProgramModel requestModel = - ProgramModel.builder() - .title("title 변경") - .content("content 변경") - .programDate(DateConverter.toEpochSecond(beforeDate)) - .programCategory(ProgramCategory.EVENT_TEAM) - .programType(ProgramType.DEMAND) - .writer(2L) - .build(); + ProgramFixture.프로그램_모델( + LocalDate.now().minusDays(1L), ProgramCategory.EVENT_TEAM, ProgramType.DEMAND, 2L); // when ProgramModel update = model.update(requestModel); @@ -268,19 +188,11 @@ void can_edit_program_before_date() { @Test @DisplayName("프로그램 생성은 생성 기준 이전 날짜는 불가능하다.") - void can_create_program_before_date() { + void cannot_create_program_before_date() { // given - LocalDate beforeDate = LocalDate.now().minusDays(1L); - ProgramModel model = - ProgramModel.builder() - .title("title 변경") - .content("content 변경") - .programDate(DateConverter.toEpochSecond(beforeDate)) - .programCategory(ProgramCategory.EVENT_TEAM) - .programType(ProgramType.DEMAND) - .writer(2L) - .build(); + ProgramFixture.프로그램_모델( + LocalDate.now().minusDays(1L), ProgramCategory.EVENT_TEAM, ProgramType.DEMAND, 2L); // when & then assertThrows(OverDateException.class, model::validateCreate); @@ -290,17 +202,9 @@ void can_create_program_before_date() { @DisplayName("프로그램 생성은 생성 기준 이후 날짜는 가능하다.") void can_create_program_over_date() { // given - LocalDate beforeDate = LocalDate.now().plusDays(1L); - ProgramModel model = - ProgramModel.builder() - .title("title 변경") - .content("content 변경") - .programDate(DateConverter.toEpochSecond(beforeDate)) - .programCategory(ProgramCategory.EVENT_TEAM) - .programType(ProgramType.DEMAND) - .writer(2L) - .build(); + ProgramFixture.프로그램_모델( + LocalDate.now().plusDays(1L), ProgramCategory.EVENT_TEAM, ProgramType.DEMAND, 2L); // when & then assertDoesNotThrow(model::validateCreate); @@ -310,17 +214,8 @@ void can_create_program_over_date() { @DisplayName("프로그램 생성은 생성 기준 당일 날짜는 가능하다.") void can_create_program_date() { // given - LocalDate beforeDate = LocalDate.now(); - ProgramModel model = - ProgramModel.builder() - .title("title 변경") - .content("content 변경") - .programDate(DateConverter.toEpochSecond(beforeDate)) - .programCategory(ProgramCategory.EVENT_TEAM) - .programType(ProgramType.DEMAND) - .writer(2L) - .build(); + ProgramFixture.프로그램_모델(LocalDate.now(), ProgramCategory.ETC, ProgramType.DEMAND, 1L); // when & then assertDoesNotThrow(model::validateCreate); diff --git a/eeos/src/test/java/com/blackcompany/eeos/program/fixture/ProgramFixture.java b/eeos/src/test/java/com/blackcompany/eeos/program/fixture/ProgramFixture.java new file mode 100644 index 00000000..5f100d76 --- /dev/null +++ b/eeos/src/test/java/com/blackcompany/eeos/program/fixture/ProgramFixture.java @@ -0,0 +1,36 @@ +package com.blackcompany.eeos.program.fixture; + +import com.blackcompany.eeos.common.utils.DateConverter; +import com.blackcompany.eeos.program.application.model.ProgramModel; +import com.blackcompany.eeos.program.persistence.ProgramCategory; +import com.blackcompany.eeos.program.persistence.ProgramType; +import java.time.LocalDate; + +public class ProgramFixture { + private static final String GITHUB_URL = "https://github.com/JNU-econovation/"; + + public static ProgramModel 프로그램_모델(LocalDate date) { + return 프로그램_모델(date, ProgramCategory.WEEKLY, ProgramType.DEMAND, 1L); + } + + public static ProgramModel 프로그램_모델(LocalDate date, Long writerId) { + return 프로그램_모델(date, ProgramCategory.WEEKLY, ProgramType.DEMAND, writerId); + } + + public static ProgramModel 프로그램_모델(LocalDate date, ProgramType programType, Long writerId) { + return 프로그램_모델(date, ProgramCategory.WEEKLY, programType, writerId); + } + + public static ProgramModel 프로그램_모델( + LocalDate date, ProgramCategory category, ProgramType type, Long writerId) { + return ProgramModel.builder() + .title(writerId.toString()) + .content(writerId.toString()) + .programDate(DateConverter.toEpochSecond(date)) + .programCategory(category) + .programType(type) + .writer(writerId) + .githubUrl(GITHUB_URL + writerId) + .build(); + } +} diff --git a/eeos/src/test/java/com/blackcompany/eeos/target/application/model/AttendModelTest.java b/eeos/src/test/java/com/blackcompany/eeos/target/application/model/AttendModelTest.java index beeb8d12..812e7441 100644 --- a/eeos/src/test/java/com/blackcompany/eeos/target/application/model/AttendModelTest.java +++ b/eeos/src/test/java/com/blackcompany/eeos/target/application/model/AttendModelTest.java @@ -1,46 +1,33 @@ -//package com.blackcompany.eeos.target.application.model; -// -//import static org.junit.jupiter.api.Assertions.*; -// -//import com.blackcompany.eeos.target.application.exception.DeniedSaveAttendException; -//import com.blackcompany.eeos.target.application.exception.NotSameBeforeAttendStatusException; -//import org.junit.jupiter.api.Assertions; -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; -// -//class AttendModelTest { -// -// @Test -// @DisplayName("이전 참석 정보가 변경전 참석정보와 일치하지 않으면 예외가 발생한다.") -// void fail_change_status() { -// // given -// AttendModel model = AttendModel.builder().status(AttendStatus.NONRESPONSE).build(); -// -// // when & then -// Assertions.assertThrows( -// NotSameBeforeAttendStatusException.class, () -> model.changeStatusByManager("late", "attend")); -// } -// -// @Test -// @DisplayName("이전 참석 정보가 변경전 참석정보와 일치하면 참석 상태를 변경한다.") -// void success_change_status() { -// // given -// AttendModel model = AttendModel.builder().status(AttendStatus.NONRESPONSE).build(); -// -// // when -// AttendModel attendModel = model.changeStatus("nonResponse", "attend"); -// -// // then -// assertEquals(AttendStatus.ATTEND.getStatus(), attendModel.getStatus()); -// } -// -// @Test -// @DisplayName("이전 참석 정보가 참석 대상자가 아니라면 참석 상태를 변경하지 못 한다.") -// void fail_change_status_when_noRelated() { -// // given -// AttendModel model = AttendModel.builder().status(AttendStatus.NONRELATED).build(); -// -// // when & then -// assertThrows(DeniedSaveAttendException.class, () -> model.changeStatus("nonRelated", "attend")); -// } -//} +package com.blackcompany.eeos.target.application.model; + +import static org.junit.jupiter.api.Assertions.*; + +import com.blackcompany.eeos.target.application.exception.DeniedSaveAttendException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class AttendModelTest { + + @Test + @DisplayName("이전 참석 정보가 변경전 참석정보와 일치하면 참석 상태를 변경한다.") + void success_change_status() { + // given + AttendModel model = AttendModel.builder().status(AttendStatus.NONRESPONSE).build(); + + // when + AttendModel attendModel = model.changeStatus("attend"); + + // then + assertEquals(AttendStatus.ATTEND.getStatus(), attendModel.getStatus()); + } + + @Test + @DisplayName("이전 참석 정보가 참석 대상자가 아니라면 참석 상태를 변경하지 못 한다.") + void fail_change_status_when_noRelated() { + // given + AttendModel model = AttendModel.builder().status(AttendStatus.NONRELATED).build(); + + // when & then + assertThrows(DeniedSaveAttendException.class, () -> model.changeStatus("attend")); + } +}