刘光辉
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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package jnpf.dmsMapper;
 
import jnpf.dmsDatabase.DmsUuidTypeHandler;
import jnpf.dmsEntity.permission.form.DmsPersonalFolderBorrowForm;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Test;
 
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
 
class DmsPersonalFolderMapperSqlTest {
 
    @Test
    void insertsBorrowSnapshotsIdempotently() {
        Configuration configuration = configuration();
        MappedStatement statement = configuration.getMappedStatement(
                DmsPersonalFolderMapper.class.getName() + ".insertBorrowSnapshots");
        DmsPersonalFolderBorrowForm.FileRelation first = relation(
                "018f0000-0000-7000-8000-000000000701",
                "018f0000-0000-7000-8000-000000000702");
        DmsPersonalFolderBorrowForm.FileRelation second = relation(
                "018f0000-0000-7000-8000-000000000703",
                "018f0000-0000-7000-8000-000000000704");
 
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("tenantId", "tenant-1");
        parameters.put("userId", "user-1");
        parameters.put("files", Arrays.asList(first, second));
        BoundSql boundSql = statement.getBoundSql(parameters);
        String sql = normalizedSql(boundSql);
 
        assertTrue(sql.contains("INSERT INTO public.dms_personal_folder_items"));
        assertTrue(sql.contains("document_id, file_version_id, valid_to"));
        assertTrue(sql.contains("'BORROW'"));
        assertEquals(10, boundSql.getParameterMappings().size());
        assertEquals(2, boundSql.getParameterMappings().stream()
                .filter(mapping -> mapping.getProperty().endsWith(".validTo")).count());
        assertTrue(sql.contains("DO UPDATE SET valid_to = EXCLUDED.valid_to"));
        assertTrue(sql.contains("EXCLUDED.valid_to IS NULL AND dms_personal_folder_items.valid_to IS NOT NULL"));
        assertTrue(sql.contains("dms_personal_folder_items.valid_to = 'infinity'::TIMESTAMPTZ"));
        assertTrue(sql.endsWith("AND dms_personal_folder_items.valid_to < EXCLUDED.valid_to)"));
    }
 
    @Test
    void insertsDownloadSnapshotsWithTheirValidity() {
        Configuration configuration = configuration();
        MappedStatement statement = configuration.getMappedStatement(
                DmsPersonalFolderMapper.class.getName() + ".insertDownloadSnapshots");
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("tenantId", "tenant-1");
        parameters.put("userId", "user-1");
        parameters.put("files", Arrays.asList(relation(
                "018f0000-0000-7000-8000-000000000701",
                "018f0000-0000-7000-8000-000000000702")));
 
        BoundSql boundSql = statement.getBoundSql(parameters);
        String sql = normalizedSql(boundSql);
 
        assertTrue(sql.contains("'DOWNLOAD'"));
        assertTrue(sql.contains("document_id, file_version_id, valid_to"));
        assertEquals(5, boundSql.getParameterMappings().size());
        assertTrue(sql.contains("DO UPDATE SET valid_to = EXCLUDED.valid_to"));
    }
 
    @Test
    void insertsFavoriteSnapshotIdempotentlyWithoutAnExpiry() {
        Configuration configuration = configuration();
        MappedStatement statement = configuration.getMappedStatement(
                DmsPersonalFolderMapper.class.getName() + ".insertFavoriteSnapshot");
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("tenantId", "tenant-1");
        parameters.put("userId", "user-1");
        parameters.put("documentId", UUID.fromString(
                "018f0000-0000-7000-8000-000000000701"));
        parameters.put("fileVersionId", UUID.fromString(
                "018f0000-0000-7000-8000-000000000702"));
 
        String sql = normalizedSql(statement.getBoundSql(parameters));
 
        assertTrue(sql.contains("'FAVORITE'"));
        assertTrue(sql.contains("file_version_id, valid_to"));
        assertTrue(sql.contains("VALUES (?, ?, 'FAVORITE', ?, ?, NULL)"));
        assertTrue(sql.endsWith("DO NOTHING"));
    }
 
