刘光辉
2 小时以前 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
package jnpf.bizcommon.onlyoffice.controller;
 
import jnpf.base.ActionResult;
import jnpf.bizcommon.onlyoffice.service.impl.OnlyOfficeTemplatePublishFenceService;
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 OnlyOfficeTemplatePublishFenceControllerTest {
 
    @BeforeClass
    public static void setUpSpringContext() {
        SpringContext.setApplicationContext(new StaticApplicationContext());
    }
 
    @Test
    public void acquireAndReleaseRequireSameTokenBeforeMutatingSession() {
        AtomicInteger checks = new AtomicInteger();
        OnlyOfficeTemplatePublishFenceService service =
                mock(OnlyOfficeTemplatePublishFenceService.class);
        when(service.acquire("tenant-1", "template-1", "template.docx", "annex"))
                .thenReturn("token-1");
        when(service.release("tenant-1", "template-1", "template.docx", "annex", "token-1"))
                .thenReturn(true);
        OnlyOfficeTemplatePublishFenceController controller =
                new OnlyOfficeTemplatePublishFenceController(checks::incrementAndGet, service);
 
        ActionResult<String> acquire = controller.acquire(
                "tenant-1", "template-1", "template.docx", "annex");
        ActionResult<Boolean> release = controller.release(
                "tenant-1", "template-1", "template.docx", "annex", "token-1");
 
        assertEquals("token-1", acquire.getData());
        assertTrue(release.getData());
        assertEquals(2, checks.get());
        verify(service).acquire("tenant-1", "template-1", "template.docx", "annex");
        verify(service).release("tenant-1", "template-1", "template.docx", "annex", "token-1");
    }
}