Open snaag opened 2 years ago
public class MemberService {
private final MemoryArticleRepository memoryArticleRepository;
public MemberService(MemoryArticleRepository memoryArticleRepository) {
this.memoryArticleRepository = memoryArticleRepository;
}
// 글 쓰기
public Long createArticle(Article article) {
Long id = memoryArticleRepository.create(article);
return id;
}
// 글 모두 불러오기
public Map<Long, Article> loadAllArticles() {
return memoryArticleRepository.loadAll();
}
// 글 하나만 불러오기
public Optional<Article> loadOneArticle(Long id) {
Article article = memoryArticleRepository.loadOne(id).get();
return Optional.ofNullable(article);
}
// 글 업데이트
public Long updateOneArticle(Long updateId, Article article) {
Long id = memoryArticleRepository.updateOne(updateId, article);
return id;
}
// 글 삭제
public void deleteOneArticle(Long id) {
memoryArticleRepository.deleteOne(id);
}
// 글 모두 삭제
public void deleteAllArticles() {
memoryArticleRepository.deleteAll();
}
}
class MemberServiceTest {
public MemoryArticleRepository memoryArticleRepository = new MemoryArticleRepository();
public MemberService memberService = new MemberService(memoryArticleRepository);
@AfterEach
public void afterEach() {
memberService.deleteAllArticles();
}
@Test
public void 글쓰기() {
// given 이러한 상황이 주어져서
Article article = new Article();
article.setName("sang-a");
article.setTitle("spring");
article.setContext("board demo");
memberService.createArticle(article);
// when 이걸 실행했을때
Integer size = memberService.loadAllArticles().size();
// then 이런 결과가 나와야함
assertThat(size).isEqualTo(1);
}
@Test
public void 글_모두_불러오기() {
// given 이러한 상황이 주어져서
Article article1 = new Article();
article1.setName("sang-a");
article1.setTitle("spring");
article1.setContext("board demo");
Article article2 = new Article();
article2.setName("sang-a (2)");
article2.setTitle("spring (2)");
article2.setContext("board demo (2)");
Article article3 = new Article();
article3.setName("sang-a (3)");
article3.setTitle("spring (3)");
article3.setContext("board demo (3)");
memberService.createArticle(article1);
memberService.createArticle(article2);
memberService.createArticle(article3);
// when 이걸 실행했을때
Integer size = memberService.loadAllArticles().size();
// then 이런 결과가 나와야함
assertThat(size).isEqualTo(3);
}
@Test
public void 글_1개_불러오기() {
// given 이러한 상황이 주어져서
Article article1 = new Article();
article1.setName("sang-a");
article1.setTitle("spring");
article1.setContext("board demo");
Article article2 = new Article();
article2.setName("sang-a (2)");
article2.setTitle("spring (2)");
article2.setContext("board demo (2)");
Article article3 = new Article();
article3.setName("sang-a (3)");
article3.setTitle("spring (3)");
article3.setContext("board demo (3)");
Long id1 = memberService.createArticle(article1);
memberService.createArticle(article2);
memberService.createArticle(article3);
// when 이걸 실행했을때
Article loadArticle = memberService.loadOneArticle(id1).get();
// then 이런 결과가 나와야함
assertThat(loadArticle).isEqualTo(article1);
}
@Test
public void 글_수정() {
// given 이러한 상황이 주어져서
Article article1 = new Article();
article1.setName("sang-a");
article1.setTitle("spring");
article1.setContext("board demo");
Article article2 = new Article();
article2.setName("sang-a (2)");
article2.setTitle("spring (2)");
article2.setContext("board demo (2)");
Long id1 = memberService.createArticle(article1);
memberService.createArticle(article2);
// when 이걸 실행했을때
Article newArticle1 = new Article();
newArticle1.setName("sang-a (updated)");
newArticle1.setTitle("spring (updated)");
newArticle1.setContext("board demo (updated)");
Long updateArticleId = memberService.updateOneArticle(id1, newArticle1);
// then 이런 결과가 나와야함
assertThat(updateArticleId).isEqualTo(id1);
assertThat(memberService.loadOneArticle(id1).get().getName())
.isEqualTo("sang-a (updated)");
}
@Test
public void 글_모두_삭제() {
// given 이러한 상황이 주어져서
Article article1 = new Article();
article1.setName("sang-a");
article1.setTitle("spring");
article1.setContext("board demo");
Article article2 = new Article();
article2.setName("sang-a (2)");
article2.setTitle("spring (2)");
article2.setContext("board demo (2)");
Article article3 = new Article();
article3.setName("sang-a (3)");
article3.setTitle("spring (3)");
article3.setContext("board demo (3)");
memberService.createArticle(article1);
memberService.createArticle(article2);
memberService.createArticle(article3);
// when 이걸 실행했을때
memberService.deleteAllArticles();
// then 이런 결과가 나와야함
assertThat(memberService.loadAllArticles().size())
.isEqualTo(0);
}
@Test
public void 글_1개_삭제() {
// given 이러한 상황이 주어져서
Article article1 = new Article();
article1.setName("sang-a");
article1.setTitle("spring");
article1.setContext("board demo");
Article article2 = new Article();
article2.setName("sang-a (2)");
article2.setTitle("spring (2)");
article2.setContext("board demo (2)");
Article article3 = new Article();
article3.setName("sang-a (3)");
article3.setTitle("spring (3)");
article3.setContext("board demo (3)");
Long id1 = memberService.createArticle(article1);
memberService.createArticle(article2);
memberService.createArticle(article3);
// when 이걸 실행했을때
memberService.deleteOneArticle(id1); // nullable...
// then 이런 결과가 나와야함
assertThat(memberService.loadAllArticles().size())
.isEqualTo(2);
}
}
https://mangkyu.tistory.com/70
Optional.empty()
Optional.of(null이 아닌 값)
Optional.ofNullable(값)
/**
* getRandomBoolean 를 사용해서 테스트
*/
@Test
public void 옵셔널1() {
for (int i=0; i<10; i++) {
Optional<Boolean> optional = getRandomBoolean();
Boolean value = optional.orElse(false);
System.out.println("optional.isPresent() = " + optional.isPresent()); // Boolean, true or false
// Boolean,
// true (진짜 true 로 나온 경우)
// or false (null 이었는데 orElse(false) 로 false 가 들어간 경우)
System.out.println("value = " + value);
}
}
/**
* getRandomInteger 를 사용해서 테스트
*/
@Test
public void 옵셔널2() {
for (int i=0; i<10; i++) {
Optional<Integer> optional = getRandomInteger();
Integer value = optional.orElse(-1);
System.out.println("optional.isPresent() = " + optional.isPresent()); // Boolean, true or false
System.out.println("value = " + value); // Integer, -1 or 다른 숫자들
}
}
/**
* 10보다 적은 숫자를 랜덤하게 고른 후,
* 5보다 적으면 true 를,
* 5와 같거나 크면 Optional.empty() == null 을 반환
* @return Optional.of(true) or Optional.empty()
*/
public Optional<Boolean> getRandomBoolean() {
double dValue = Math.random();
int iValue = (int)(dValue * 10);
if(iValue < 5) return Optional.of(true);
return Optional.empty();
}
/**
* 10보다 적은 숫자를 랜덤하게 고른 후,
* 5보다 적으면 숫자를,
* 5와 같거나 크면 Optional.empty() == null 을 반환
* @return Optional.of(Integer) or Optional.empty()
*/
public Optional<Integer> getRandomInteger() {
double dValue = Math.random();
int iValue = (int)(dValue * 10);
if(iValue < 5) return Optional.of(iValue);
return Optional.empty();
}