    @Test
    void validatesOnlyReadableFileVersionsWithinTheTenant() {
        Configuration configuration = configuration();
        MappedStatement statement = configuration.getMappedStatement(
                DmsPersonalFolderMapper.class.getName() + ".selectReadableVersions");
 
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("tenantId", "tenant-1");
        parameters.put("fileVersionIds", Arrays.asList(
                UUID.fromString("018f0000-0000-7000-8000-000000000702"),
                UUID.fromString("018f0000-0000-7000-8000-000000000704")));
        String sql = normalizedSql(statement.getBoundSql(parameters));
 
        assertTrue(sql.contains("WHERE v.tenant_id = ? AND v.id IN ( ? , ? )"));
        assertTrue(sql.contains("s.lifecycle_status IN ('EFFECTIVE', 'SUPERSEDED', 'EXPIRED', 'REVOKED', 'OBSOLETE')"));
        assertTrue(sql.contains("s.lifecycle_status = 'INVALIDATED' AND s.approved_at IS NOT NULL"));
    }
 
    @Test
    void loadsTheFileVersionSnapshotForTheCurrentTenantUserAndFolderType() {
        Configuration configuration = configuration();
        MappedStatement statement = configuration.getMappedStatement(
                DmsPersonalFolderMapper.class.getName() + ".selectFiles");
 
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("tenantId", "tenant-1");
        parameters.put("userId", "user-1");
        parameters.put("folderType", "BORROW");
        parameters.put("keyword", "report");
        parameters.put("limit", 100);
        BoundSql boundSql = statement.getBoundSql(parameters);
        String sql = normalizedSql(boundSql);
 
        assertTrue(sql.contains("JOIN public.dms_personal_folder_items personal_item"));
        assertTrue(sql.contains("personal_item.tenant_id = ?"));
        assertTrue(sql.contains("personal_item.user_id = ?"));
        assertTrue(sql.contains("personal_item.folder_type = ?"));
        assertTrue(sql.contains("personal_item.valid_to AS valid_to"));
        assertFalse(sql.contains("personal_item.valid_to IS NULL OR CURRENT_TIMESTAMP < personal_item.valid_to"));
        assertTrue(sql.contains("personal_item.file_version_id = r.id"));
        assertTrue(sql.contains("r.resource_type = 'FILE_VERSION'"));
        assertTrue(sql.contains("LOWER(COALESCE(v.file_name, ''))"));
        assertTrue(sql.contains("LOWER(COALESCE(version_document.name, ''))"));
        assertFalse(sql.contains("personal_item.document_id = r.id"));
        assertFalse(sql.contains("LOWER(COALESCE(current_version.file_name, ''))"));
        assertTrue(sql.endsWith("LIMIT ?"));
        assertFalse(sql.contains("dms_folders child"));
    }
 
    private Configuration configuration() {
        Configuration configuration = new Configuration();
        configuration.getTypeHandlerRegistry().register(UUID.class, DmsUuidTypeHandler.class);
        configuration.addMapper(DmsPersonalFolderMapper.class);
        return configuration;
    }
 
    private String normalizedSql(BoundSql boundSql) {
        return boundSql.getSql().replaceAll("\\s+", " ").trim();
    }
 
    private DmsPersonalFolderBorrowForm.FileRelation relation(
            String documentId, String fileVersionId) {
        DmsPersonalFolderBorrowForm.FileRelation relation =
                new DmsPersonalFolderBorrowForm.FileRelation();
        relation.setDocumentId(UUID.fromString(documentId));
        relation.setFileVersionId(UUID.fromString(fileVersionId));
        relation.setValidTo(new Date(fileVersionId.endsWith("702")
                ? 4102444800000L : 4102531200000L));
        return relation;
    }
}