Skip to content

Commit

Permalink
test: StyleService 테스트 작성
Browse files Browse the repository at this point in the history
- StyleService 테스트 작성
- StyleFakeRepository 작성
- StyleService 메서드 이름 수정
  • Loading branch information
devholic22 committed Feb 23, 2024
1 parent 18db8df commit e58e6b2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class StyleService {

@Transactional
public void saveMemberStyles(final Long memberId, final List<String> styleNames) {
deleteBeforeMemberStyles(memberId);
deleteMemberStyles(memberId);
List<Style> memberStyles = StyleName.findAllByNames(styleNames)
.stream()
.map(styleName -> new Style(memberId, styleName))
Expand All @@ -27,7 +27,7 @@ public void saveMemberStyles(final Long memberId, final List<String> styleNames)
}

@Transactional
public void deleteBeforeMemberStyles(final Long memberId) {
public void deleteMemberStyles(final Long memberId) {
styleRepository.deleteStylesByMemberId(memberId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.atwoz.member.application.info;

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

import com.atwoz.member.application.info.style.StyleService;
import com.atwoz.member.domain.info.style.Style;
import com.atwoz.member.domain.info.style.StyleRepository;
import com.atwoz.member.infrastructure.info.StyleFakeRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
@ExtendWith(MockitoExtension.class)
public class StyleServiceTest {

private StyleService styleService;
private StyleRepository styleRepository;

@BeforeEach
void init() {
styleRepository = new StyleFakeRepository();
styleService = new StyleService(styleRepository);
}

@Test
void 회원의_모든_스타일을_저장한다() {
// given
Long memberId = 1L;
List<String> styleNames = List.of("긍정적", "유머 감각");

// when
styleService.saveMemberStyles(memberId, styleNames);

// then
List<Style> saveStyles = styleRepository.findAllByMemberId(memberId);
assertThat(saveStyles.size()).isEqualTo(2);
}

@Test
void 회원의_모든_스타일을_삭제한다() {
// given
Long memberId = 1L;
List<String> styleNames = List.of("긍정적", "유머 감각");
styleService.saveMemberStyles(memberId, styleNames);

// when
styleService.deleteMemberStyles(memberId);

// then
assertThat(styleRepository.findAllByMemberId(memberId)).isEmpty();
}

@Test
void 회원의_모든_스타일을_조회한다() {
// given
Long memberId = 1L;
List<String> styleNames = List.of("긍정적", "유머 감각");
styleService.saveMemberStyles(memberId, styleNames);

// when
List<Style> memberStyles = styleService.findMemberStyles(memberId);

// then
assertThat(memberStyles.size()).isEqualTo(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.atwoz.member.infrastructure.info;

import com.atwoz.member.domain.info.style.Style;
import com.atwoz.member.domain.info.style.StyleRepository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StyleFakeRepository implements StyleRepository {

private final Map<Long, Style> map = new HashMap<>();
private long id = 1L;

@Override
public void save(final Style style) {
Style newStyle = new Style(style.getMemberId(), style.getStyleName());
map.put(id, newStyle);
id++;
}

@Override
public void saveAll(final List<Style> styles) {
styles.stream()
.map(style -> new Style(style.getMemberId(), style.getStyleName()))
.forEach(style -> map.put(id++, style));
}

@Override
public void deleteStylesByMemberId(final Long memberId) {
map.values().removeIf(style -> memberId.equals(style.getMemberId()));
}

@Override
public List<Style> findAllByMemberId(final Long memberId) {
return map.values()
.stream()
.filter(style -> memberId.equals(style.getMemberId()))
.toList();
}
}

0 comments on commit e58e6b2

Please sign in to comment.