刘光辉
16 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.elnService.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.elnEntity.ElnMubanFileVo;
import jnpf.elnMapper.ElnDianziJiluMubanMapper;
import jnpf.exception.DataException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
 
public class ElnDianziJiluMubanServiceImplTest {
 
    @Test
    public void marksProductionConstructorForSpringInjection() throws Exception {
        Constructor<ElnDianziJiluMubanServiceImpl> constructor =
                ElnDianziJiluMubanServiceImpl.class.getConstructor(ElnDianziJiluMubanMapper.class);
 
        Assert.assertNotNull(constructor.getAnnotation(Autowired.class));
    }
 
    @Test
    public void returnsFirstFileForCurrentTenant() {
        AtomicReference<String> tenant = new AtomicReference<>();
        AtomicReference<String> item = new AtomicReference<>();
        ElnDianziJiluMubanMapper mapper = (requestPage, tenantId, jianyanXiangmu) -> {
            assertEquals(1L, requestPage.getCurrent());
            assertEquals(1L, requestPage.getSize());
            Assert.assertFalse(requestPage.searchCount());
            tenant.set(tenantId);
            item.set(jianyanXiangmu);
            return resultPage(
                    "[{\"fileId\":\"file-1.docx\",\"name\":\"记录模板.docx\","
                            + "\"url\":\"/api/file/Image/annex/file-1.docx\",\"fileSize\":128,"
                            + "\"fileExtension\":\"docx\",\"fileVersionId\":null}]");
        };
 
        ElnMubanFileVo result = new ElnDianziJiluMubanServiceImpl(mapper, () -> "tenant-1")
                .getFile("item-1");
 
        assertEquals("tenant-1", tenant.get());
        assertEquals("item-1", item.get());
        assertEquals("file-1.docx", result.getFileId());
        assertEquals("记录模板.docx", result.getName());
        assertEquals(Long.valueOf(128), result.getFileSize());
    }
 
    @Test
    public void returnsNullWhenTemplateOrArrayIsEmpty() {
        assertNull(new ElnDianziJiluMubanServiceImpl(
                (page, tenant, item) -> resultPage(), () -> "tenant-1").getFile("item-1"));
        assertNull(new ElnDianziJiluMubanServiceImpl(
                (page, tenant, item) -> resultPage("[]"), () -> "tenant-1").getFile("item-1"));
    }
 
    @Test(expected = DataException.class)
    public void rejectsMalformedFileJson() {
        new ElnDianziJiluMubanServiceImpl(
                (page, tenant, item) -> resultPage("not-json"),
                () -> "tenant-1").getFile("item-1");
    }
 
    @Test(expected = DataException.class)
    public void rejectsJsonNull() {
        new ElnDianziJiluMubanServiceImpl(
                (page, tenant, item) -> resultPage("null"),
                () -> "tenant-1").getFile("item-1");
    }
 
    @Test(expected = DataException.class)
    public void rejectsNullFirstFile() {
        new ElnDianziJiluMubanServiceImpl(
                (page, tenant, item) -> resultPage("[null]"),
                () -> "tenant-1").getFile("item-1");
    }
 
    private static IPage<String> resultPage(String... records) {
        return new Page<String>(1, 1, false).setRecords(Arrays.asList(records));
    }
}