|
| 1 | +package com.clubber.ClubberServer.integration.domain.calendar.service; |
| 2 | + |
| 3 | +import com.clubber.ClubberServer.domain.admin.domain.Admin; |
| 4 | +import com.clubber.ClubberServer.domain.admin.repository.AdminRepository; |
| 5 | +import com.clubber.ClubberServer.domain.calendar.dto.GetCalendarDuplicateRequest; |
| 6 | +import com.clubber.ClubberServer.domain.calendar.dto.GetCalendarDuplicateResponse; |
| 7 | +import com.clubber.ClubberServer.domain.calendar.entity.Calendar; |
| 8 | +import com.clubber.ClubberServer.domain.calendar.repository.CalendarRepository; |
| 9 | +import com.clubber.ClubberServer.domain.calendar.service.CalendarAdminService; |
| 10 | +import com.clubber.ClubberServer.domain.club.domain.Club; |
| 11 | +import com.clubber.ClubberServer.domain.club.repository.ClubRepository; |
| 12 | +import com.clubber.ClubberServer.domain.recruit.domain.RecruitType; |
| 13 | +import com.clubber.ClubberServer.global.config.security.AuthDetails; |
| 14 | +import com.clubber.ClubberServer.integration.util.fixture.AdminFixture; |
| 15 | +import com.clubber.ClubberServer.integration.util.fixture.CalendarFixture; |
| 16 | +import com.clubber.ClubberServer.integration.util.fixture.ClubFixture; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.boot.test.context.SpringBootTest; |
| 20 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 21 | +import org.springframework.security.core.context.SecurityContext; |
| 22 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 23 | +import org.springframework.test.context.ActiveProfiles; |
| 24 | +import org.springframework.transaction.annotation.Transactional; |
| 25 | + |
| 26 | +import java.time.LocalDateTime; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.*; |
| 29 | + |
| 30 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 31 | +@Transactional |
| 32 | +@ActiveProfiles("test") |
| 33 | +public class CalendarAdminServiceTest { |
| 34 | + @Autowired |
| 35 | + private CalendarAdminService calendarAdminService; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private CalendarRepository calendarRepository; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private ClubRepository clubRepository; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private AdminRepository adminRepository; |
| 45 | + |
| 46 | + private void createSecurityContext(Admin admin) { |
| 47 | + SecurityContext context = SecurityContextHolder.createEmptyContext(); |
| 48 | + |
| 49 | + AuthDetails adminDetails = new AuthDetails(admin.getId().toString(), "ADMIN"); |
| 50 | + UsernamePasswordAuthenticationToken adminToken = new UsernamePasswordAuthenticationToken( |
| 51 | + adminDetails, "user", adminDetails.getAuthorities()); |
| 52 | + context.setAuthentication(adminToken); |
| 53 | + SecurityContextHolder.setContext(context); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void 정규모집_이번달_캘린더_시작시_중복있음() { |
| 58 | + //given |
| 59 | + Club club = ClubFixture.aClub().build(); |
| 60 | + Club savedClub = clubRepository.save(club); |
| 61 | + |
| 62 | + Admin admin = AdminFixture.aAdmin().club(savedClub).build(); |
| 63 | + Admin savedAdmin = adminRepository.save(admin); |
| 64 | + createSecurityContext(savedAdmin); |
| 65 | + |
| 66 | + LocalDateTime startAt = LocalDateTime.of(2025, 1, 1, 0, 0); |
| 67 | + LocalDateTime endAt = LocalDateTime.of(2025, 1, 4, 23, 59); |
| 68 | + RecruitType recruitType = RecruitType.REGULAR; |
| 69 | + |
| 70 | + Calendar calendar = CalendarFixture.aCalendar() |
| 71 | + .club(savedClub) |
| 72 | + .recruitType(recruitType) |
| 73 | + .startAt(startAt) |
| 74 | + .endAt(endAt) |
| 75 | + .build(); |
| 76 | + |
| 77 | + calendarRepository.save(calendar); |
| 78 | + |
| 79 | + //when |
| 80 | + GetCalendarDuplicateRequest request = new GetCalendarDuplicateRequest(recruitType, startAt); |
| 81 | + |
| 82 | + //then |
| 83 | + GetCalendarDuplicateResponse response = calendarAdminService.checkDuplicateCalendar(request); |
| 84 | + assertThat(response).isNotNull(); |
| 85 | + assertThat(response.isExist()).isTrue(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void 정규모집_다음달_시작_캘린더_저장시_중복_없음() { |
| 90 | + //given |
| 91 | + Club club = ClubFixture.aClub().build(); |
| 92 | + Club savedClub = clubRepository.save(club); |
| 93 | + |
| 94 | + Admin admin = AdminFixture.aAdmin().club(savedClub).build(); |
| 95 | + Admin savedAdmin = adminRepository.save(admin); |
| 96 | + createSecurityContext(savedAdmin); |
| 97 | + |
| 98 | + LocalDateTime startAt = LocalDateTime.of(2025, 1, 1, 0, 0); |
| 99 | + LocalDateTime endAt = LocalDateTime.of(2025, 1, 4, 23, 59); |
| 100 | + RecruitType recruitType = RecruitType.REGULAR; |
| 101 | + |
| 102 | + Calendar calendar = CalendarFixture.aCalendar() |
| 103 | + .club(savedClub) |
| 104 | + .recruitType(recruitType) |
| 105 | + .startAt(startAt) |
| 106 | + .endAt(endAt) |
| 107 | + .build(); |
| 108 | + |
| 109 | + calendarRepository.save(calendar); |
| 110 | + |
| 111 | + //when |
| 112 | + LocalDateTime nextMonthStartAt = LocalDateTime.of(2025, 2, 1, 0, 0); |
| 113 | + GetCalendarDuplicateRequest request = new GetCalendarDuplicateRequest(recruitType, nextMonthStartAt); |
| 114 | + |
| 115 | + //then |
| 116 | + GetCalendarDuplicateResponse response = calendarAdminService.checkDuplicateCalendar(request); |
| 117 | + assertThat(response).isNotNull(); |
| 118 | + assertThat(response.isExist()).isFalse(); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void 상시모집_캘린더_중복() { |
| 123 | + //given |
| 124 | + Club club = ClubFixture.aClub().build(); |
| 125 | + Club savedClub = clubRepository.save(club); |
| 126 | + |
| 127 | + Admin admin = AdminFixture.aAdmin().club(savedClub).build(); |
| 128 | + Admin savedAdmin = adminRepository.save(admin); |
| 129 | + createSecurityContext(savedAdmin); |
| 130 | + |
| 131 | + RecruitType recruitType = RecruitType.ALWAYS; |
| 132 | + |
| 133 | + Calendar calendar = CalendarFixture.aCalendar() |
| 134 | + .club(savedClub) |
| 135 | + .recruitType(recruitType) |
| 136 | + .startAt(null) |
| 137 | + .endAt(null) |
| 138 | + .build(); |
| 139 | + |
| 140 | + calendarRepository.save(calendar); |
| 141 | + |
| 142 | + //when |
| 143 | + GetCalendarDuplicateRequest request = new GetCalendarDuplicateRequest(recruitType, null); |
| 144 | + |
| 145 | + //then |
| 146 | + GetCalendarDuplicateResponse response = calendarAdminService.checkDuplicateCalendar(request); |
| 147 | + assertThat(response).isNotNull(); |
| 148 | + assertThat(response.isExist()).isTrue(); |
| 149 | + } |
| 150 | +} |
0 commit comments