刘光辉
4 小时以前 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
package jnpf.elnMapper;
 
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jnpf.elnEntity.field.ElnFieldDefEntity;
import jnpf.elnEntity.field.ElnFieldVersionEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.UnknownTypeHandler;
import org.junit.Test;
 
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
 
public class ElnFieldMapperContractTest {
 
    @Test
    public void mapsFieldDefinitionColumnsAndMapper() throws Exception {
        assertEquals("eln_field_def", tableName(ElnFieldDefEntity.class));
        assertTableFields(ElnFieldDefEntity.class, fieldDefinitionColumns());
        assertEquals("f_id", tableId(ElnFieldDefEntity.class, "id"));
        assertMapper(ElnFieldDefMapper.class);
    }
 
    @Test
    public void mapsFieldVersionColumnsAndMapper() throws Exception {
        assertEquals("eln_field_version", tableName(ElnFieldVersionEntity.class));
        assertTableFields(ElnFieldVersionEntity.class, fieldVersionColumns());
        assertEquals("f_id", tableId(ElnFieldVersionEntity.class, "id"));
        assertJsonbField(ElnFieldVersionEntity.class, "bangdingPeizhi");
        assertJsonbField(ElnFieldVersionEntity.class, "kongjianPeizhi");
        assertMapper(ElnFieldVersionMapper.class);
    }
 
    @Test
    public void insertsFieldCodeWithoutAbortingPostgresTransactionOnConflict() throws Exception {
        Method method = ElnFieldDefMapper.class.getDeclaredMethod(
                "insertIgnoreCode", ElnFieldDefEntity.class);
        Insert insert = method.getAnnotation(Insert.class);
 
        assertEquals(int.class, method.getReturnType());
        assertTrue(String.join(" ", insert.value()).contains(
                "ON CONFLICT (f_tenant_id, ziduan_bianma) DO NOTHING"));
    }
 
    private static String tableName(Class<?> entityType) {
        return entityType.getAnnotation(TableName.class).value();
    }
 
    private static void assertTableFields(Class<?> entityType, Map<String, String> columns)
            throws Exception {
        for (Map.Entry<String, String> entry : columns.entrySet()) {
            TableField annotation = entityType.getDeclaredField(entry.getKey())
                    .getAnnotation(TableField.class);
            assertEquals(entry.getValue(), annotation.value());
        }
    }
 
    private static String tableId(Class<?> entityType, String fieldName) throws Exception {
        return entityType.getDeclaredField(fieldName).getAnnotation(TableId.class).value();
    }
 
    private static void assertJsonbField(Class<?> entityType, String fieldName) throws Exception {
        TableField annotation = entityType.getDeclaredField(fieldName).getAnnotation(TableField.class);
        assertEquals(JdbcType.OTHER, annotation.jdbcType());
        assertTrue(annotation.typeHandler() != UnknownTypeHandler.class);
    }
 
    private static void assertMapper(Class<?> mapperType) {
        assertTrue(BaseMapper.class.isAssignableFrom(mapperType));
        assertTrue(mapperType.isAnnotationPresent(Mapper.class));
    }
 
    private static Map<String, String> fieldDefinitionColumns() {
        Map<String, String> columns = platformColumns();
        columns.put("ziduanBianma", "ziduan_bianma");
        columns.put("zuoyongFanwei", "zuoyong_fanwei");
        columns.put("mubanBianhao", "muban_bianhao");
        columns.put("dangqianZiduanBanbenId", "dangqian_ziduan_banben_id");
        columns.put("bizEnabled", "biz_enabled");
        columns.put("beizhu", "beizhu");
        return columns;
    }
 
    private static Map<String, String> fieldVersionColumns() {
        Map<String, String> columns = platformColumns();
        columns.put("ziduanDingyiId", "ziduan_dingyi_id");
        columns.put("banbenHao", "banben_hao");
        columns.put("ziduanMingcheng", "ziduan_mingcheng");
        columns.put("shujuLeixing", "shuju_leixing");
        columns.put("danwei", "danwei");
        columns.put("shifouBitian", "shifou_bitian");
        columns.put("piciMoshi", "pici_moshi");
        columns.put("bangdingPeizhi", "bangding_peizhi");
        columns.put("kongjianPeizhi", "kongjian_peizhi");
        columns.put("recommendedCheckboxGroupCode", "recommended_checkbox_group_code");
        columns.put("recommendedCheckboxMinSelected", "recommended_checkbox_min_selected");
        columns.put("recommendedCheckboxMaxSelected", "recommended_checkbox_max_selected");
        columns.put("bizStatus", "biz_status");
        columns.put("zuofeiYuanyin", "zuofei_yuanyin");
        return columns;
    }
 
    private static Map<String, String> platformColumns() {
        Map<String, String> columns = new LinkedHashMap<>();
        columns.put("fTenantId", "f_tenant_id");
        columns.put("fVersion", "f_version");
        columns.put("fCreatorUserId", "f_creator_user_id");
        columns.put("fCreatorTime", "f_creator_time");
        columns.put("fLastModifyUserId", "f_last_modify_user_id");
        columns.put("fLastModifyTime", "f_last_modify_time");
        columns.put("fDeleteUserId", "f_delete_user_id");
        columns.put("fDeleteTime", "f_delete_time");
        columns.put("fDeleteMark", "f_delete_mark");
        return columns;
    }
}