Skip to content

Commit

Permalink
test: 스레딩을 사용한 공지 업데이트 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
zbqmgldjfh committed Jan 14, 2024
1 parent d2672fd commit 36f8d41
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.kustacks.kuring.worker.update;

import com.kustacks.kuring.message.firebase.FirebaseService;
import com.kustacks.kuring.notice.domain.Notice;
import com.kustacks.kuring.notice.domain.NoticeRepository;
import com.kustacks.kuring.worker.scrap.KuisNoticeScraperTemplate;
import com.kustacks.kuring.worker.scrap.client.notice.LibraryNoticeApiClient;
import com.kustacks.kuring.worker.update.notice.CategoryNoticeUpdater;
import com.kustacks.kuring.worker.update.notice.dto.response.CommonNoticeFormatDto;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.TestPropertySource;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;


@SpringBootTest
@TestPropertySource(properties = "spring.config.location=" +
"classpath:/application.yml" +
",classpath:/application-test.yml" +
",classpath:/test-constants.properties")
public class CategoryNoticeUpdaterTest {

@MockBean
KuisNoticeScraperTemplate scrapperTemplate;

@MockBean
FirebaseService firebaseService;

@MockBean
LibraryNoticeApiClient libraryNoticeApiClient;

@Autowired
CategoryNoticeUpdater categoryNoticeUpdater;

@Autowired
ThreadPoolTaskExecutor noticeUpdaterThreadTaskExecutor;

@Autowired
NoticeRepository noticeRepository;

@DisplayName("공지 업데이트 테스트")
@Test
public void notice_scrap_async_test() throws InterruptedException {
// given
doReturn(createNoticesFixture()).when(scrapperTemplate).scrap(any(), any());
doReturn(createLibraryFixture()).when(libraryNoticeApiClient).request(any());
doNothing().when(firebaseService).sendNotificationList(anyList());

// when
categoryNoticeUpdater.update();
noticeUpdaterThreadTaskExecutor.getThreadPoolExecutor().awaitTermination(1, TimeUnit.SECONDS);
List<Notice> notices = noticeRepository.findAll();

// then
assertAll(
() -> Assertions.assertThat(notices).filteredOn("categoryName", "library").size().isEqualTo(7),
() -> Assertions.assertThat(notices).filteredOn("categoryName", "bachelor").size().isEqualTo(9),
() -> Assertions.assertThat(notices).filteredOn("categoryName", "scholarship").size().isEqualTo(9),
() -> Assertions.assertThat(notices).filteredOn("categoryName", "employment").size().isEqualTo(9),
() -> Assertions.assertThat(notices).filteredOn("categoryName", "national").size().isEqualTo(9),
() -> Assertions.assertThat(notices).filteredOn("categoryName", "student").size().isEqualTo(9),
() -> Assertions.assertThat(notices).filteredOn("categoryName", "industry_university").size().isEqualTo(9),
() -> Assertions.assertThat(notices).filteredOn("categoryName", "normal").size().isEqualTo(9)
);
}

private static List<CommonNoticeFormatDto> createLibraryFixture() {
return List.of(
new CommonNoticeFormatDto("1", "2021-01-01", "library1", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71921", false),
new CommonNoticeFormatDto("2", "2021-01-01", "library2", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71922", false),
new CommonNoticeFormatDto("3", "2021-01-01", "library3", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71923", false),
new CommonNoticeFormatDto("4", "2021-01-01", "library4", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71924", false),
new CommonNoticeFormatDto("5", "2021-01-01", "library5", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71925", false),
new CommonNoticeFormatDto("6", "2021-01-01", "library6", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71926", false),
new CommonNoticeFormatDto("7", "2021-01-01", "library7", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71927", false)
);
}

private static List<CommonNoticeFormatDto> createNoticesFixture() {
return List.of(
new CommonNoticeFormatDto("1", "2021-01-01", "subject1", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71921", false),
new CommonNoticeFormatDto("2", "2021-01-01", "subject2", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71922", false),
new CommonNoticeFormatDto("3", "2021-01-01", "subject3", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71923", false),
new CommonNoticeFormatDto("4", "2021-01-01", "subject4", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71924", false),
new CommonNoticeFormatDto("5", "2021-01-01", "subject5", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71925", false),
new CommonNoticeFormatDto("6", "2021-01-01", "subject6", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71926", false),
new CommonNoticeFormatDto("7", "2021-01-01", "subject7", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71927", false),
new CommonNoticeFormatDto("8", "2021-01-01", "subject8", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71928", false),
new CommonNoticeFormatDto("9", "2021-01-01", "subject9", "2021-01-01", "https://library.konkuk.ac.kr/library-guide/bulletins/notice/71929", false)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.kustacks.kuring.worker.update;

import com.kustacks.kuring.message.firebase.FirebaseService;
import com.kustacks.kuring.notice.domain.DepartmentNotice;
import com.kustacks.kuring.notice.domain.DepartmentNoticeRepository;
import com.kustacks.kuring.worker.scrap.DepartmentNoticeScraperTemplate;
import com.kustacks.kuring.worker.scrap.dto.ComplexNoticeFormatDto;
import com.kustacks.kuring.worker.update.notice.DepartmentNoticeUpdater;
import com.kustacks.kuring.worker.update.notice.dto.response.CommonNoticeFormatDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.TestPropertySource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;


@SpringBootTest
@TestPropertySource(properties = "spring.config.location=" +
"classpath:/application.yml" +
",classpath:/application-test.yml" +
",classpath:/test-constants.properties")
public class DepartmentNoticeUpdaterTest {

@MockBean
DepartmentNoticeScraperTemplate scrapperTemplate;

@MockBean
FirebaseService firebaseService;

@Autowired
DepartmentNoticeUpdater departmentNoticeUpdater;

@Autowired
ThreadPoolTaskExecutor noticeUpdaterThreadTaskExecutor;

@Autowired
DepartmentNoticeRepository departmentNoticeRepository;

@DisplayName("학과별 공지 업데이트 테스트")
@Test
public void department_scrap_async_test() throws InterruptedException {
// given
doReturn(createDepartmentNoticesFixture()).when(scrapperTemplate).scrap(any(), any());
doNothing().when(firebaseService).sendNotificationList(anyList());

// when
departmentNoticeUpdater.update();
noticeUpdaterThreadTaskExecutor.getThreadPoolExecutor().awaitTermination(3, TimeUnit.SECONDS);
List<DepartmentNotice> notices = departmentNoticeRepository.findAll();


// then
assertThat(notices.size()).isEqualTo(3720);
}

private static List<ComplexNoticeFormatDto> createDepartmentNoticesFixture() {
List<ComplexNoticeFormatDto> result = new ArrayList<>();
List<CommonNoticeFormatDto> importantNoticeList = new ArrayList<>();
List<CommonNoticeFormatDto> normalNoticeList = new ArrayList<>();

for(int i = 0; i < 30; i++) {
importantNoticeList.add(new CommonNoticeFormatDto(String.valueOf(i), "2021-01-01",
"important" + i, "2021-01-01",
"https://library.konkuk.ac.kr/library-guide/bulletins/important/71921",
true));

normalNoticeList.add(new CommonNoticeFormatDto(String.valueOf(i), "2021-01-01",
"normal" + i, "2021-01-01",
"https://library.konkuk.ac.kr/library-guide/bulletins/normal/71921",
false));
}

result.add(new ComplexNoticeFormatDto(importantNoticeList, normalNoticeList));
return result;
}
}
45 changes: 45 additions & 0 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
spring:
datasource:
url: jdbc:tc:mariadb:10.2:///kuring?TC_REUSABLE=true
username: test # 테스트컨테이너 용이라 노출되도 상관없다
password: test1234
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
p6spy:
enable-logging: true
flyway:
enabled: false
jpa:
hibernate:
ddl-auto: create
show-sql: true
properties:
hibernate:
format_sql: true
dialect: com.kustacks.kuring.config.CustomMariaDbDialect
defer-datasource-initialization: true
open-in-view: false
thymeleaf:
cache: true
enabled: true
prefix: classpath:/templates/
suffix: .html
jackson:
serialization:
FAIL_ON_EMPTY_BEANS: false

testcontainers:
reuse:
enable: true

sentry:
dsn: ""

logging:
level:
p6spy: debug

security:
jwt:
token:
secret-key: test-secret-key-test-secret-key-test-secret-key-test-secret-key
expire-length: 3600000
46 changes: 2 additions & 44 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,3 @@
spring:
datasource:
url: jdbc:tc:mariadb:10.2:///kuring?TC_REUSABLE=true
username: test # 테스트컨테이너 용이라 노출되도 상관없다
password: test1234
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
p6spy:
enable-logging: true
flyway:
enabled: false
jpa:
hibernate:
ddl-auto: create
show-sql: true
properties:
hibernate:
format_sql: true
dialect: com.kustacks.kuring.config.CustomMariaDbDialect
defer-datasource-initialization: true
open-in-view: false
thymeleaf:
cache: true
enabled: true
prefix: classpath:/templates/
suffix: .html
jackson:
serialization:
FAIL_ON_EMPTY_BEANS: false

testcontainers:
reuse:
enable: true

sentry:
dsn: ""

logging:
level:
p6spy: debug

security:
jwt:
token:
secret-key: test-secret-key-test-secret-key-test-secret-key-test-secret-key
expire-length: 3600000
profiles:
active: test

0 comments on commit 36f8d41

Please sign in to comment.