Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/#127 about domain, service testcode 구현 #128

Merged
merged 7 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kgu.developers.api.about.application;


import static kgu.developers.domain.about.domain.MainCategory.DEPT_INTRO;
import static kgu.developers.domain.about.domain.MainCategory.EDU_ACTIVITIES;
import static kgu.developers.domain.about.domain.SubCategory.CLUB_INTRO;
Expand All @@ -10,22 +9,24 @@
import static kgu.developers.domain.about.domain.SubCategory.HISTORY;
import static kgu.developers.domain.about.domain.SubCategory.LEARNING_ACTIVITIES;

import java.util.EnumMap;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import kgu.developers.api.about.presentation.Exception.AboutNotFoundException;
import kgu.developers.api.about.presentation.Exception.CategoryNotMatchException;
import kgu.developers.api.about.presentation.request.AboutRequest;
import kgu.developers.api.about.presentation.request.AboutUpdateRequest;
import kgu.developers.api.about.presentation.response.AboutPersistResponse;
import kgu.developers.api.about.presentation.response.AboutResponse;
import kgu.developers.domain.about.domain.About;
import kgu.developers.domain.about.domain.AboutRepository;
import kgu.developers.domain.about.domain.MainCategory;
import kgu.developers.domain.about.domain.SubCategory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.EnumMap;
import java.util.Map;
import java.util.Set;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -67,17 +68,16 @@ public AboutResponse getAbout(MainCategory main, SubCategory sub, String detail)
}

