-
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.
- 전략 + 플라이웨이트 패턴 적용 테스트 포함 Resolves: #38
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
application/wypl-core/src/main/java/com/wypl/wyplcore/calendar/service/CalendarService.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,48 @@ | ||
package com.wypl.wyplcore.calendar.service; | ||
|
||
import com.wypl.jpacalendardomain.calendar.domain.Calendar; | ||
import com.wypl.jpacalendardomain.calendar.domain.MemberCalendar; | ||
import com.wypl.jpacalendardomain.calendar.mapper.ScheduleMapper; | ||
import com.wypl.jpacalendardomain.calendar.repository.ScheduleInfoRepository; | ||
import com.wypl.jpacalendardomain.calendar.repository.ScheduleRepository; | ||
import com.wypl.jpamemberdomain.member.Member; | ||
import com.wypl.wyplcore.auth.domain.AuthMember; | ||
import com.wypl.wyplcore.calendar.data.response.CalendarFindResponse; | ||
import com.wypl.wyplcore.calendar.data.response.FindCalendarResponse; | ||
import com.wypl.wyplcore.calendar.service.strategy.CalendarStrategy; | ||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||
import com.wypl.wyplcore.calendar.data.request.CalendarFindRequest; | ||
import com.wypl.wyplcore.schedule.data.response.ScheduleFindResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CalendarService { | ||
|
||
private final ScheduleRepository scheduleRepository; | ||
private final ScheduleInfoRepository scheduleInfoRepository; | ||
private final Map<CalendarType, CalendarStrategy> calendarStrategyMap; | ||
|
||
@Transactional | ||
public FindCalendarResponse findCalendar(AuthMember authMember, long calendarId, CalendarFindRequest calendarFindRequest) { | ||
|
||
Calendar foundCalendar = null; // FIXME: calendarId로 foundCalendar 엔티티 검증 필요. | ||
MemberCalendar foundMemberCalendar = null; // Fixme: memberCalendar 엔티티 검증 필요. | ||
Member foundMember = null; // FIXME: member 엔티티 검증 필요. | ||
|
||
CalendarType calendarType = calendarFindRequest.calendarType(); | ||
LocalDate startDate = calendarFindRequest.startDate(); | ||
|
||
List<ScheduleFindResponse> foundScheduleFindResponses = calendarStrategyMap.get(calendarType).getAllSchedule(foundCalendar.getId(), startDate); | ||
|
||
|
||
CalendarFindResponse calendarFindResponse = new CalendarFindResponse(foundCalendar.getId(), foundMemberCalendar.getColor(), foundCalendar.getName()); | ||
return new FindCalendarResponse(calendarFindResponse, foundScheduleFindResponses); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...src/test/java/com/wypl/wyplcore/calendar/service/strategy/CalendarStrategyConfigTest.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,45 @@ | ||
package com.wypl.wyplcore.calendar.service.strategy; | ||
|
||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContext; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@SpringBootTest | ||
class CalendarStrategyConfigTest { | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Test | ||
void testRegisterBean(){ | ||
assertNotNull(applicationContext.getBean(CalendarStrategyConfig.class)); | ||
assertTrue(applicationContext.containsBean("calendarStrategyMap")); | ||
|
||
Map<?, ?> instance = (Map<?, ?>) applicationContext.getBean("calendarStrategyMap"); | ||
for (Map.Entry<?, ?> entry : instance.entrySet()) { | ||
assertThat(entry.getKey()).isInstanceOf(CalendarType.class); | ||
assertThat(entry.getValue()).isInstanceOf(CalendarStrategy.class); | ||
System.out.println(entry.getKey() + " : " + entry.getValue()); | ||
} | ||
} | ||
|
||
@Test | ||
void testStrategyFlyweight() { | ||
|
||
// Flyweight(CalendarStrategy)의 공유 역할을 하는 calendarStrategyMap이 싱글톤으로 관리되는 지 확인 | ||
Map<?, ?> instance = (Map<?, ?>) applicationContext.getBean("calendarStrategyMap"); | ||
Map<?, ?> instance2 = (Map<?, ?>) applicationContext.getBean("calendarStrategyMap"); | ||
assertEquals(instance, instance2); | ||
|
||
// 여러 번 호출 시 같은 전략 인스턴스가 호출되는 지 확인 | ||
assertEquals(instance.get("DAY"), instance2.get("DAY")); | ||
} | ||
} | ||
|