Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public interface EgovAuthorGroupService {

void delete(AuthorGroupVO authorGroupVO, Map<String, String> userInfo);

void insertOrUpdateList(String[] userIds, String[] mberTyCodes, String[] authorCodes, String[] regYns, AuthorGroupVO authorGroupVO);

void deleteList(String[] userIds, AuthorGroupVO authorGroupVO, Map<String, String> userInfo);

}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,31 @@ public void delete(AuthorGroupVO authorGroupVO, Map<String, String> userInfo) {
repository.deleteById(scrtyDtrmnTrgetId);
}

@Transactional
@Override
public void insertOrUpdateList(String[] userIds, String[] mberTyCodes, String[] authorCodes, String[] regYns, AuthorGroupVO authorGroupVO) {
for (int i = 0; i < userIds.length; i++) {
authorGroupVO.setScrtyDtrmnTrgetId(userIds[i]);
authorGroupVO.setMberTyCode(mberTyCodes[i]);
authorGroupVO.setAuthorCode(authorCodes[i]);

if ("N".equals(regYns[i])) {
this.insert(authorGroupVO);
} else {
this.update(authorGroupVO);
}
}
}

@Transactional
@Override
public void deleteList(String[] userIds, AuthorGroupVO authorGroupVO, Map<String, String> userInfo) {
for (String userId : userIds) {
authorGroupVO.setScrtyDtrmnTrgetId(userId);
this.delete(authorGroupVO, userInfo);
}
}

private void requireAuthenticated(Map<String, String> userInfo) {
if (userInfo == null || ObjectUtils.isEmpty(userInfo.get("uniqId"))) {
throw new IllegalStateException("인증 정보가 없습니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,7 @@ public ResponseEntity<?> authorGroupInsert(
String[] strAuthorCodes = authorCode.split(";");
String[] strRegYns = regYns.split(";");

for (int i = 0; i < strUserIds.length; i++) {
authorGroupVO.setScrtyDtrmnTrgetId(strUserIds[i]);
authorGroupVO.setMberTyCode(strMberTyCodes[i]);
authorGroupVO.setAuthorCode(strAuthorCodes[i]);

if ("N".equals(strRegYns[i])) {
service.insert(authorGroupVO);
} else {
service.update(authorGroupVO);
}
}
service.insertOrUpdateList(strUserIds, strMberTyCodes, strAuthorCodes, strRegYns, authorGroupVO);

return ResponseEntity.ok().build();
}
Expand All @@ -95,10 +85,7 @@ public ResponseEntity<?> authorGroupInsert(
public ResponseEntity<?> authorGroupDelete(@RequestParam String userIds, @ModelAttribute AuthorGroupVO authorGroupVO, HttpServletRequest request) {
Map<String, String> userInfo = extracted(request);
String[] strUserIds = userIds.split(";");
for (String strUserId : strUserIds) {
authorGroupVO.setScrtyDtrmnTrgetId(strUserId);
service.delete(authorGroupVO, userInfo);
}
service.deleteList(strUserIds, authorGroupVO, userInfo);

return ResponseEntity.ok().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package egovframework.com.sec.rgm.service.impl;

import egovframework.com.sec.rgm.entity.Emplyrscrtyestbs;
import egovframework.com.sec.rgm.repository.EgovAuthorGroupRepository;
import egovframework.com.sec.rgm.service.AuthorGroupVO;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

/**
* authorGroupInsert·authorGroupDelete가 항목마다 서비스를 따로 호출하던 것을
* 서비스 메서드 하나로 옮겼다. 컨트롤러 루프가 매 항목마다 별도 트랜잭션을 여는
* insert·update·delete를 직접 호출해, 루프 중간에 예외가 나면 그 뒤 항목은
* 반영되지 않고 앞 항목만 커밋된 채 남을 수 있었다.
*/
class EgovAuthorGroupServiceImplBatchTest {

private final EgovAuthorGroupRepository repository = mock(EgovAuthorGroupRepository.class);
private final EgovAuthorGroupServiceImpl service = new EgovAuthorGroupServiceImpl(repository, null, null);

@Test
void insertOrUpdateListInsertsNewAndUpdatesExisting() {
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
Emplyrscrtyestbs existing = new Emplyrscrtyestbs();
existing.setScrtyDtrmnTrgetId("U02");
existing.setMberTyCode("USR");
existing.setAuthorCode("OLDCODE");
when(repository.findById("U02")).thenReturn(Optional.of(existing));

String[] userIds = {"U01", "U02"};
String[] mberTyCodes = {"USR", "USR"};
String[] authorCodes = {"A01", "A02"};
String[] regYns = {"N", "Y"};

service.insertOrUpdateList(userIds, mberTyCodes, authorCodes, regYns, new AuthorGroupVO());

ArgumentCaptor<Emplyrscrtyestbs> saved = ArgumentCaptor.forClass(Emplyrscrtyestbs.class);
verify(repository, times(2)).save(saved.capture());
assertThat(saved.getAllValues())
.extracting(Emplyrscrtyestbs::getScrtyDtrmnTrgetId)
.containsExactly("U01", "U02");
assertThat(saved.getAllValues().get(1).getAuthorCode()).isEqualTo("A02");
}

@Test
void deleteListDeletesEachUserWithAuthenticatedCaller() {
String[] userIds = {"U01", "U02", "U03"};
Map<String, String> userInfo = new HashMap<>();
userInfo.put("uniqId", "admin1");

service.deleteList(userIds, new AuthorGroupVO(), userInfo);

verify(repository, times(1)).deleteById("U01");
verify(repository, times(1)).deleteById("U02");
verify(repository, times(1)).deleteById("U03");
}
}
Loading