刘光辉
117 分钟以前 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package jnpf.elnController;
 
import jnpf.elnEntity.runtime.ElnRecordForms;
import jnpf.elnEntity.runtime.ElnRecordVos;
import jnpf.elnEntity.ElnDocumentFileVo;
import jnpf.elnService.ElnRecordBatchCreationService;
import jnpf.elnService.ElnRecordCreationService;
import jnpf.elnService.ElnRecordRecalculationService;
import jnpf.elnService.ElnRecordSharedFillService;
import jnpf.elnService.ElnRecordSourceSyncService;
import jnpf.elnService.ElnRecordFillTicketService;
import jnpf.util.context.SpringContext;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
 
public class ElnRecordControllerTest {
 
    @BeforeClass
    public static void setUpSpringContext() {
        SpringContext.setApplicationContext(new StaticApplicationContext());
    }
 
    @Test
    public void createsByTestWorkItemAndOperationWithoutAcceptingTemplateId() throws Exception {
        AtomicReference<ElnRecordForms.Create> captured = new AtomicReference<>();
        ElnRecordVos.Created expected = new ElnRecordVos.Created();
        expected.setRecordId("record-1");
        expected.setRevisionId("revision-1");
        ElnDocumentFileVo document = new ElnDocumentFileVo();
        document.setFileId("revision-1.docx");
        expected.setDocument(document);
        ElnRecordCreationService service = command -> {
            captured.set(command);
            return expected;
        };
        ElnRecordRecalculationService recalculation = (recordId, command) -> null;
        ElnRecordController controller = controller(service, command -> null, recalculation, sourceSyncService());
        ElnRecordForms.Create form = new ElnRecordForms.Create();
        form.setTestWorkItemId("item-1");
        form.setOperationCode("click-1");
 
        assertSame(expected, controller.create(form).getData());
        assertSame(form, captured.get());
        assertEquals("item-1", captured.get().getTestWorkItemId());
        assertEquals("click-1", captured.get().getOperationCode());
        assertEquals("revision-1.docx", controller.create(form).getData().getDocument().getFileId());
 
        RequestMapping mapping = ElnRecordController.class.getAnnotation(RequestMapping.class);
        Method create = ElnRecordController.class.getMethod("create", ElnRecordForms.Create.class);
        assertEquals("/records", mapping.value()[0]);
        assertEquals(0, create.getAnnotation(PostMapping.class).value().length);
        assertEquals(2, ElnRecordForms.Create.class.getDeclaredFields().length);
    }
 
    @Test
    public void exposesProtectedFullFormulaRecalculationCommand() throws Exception {
        AtomicReference<String> recordId = new AtomicReference<>();
        AtomicReference<ElnRecordForms.Recalculate> captured = new AtomicReference<>();
        ElnRecordVos.Recalculated expected = new ElnRecordVos.Recalculated();
        ElnRecordRecalculationService recalculation = (id, command) -> {
            recordId.set(id);
            captured.set(command);
            return expected;
        };
        ElnRecordController controller = controller(command -> null, command -> null,
                recalculation, sourceSyncService());
        ElnRecordForms.Recalculate form = new ElnRecordForms.Recalculate();
        form.setExpectedRecordVersion(3);
        form.setOperationCode("recalculate-1");
 
        assertSame(expected, controller.recalculateAll("record-1", form).getData());
        assertEquals("record-1", recordId.get());
        assertSame(form, captured.get());
        Method method = ElnRecordController.class.getMethod(
                "recalculateAll", String.class, ElnRecordForms.Recalculate.class);
        assertEquals("/{recordId}/formulas/recalculate",
                method.getAnnotation(PostMapping.class).value()[0]);
        assertEquals("recordId", method.getParameters()[0]
                .getAnnotation(PathVariable.class).value());
    }
 
    @Test
    public void exposesSourceSyncPreviewAndConfirmationCommands() throws Exception {
        AtomicReference<String> previewRecordId = new AtomicReference<>();
        AtomicReference<String> syncRecordId = new AtomicReference<>();
        AtomicReference<ElnRecordForms.SourceSync> captured = new AtomicReference<>();
        ElnRecordVos.SourceSyncPreview preview = new ElnRecordVos.SourceSyncPreview();
        ElnRecordVos.SourceSynchronized synchronizedResult =
                new ElnRecordVos.SourceSynchronized();
        ElnRecordSourceSyncService sourceSync = new ElnRecordSourceSyncService() {
            @Override
            public ElnRecordVos.SourceSyncPreview preview(String recordId) {
                previewRecordId.set(recordId);
                return preview;
            }
 
            @Override
            public ElnRecordVos.SourceSyncPreview preview(
                    ElnRecordFillTicketService.FillTicketContext ticket) {
                return null;
            }
 
            @Override
            public ElnRecordVos.SourceSynchronized synchronize(
                    String recordId, ElnRecordForms.SourceSync command) {
                syncRecordId.set(recordId);
                captured.set(command);
                return synchronizedResult;
            }
 
            @Override
            public ElnRecordVos.SourceSynchronized synchronize(
                    ElnRecordFillTicketService.FillTicketContext ticket,
                    ElnRecordForms.SourceSync command) {
                return null;
            }
        };
        ElnRecordController controller = controller(command -> null, command -> null,
                (recordId, command) -> null, sourceSync);
        ElnRecordForms.SourceSync form = new ElnRecordForms.SourceSync();
        form.setExpectedRecordVersion(4);
        form.setPreviewToken("preview-token");
        form.setOperationCode("sync-1");
 
        assertSame(preview, controller.previewSourceSync("record-1").getData());
        assertSame(synchronizedResult,
                controller.synchronizeSource("record-1", form).getData());
        assertEquals("record-1", previewRecordId.get());
        assertEquals("record-1", syncRecordId.get());
        assertSame(form, captured.get());
        Method previewMethod = ElnRecordController.class.getMethod(
                "previewSourceSync", String.class);
        Method syncMethod = ElnRecordController.class.getMethod(
                "synchronizeSource", String.class, ElnRecordForms.SourceSync.class);
        assertEquals("/{recordId}/sourceSync/preview",
                previewMethod.getAnnotation(PostMapping.class).value()[0]);
        assertEquals("/{recordId}/sourceSync",
                syncMethod.getAnnotation(PostMapping.class).value()[0]);
    }
 
