diff --git a/build.gradle b/build.gradle index 5da7633..3911368 100644 --- a/build.gradle +++ b/build.gradle @@ -39,6 +39,8 @@ dependencies { asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor:2.0.2.RELEASE' testCompile("org.springframework.boot:spring-boot-starter-test") implementation 'org.springframework.restdocs:spring-restdocs-mockmvc' + //이거 문서화때문에 추가함 + testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:2.0.2.RELEASE' // annotationProcessor 'org.projectlombok:lombok' testImplementation('org.springframework.boot:spring-boot-starter-test') { @@ -50,6 +52,7 @@ ext { } test { useJUnitPlatform() + outputs.dir snippetsDir } asciidoctor { diff --git a/src/docs/asciidoc/notice.adoc b/src/docs/asciidoc/notice.adoc new file mode 100644 index 0000000..e37b3e0 --- /dev/null +++ b/src/docs/asciidoc/notice.adoc @@ -0,0 +1,199 @@ += NUTEE REST API Guide(Crawl) +이문혁; +:doctype: book +:icons: font +:source-highlighter: highlightjs +:toc: left +:toclevels: 4 +:sectlinks: +:operation-curl-request-title: Example request +:operation-http-response-title: Example response + +[[overview]] += 개요 + +[[overview-http-verbs]] +== HTTP 동사 + +본 REST API에서 사용하는 HTTP 동사(verbs)는 가능한한 표준 HTTP와 REST 규약을 따른다. + +|=== +| 동사 | 용례 + +| `GET` +| 리소스를 가져올 때 사용 + +| `POST` +| 새 리소스를 만들 때 사용 + +| `PUT` +| 기존 리소스의 전체를 수정할 때 사용 + +| `PATCH` +| 기존 리소스의 일부를 수정할 때 사용 + +| `DELETE` +| 기존 리소스를 삭제할 떄 사용 +|=== + +[[overview-http-status-codes]] +== HTTP 상태 코드 + +본 REST API에서 사용하는 HTTP 상태 코드는 가능한한 표준 HTTP와 REST 규약을 따릅니다. + +|=== +| 상태 코드 | 용례 + +| `200 OK` +| 요청을 성공적으로 처리함 + +| `201 Created` +| 새 리소스를 성공적으로 생성함. 응답의 `Location` 헤더에 해당 리소스의 URI가 담겨있다. + +| `204 No Content` +| 기존 리소스를 성공적으로 수정함. + +| `400 Bad Request` +| 잘못된 요청을 보낸 경우. 응답 본문에 더 오류에 대한 정보가 담겨있다. + +| `404 Not Found` +| 요청한 리소스가 없음. +|=== + +[[overview-nutee-status-codes]] +== NUTEE 내부 상태코드 + +NUTEE REST API에서 사용하는 NUTEE 상태 코드입니다. + +|=== +| 상태 코드 | 용례 + +| `10` +| 문제 없이 정상 작동함. + +| `21` +| 권한이 없는 요청임. + +| `22` +| 요청에 해당하는 리소스가 존재하지 않음. + +| `23` +| request 에 비어있는 내용이 존재함. + +| `51` +| 데이터베이스 중복. + +| `52` +| 잘못된 인자를 전달하였음. + + +|=== + + +[[overview-errors]] +== 오류 + + +[[overview-hypermedia]] +== 하이퍼미디어 + +본 REST API는 하이퍼미디어와 사용하며 응답에 담겨있는 리소스는 다른 리소스에 대한 링크를 가지고 있다. +응답은 http://stateless.co/hal_specification.html[Hypertext Application from resource to resource. Language (HAL)] 형식을 따른다. +링크는 `_links`라는 키로 제공한다. 본 API의 사용자(클라이언트)는 URI를 직접 생성하지 않아야 하며, 리소스에서 제공하는 링크를 사용해야 한다. + + +[[api]] +== API(NOTICE) + +[[resources-auth]] +== 학교공지 + +notice는 학교 공지목록을 크롤링한다. + +[[resources-get-haksa]] +=== 학사목록 + +`GET` 요청을 사용 하여 학사공지 목록을 가져온다. + +[[http-request]] +==== http-request +include::{snippets}/get-haksa/http-request.adoc[] + +[[http-response]] +==== http-response +include::{snippets}/get-haksa/http-response.adoc[] + + + +[[resources-get-sooup]] +=== 수업목록 + +`GET` 요청을 사용 하여 수업공지 목록을 가져온다. + +[[http-request]] +==== http-request +include::{snippets}/get-sooup/http-request.adoc[] + +[[http-response]] +==== http-response +include::{snippets}/get-sooup/http-response.adoc[] + + + +[[resources-get-hakjum]] +=== 학점목록 + +`GET` 요청을 사용 하여 학점공지 목록을 가져온다. + +[[http-request]] +==== http-request +include::{snippets}/get-hakjum/http-request.adoc[] + +[[http-response]] +==== http-response +include::{snippets}/get-hakjum/http-response.adoc[] + + + +[[resources-get-janghak]] +=== 장학목록 + +`GET` 요청을 사용 하여 장학공지 목록을 가져온다. + +[[http-request]] +==== http-request +include::{snippets}/get-janghak/http-request.adoc[] + +[[http-response]] +==== http-response +include::{snippets}/get-janghak/http-response.adoc[] + + + +[[resources-get-illban]] +=== 일반목록 + +`GET` 요청을 사용 하여 일반공지 목록을 가져온다. + +[[http-request]] +==== http-request +include::{snippets}/get-ilban/http-request.adoc[] + +[[http-response]] +==== http-response +include::{snippets}/get-ilban/http-response.adoc[] + + + +[[resources-get-hangsa]] +=== 행사목록 + +`GET` 요청을 사용 하여 행사공지 목록을 가져온다. + +[[http-request]] +==== http-request +include::{snippets}/get-hangsa/http-request.adoc[] + +[[http-response]] +==== http-response +include::{snippets}/get-hangsa/http-response.adoc[] \ No newline at end of file diff --git a/src/main/java/kr/nutee/crawler/Controller/NoticeController.java b/src/main/java/kr/nutee/crawler/Controller/NoticeController.java index 57277d9..e048bd9 100644 --- a/src/main/java/kr/nutee/crawler/Controller/NoticeController.java +++ b/src/main/java/kr/nutee/crawler/Controller/NoticeController.java @@ -5,7 +5,9 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -15,7 +17,7 @@ import java.io.IOException; @RestController -@RequestMapping(path = "/crawl", consumes = MediaType.APPLICATION_JSON_VALUE) +@RequestMapping(path = "/crawl")//, consumes = MediaType.APPLICATION_JSON_VALUE) @RequiredArgsConstructor @ResponseBody @Slf4j @@ -23,62 +25,68 @@ public class NoticeController { @Autowired NoticeService noticeService; - @GetMapping("/haksa") - public ResponseEntity crawlHaksa() throws IOException { - return noticeService.getNoticeApi("학사","haksa"); - } - - @GetMapping("/sooup") - public ResponseEntity crawlSooup() throws IOException { - return noticeService.getNoticeApi("수업","sooup"); - } - - @GetMapping("/hakjum") - public ResponseEntity crawlHakjum() throws IOException { - return noticeService.getNoticeApi("학점","hakjum"); - } - - @GetMapping("/janghak") - public ResponseEntity crawlJanghak() throws IOException { - return noticeService.getNoticeApi("장학","janghak"); - } - - @GetMapping("/ilban") - public ResponseEntity crawlIlban() throws IOException { - return noticeService.getNoticeApi("일반","ilban"); - } - - @GetMapping("/hangsa") - public ResponseEntity crawlHangsa() throws IOException { - return noticeService.getNoticeApi("행사","hangsa"); - } -// @GetMapping(path = "/haksa") -// public ResponseEntity crawlHaksa(@PageableDefault(size=10) Pageable pageable) throws IOException { -// return noticeService.getNoticeApi("학사","haksa", pageable); +// @GetMapping("/haksa") +// public ResponseEntity crawlHaksa() throws IOException { +// return noticeService.getNoticeApi("학사","haksa"); // } // -// @GetMapping(path = "/sooup") -// public ResponseEntity crawlSooup(@PageableDefault(size=10) Pageable pageable) throws IOException { -// return noticeService.getNoticeApi("수업","sooup", pageable); +// @GetMapping("/sooup") +// public ResponseEntity crawlSooup() throws IOException { +// return noticeService.getNoticeApi("수업","sooup"); // } // -// @GetMapping(path = "/hakjum") -// public ResponseEntity crawlHakjum(@PageableDefault(size=10) Pageable pageable) throws IOException { -// return noticeService.getNoticeApi("학점","hakjum", pageable); +// @GetMapping("/hakjum") +// public ResponseEntity crawlHakjum() throws IOException { +// return noticeService.getNoticeApi("학점","hakjum"); // } // -// @GetMapping(path = "/janghak") -// public ResponseEntity crawlJanghak(@PageableDefault(size=10) Pageable pageable) throws IOException { -// return noticeService.getNoticeApi("장학","janghak", pageable); +// @GetMapping("/janghak") +// public ResponseEntity crawlJanghak() throws IOException { +// return noticeService.getNoticeApi("장학","janghak"); // } // -// @GetMapping(path = "/ilban") -// public ResponseEntity crawlIlban(@PageableDefault(size=10) Pageable pageable) throws IOException { -// return noticeService.getNoticeApi("일반","ilban", pageable); +// @GetMapping("/ilban") +// public ResponseEntity crawlIlban() throws IOException { +// return noticeService.getNoticeApi("일반","ilban"); // } // -// @GetMapping(path = "/hangsa") -// public ResponseEntity crawlHangsa(@PageableDefault(size=10) Pageable pageable) throws IOException { -// return noticeService.getNoticeApi("행사","hangsa", pageable); +// @GetMapping("/hangsa") +// public ResponseEntity crawlHangsa() throws IOException { +// return noticeService.getNoticeApi("행사","hangsa"); +// } + +// @GetMapping(path = "/test") +// public ResponseEntity pageTest(@PageableDefault(size=6,sort="no",direction = Sort.Direction.DESC) Pageable pageable) throws IOException { +// return noticeService.testPage("학사","haksa", pageable); // } + + @GetMapping(path = "/haksa") + public ResponseEntity crawlHaksa(@PageableDefault(size=6,sort="no",direction=Sort.Direction.DESC) Pageable pageable) throws IOException { + return noticeService.getNoticePage("학사","haksa", pageable); + } + + @GetMapping(path = "/sooup") + public ResponseEntity crawlSooup(@PageableDefault(size=6,sort="no",direction=Sort.Direction.DESC) Pageable pageable) throws IOException { + return noticeService.getNoticePage("수업","sooup", pageable); + } + + @GetMapping(path = "/hakjum") + public ResponseEntity crawlHakjum(@PageableDefault(size=6,sort="no",direction=Sort.Direction.DESC) Pageable pageable) throws IOException { + return noticeService.getNoticePage("학점","hakjum", pageable); + } + + @GetMapping(path = "/janghak") + public ResponseEntity crawlJanghak(@PageableDefault(size=6,sort="no",direction=Sort.Direction.DESC) Pageable pageable) throws IOException { + return noticeService.getNoticePage("장학","janghak", pageable); + } + + @GetMapping(path = "/ilban") + public ResponseEntity crawlIlban(@PageableDefault(size=6,sort="no",direction=Sort.Direction.DESC) Pageable pageable) throws IOException { + return noticeService.getNoticePage("일반","ilban", pageable); + } + + @GetMapping(path = "/hangsa") + public ResponseEntity crawlHangsa(@PageableDefault(size=6,sort="no",direction=Sort.Direction.DESC) Pageable pageable) throws IOException { + return noticeService.getNoticePage("행사","hangsa", pageable); + } } \ No newline at end of file diff --git a/src/main/java/kr/nutee/crawler/Service/NoticeService.java b/src/main/java/kr/nutee/crawler/Service/NoticeService.java index 5be65d8..b7e0e7d 100644 --- a/src/main/java/kr/nutee/crawler/Service/NoticeService.java +++ b/src/main/java/kr/nutee/crawler/Service/NoticeService.java @@ -1,7 +1,7 @@ package kr.nutee.crawler.Service; import kr.nutee.crawler.Controller.NoticeController; -import kr.nutee.crawler.domain.entity.Notice; +import kr.nutee.crawler.domain.Notice; import kr.nutee.crawler.dto.Resource.ResponseResource; import kr.nutee.crawler.dto.Response.NoticeData; import kr.nutee.crawler.dto.Response.Response; @@ -10,6 +10,8 @@ import lombok.extern.slf4j.Slf4j; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @@ -80,28 +82,36 @@ public ResponseEntity getNoticeApi(String category, String url return ResponseEntity.ok().body(resource); } -// public ResponseEntity getNoticeApi(String category, String url, Pageable pageable) { -//// Page list = noticeRepository.findAll(pageable); -// Page list = noticeRepository.findAllByCategory(category, pageable); -// List listData = new ArrayList<>(); -// -// -// for(int i = 0; i getNoticePage(String category, String url, Pageable pageable) { +// Page list = noticeRepository.findAll(pageable); + Page list = noticeRepository.findAllByCategoryAndHit(category,0, pageable); + List listData = new ArrayList<>(); + +// int start = Math.max(1,list.getPageable().getPageNumber()-4); +// int end = Math.min(list.getTotalPages(), list.getPageable().getPageNumber()+4); + //페이지 바 페이지 몇개 보여줄 지. + int total = list.getTotalPages(); + int page = list.getPageable().getPageNumber(); + + for(int i = 0; i { Notice findNoticeByHrefAndCategory(String href, String category); List findAllByCategory(String category); -// Page findAllByCategory(String category, Pageable pageable); + Page findAllByCategoryAndHit(String category,int hit, Pageable pageable); // Notice findNoticeByTitleAndCategory(String title, String category); } \ No newline at end of file diff --git a/src/test/java/kr/nutee/crawler/Common/RestDocsConfiguration.java b/src/test/java/kr/nutee/crawler/Common/RestDocsConfiguration.java index 5812e9e..54769e0 100644 --- a/src/test/java/kr/nutee/crawler/Common/RestDocsConfiguration.java +++ b/src/test/java/kr/nutee/crawler/Common/RestDocsConfiguration.java @@ -1,19 +1,21 @@ -//package kr.nutee.crawler.Common; -// -//import org.springframework.boot.test.autoconfigure.restdocs.RestDocsMockMvcConfigurationCustomizer; -//import org.springframework.boot.test.context.TestConfiguration; -//import org.springframework.context.annotation.Bean; -// -//import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; -// -//@TestConfiguration -//public class RestDocsConfiguration { -// -// @Bean -// public RestDocsMockMvcConfigurationCustomizer restDocsMockMvcConfigurationCustomizer(){ -// return v -> v.operationPreprocessors() -// .withRequestDefaults(prettyPrint()) -// .withResponseDefaults(prettyPrint()); -// } -// -//} +package kr.nutee.crawler.Common; + +import org.springframework.boot.test.autoconfigure.restdocs.RestDocsMockMvcConfigurationCustomizer; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; + +import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; + +@TestConfiguration +public class RestDocsConfiguration { + + @Bean + public RestDocsMockMvcConfigurationCustomizer restDocsMockMvcConfigurationCustomizer(){ + return v -> v.operationPreprocessors() + .withRequestDefaults(prettyPrint()) + .withResponseDefaults(prettyPrint()); + } + + + +} diff --git a/src/test/java/kr/nutee/crawler/Controller/BaseControllerTest.java b/src/test/java/kr/nutee/crawler/Controller/BaseControllerTest.java index 6451506..ced2260 100644 --- a/src/test/java/kr/nutee/crawler/Controller/BaseControllerTest.java +++ b/src/test/java/kr/nutee/crawler/Controller/BaseControllerTest.java @@ -1,64 +1,64 @@ -//package kr.nutee.crawler.Controller; -// -//import com.fasterxml.jackson.databind.ObjectMapper; -//import kr.nutee.crawler.Common.RestDocsConfiguration; -//import kr.nutee.crawler.Service.NoticeService; -//import kr.nutee.crawler.repository.NoticeRepository; -//import org.junit.jupiter.api.MethodOrderer; -//import org.junit.jupiter.api.TestInstance; -//import org.junit.jupiter.api.TestMethodOrder; -//import org.junit.jupiter.api.extension.ExtendWith; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; -//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -//import org.springframework.boot.test.context.SpringBootTest; -//import org.springframework.context.annotation.Import; -//import org.springframework.restdocs.RestDocumentationExtension; -//import org.springframework.security.crypto.password.PasswordEncoder; -//import org.springframework.test.web.servlet.MockMvc; -//import org.springframework.transaction.annotation.Transactional; -//import org.springframework.web.context.WebApplicationContext; -// -// -////@AutoConfigureRestDocs -////@TestInstance(TestInstance.Lifecycle.PER_CLASS) -////@Import(RestDocsConfiguration.class) -////@ExtendWith(RestDocumentationExtension.class) -////@TestMethodOrder(MethodOrderer.OrderAnnotation.class) -////@Transactional -////@RunWith(SpringRunner.class) -////@AutoConfigureMockMvc -////@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) -// -// -// -//@SpringBootTest -//@AutoConfigureMockMvc +package kr.nutee.crawler.Controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import kr.nutee.crawler.Common.RestDocsConfiguration; +import kr.nutee.crawler.Service.NoticeService; +import kr.nutee.crawler.repository.NoticeRepository; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.restdocs.RestDocumentationExtension; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + //@AutoConfigureRestDocs //@TestInstance(TestInstance.Lifecycle.PER_CLASS) //@Import(RestDocsConfiguration.class) //@ExtendWith(RestDocumentationExtension.class) //@TestMethodOrder(MethodOrderer.OrderAnnotation.class) //@Transactional -//public class BaseControllerTest { -// -// @Autowired -// MockMvc mockMvc; -// -// @Autowired -// private NoticeService noticeService; -// -// @Autowired -// private NoticeRepository noticeRepository; -// -// @Autowired -// private WebApplicationContext ctx; -// -// @Autowired -// protected ObjectMapper objectMapper; -// -// @Autowired -// protected PasswordEncoder passwordEncoder; -// -//// protected String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJtZjAwMDEiLCJyb2xlIjoiUk9MRV9NQU5BR0VSIiwiaWQiOjEsImV4cCI6MTkxMjA2NDU4NiwiaWF0IjoxNTk2NzA0NTg2fQ.VmpRq6R0NhyteAp2ToaPPbjAANcSfZTMKvrXxCd3iFBcm3gVLn9GYd6lJQ07gRIyk_U38x4t7VEpzA2qcbMAgA"; -//} +//@RunWith(SpringRunner.class) +//@AutoConfigureMockMvc +//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) + + + +@SpringBootTest +@AutoConfigureMockMvc +@AutoConfigureRestDocs +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@Import(RestDocsConfiguration.class) +@ExtendWith(RestDocumentationExtension.class) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@Transactional +public class BaseControllerTest { + + @Autowired + MockMvc mockMvc; + + @Autowired + private NoticeService noticeService; + + @Autowired + private NoticeRepository noticeRepository; + + @Autowired + private WebApplicationContext ctx; + + @Autowired + protected ObjectMapper objectMapper; + + @Autowired + protected PasswordEncoder passwordEncoder; + +// protected String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJtZjAwMDEiLCJyb2xlIjoiUk9MRV9NQU5BR0VSIiwiaWQiOjEsImV4cCI6MTkxMjA2NDU4NiwiaWF0IjoxNTk2NzA0NTg2fQ.VmpRq6R0NhyteAp2ToaPPbjAANcSfZTMKvrXxCd3iFBcm3gVLn9GYd6lJQ07gRIyk_U38x4t7VEpzA2qcbMAgA"; +} diff --git a/src/test/java/kr/nutee/crawler/Controller/CrwalingTest.java b/src/test/java/kr/nutee/crawler/Controller/CrwalingTest.java index 1920a56..b830c3b 100644 --- a/src/test/java/kr/nutee/crawler/Controller/CrwalingTest.java +++ b/src/test/java/kr/nutee/crawler/Controller/CrwalingTest.java @@ -1,56 +1,52 @@ -//package kr.nutee.crawler.Controller; -// -//import org.jsoup.Jsoup; -//import org.jsoup.nodes.Document; -//import org.jsoup.nodes.Element; -//import org.jsoup.select.Elements; -//import org.junit.Ignore; -//import org.junit.jupiter.api.Test; -// -//import java.io.IOException; -//import java.text.DateFormat; -//import java.text.SimpleDateFormat; -//import java.util.*; -// -//import static org.junit.jupiter.api.Assertions.*; -// -//class CrwalingTest { -// -// @Test -// void getPageData() throws IOException { -// String URL = "http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10004&searchBun=51"; -// Document doc = Jsoup.connect(URL).get(); -// int size = doc.getElementsByTag("td").size(); -// List> list = new ArrayList<>(); -// for (int i = 0; i < size; i += 6) { -// Map map = new LinkedHashMap<>(); -// map.put("no", doc.getElementsByTag("td").get(i).text()); -// map.put("title", doc.getElementsByTag("td").get(i + 1).text()); -// map.put("href", "http://skhu.ac.kr/board/" + doc.getElementsByTag("td").get(i + 1).getElementsByTag("a").attr("href")); -// map.put("author", doc.getElementsByTag("td").get(i + 3).text()); -// map.put("date", doc.getElementsByTag("td").get(i + 4).text()); -// list.add(map); -// } -// System.out.println(list); -// } -// -// @Test -// void crawlSooup() { -// } -// -// @Test -// void crawlHakjum() { -// } -// -// @Test -// void crawlJanghak() { -// } -// -// @Test -// void crawlIlban() { -// } -// -// @Test -// void crawlHangsa() { -// } -//} \ No newline at end of file +package kr.nutee.crawler.Controller; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +class CrwalingTest { + + @Test + void getPageData() throws IOException { + String URL = "http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10004&searchBun=51"; + Document doc = Jsoup.connect(URL).get(); + int size = doc.getElementsByTag("td").size(); + List> list = new ArrayList<>(); + for (int i = 0; i < size; i += 6) { + Map map = new LinkedHashMap<>(); + map.put("no", doc.getElementsByTag("td").get(i).text()); + map.put("title", doc.getElementsByTag("td").get(i + 1).text()); + map.put("href", "http://skhu.ac.kr/board/" + doc.getElementsByTag("td").get(i + 1).getElementsByTag("a").attr("href")); + map.put("author", doc.getElementsByTag("td").get(i + 3).text()); + map.put("date", doc.getElementsByTag("td").get(i + 4).text()); + list.add(map); + } + System.out.println(list); + } + + @Test + void crawlSooup() { + } + + @Test + void crawlHakjum() { + } + + @Test + void crawlJanghak() { + } + + @Test + void crawlIlban() { + } + + @Test + void crawlHangsa() { + } +} \ No newline at end of file diff --git a/src/test/java/kr/nutee/crawler/Controller/NoticeControllerTest.java b/src/test/java/kr/nutee/crawler/Controller/NoticeControllerTest.java index 8bcbf05..28f1228 100644 --- a/src/test/java/kr/nutee/crawler/Controller/NoticeControllerTest.java +++ b/src/test/java/kr/nutee/crawler/Controller/NoticeControllerTest.java @@ -1,144 +1,151 @@ -//package kr.nutee.crawler.Controller; -// -//import org.junit.jupiter.api.Order; -//import org.junit.jupiter.api.Test; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.hateoas.MediaTypes; -//import org.springframework.http.HttpHeaders; -//import org.springframework.http.MediaType; -//import org.springframework.test.web.servlet.MockMvc; -//import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; -//import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -// -//import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; -//import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -// -// -//public class NoticeControllerTest extends BaseControllerTest { -// -// @Autowired -// MockMvc mockMvc; -// -// @Test @Order(1) -// public void crawlHaksa() throws Exception { -// -// MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/haksa") -// .contentType(MediaType.APPLICATION_JSON) -// .accept(MediaTypes.HAL_JSON_VALUE) -// .header("Access-Control-Request-Method","GET") -// .header("Origin", "http://nutee.kr:9709/crawl"); -// -// mockMvc.perform(builder) -// .andDo(print()) -// .andExpect(status().isOk()) -// .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) -// .andExpect(jsonPath("code").exists()) -// .andExpect(jsonPath("message").exists()) -// .andExpect(jsonPath("body",4).exists()) -// .andExpect(jsonPath("_links.self").exists()) -// .andDo(document("haksa")); -// } -// -// @Test @Order(2) -// public void crawlSooup() throws Exception { -// -// MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/sooup") -// .contentType(MediaType.APPLICATION_JSON) -// .accept(MediaTypes.HAL_JSON_VALUE) -// .header("Access-Control-Request-Method","GET") -// .header("Origin", "http://hello"); -// -// mockMvc.perform(builder) -// .andDo(print()) -// .andExpect(status().isOk()) -// .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) -// .andExpect(jsonPath("code").exists()) -// .andExpect(jsonPath("message").exists()) -// .andExpect(jsonPath("body",4).exists()) -// .andExpect(jsonPath("_links.self").exists()) -// .andDo(document("sooup")); -// } -// -// @Test @Order(3) -// public void crawlHakjum() throws Exception { -// -// MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/hakjum") -// .contentType(MediaType.APPLICATION_JSON) -// .accept(MediaTypes.HAL_JSON_VALUE) -// .header("Access-Control-Request-Method","GET") -// .header("Origin", "http://hello"); -// -// mockMvc.perform(builder) -// .andDo(print()) -// .andExpect(status().isOk()) -// .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) -// .andExpect(jsonPath("code").exists()) -// .andExpect(jsonPath("message").exists()) -// .andExpect(jsonPath("body",4).exists()) -// .andExpect(jsonPath("_links.self").exists()) -// .andDo(document("hakjum")); -// } -// -// @Test @Order(4) -// public void crawlJanghak() throws Exception { -// -// MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/janghak") -// .contentType(MediaType.APPLICATION_JSON) -// .accept(MediaTypes.HAL_JSON_VALUE) -// .header("Access-Control-Request-Method","GET") -// .header("Origin", "http://hello"); -// -// mockMvc.perform(builder) -// .andDo(print()) -// .andExpect(status().isOk()) -// .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) -// .andExpect(jsonPath("code").exists()) -// .andExpect(jsonPath("message").exists()) -// .andExpect(jsonPath("body",4).exists()) -// .andExpect(jsonPath("_links.self").exists()) -// .andDo(document("janghak")); -// } -// -// @Test @Order(5) -// public void crawlIlban() throws Exception { -// -// MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/ilban") -// .contentType(MediaType.APPLICATION_JSON) -// .accept(MediaTypes.HAL_JSON_VALUE) -// .header("Access-Control-Request-Method","GET") -// .header("Origin", "http://hello"); -// -// mockMvc.perform(builder) -// .andDo(print()) -// .andExpect(status().isOk()) -// .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) -// .andExpect(jsonPath("code").exists()) -// .andExpect(jsonPath("message").exists()) -// .andExpect(jsonPath("body",4).exists()) -// .andExpect(jsonPath("_links.self").exists()) -// .andDo(document("ilban")); -// } -// -// @Test @Order(6) -// public void crawlHangsa() throws Exception { -// -// MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/hangsa") -// .contentType(MediaType.APPLICATION_JSON) -// .accept(MediaTypes.HAL_JSON_VALUE) -// .header("Access-Control-Request-Method","GET") -// .header("Origin", "http://hello"); -// -// mockMvc.perform(builder) -// .andDo(print()) -// .andExpect(status().isOk()) -// .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) -// .andExpect(jsonPath("code").exists()) -// .andExpect(jsonPath("message").exists()) -// .andExpect(jsonPath("body",4).exists()) -// .andExpect(jsonPath("_links.self").exists()) -// .andDo(document("hangsa")); -// } -// -// -//} \ No newline at end of file +package kr.nutee.crawler.Controller; + +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.hateoas.MediaTypes; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + + +public class NoticeControllerTest extends BaseControllerTest { + + @Autowired + MockMvc mockMvc; + + @Test @Order(1) + public void crawlHaksa() throws Exception { + + String pageNo = "5"; +// PageNumber body = PageNumber.builder() +// .pageNo(pageNo) +// .build(); + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/haksa") + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaTypes.HAL_JSON_VALUE) + .header("Access-Control-Request-Method","GET") + .header("Origin", "http://nutee.kr:9709/crawl") + .param("page", "5"); +// .content(objectMapper.writeValueAsString(body)); + + mockMvc.perform(builder) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) + .andExpect(jsonPath("code").exists()) + .andExpect(jsonPath("message").exists()) + .andExpect(jsonPath("body",4).exists()) + .andExpect(jsonPath("_links.self").exists()) + .andDo(document("get-haksa")); + } + + @Test @Order(2) + public void crawlSooup() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/sooup") + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaTypes.HAL_JSON_VALUE) + .header("Access-Control-Request-Method","GET") + .header("Origin", "http://hello"); + + mockMvc.perform(builder) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) + .andExpect(jsonPath("code").exists()) + .andExpect(jsonPath("message").exists()) + .andExpect(jsonPath("body",4).exists()) + .andExpect(jsonPath("_links.self").exists()) + .andDo(document("get-sooup")); + } + + @Test @Order(3) + public void crawlHakjum() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/hakjum") + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaTypes.HAL_JSON_VALUE) + .header("Access-Control-Request-Method","GET") + .header("Origin", "http://hello"); + + mockMvc.perform(builder) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) + .andExpect(jsonPath("code").exists()) + .andExpect(jsonPath("message").exists()) + .andExpect(jsonPath("body",4).exists()) + .andExpect(jsonPath("_links.self").exists()) + .andDo(document("get-hakjum")); + } + + @Test @Order(4) + public void crawlJanghak() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/janghak") + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaTypes.HAL_JSON_VALUE) + .header("Access-Control-Request-Method","GET") + .header("Origin", "http://hello"); + + mockMvc.perform(builder) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) + .andExpect(jsonPath("code").exists()) + .andExpect(jsonPath("message").exists()) + .andExpect(jsonPath("body",4).exists()) + .andExpect(jsonPath("_links.self").exists()) + .andDo(document("get-janghak")); + } + + @Test @Order(5) + public void crawlIlban() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/ilban") + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaTypes.HAL_JSON_VALUE) + .header("Access-Control-Request-Method","GET") + .header("Origin", "http://hello"); + + mockMvc.perform(builder) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) + .andExpect(jsonPath("code").exists()) + .andExpect(jsonPath("message").exists()) + .andExpect(jsonPath("body",4).exists()) + .andExpect(jsonPath("_links.self").exists()) + .andDo(document("get-ilban")); + } + + @Test @Order(6) + public void crawlHangsa() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/crawl/hangsa") + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaTypes.HAL_JSON_VALUE) + .header("Access-Control-Request-Method","GET") + .header("Origin", "http://hello"); + + mockMvc.perform(builder) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE + ";charset=UTF-8")) + .andExpect(jsonPath("code").exists()) + .andExpect(jsonPath("message").exists()) + .andExpect(jsonPath("body",4).exists()) + .andExpect(jsonPath("_links.self").exists()) + .andDo(document("get-hangsa")); + } + + +} \ No newline at end of file diff --git a/src/test/java/kr/nutee/crawler/Controller/SchedulerService.java b/src/test/java/kr/nutee/crawler/Controller/SchedulerService.java index 6b7d2f1..fb3abaa 100644 --- a/src/test/java/kr/nutee/crawler/Controller/SchedulerService.java +++ b/src/test/java/kr/nutee/crawler/Controller/SchedulerService.java @@ -1,32 +1,32 @@ -//package kr.nutee.crawler.Controller; -// -//import org.junit.Test; -//import org.springframework.scheduling.annotation.Scheduled; -//import org.springframework.stereotype.Component; -// -//import java.text.DateFormat; -//import java.text.SimpleDateFormat; -//import java.util.Calendar; -// -//@Component -//public class SchedulerService { -// -// // @Scheduled(cron = "0 0 20 * * *") -// @Test -// @Scheduled(fixedDelay = 10000) -// public void test()/* throws IOException*/ { -// DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); -// CrwalingTest craw = new CrwalingTest(); -//// List> list = craw.getPageData(); -// -// String today = format.format(Calendar.getInstance().getTime()); -//// String newPage = list.get(0).get("date"); -// System.out.println("Test시작"); -// System.out.println(today); -//// System.out.println(newPage); -// -//// if(today.equals(newPage)) { -//// System.out.println("hello"); -//// } -// } -//} +package kr.nutee.crawler.Controller; + +import org.junit.Test; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; + +@Component +public class SchedulerService { + + // @Scheduled(cron = "0 0 20 * * *") + @Test + @Scheduled(fixedDelay = 10000) + public void test()/* throws IOException*/ { + DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + CrwalingTest craw = new CrwalingTest(); +// List> list = craw.getPageData(); + + String today = format.format(Calendar.getInstance().getTime()); +// String newPage = list.get(0).get("date"); + System.out.println("Test시작"); + System.out.println(today); +// System.out.println(newPage); + +// if(today.equals(newPage)) { +// System.out.println("hello"); +// } + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..c09c39d --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,26 @@ +server: + port: 9709 + servlet: + encoding: + charset: UTF-8 + enabled: true + force: true + + +spring: + #MYSQL CONFIG + datasource: + url: jdbc:mysql://nutee.cmwlqxe5se7d.ap-northeast-2.rds.amazonaws.com:3306/nutee_crawl?useUnicode=true&characterEncoding=UTF-8 + username: nutee + password: since2012! + + jpa: + hibernate: + ddl-auto: update + properties: + hibernate: + format_sql: true + open-in-view: false + database-platform: org.hibernate.dialect.MySQL5InnoDBDialect + main: + allow-bean-definition-overriding: true \ No newline at end of file