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 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 recordId = new AtomicReference<>(); AtomicReference 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 previewRecordId = new AtomicReference<>(); AtomicReference syncRecordId = new AtomicReference<>(); AtomicReference 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 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 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 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); } }