package jnpf.elnService.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import jnpf.elnEntity.runtime.ElnRecordVos; import jnpf.elnEntity.runtime.ElnSharedFillOperationEntity; import jnpf.elnMapper.ElnSharedFillOperationMapper; import jnpf.util.JsonUtil; import org.junit.Test; import java.util.Collections; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class ElnRecordSourceSyncOperationServiceTest { @Test public void persistsPendingDocumentResultAndConfirmsItIdempotently() { ElnSharedFillOperationMapper operations = mock(ElnSharedFillOperationMapper.class); ElnSharedFillOperationEntity open = operation("OPEN", null); when(operations.insertIgnore(any(ElnSharedFillOperationEntity.class))).thenReturn(1); when(operations.selectOne(any(Wrapper.class))).thenReturn(open); when(operations.prepareResult(any(String.class), any(String.class), any(String.class), any(String.class), any(String.class))).thenReturn(1); when(operations.confirmPrepared(any(String.class), any(String.class), any(String.class))).thenReturn(1, 0); ElnRecordSourceSyncOperationService service = new ElnRecordSourceSyncOperationService(operations); ElnRecordVos.SourceSynchronized result = new ElnRecordVos.SourceSynchronized(); result.setOperationCode("sync-1"); result.setChangedFieldCodes(Collections.singletonList("source")); result.setRecordId("record-1"); assertNull(service.begin("tenant-1", "user-1", "sync-1", "digest-1")); assertSame(result, service.prepare( "tenant-1", "user-1", "sync-1", "digest-1", result)); ElnSharedFillOperationEntity pending = operation( "OPEN", JsonUtil.getObjectToString(result)); when(operations.insertIgnore(any(ElnSharedFillOperationEntity.class))).thenReturn(0); when(operations.selectOne(any(Wrapper.class))).thenReturn(pending); when(operations.selectList(any(Wrapper.class))) .thenReturn(Collections.singletonList(pending)); assertEquals(Collections.singletonList("source"), service.begin( "tenant-1", "user-1", "sync-1", "digest-1").getChangedFieldCodes()); assertEquals("sync-1", service.pending( "tenant-1", "user-1", "record-1").getOperationCode()); assertTrue(service.hasPendingDocumentConfirmation("tenant-1", "record-1")); assertTrue(service.confirm("tenant-1", "user-1", "sync-1")); ElnSharedFillOperationEntity completed = operation( "COMPLETED", JsonUtil.getObjectToString(result)); when(operations.selectOne(any(Wrapper.class))).thenReturn(completed); assertFalse(service.confirm("tenant-1", "user-1", "sync-1")); } private static ElnSharedFillOperationEntity operation(String status, String resultJson) { ElnSharedFillOperationEntity value = new ElnSharedFillOperationEntity(); value.setOperationCode("sync-1"); value.setRequestDigest("digest-1"); value.setOperationStatus(status); value.setResultJson(resultJson); value.setOperatorUserId("user-1"); value.setFTenantId("tenant-1"); return value; } }