diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/.idea/code-review.iml b/.idea/code-review.iml
new file mode 100644
index 0000000..d6ebd48
--- /dev/null
+++ b/.idea/code-review.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..b66d342
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..07225ff
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/db/wiseSaying/1.json b/db/wiseSaying/1.json
new file mode 100644
index 0000000..a23802f
--- /dev/null
+++ b/db/wiseSaying/1.json
@@ -0,0 +1 @@
+{"author":"작자미상","id":1,"content":"현재를 사랑하라."}
\ No newline at end of file
diff --git a/db/wiseSaying/10.json b/db/wiseSaying/10.json
new file mode 100644
index 0000000..d48fd4c
--- /dev/null
+++ b/db/wiseSaying/10.json
@@ -0,0 +1 @@
+{"author":"작자미상 10","id":10,"content":"명언 10"}
\ No newline at end of file
diff --git a/db/wiseSaying/11.json b/db/wiseSaying/11.json
new file mode 100644
index 0000000..a5f1a44
--- /dev/null
+++ b/db/wiseSaying/11.json
@@ -0,0 +1 @@
+{"author":"작자미상 11","id":11,"content":"명언 11"}
\ No newline at end of file
diff --git a/db/wiseSaying/2.json b/db/wiseSaying/2.json
new file mode 100644
index 0000000..1e1ccbe
--- /dev/null
+++ b/db/wiseSaying/2.json
@@ -0,0 +1 @@
+{"author":"작자미상 2","id":2,"content":"명언 2"}
\ No newline at end of file
diff --git a/db/wiseSaying/3.json b/db/wiseSaying/3.json
new file mode 100644
index 0000000..4593a60
--- /dev/null
+++ b/db/wiseSaying/3.json
@@ -0,0 +1 @@
+{"author":"작자미상 3","id":3,"content":"명언 3"}
\ No newline at end of file
diff --git a/db/wiseSaying/4.json b/db/wiseSaying/4.json
new file mode 100644
index 0000000..75d791c
--- /dev/null
+++ b/db/wiseSaying/4.json
@@ -0,0 +1 @@
+{"author":"작자미상 4","id":4,"content":"명언 4"}
\ No newline at end of file
diff --git a/db/wiseSaying/5.json b/db/wiseSaying/5.json
new file mode 100644
index 0000000..5f7bb0c
--- /dev/null
+++ b/db/wiseSaying/5.json
@@ -0,0 +1 @@
+{"author":"작자미상 5","id":5,"content":"명언 5"}
\ No newline at end of file
diff --git a/db/wiseSaying/6.json b/db/wiseSaying/6.json
new file mode 100644
index 0000000..538798e
--- /dev/null
+++ b/db/wiseSaying/6.json
@@ -0,0 +1 @@
+{"author":"작자미상 6","id":6,"content":"명언 6"}
\ No newline at end of file
diff --git a/db/wiseSaying/7.json b/db/wiseSaying/7.json
new file mode 100644
index 0000000..c629120
--- /dev/null
+++ b/db/wiseSaying/7.json
@@ -0,0 +1 @@
+{"author":"작자미상 7","id":7,"content":"명언 ㅕ7"}
\ No newline at end of file
diff --git a/db/wiseSaying/8.json b/db/wiseSaying/8.json
new file mode 100644
index 0000000..abe1677
--- /dev/null
+++ b/db/wiseSaying/8.json
@@ -0,0 +1 @@
+{"author":"작자미상 8","id":8,"content":"명언 8"}
\ No newline at end of file
diff --git a/db/wiseSaying/9.json b/db/wiseSaying/9.json
new file mode 100644
index 0000000..478169f
--- /dev/null
+++ b/db/wiseSaying/9.json
@@ -0,0 +1 @@
+{"author":"작자미상 9","id":9,"content":"명언 9"}
\ No newline at end of file
diff --git a/db/wiseSaying/data.json b/db/wiseSaying/data.json
new file mode 100644
index 0000000..e365e26
--- /dev/null
+++ b/db/wiseSaying/data.json
@@ -0,0 +1 @@
+[{"author":"작자미상 1","id":1,"content":"명언 1"},{"author":"작자미상 2","id":2,"content":"명언 2"},{"author":"작자미상 3","id":3,"content":"명언 3"},{"author":"작자미상 4","id":4,"content":"명언 4"},{"author":"작자미상 5","id":5,"content":"명언 5"},{"author":"작자미상 6","id":6,"content":"명언 6"},{"author":"작자미상 7","id":7,"content":"명언 ㅕ7"},{"author":"작자미상 8","id":8,"content":"명언 8"},{"author":"작자미상 9","id":9,"content":"명언 9"},{"author":"작자미상 10","id":10,"content":"명언 10"},{"author":"작자미상 11","id":11,"content":"명언 11"}]
\ No newline at end of file
diff --git a/db/wiseSaying/lastId.txt b/db/wiseSaying/lastId.txt
new file mode 100644
index 0000000..9d60796
--- /dev/null
+++ b/db/wiseSaying/lastId.txt
@@ -0,0 +1 @@
+11
\ No newline at end of file
diff --git a/src/main/java/org/example/App.java b/src/main/java/org/example/App.java
new file mode 100644
index 0000000..1741150
--- /dev/null
+++ b/src/main/java/org/example/App.java
@@ -0,0 +1,54 @@
+package org.example.wiseSaying;
+
+import org.json.simple.parser.ParseException;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+public class App {
+ private final WiseSayingRepository wiseSayingRepository = new WiseSayingRepository();
+ private final WiseSayingService wiseSayingService = new WiseSayingService(wiseSayingRepository);
+ private final WiseSayingController wiseSayingController = new WiseSayingController(wiseSayingService);
+
+ public void run() throws IOException, ParseException {
+ System.out.println("== 명언 앱 ==");
+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+ wiseSayingRepository.initDatabase();
+
+ while (true) {
+ System.out.print("명령) ");
+ String command = br.readLine();
+
+ if (command.equals("종료")) {
+ System.out.println("명언 앱을 종료합니다.");
+ break;
+ }
+
+ handleCommand(command, br);
+ }
+ }
+
+ /// 명령어 핸들러
+ public void handleCommand(String command, BufferedReader br) throws IOException {
+ if (command.equals("등록")) {
+ wiseSayingController.createWiseSaying(br);
+ } else if (command.startsWith("목록?page=") || command.equals("목록")) {
+ wiseSayingController.printWiseSayings(command);
+ } else if (command.startsWith("삭제?id=")) {
+ int id = Integer.parseInt(command.split("=")[1]);
+ wiseSayingController.deleteWiseSayingById(id);
+ } else if (command.startsWith("수정?id=")) {
+ int id = Integer.parseInt(command.split("=")[1]);
+ wiseSayingController.updateWiseSayingById(id, br);
+ } else if (command.equals("빌드")) {
+ wiseSayingController.buildWiseSaying();
+ } else if (command.startsWith("목록?keywordType=")) {
+ String keywordType = command.split("=")[1].split("&")[0];
+ String keyword = command.split("=")[2];
+ wiseSayingController.searchWiseSaying(keywordType, keyword);
+ }else {
+ System.out.println("알 수 없는 명령입니다.");
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java
new file mode 100644
index 0000000..da1b3ce
--- /dev/null
+++ b/src/main/java/org/example/Main.java
@@ -0,0 +1,12 @@
+package org.example;
+
+import org.json.simple.parser.ParseException;
+
+import java.io.IOException;
+
+public class Main {
+ public static void main(String[] args) throws IOException, ParseException {
+ App app = new App();
+ app.run();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/example/WiseSaying.java b/src/main/java/org/example/WiseSaying.java
new file mode 100644
index 0000000..cc66746
--- /dev/null
+++ b/src/main/java/org/example/WiseSaying.java
@@ -0,0 +1,37 @@
+package org.example.wiseSaying;
+
+/**
+ * 명언 객체
+ */
+
+public class WiseSaying {
+ private final int id;
+ private String author;
+ private String content;
+
+ public WiseSaying(int id, String author, String content) {
+ this.id = id;
+ this.author = author;
+ this.content = content;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public String getAuthor() {
+ return author;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public void setAuthor(String author) {
+ this.author = author;
+ }
+
+ public void setContent(String content) {
+ this.content = content;
+ }
+}
diff --git a/src/main/java/org/example/WiseSayingController.java b/src/main/java/org/example/WiseSayingController.java
new file mode 100644
index 0000000..bbd3d0e
--- /dev/null
+++ b/src/main/java/org/example/WiseSayingController.java
@@ -0,0 +1,109 @@
+package org.example.wiseSaying;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.List;
+
+/**
+ * 컨트롤러
+ */
+
+public class WiseSayingController {
+ private final WiseSayingService wiseSayingService;
+ private final String DB_PATH = "db/wiseSaying";
+ private int lastId = 0;
+
+ public WiseSayingController(WiseSayingService wiseSayingService) {
+ this.wiseSayingService = wiseSayingService;
+ }
+
+ /// 빌드 명령어
+ public void buildWiseSaying() throws IOException {
+ List wiseSayings = wiseSayingService.findAllWiseSayings();
+
+ JSONArray jsonArray = new JSONArray();
+
+ for (WiseSaying wiseSaying : wiseSayings) {
+ JSONObject json = new JSONObject();
+ json.put("id", wiseSaying.getId());
+ json.put("author", wiseSaying.getAuthor());
+ json.put("content", wiseSaying.getContent());
+ jsonArray.add(json);
+ }
+
+ File file = new File(DB_PATH, "data.json");
+ Files.writeString(file.toPath(), jsonArray.toJSONString());
+ System.out.println("data.json 파일의 내용이 갱신되었습니다.");
+ }
+
+ /// 명언 등록
+ public void createWiseSaying(BufferedReader br) throws IOException {
+ System.out.print("명언: ");
+ String content = br.readLine();
+ System.out.print("작가: ");
+ String author = br.readLine();
+ lastId++;
+
+ WiseSaying wiseSaying = new WiseSaying(lastId, author, content);
+ wiseSayingService.addWiseSaying(wiseSaying);
+
+ System.out.println(lastId + "번 명언이 등록되었습니다.");
+ }
+
+
+ /// 명언 조회
+ public void printWiseSayings(String command) {
+ System.out.println("번호 / 작가 / 명언");
+ System.out.println("----------------------");
+
+ wiseSayingService.printWiseSaying(command);
+ }
+
+ /// 명언 수정
+ public void updateWiseSayingById(int id, BufferedReader br) throws IOException {
+ if (id == -1) return;
+
+ WiseSaying wiseSaying = wiseSayingService.findWiseSayingById(id);
+
+ if (wiseSaying == null) {
+ System.out.println(id + "번 명언은 존재하지 않습니다.");
+ }
+
+ System.out.println("명언(기존): " + wiseSaying.getContent());
+ System.out.print("명언: ");
+ String newContent = br.readLine();
+ System.out.println("작가(기존): " + wiseSaying.getAuthor());
+ System.out.print("작가: ");
+ String newAuthor = br.readLine();
+
+ wiseSayingService.updateWiseSaying(wiseSaying, newContent, newAuthor);
+
+ System.out.println(id + "번 명언이 수정되었습니다.");
+ }
+
+ /// 명언 삭제
+ public void deleteWiseSayingById(int id) throws IOException {
+ if (wiseSayingService.deleteWiseSaying(id)) {
+ System.out.println(id + "번 명언이 삭제되었습니다.");
+ } else {
+ System.out.println(id + "번 명언은 존재하지 않습니다.");
+ }
+ }
+
+ /// 명언 검색
+ public void searchWiseSaying(String keywordType, String keyword) {
+ System.out.println("----------------------");
+ System.out.println("검색타입 : " + keywordType);
+ System.out.println("검색어 : " + keyword);
+ System.out.println("----------------------");
+ System.out.println("번호 / 작가 / 명언");
+ System.out.println("----------------------");
+
+ wiseSayingService.printWiseSayingByKeyword(keywordType, keyword);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/example/WiseSayingRepository.java b/src/main/java/org/example/WiseSayingRepository.java
new file mode 100644
index 0000000..df45689
--- /dev/null
+++ b/src/main/java/org/example/WiseSayingRepository.java
@@ -0,0 +1,132 @@
+package org.example.wiseSaying;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 레포지토리
+ */
+
+public class WiseSayingRepository {
+ private final List wiseSayings = new ArrayList<>();
+ private final String DB_PATH = "db/wiseSaying";
+ private final String DATA_FILE = DB_PATH + "/data.json";
+ private final String LAST_ID_FILE = DB_PATH + "/lastId.txt";
+ private int lastId = 0;
+
+ /// 파일 영속성 초기화
+ public void initDatabase() throws IOException, ParseException {
+ Files.createDirectories(Paths.get(DB_PATH));
+
+ File lastIdFile = new File(LAST_ID_FILE);
+ if (lastIdFile.exists()) {
+ lastId = Integer.parseInt(Files.readString(lastIdFile.toPath()).trim());
+ }
+
+ File jsonFile = new File(DATA_FILE);
+ if (jsonFile.exists()) {
+ String content = Files.readString(jsonFile.toPath());
+ JSONParser jsonParser = new JSONParser();
+ JSONArray jsonArray = (JSONArray) jsonParser.parse(content);
+
+ for (Object obj : jsonArray) {
+ JSONObject json = (JSONObject) obj;
+ WiseSaying wiseSaying = parseWiseSaying(json);
+ if (wiseSaying != null) {
+ wiseSayings.add(wiseSaying);
+ }
+ }
+ }
+ }
+
+ /// JSON -> WiseSaying
+ public WiseSaying parseWiseSaying(JSONObject json) {
+ int id = Integer.parseInt(json.get("id").toString());
+ String author = json.get("author").toString();
+ String content = json.get("content").toString();
+
+ return new WiseSaying(id, author, content);
+ }
+
+ /// WiseSaying -> File
+ public void saveWiseSayingToFile(WiseSaying wiseSaying) throws IOException {
+ JSONObject json = new JSONObject();
+ json.put("id", wiseSaying.getId());
+ json.put("author", wiseSaying.getAuthor());
+ json.put("content", wiseSaying.getContent());
+
+ File file = new File(DB_PATH, wiseSaying.getId() + ".json");
+ Files.createDirectories(file.getParentFile().toPath());
+ Files.writeString(file.toPath(), json.toString());
+ }
+
+ /// LastId -> File
+ public void saveLastId(WiseSaying wiseSaying) throws IOException {
+ File lastIdFile = new File(LAST_ID_FILE);
+ Files.writeString(lastIdFile.toPath(), String.valueOf(wiseSaying.getId()));
+ }
+
+ /// 명언 저장
+ public void saveWiseSaying(WiseSaying wiseSaying) throws IOException {
+ wiseSayings.add(wiseSaying);
+
+ saveWiseSayingToFile(wiseSaying);
+ updateDataJsonFile();
+ }
+
+ /// 모든 명언 조회
+ public List findAllWiseSayings() {
+ return new ArrayList<>(wiseSayings);
+ }
+
+ /// id로 명언 조회
+ public WiseSaying findWiseSayingById(int id) {
+ return wiseSayings.stream()
+ .filter(wiseSaying -> wiseSaying.getId() == id)
+ .findFirst()
+ .orElse(null);
+ }
+
+ /// 명언 삭제
+ public boolean deleteWiseSayingById(int id) throws IOException {
+ WiseSaying wiseSaying = findWiseSayingById(id);
+
+ if (wiseSaying != null) {
+ wiseSayings.remove(wiseSaying);
+
+ File file = new File(DB_PATH, wiseSaying.getId() + ".json");
+ if (file.exists()) {
+ file.delete();
+ }
+
+ updateDataJsonFile();
+ return true;
+ }
+
+ return false;
+ }
+
+ /// data.json 갱신
+ private void updateDataJsonFile() throws IOException {
+ JSONArray jsonArray = new JSONArray();
+
+ for (WiseSaying ws : wiseSayings) {
+ JSONObject json = new JSONObject();
+ json.put("id", ws.getId());
+ json.put("author", ws.getAuthor());
+ json.put("content", ws.getContent());
+ jsonArray.add(json);
+ }
+
+ Files.writeString(Paths.get(DATA_FILE), jsonArray.toJSONString());
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/example/WiseSayingService.java b/src/main/java/org/example/WiseSayingService.java
new file mode 100644
index 0000000..a350dd5
--- /dev/null
+++ b/src/main/java/org/example/WiseSayingService.java
@@ -0,0 +1,128 @@
+package org.example.wiseSaying;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 서비스
+ */
+
+public class WiseSayingService {
+ private final WiseSayingRepository wiseSayingRepository;
+
+ public WiseSayingService(WiseSayingRepository wiseSayingRepository) {
+ this.wiseSayingRepository = wiseSayingRepository;
+ }
+
+ public void addWiseSaying(WiseSaying wiseSaying) throws IOException {
+ wiseSayingRepository.saveWiseSaying(wiseSaying);
+ wiseSayingRepository.saveWiseSayingToFile(wiseSaying);
+ wiseSayingRepository.saveLastId(wiseSaying);
+ }
+
+ public List findAllWiseSayings() {
+ return wiseSayingRepository.findAllWiseSayings();
+ }
+
+ public WiseSaying findWiseSayingById(int id) {
+ return wiseSayingRepository.findWiseSayingById(id);
+ }
+
+ public void printWiseSaying(String command) {
+ int page = 1;
+ int itemsPerPage = 5;
+
+ if (command.contains("?page=")) {
+ try {
+ page = Integer.parseInt(command.split("=")[1]);
+ } catch (NumberFormatException e) {
+ System.out.println("잘못된 페이지 번호입니다.");
+ return;
+ }
+ }
+
+ List wiseSayings = findAllWiseSayings();
+
+ wiseSayings.sort((ws1, ws2) -> Integer.compare(ws2.getId(), ws1.getId()));
+
+ int totalItems = wiseSayings.size();
+ int totalPages = (int) Math.ceil((double) totalItems / itemsPerPage);
+
+ if (page > totalPages) {
+ System.out.println("잘못된 페이지 번호입니다.");
+ return;
+ }
+
+ int startIndex = (page - 1) * itemsPerPage;
+ int endIndex = Math.min(startIndex + itemsPerPage, totalItems);
+
+ for (int i = startIndex; i < endIndex; i++) {
+ WiseSaying wiseSaying = wiseSayings.get(i);
+ System.out.printf("%d / %s / %s%n", wiseSaying.getId(), wiseSaying.getAuthor(), wiseSaying.getContent());
+ }
+
+ System.out.println("----------------------");
+ System.out.print("페이지 : ");
+ for (int i = 1; i <= totalPages; i++) {
+ if (i == page) {
+ System.out.print("[" + i + "]");
+ } else {
+ System.out.print(i + " ");
+ }
+ if (i != totalPages) {
+ System.out.print(" / ");
+ }
+ }
+ System.out.println();
+ }
+
+ public void printWiseSayingByKeyword(String keywordType, String keyword) {
+ List wiseSayings = findAllWiseSayings();
+
+ List filteredWiseSayings = new ArrayList<>();
+ for (WiseSaying wiseSaying: wiseSayings) {
+ switch (keywordType) {
+ case "id":
+ if (String.valueOf(wiseSaying.getId()).equals(keyword)) {
+ filteredWiseSayings.add(wiseSaying);
+ }
+ break;
+ case "content":
+ if (wiseSaying.getContent().contains(keyword)) {
+ filteredWiseSayings.add(wiseSaying);
+ }
+ break;
+ case "author":
+ if (wiseSaying.getAuthor().contains(keyword)) {
+ filteredWiseSayings.add(wiseSaying);
+ }
+ break;
+ default:
+ System.out.println("일치하는 키워드가 없습니다.");
+ return;
+ }
+ }
+
+ if (filteredWiseSayings.isEmpty()) {
+ System.out.println("해당 명언을 찾을 수 없습니다.");
+ } else {
+ for (int i = filteredWiseSayings.size() - 1; i >= 0; i--) {
+ WiseSaying wiseSaying = filteredWiseSayings.get(i);
+ System.out.printf("%d / %s / %s%n", wiseSaying.getId(), wiseSaying.getAuthor(), wiseSaying.getContent());
+ }
+ }
+ }
+
+ public void updateWiseSaying(WiseSaying wiseSaying, String newContent, String newAuthor) throws IOException {
+ wiseSaying.setContent(newContent);
+ wiseSaying.setAuthor(newAuthor);
+ wiseSayingRepository.saveWiseSaying(wiseSaying);
+ wiseSayingRepository.saveWiseSayingToFile(wiseSaying);
+ wiseSayingRepository.saveLastId(wiseSaying);
+ }
+
+ public boolean deleteWiseSaying(int id) throws IOException {
+ return wiseSayingRepository.deleteWiseSayingById(id);
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/TestUtil.java b/src/test/java/TestUtil.java
new file mode 100644
index 0000000..c0a8c62
--- /dev/null
+++ b/src/test/java/TestUtil.java
@@ -0,0 +1,24 @@
+import java.io.*;
+
+public class TestUtil {
+
+ public static ByteArrayInputStream getInputStream(String input) {
+ return new ByteArrayInputStream(input.getBytes());
+ }
+
+ public static ByteArrayOutputStream setOutToByteArray() {
+ final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ System.setOut(new PrintStream(output));
+
+ return output;
+ }
+
+ public static void clearSetOutToByteArray(final ByteArrayOutputStream output) {
+ System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
+ try {
+ output.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/WiseSayingControllerTest.java b/src/test/java/WiseSayingControllerTest.java
new file mode 100644
index 0000000..1b60e04
--- /dev/null
+++ b/src/test/java/WiseSayingControllerTest.java
@@ -0,0 +1,173 @@
+import org.example.wiseSaying.WiseSayingController;
+import org.example.wiseSaying.WiseSayingRepository;
+import org.example.wiseSaying.WiseSayingService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class WiseSayingControllerTest {
+ private WiseSayingRepository wiseSayingRepository;
+ private WiseSayingService wiseSayingService;
+ private WiseSayingController wiseSayingController;
+
+ @BeforeEach
+ void beforeEach() {
+ wiseSayingRepository = new WiseSayingRepository();
+ wiseSayingService = new WiseSayingService(wiseSayingRepository);
+ wiseSayingController = new WiseSayingController(wiseSayingService);
+ }
+
+ @Test
+ @DisplayName("명언 등록")
+ void createWiseSaying() throws IOException {
+ final String input = ("""
+ 현재를 사랑하라.
+ 작자미상
+ """);
+ ByteArrayInputStream inputStream = TestUtil.getInputStream(input);
+ BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
+ ByteArrayOutputStream outputStream = TestUtil.setOutToByteArray();
+
+ Path dbPath = Path.of("db/wiseSaying");
+ if (!Files.exists(dbPath)) {
+ Files.createDirectories(dbPath);
+ }
+
+ wiseSayingController.createWiseSaying(br);
+ TestUtil.clearSetOutToByteArray(outputStream);
+
+ Path filePath = Path.of(dbPath.toString(), "1.json");
+ assertThat(Files.exists(filePath)).isTrue();
+
+ String content = Files.readString(filePath);
+
+ assertThat(content)
+ .contains("현재를 사랑하라.", "작자미상");
+ }
+
+ @Test
+ @DisplayName("명언 조회")
+ void printWiseSaying() throws IOException {
+ String input = """
+ 현재를 사랑하라.
+ 작자미상
+ """;
+ ByteArrayInputStream inputStream = TestUtil.getInputStream(input);
+ BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
+ ByteArrayOutputStream outputStream = TestUtil.setOutToByteArray();
+
+ wiseSayingController.createWiseSaying(br);
+ TestUtil.clearSetOutToByteArray(outputStream);
+
+ outputStream = TestUtil.setOutToByteArray();
+ wiseSayingController.printWiseSayings("목록?page=1");
+ TestUtil.clearSetOutToByteArray(outputStream);
+
+ String output = outputStream.toString().trim();
+
+ assertThat(output).contains("번호 / 작가 / 명언")
+ .contains("----------------------")
+ .contains("1 / 작자미상 / 현재를 사랑하라.");
+ }
+
+ @Test
+ @DisplayName("명언 수정")
+ void updateWiseSaying() throws IOException {
+ String input = """
+ 현재를 사랑하라.
+ 작자미상
+ 미래를 꿈꾸다.
+ 작자미상
+ """;
+ ByteArrayInputStream inputStream = TestUtil.getInputStream(input);
+ BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
+ ByteArrayOutputStream outputStream = TestUtil.setOutToByteArray();
+
+ wiseSayingController.createWiseSaying(br);
+ TestUtil.clearSetOutToByteArray(outputStream);
+
+ Path dbPath = Path.of("db/wiseSaying");
+ if (!Files.exists(dbPath)) {
+ Files.createDirectories(dbPath);
+ }
+
+ Path filePath = Path.of(dbPath.toString(), "1.json");
+ assertThat(Files.exists(filePath)).isTrue();
+
+ wiseSayingController.updateWiseSayingById(1, br);
+ TestUtil.clearSetOutToByteArray(outputStream);
+
+ String fileContent = Files.readString(filePath);
+
+ assertThat(fileContent)
+ .contains("미래를 꿈꾸다.")
+ .contains("작자미상");
+ }
+
+ @Test
+ @DisplayName("명언 삭제")
+ void deleteWiseSaying() throws IOException {
+ String input = """
+ 현재를 사랑하라.
+ 작자미상
+ """;
+ ByteArrayInputStream inputStream = TestUtil.getInputStream(input);
+ BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
+ ByteArrayOutputStream outputStream = TestUtil.setOutToByteArray();
+
+ wiseSayingController.createWiseSaying(br);
+ TestUtil.clearSetOutToByteArray(outputStream);
+
+ Path dbPath = Path.of("db/wiseSaying");
+ if (!Files.exists(dbPath)) {
+ Files.createDirectories(dbPath);
+ }
+
+ Path filePath = Path.of(dbPath.toString(), "1.json");
+ assertThat(Files.exists(filePath)).isTrue();
+
+ TestUtil.clearSetOutToByteArray(outputStream);
+ wiseSayingController.deleteWiseSayingById(1);
+
+ assertThat(filePath).doesNotExist();
+ }
+
+ @Test
+ @DisplayName("명언 빌드")
+ void buildWiseSaying() throws IOException {
+ String input = """
+ 현재를 사랑하라.
+ 작자미상
+ """;
+ ByteArrayInputStream inputStream = TestUtil.getInputStream(input);
+ BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
+ ByteArrayOutputStream outputStream = TestUtil.setOutToByteArray();
+
+ wiseSayingController.createWiseSaying(br);
+ TestUtil.clearSetOutToByteArray(outputStream);
+
+ Path dbPath = Path.of("db/wiseSaying");
+ if (!Files.exists(dbPath)) {
+ Files.createDirectories(dbPath);
+ }
+
+ Path filePath = Path.of(dbPath.toString(), "data.json");
+ assertThat(Files.exists(filePath)).isTrue();
+
+ TestUtil.clearSetOutToByteArray(outputStream);
+ wiseSayingController.buildWiseSaying();
+
+ String fileContent = Files.readString(filePath);
+
+ assertThat(filePath).exists();
+
+ assertThat(fileContent)
+ .contains("[{\"author\":\"작자미상\",\"id\":1,\"content\":\"현재를 사랑하라.\"}]");
+ }
+}