@Transactional
public void updateAbout(Long id, AboutRequest request) {
public void updateAbout(Long id, AboutUpdateRequest request) {
About about = aboutRepository.findById(id)
.orElseThrow(AboutNotFoundException::new);

about.updateContent(request.content());
}

private void categoryMatchCheck(MainCategory mainCategory, SubCategory subCategory) {
if (CATEGORY_MAP.getOrDefault(mainCategory, Set.of()).contains(subCategory))
return;

throw new CategoryNotMatchException();
if (!CATEGORY_MAP.getOrDefault(mainCategory, Set.of()).contains(subCategory)) {
throw new CategoryNotMatchException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.NO_CONTENT;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -11,20 +21,12 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import kgu.developers.api.about.application.AboutService;
import kgu.developers.api.about.presentation.request.AboutRequest;
import kgu.developers.api.about.presentation.request.AboutUpdateRequest;
import kgu.developers.api.about.presentation.response.AboutPersistResponse;
import kgu.developers.api.about.presentation.response.AboutResponse;
import kgu.developers.domain.about.domain.MainCategory;
import kgu.developers.domain.about.domain.SubCategory;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -69,7 +71,7 @@ public ResponseEntity<AboutResponse> getAbout(
@PatchMapping("/{id}")
public ResponseEntity<Void> updateAbout(
@PathVariable Long id,
@RequestBody AboutRequest request
@RequestBody AboutUpdateRequest request
) {
aboutService.updateAbout(id, request);
return ResponseEntity.status(NO_CONTENT).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import jakarta.validation.constraints.NotNull;
import kgu.developers.domain.about.domain.MainCategory;
import kgu.developers.domain.about.domain.SubCategory;
import lombok.Builder;

@Builder
public record AboutRequest(
@Schema(description = "메인 카테고리", example = "EDU_ACTIVITIES", requiredMode = REQUIRED)
@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kgu.developers.api.about.presentation.request;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;

@Builder
public record AboutUpdateRequest(
@Schema(description = "페이지 내용(JSON 형식)", example = "{key:value}", requiredMode = REQUIRED)
@NotNull
String content
) {
}
175 changes: 175 additions & 0 deletions aics-api/src/testFixtures/java/about/application/AboutServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package about.application;

import static kgu.developers.domain.about.domain.MainCategory.DEPT_INTRO;
import static kgu.developers.domain.about.domain.MainCategory.EDU_ACTIVITIES;
import static kgu.developers.domain.about.domain.SubCategory.CURRICULUM;
import static kgu.developers.domain.about.domain.SubCategory.HISTORY;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import kgu.developers.api.about.application.AboutService;
import kgu.developers.api.about.presentation.Exception.AboutNotFoundException;
import kgu.developers.api.about.presentation.Exception.CategoryNotMatchException;
import kgu.developers.api.about.presentation.request.AboutRequest;
import kgu.developers.api.about.presentation.request.AboutUpdateRequest;
import kgu.developers.api.about.presentation.response.AboutPersistResponse;
import kgu.developers.api.about.presentation.response.AboutResponse;
import kgu.developers.domain.about.domain.About;
import kgu.developers.domain.about.domain.MainCategory;
import kgu.developers.domain.about.domain.SubCategory;
import mock.FakeAboutRepository;

public class AboutServiceTest {
private AboutService aboutService;

@BeforeEach
public void init() {
FakeAboutRepository fakeAboutRepository = new FakeAboutRepository();
this.aboutService = new AboutService(fakeAboutRepository);

fakeAboutRepository.save(About.builder()
.mainCategory(EDU_ACTIVITIES)
.subCategory(CURRICULUM)
.detailCategory("initDetail")
.content("initContent")
.build());
}

@Test
@DisplayName("createAbout은 about을 생성할 수 있다.")
public void createAbout_Success() {
// given
MainCategory main = DEPT_INTRO;
SubCategory sub = HISTORY;
String detail = "detail";
String content = "content";

AboutRequest request = AboutRequest.builder()
.main(main)
.sub(sub)
.detail(detail)
.content(content)
.build();

// when
AboutPersistResponse response = aboutService.createAbout(request);

// then
AboutResponse aboutResponse = aboutService.getAbout(main, sub, detail);

assertEquals(response.id(), 2L);
assertEquals(aboutResponse.content(), content);
}

@Test
@DisplayName("createAbout은 메인 카테고리와 서브 카테고리의 관계가 올바르지 않은 생성 요청 시 CategoryNotMatchException을 발생 한다.")
public void createAbout_CategoryNotMatch_ThrowsException() {
// given
MainCategory main = DEPT_INTRO;
SubCategory sub = CURRICULUM;
String detail = "detail";
String content = "content";

AboutRequest request = AboutRequest.builder()
.main(main)
.sub(sub)
.detail(detail)
.content(content)
.build();

// when
// then
assertThatThrownBy(() -> {
aboutService.createAbout(request);
}).isInstanceOf(CategoryNotMatchException.class);
}

@Test
@DisplayName("getAbout은 About을 조회할 수 있다.")
public void getAbout_Success() {
// given
MainCategory main = EDU_ACTIVITIES;
SubCategory sub = CURRICULUM;
String detail = "initDetail";

// when
AboutResponse aboutResponse = aboutService.getAbout(main, sub, detail);

// then
assertEquals(aboutResponse.content(), "initContent");
}

@Test
@DisplayName("getAbout은 메인 카테고리와 서브 카테고리의 관계가 올바르지 않을 시 CategoryNotMatchException을 발생 한다.")
public void getAbout_CategoryNotMatch_ThrowsException() {
// given
MainCategory main = DEPT_INTRO;
SubCategory sub = CURRICULUM;
String detail = "failDetail";

// when
// then
assertThatThrownBy(() -> {
aboutService.getAbout(main, sub, detail);
}).isInstanceOf(CategoryNotMatchException.class);
}

@Test
@DisplayName("getAbout은 존재하지 않는 카테고리로 조회 시 AboutNotFoundException을 발생 한다.")
public void getAbout_AboutNotFound_ThrowsException() {
// given
MainCategory main = DEPT_INTRO;
SubCategory sub = HISTORY;
String detail = "failDetail";

// when
// then
assertThatThrownBy(() -> {
aboutService.getAbout(main, sub, null);
}).isInstanceOf(AboutNotFoundException.class);

assertThatThrownBy(() -> {
aboutService.getAbout(main, sub, detail);
}).isInstanceOf(AboutNotFoundException.class);
}

@Test
@DisplayName("updateAbout은 About의 content를 수정할 수 있다.")
public void updateAbout_Success() {
// given
Long id = 1L;

AboutUpdateRequest request = AboutUpdateRequest.builder()
.content("updateContent")
.build();

// when
aboutService.updateAbout(id, request);

// then
AboutResponse response = aboutService.getAbout(EDU_ACTIVITIES, CURRICULUM, "initDetail");

assertEquals(response.content(), "updateContent");
}

@Test
@DisplayName("updateAbout은 존재하지 않는 id로 수정 요청 시 AboutNotFoundException을 발생 한다.")
public void updateAbout_AboutNotFound_ThrowsException() {
// given
Long id = 0L;

AboutUpdateRequest request = AboutUpdateRequest.builder()
.content("updateContent")
.build();

// when
// then
assertThatThrownBy(() -> {
aboutService.updateAbout(id, request);
}).isInstanceOf(AboutNotFoundException.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package about.application;

public class AboutServiceTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package about.domain;

import static kgu.developers.domain.about.domain.MainCategory.DEPT_INTRO;
import static kgu.developers.domain.about.domain.SubCategory.HISTORY;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import kgu.developers.domain.about.domain.About;
import kgu.developers.domain.about.domain.MainCategory;
import kgu.developers.domain.about.domain.SubCategory;

public class AboutDomainTest {
@Test
@DisplayName("ABOUT 객체를 생성할 수 있다.")
public void createAbout_Success() {
// given
MainCategory mainCategory = DEPT_INTRO;
SubCategory subCategory = HISTORY;
String detailCategory = "test";
String content = "testContent";

// when
About about = About.create(mainCategory, subCategory, detailCategory, content);

// then
assertEquals(about.getMainCategory(), mainCategory);
assertEquals(about.getSubCategory(), subCategory);
assertEquals(about.getDetailCategory(), detailCategory);
assertEquals(about.getContent(), content);
}

@Test
@DisplayName("UpdateContent는 About 객체의 content를 수정할 수 있다.")
public void updateContent_Success() {
// given
String detailCategory = "test";
String content = "testContent";
About about = About.create(DEPT_INTRO, HISTORY, detailCategory, content);

String updateContent = "updateContent";

// when
about.updateContent(updateContent);

// then
assertEquals(about.getContent(), updateContent);
}
}
Loading
Loading