package jnpf.elnService.impl; import jnpf.elnEntity.runtime.ElnRecordForms; import jnpf.elnEntity.runtime.ElnRecordVos; import jnpf.elnService.ElnRecordCreationService; import jnpf.exception.DataException; import org.junit.Test; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; public class ElnRecordBatchCreationServiceImplTest { @Test public void suspendsAnyOuterTransactionBeforeInvokingSingleRecordCreation() throws Exception { Method method = ElnRecordBatchCreationServiceImpl.class.getMethod( "create", ElnRecordForms.BatchCreate.class); assertEquals(Propagation.NOT_SUPPORTED, method.getAnnotation(Transactional.class).propagation()); } @Test public void createsEachUniqueItemInRequestOrderAndKeepsFailuresIndependent() { List invoked = new ArrayList<>(); ElnRecordCreationService creationService = command -> { invoked.add(command.getTestWorkItemId() + ":" + command.getOperationCode()); if ("item-2".equals(command.getTestWorkItemId())) { throw new DataException("检验项无可用模板"); } if ("item-3".equals(command.getTestWorkItemId())) { throw new IllegalStateException("database connection detail"); } return created(command.getTestWorkItemId(), false); }; ElnRecordBatchCreationServiceImpl service = new ElnRecordBatchCreationServiceImpl(creationService); ElnRecordVos.BatchCreated result = service.create(batch("click-1", " item-1 ", "item-2", "item-1", "item-3", "item-4")); assertEquals(Arrays.asList("item-1:click-1", "item-2:click-1", "item-3:click-1", "item-4:click-1"), invoked); assertEquals(4, result.getItems().size()); assertEquals("CREATED", result.getItems().get(0).getStatus()); assertEquals("item-1", result.getItems().get(0).getCreated().getRecordId()); assertEquals("FAILED", result.getItems().get(1).getStatus()); assertEquals("BUSINESS_ERROR", result.getItems().get(1).getErrorCode()); assertEquals("检验项无可用模板", result.getItems().get(1).getErrorMessage()); assertEquals("FAILED", result.getItems().get(2).getStatus()); assertEquals("SYSTEM_ERROR", result.getItems().get(2).getErrorCode()); assertEquals("电子记录创建失败,请稍后重试", result.getItems().get(2).getErrorMessage()); assertEquals("CREATED", result.getItems().get(3).getStatus()); } @Test public void exposesExistingResultsFromTheSingleRecordService() { ElnRecordBatchCreationServiceImpl service = new ElnRecordBatchCreationServiceImpl( command -> created(command.getTestWorkItemId(), true)); ElnRecordVos.BatchCreated result = service.create(batch("click-1", "item-1")); assertEquals("EXISTING", result.getItems().get(0).getStatus()); assertTrue(result.getItems().get(0).getCreated().isExisting()); } @Test public void rejectsEmptySelectionAndEmptyItemBeforeCallingTheSingleRecordService() { ElnRecordBatchCreationServiceImpl service = new ElnRecordBatchCreationServiceImpl( command -> { fail("不应调用单项创建服务"); return null; }); expectDataException(() -> service.create(batch("click-1")), "检验项不能为空"); expectDataException(() -> service.create(batch("click-1", "item-1", " ")), "检验项不能为空"); expectDataException(() -> service.create(batch(" ", "item-1")), "操作码不能为空"); } private static ElnRecordForms.BatchCreate batch(String operationCode, String... testWorkItemIds) { ElnRecordForms.BatchCreate command = new ElnRecordForms.BatchCreate(); command.setOperationCode(operationCode); command.setTestWorkItemIds(Arrays.asList(testWorkItemIds)); return command; } private static ElnRecordVos.Created created(String id, boolean existing) { ElnRecordVos.Created created = new ElnRecordVos.Created(); created.setRecordId(id); created.setExisting(existing); return created; } private static void expectDataException(Runnable action, String message) { try { action.run(); fail("预期抛出 DataException"); } catch (DataException expected) { assertTrue(expected.getMessage().contains(message)); } } }