刘光辉
5 小时以前 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
package jnpf.bizcommon.onlyoffice.controller;
 
import jnpf.base.ActionResult;
import jnpf.bizcommon.onlyoffice.service.impl.OnlyOfficeRecordRevisionFenceService;
import jnpf.util.context.SpringContext;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.StaticApplicationContext;
 
import java.util.concurrent.atomic.AtomicInteger;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
public class OnlyOfficeRecordRevisionFenceControllerTest {
 
    @BeforeClass
    public static void setUpSpringContext() {
        SpringContext.setApplicationContext(new StaticApplicationContext());
    }
 
    @Test
    public void acquireCompleteAndReleaseRequireSameToken() {
        AtomicInteger checks = new AtomicInteger();
        OnlyOfficeRecordRevisionFenceService service =
                mock(OnlyOfficeRecordRevisionFenceService.class);
        when(service.acquire("tenant-1", "record-1", "record.docx", "annex"))
                .thenReturn("token-1");
        when(service.complete("tenant-1", "record-1", "record.docx", "annex", "token-1"))
                .thenReturn(true);
        when(service.release("tenant-1", "record-1", "record.docx", "annex", "token-1"))
                .thenReturn(true);
        OnlyOfficeRecordRevisionFenceController controller =
                new OnlyOfficeRecordRevisionFenceController(checks::incrementAndGet, service);
 
        ActionResult<String> acquire = controller.acquire(
                "tenant-1", "record-1", "record.docx", "annex");
        ActionResult<Boolean> complete = controller.complete(
                "tenant-1", "record-1", "record.docx", "annex", "token-1");
        ActionResult<Boolean> release = controller.release(
                "tenant-1", "record-1", "record.docx", "annex", "token-1");
 
        assertEquals("token-1", acquire.getData());
        assertTrue(complete.getData());
        assertTrue(release.getData());
        assertEquals(3, checks.get());
        verify(service).acquire("tenant-1", "record-1", "record.docx", "annex");
        verify(service).complete("tenant-1", "record-1", "record.docx", "annex", "token-1");
        verify(service).release("tenant-1", "record-1", "record.docx", "annex", "token-1");
    }
}