feat: 시트 업로드 시드 서비스 추가
@fb32720244f1c6b49b3be1225e83d024c456b55a
+++ src/main/java/kr/itn/itnhub/seed/SeedReport.java
... | ... | @@ -0,0 +1,4 @@ |
| 1 | +package kr.itn.itnhub.seed; | |
| 2 | + | |
| 3 | +public record SeedReport(int created, int updated, int total) { | |
| 4 | +} |
+++ src/main/java/kr/itn/itnhub/seed/SeedService.java
... | ... | @@ -0,0 +1,64 @@ |
| 1 | +package kr.itn.itnhub.seed; | |
| 2 | + | |
| 3 | +import kr.itn.itnhub.org.Organization; | |
| 4 | +import kr.itn.itnhub.org.OrganizationMapper; | |
| 5 | +import org.springframework.stereotype.Service; | |
| 6 | +import org.springframework.transaction.annotation.Transactional; | |
| 7 | + | |
| 8 | +import java.io.InputStream; | |
| 9 | +import java.util.HashSet; | |
| 10 | +import java.util.List; | |
| 11 | +import java.util.Set; | |
| 12 | + | |
| 13 | +@Service | |
| 14 | +public class SeedService { | |
| 15 | + | |
| 16 | + private final SeedParser parser; | |
| 17 | + private final OrganizationMapper mapper; | |
| 18 | + | |
| 19 | + public SeedService(SeedParser parser, OrganizationMapper mapper) { | |
| 20 | + this.parser = parser; | |
| 21 | + this.mapper = mapper; | |
| 22 | + } | |
| 23 | + | |
| 24 | + @Transactional | |
| 25 | + public SeedReport seed(InputStream xlsx) { | |
| 26 | + List<SeedRow> rows = parser.parse(xlsx); | |
| 27 | + | |
| 28 | + Set<String> before = new HashSet<>(); | |
| 29 | + for (Organization existing : mapper.findAll()) { | |
| 30 | + before.add(key(existing.getOrgNo(), existing.getOrgName())); | |
| 31 | + } | |
| 32 | + | |
| 33 | + int created = 0; | |
| 34 | + int updated = 0; | |
| 35 | + | |
| 36 | + for (SeedRow row : rows) { | |
| 37 | + if (before.contains(key(row.orgNo(), row.orgName()))) { | |
| 38 | + updated++; | |
| 39 | + } else { | |
| 40 | + created++; | |
| 41 | + } | |
| 42 | + mapper.upsertBySeed(toOrganization(row)); | |
| 43 | + } | |
| 44 | + | |
| 45 | + return new SeedReport(created, updated, rows.size()); | |
| 46 | + } | |
| 47 | + | |
| 48 | + private String key(String orgNo, String orgName) { | |
| 49 | + return orgNo + " " + orgName; | |
| 50 | + } | |
| 51 | + | |
| 52 | + private Organization toOrganization(SeedRow row) { | |
| 53 | + Organization org = new Organization(); | |
| 54 | + org.setOrgNo(row.orgNo()); | |
| 55 | + org.setOrgName(row.orgName()); | |
| 56 | + org.setChannelSlug(row.channelSlug()); | |
| 57 | + org.setDeptName(row.deptName()); | |
| 58 | + org.setManagerName(row.managerName()); | |
| 59 | + org.setManagerTitle(row.managerTitle()); | |
| 60 | + org.setManagerPhone(row.managerPhone()); | |
| 61 | + org.setManagerEmail(row.managerEmail()); | |
| 62 | + return org; | |
| 63 | + } | |
| 64 | +} |
+++ src/test/java/kr/itn/itnhub/seed/SeedServiceTest.java
... | ... | @@ -0,0 +1,117 @@ |
| 1 | +package kr.itn.itnhub.seed; | |
| 2 | + | |
| 3 | +import kr.itn.itnhub.AbstractDbTest; | |
| 4 | +import kr.itn.itnhub.org.OrgStatus; | |
| 5 | +import kr.itn.itnhub.org.Organization; | |
| 6 | +import kr.itn.itnhub.org.OrganizationMapper; | |
| 7 | +import org.apache.poi.ss.usermodel.Row; | |
| 8 | +import org.apache.poi.ss.usermodel.Sheet; | |
| 9 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
| 10 | +import org.junit.jupiter.api.BeforeEach; | |
| 11 | +import org.junit.jupiter.api.Test; | |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | +import org.springframework.jdbc.core.JdbcTemplate; | |
| 14 | + | |
| 15 | +import java.io.ByteArrayInputStream; | |
| 16 | +import java.io.ByteArrayOutputStream; | |
| 17 | +import java.util.List; | |
| 18 | + | |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 20 | + | |
| 21 | +class SeedServiceTest extends AbstractDbTest { | |
| 22 | + | |
| 23 | + @Autowired | |
| 24 | + SeedService seedService; | |
| 25 | + | |
| 26 | + @Autowired | |
| 27 | + OrganizationMapper mapper; | |
| 28 | + | |
| 29 | + @Autowired | |
| 30 | + JdbcTemplate jdbc; | |
| 31 | + | |
| 32 | + @BeforeEach | |
| 33 | + void clean() { | |
| 34 | + jdbc.update("delete from organization"); | |
| 35 | + } | |
| 36 | + | |
| 37 | + private byte[] workbook(String[]... dataRows) throws Exception { | |
| 38 | + try (XSSFWorkbook wb = new XSSFWorkbook(); | |
| 39 | + ByteArrayOutputStream out = new ByteArrayOutputStream()) { | |
| 40 | + | |
| 41 | + Sheet sheet = wb.createSheet(SeedParser.SHEET_NAME); | |
| 42 | + sheet.createRow(0); | |
| 43 | + sheet.createRow(1); | |
| 44 | + | |
| 45 | + int rowNum = 2; | |
| 46 | + for (String[] data : dataRows) { | |
| 47 | + Row row = sheet.createRow(rowNum++); | |
| 48 | + row.createCell(1).setCellValue(data[0]); | |
| 49 | + row.createCell(2).setCellValue(data[1]); | |
| 50 | + row.createCell(5).setCellValue(data[2]); | |
| 51 | + if (data[3] != null) row.createCell(6).setCellValue(data[3]); | |
| 52 | + if (data[4] != null) row.createCell(7).setCellValue(data[4]); | |
| 53 | + if (data[5] != null) row.createCell(8).setCellValue(data[5]); | |
| 54 | + if (data[6] != null) row.createCell(9).setCellValue(data[6]); | |
| 55 | + if (data[7] != null) row.createCell(10).setCellValue(data[7]); | |
| 56 | + } | |
| 57 | + wb.write(out); | |
| 58 | + return out.toByteArray(); | |
| 59 | + } | |
| 60 | + } | |
| 61 | + | |
| 62 | + @Test | |
| 63 | + void 최초_시드는_전부_신규다() throws Exception { | |
| 64 | + byte[] xlsx = workbook( | |
| 65 | + new String[]{"001", "00", "국제방송교류재단", "팀", "송민지", "과장", "02-1", "a@b.kr"}, | |
| 66 | + new String[]{"002", "00", "세종학당재단", "팀", "권유나", "6급", "02-2", "c@d.kr"}); | |
| 67 | + | |
| 68 | + SeedReport report = seedService.seed(new ByteArrayInputStream(xlsx)); | |
| 69 | + | |
| 70 | + assertThat(report.created()).isEqualTo(2); | |
| 71 | + assertThat(report.updated()).isZero(); | |
| 72 | + assertThat(report.total()).isEqualTo(2); | |
| 73 | + assertThat(mapper.findAll()).hasSize(2); | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Test | |
| 77 | + void 같은_파일을_다시_올려도_행이_늘지_않는다() throws Exception { | |
| 78 | + byte[] xlsx = workbook( | |
| 79 | + new String[]{"001", "00", "국제방송교류재단", "팀", "송민지", "과장", "02-1", "a@b.kr"}); | |
| 80 | + | |
| 81 | + seedService.seed(new ByteArrayInputStream(xlsx)); | |
| 82 | + SeedReport second = seedService.seed(new ByteArrayInputStream(xlsx)); | |
| 83 | + | |
| 84 | + assertThat(second.created()).isZero(); | |
| 85 | + assertThat(second.updated()).isEqualTo(1); | |
| 86 | + assertThat(mapper.findAll()).hasSize(1); | |
| 87 | + } | |
| 88 | + | |
| 89 | + @Test | |
| 90 | + void 재시드는_이미_만든_채널ID를_보존한다() throws Exception { | |
| 91 | + byte[] xlsx = workbook( | |
| 92 | + new String[]{"001", "00", "국제방송교류재단", "팀", "송민지", "과장", "02-1", "a@b.kr"}); | |
| 93 | + | |
| 94 | + seedService.seed(new ByteArrayInputStream(xlsx)); | |
| 95 | + Long id = mapper.findAll().get(0).getId(); | |
| 96 | + mapper.updateChannelIdMj(id, "chan-mj"); | |
| 97 | + mapper.updateChannelIdLaw(id, "chan-law"); | |
| 98 | + | |
| 99 | + seedService.seed(new ByteArrayInputStream(xlsx)); | |
| 100 | + | |
| 101 | + assertThat(mapper.findById(id).getStatus()).isEqualTo(OrgStatus.ACTIVE); | |
| 102 | + } | |
| 103 | + | |
| 104 | + @Test | |
| 105 | + void 담당자정보가_없는_기관은_정보대기로_들어온다() throws Exception { | |
| 106 | + byte[] xlsx = workbook( | |
| 107 | + new String[]{"008", "00", "경찰청", "데이터정책계", "박진우", "경위", "02-3", "e@f.kr"}, | |
| 108 | + new String[]{"008", "01", "경찰청_치안정책연구소", null, null, null, null, null}); | |
| 109 | + | |
| 110 | + seedService.seed(new ByteArrayInputStream(xlsx)); | |
| 111 | + | |
| 112 | + List<Organization> all = mapper.findAll(); | |
| 113 | + assertThat(all).hasSize(2); | |
| 114 | + assertThat(all).extracting(Organization::getStatus) | |
| 115 | + .containsExactly(OrgStatus.READY, OrgStatus.INFO_PENDING); | |
| 116 | + } | |
| 117 | +} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?