diff --git a/build.gradle b/build.gradle index 1bf530c..14e4577 100644 --- a/build.gradle +++ b/build.gradle @@ -32,10 +32,12 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.jsoup:jsoup:1.16.1' implementation 'com.h2database:h2' + implementation 'org.mapstruct:mapstruct:1.6.2' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' runtimeOnly 'com.h2database:h2' annotationProcessor 'org.projectlombok:lombok' + annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.2' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' diff --git a/src/main/java/com/catspot/crawler/CrawlerScheduler.java b/src/main/java/com/catspot/crawler/CrawlerScheduler.java index 739e713..32527e8 100644 --- a/src/main/java/com/catspot/crawler/CrawlerScheduler.java +++ b/src/main/java/com/catspot/crawler/CrawlerScheduler.java @@ -1,5 +1,7 @@ package com.catspot.crawler; +import com.catspot.studyplace.StudyPlace; +import com.catspot.studyplace.StudyPlaceRepository; import lombok.AllArgsConstructor; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; diff --git a/src/main/java/com/catspot/crawler/LibraryCrawler.java b/src/main/java/com/catspot/crawler/LibraryCrawler.java index 6613849..5713e37 100644 --- a/src/main/java/com/catspot/crawler/LibraryCrawler.java +++ b/src/main/java/com/catspot/crawler/LibraryCrawler.java @@ -1,12 +1,12 @@ package com.catspot.crawler; +import com.catspot.studyplace.StudyPlace; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; public class LibraryCrawler { diff --git a/src/main/java/com/catspot/crawler/StudyPlace.java b/src/main/java/com/catspot/studyplace/StudyPlace.java similarity index 57% rename from src/main/java/com/catspot/crawler/StudyPlace.java rename to src/main/java/com/catspot/studyplace/StudyPlace.java index 820a541..566ff5f 100644 --- a/src/main/java/com/catspot/crawler/StudyPlace.java +++ b/src/main/java/com/catspot/studyplace/StudyPlace.java @@ -1,4 +1,4 @@ -package com.catspot.crawler; +package com.catspot.studyplace; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -18,10 +18,4 @@ public class StudyPlace { private Integer allSeats; private Integer useSeats; private Integer restSeats; - - @Override - public String toString() { - return "placeIdx=" + placeIdx + ", placeName=" + placeName + ", url=" + url + - ", allSeats=" + allSeats + ", useSeats=" + useSeats + ", restSeats=" + restSeats; - } } diff --git a/src/main/java/com/catspot/studyplace/StudyPlaceController.java b/src/main/java/com/catspot/studyplace/StudyPlaceController.java new file mode 100644 index 0000000..fc4f051 --- /dev/null +++ b/src/main/java/com/catspot/studyplace/StudyPlaceController.java @@ -0,0 +1,28 @@ +package com.catspot.studyplace; + +import java.util.ArrayList; +import java.util.List; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api") +@AllArgsConstructor +public class StudyPlaceController { + private final StudyPlaceRepository studyPlaceRepository; + + @GetMapping("/study-seat") + public StudyPlaceResponse getAllStudyPlaces() { + List allStudyPlaces = studyPlaceRepository.findAll(); + List studyPlaceDtos = new ArrayList<>(); + + for (StudyPlace studyPlace : allStudyPlaces) { + StudyPlaceDto dto = StudyPlaceMapper.INSTANCE.studyPlaceToStudyPlaceDto(studyPlace); + studyPlaceDtos.add(dto); + } + + return StudyPlaceResponse.builder().data(studyPlaceDtos).build(); + } +} diff --git a/src/main/java/com/catspot/studyplace/StudyPlaceDto.java b/src/main/java/com/catspot/studyplace/StudyPlaceDto.java new file mode 100644 index 0000000..e5e220d --- /dev/null +++ b/src/main/java/com/catspot/studyplace/StudyPlaceDto.java @@ -0,0 +1,15 @@ +package com.catspot.studyplace; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class StudyPlaceDto { + private Long placeIdx; + private String placeName; + private String url; + private Integer allSeats; + private Integer useSeats; + private Integer restSeats; +} diff --git a/src/main/java/com/catspot/studyplace/StudyPlaceMapper.java b/src/main/java/com/catspot/studyplace/StudyPlaceMapper.java new file mode 100644 index 0000000..1ccd79d --- /dev/null +++ b/src/main/java/com/catspot/studyplace/StudyPlaceMapper.java @@ -0,0 +1,13 @@ +package com.catspot.studyplace; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface StudyPlaceMapper { + + StudyPlaceMapper INSTANCE = Mappers.getMapper(StudyPlaceMapper.class); + + StudyPlaceDto studyPlaceToStudyPlaceDto(StudyPlace studyPlace); +} diff --git a/src/main/java/com/catspot/crawler/StudyPlaceRepository.java b/src/main/java/com/catspot/studyplace/StudyPlaceRepository.java similarity index 86% rename from src/main/java/com/catspot/crawler/StudyPlaceRepository.java rename to src/main/java/com/catspot/studyplace/StudyPlaceRepository.java index a5344c8..af69dfd 100644 --- a/src/main/java/com/catspot/crawler/StudyPlaceRepository.java +++ b/src/main/java/com/catspot/studyplace/StudyPlaceRepository.java @@ -1,4 +1,4 @@ -package com.catspot.crawler; +package com.catspot.studyplace; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; diff --git a/src/main/java/com/catspot/studyplace/StudyPlaceResponse.java b/src/main/java/com/catspot/studyplace/StudyPlaceResponse.java new file mode 100644 index 0000000..9d35d92 --- /dev/null +++ b/src/main/java/com/catspot/studyplace/StudyPlaceResponse.java @@ -0,0 +1,13 @@ +package com.catspot.studyplace; + +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@AllArgsConstructor +@Builder +public class StudyPlaceResponse { + private List data; +} diff --git a/src/test/java/com/catspot/crawler/LibraryCrawlerTest.java b/src/test/java/com/catspot/crawler/LibraryCrawlerTest.java index f66483f..792b602 100644 --- a/src/test/java/com/catspot/crawler/LibraryCrawlerTest.java +++ b/src/test/java/com/catspot/crawler/LibraryCrawlerTest.java @@ -1,8 +1,8 @@ package com.catspot.crawler; +import com.catspot.studyplace.StudyPlace; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.HashMap; import java.util.List; class LibraryCrawlerTest { diff --git a/src/test/java/com/catspot/studyplace/StudyPlaceControllerTest.java b/src/test/java/com/catspot/studyplace/StudyPlaceControllerTest.java new file mode 100644 index 0000000..87b6745 --- /dev/null +++ b/src/test/java/com/catspot/studyplace/StudyPlaceControllerTest.java @@ -0,0 +1,51 @@ +package com.catspot.studyplace; + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +@WebMvcTest(StudyPlaceController.class) +public class StudyPlaceControllerTest { + @MockBean + private StudyPlaceRepository studyPlaceRepository; + + @Autowired + private MockMvc mockMvc; + + @Test + void 열람실_데이터_조회() throws Exception { + // given + List response = studyPlaceResponse(); + doReturn(response).when(studyPlaceRepository).findAll(); + + // then + ResultActions resultActions = mockMvc.perform( + MockMvcRequestBuilders.get("/api/study-seat")) + .andExpect(status().isOk()); + } + + private List studyPlaceResponse() { + List response = new ArrayList(); + for (int i = 0; i < 5; i++) { + StudyPlace studyPlace = StudyPlace.builder() + .placeIdx((long) i) + .placeName("열람실A") + .url("https://열람실A.com") + .allSeats(100) + .useSeats(10) + .restSeats(90) + .build(); + response.add(studyPlace); + } + return response; + } +} diff --git a/src/test/java/com/catspot/studyplace/StudyPlaceDtoTest.java b/src/test/java/com/catspot/studyplace/StudyPlaceDtoTest.java new file mode 100644 index 0000000..c03284f --- /dev/null +++ b/src/test/java/com/catspot/studyplace/StudyPlaceDtoTest.java @@ -0,0 +1,20 @@ +package com.catspot.studyplace; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class StudyPlaceDtoTest { + @Test + public void DTO_생성_테스트() { + StudyPlaceDto dto = StudyPlaceDto.builder() + .placeIdx(1L) + .placeName("1") + .url("url") + .allSeats(10) + .useSeats(2) + .restSeats(8) + .build(); + + Assertions.assertEquals(dto.getPlaceIdx(), 1); + } +} diff --git a/src/test/java/com/catspot/studyplace/StudyPlaceMapperTest.java b/src/test/java/com/catspot/studyplace/StudyPlaceMapperTest.java new file mode 100644 index 0000000..d0d8607 --- /dev/null +++ b/src/test/java/com/catspot/studyplace/StudyPlaceMapperTest.java @@ -0,0 +1,43 @@ +package com.catspot.studyplace; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +public class StudyPlaceMapperTest { + @Test + public void DTO_매핑_테스트() { + StudyPlace studyPlace = getStudyPlace(); + + StudyPlaceDto studyPlaceDto = StudyPlaceMapper.INSTANCE.studyPlaceToStudyPlaceDto(studyPlace); + + assertNotNull(studyPlaceDto); + assertEquals(studyPlaceDto.getPlaceIdx(), studyPlace.getPlaceIdx()); + assertEquals(studyPlaceDto.getPlaceName(), studyPlace.getPlaceName()); + assertEquals(studyPlaceDto.getUrl(), studyPlace.getUrl()); + assertEquals(studyPlaceDto.getAllSeats(), studyPlace.getAllSeats()); + assertEquals(studyPlaceDto.getUseSeats(), studyPlace.getUseSeats()); + assertEquals(studyPlaceDto.getRestSeats(), studyPlace.getRestSeats()); + } + + @Test + public void DTO_null_매핑_테스트() { + StudyPlace studyPlace = null; + + StudyPlaceDto studyPlaceDto = StudyPlaceMapper.INSTANCE.studyPlaceToStudyPlaceDto(studyPlace); + + assertNull(studyPlaceDto); + } + + private StudyPlace getStudyPlace() { + StudyPlace studyPlace = StudyPlace.builder() + .placeIdx(1L) + .placeName("1") + .url("url") + .allSeats(10) + .useSeats(2) + .restSeats(8) + .build(); + return studyPlace; + } +} diff --git a/src/test/java/com/catspot/crawler/StudyPlaceRepositoryTest.java b/src/test/java/com/catspot/studyplace/StudyPlaceRepositoryTest.java similarity index 96% rename from src/test/java/com/catspot/crawler/StudyPlaceRepositoryTest.java rename to src/test/java/com/catspot/studyplace/StudyPlaceRepositoryTest.java index 0a08ad0..cfabfe5 100644 --- a/src/test/java/com/catspot/crawler/StudyPlaceRepositoryTest.java +++ b/src/test/java/com/catspot/studyplace/StudyPlaceRepositoryTest.java @@ -1,4 +1,4 @@ -package com.catspot.crawler; +package com.catspot.studyplace; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/catspot/studyplace/StudyPlaceResponseTest.java b/src/test/java/com/catspot/studyplace/StudyPlaceResponseTest.java new file mode 100644 index 0000000..4b6516f --- /dev/null +++ b/src/test/java/com/catspot/studyplace/StudyPlaceResponseTest.java @@ -0,0 +1,35 @@ +package com.catspot.studyplace; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class StudyPlaceResponseTest { + @Test + void 열람실_응답클래스_생성() { + List studyPlaces = studyPlaceResponse(); + StudyPlaceResponse studyPlaceResponse = new StudyPlaceResponse(studyPlaces); + + assertFalse(studyPlaceResponse.getData().isEmpty()); + } + + private List studyPlaceResponse() { + List response = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + StudyPlaceDto studyPlaceDto = StudyPlaceDto.builder() + .placeIdx((long) i) + .placeName("열람실A") + .url("https://열람실A.com") + .allSeats(100) + .useSeats(10) + .restSeats(90) + .build(); + + response.add(studyPlaceDto); + } + return response; + } +} diff --git a/src/test/java/com/catspot/crawler/StudyPlaceTest.java b/src/test/java/com/catspot/studyplace/StudyPlaceTest.java similarity index 93% rename from src/test/java/com/catspot/crawler/StudyPlaceTest.java rename to src/test/java/com/catspot/studyplace/StudyPlaceTest.java index 2a99742..55a318c 100644 --- a/src/test/java/com/catspot/crawler/StudyPlaceTest.java +++ b/src/test/java/com/catspot/studyplace/StudyPlaceTest.java @@ -1,4 +1,4 @@ -package com.catspot.crawler; +package com.catspot.studyplace; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test;