刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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<String> 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));
        }
    }
}