    @Test
    public void exposesBatchCreateWithoutAcceptingTemplateId() throws Exception {
        AtomicReference<ElnRecordForms.BatchCreate> captured = new AtomicReference<>();
        ElnRecordVos.BatchCreated expected = new ElnRecordVos.BatchCreated();
        ElnRecordBatchCreationService batchService = command -> {
            captured.set(command);
            return expected;
        };
        ElnRecordController controller = controller(command -> null, batchService,
                (recordId, command) -> null, sourceSyncService());
        ElnRecordForms.BatchCreate form = new ElnRecordForms.BatchCreate();
        form.setTestWorkItemIds(java.util.Arrays.asList("item-1", "item-2"));
        form.setOperationCode("click-1");
 
        assertSame(expected, controller.createBatch(form).getData());
        assertSame(form, captured.get());
        Method method = ElnRecordController.class.getMethod("createBatch", ElnRecordForms.BatchCreate.class);
        assertEquals("/batch", method.getAnnotation(PostMapping.class).value()[0]);
        assertEquals(2, ElnRecordForms.BatchCreate.class.getDeclaredFields().length);
    }
 
    @Test
    public void exposesSharedFillPreviewThroughTheSharedFillService() throws Exception {
        AtomicReference<ElnRecordForms.SharedFillPreview> captured = new AtomicReference<>();
        ElnRecordVos.SharedFillPreview expected = new ElnRecordVos.SharedFillPreview();
        ElnRecordSharedFillService sharedFill = command -> {
            captured.set(command);
            return expected;
        };
        ElnRecordController controller = new ElnRecordController(
                command -> null, command -> null, (recordId, command) -> null,
                sourceSyncService(), sharedFill);
        ElnRecordForms.SharedFillPreview form = new ElnRecordForms.SharedFillPreview();
        form.setTestWorkItemIds(java.util.Arrays.asList("item-1", "item-2"));
 
        assertSame(expected, controller.previewSharedFill(form).getData());
        assertSame(form, captured.get());
        Method method = ElnRecordController.class.getMethod(
                "previewSharedFill", ElnRecordForms.SharedFillPreview.class);
        assertEquals("/sharedFill/preview",
                method.getAnnotation(PostMapping.class).value()[0]);
    }
 
    @Test
    public void exposesSharedFillExecutionThroughTheSharedFillService() throws Exception {
        AtomicReference<ElnRecordForms.SharedFillExecute> captured = new AtomicReference<>();
        ElnRecordVos.SharedFilled expected = new ElnRecordVos.SharedFilled();
        ElnRecordSharedFillService sharedFill = new ElnRecordSharedFillService() {
            @Override
            public ElnRecordVos.SharedFillPreview preview(
                    ElnRecordForms.SharedFillPreview command) {
                return null;
            }
 
            @Override
            public ElnRecordVos.SharedFilled execute(
                    ElnRecordForms.SharedFillExecute command) {
                captured.set(command);
                return expected;
            }
        };
        ElnRecordController controller = new ElnRecordController(
                command -> null, command -> null, (recordId, command) -> null,
                sourceSyncService(), sharedFill);
        ElnRecordForms.SharedFillExecute form = new ElnRecordForms.SharedFillExecute();
        form.setOperationCode("shared-fill-1");
 
        assertSame(expected, controller.executeSharedFill(form).getData());
        assertSame(form, captured.get());
        Method method = ElnRecordController.class.getMethod(
                "executeSharedFill", ElnRecordForms.SharedFillExecute.class);
        assertEquals("/sharedFill/execute",
                method.getAnnotation(PostMapping.class).value()[0]);
    }
 
    private static ElnRecordSourceSyncService sourceSyncService() {
        return new ElnRecordSourceSyncService() {
            @Override
            public ElnRecordVos.SourceSyncPreview preview(String recordId) {
                return null;
            }
 
            @Override
            public ElnRecordVos.SourceSyncPreview preview(
                    ElnRecordFillTicketService.FillTicketContext ticket) {
                return null;
            }
 
            @Override
            public ElnRecordVos.SourceSynchronized synchronize(
                    String recordId, ElnRecordForms.SourceSync command) {
                return null;
            }
 
            @Override
            public ElnRecordVos.SourceSynchronized synchronize(
                    ElnRecordFillTicketService.FillTicketContext ticket,
                    ElnRecordForms.SourceSync command) {
                return null;
            }
        };
    }
 
    private static ElnRecordController controller(
            ElnRecordCreationService creationService,
            ElnRecordBatchCreationService batchCreationService,
            ElnRecordRecalculationService recalculationService,
            ElnRecordSourceSyncService sourceSyncService) {
        return new ElnRecordController(creationService, batchCreationService,
                recalculationService, sourceSyncService, command -> null);
    }
}