刘光辉
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
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
166
167
168
169
170
171
172
173
174
175
package jnpf.elnService.cross;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import jnpf.elnEntity.field.ElnFieldDefEntity;
import jnpf.elnEntity.field.ElnFieldVersionEntity;
import jnpf.elnEntity.template.ElnTemplateBindingEntity;
import jnpf.elnEntity.template.ElnTemplateCrossReferenceEntity;
import jnpf.elnEntity.template.ElnTemplateVersionEntity;
import jnpf.elnMapper.ElnFieldVersionMapper;
import jnpf.elnMapper.ElnFieldDefMapper;
import jnpf.elnMapper.ElnTemplateBindingMapper;
import jnpf.elnMapper.ElnTemplateCrossReferenceMapper;
import jnpf.elnMapper.ElnTemplateMapper;
import jnpf.elnService.cross.impl.ElnCrossTemplateBindingLifecycleParticipant;
import jnpf.elnService.cross.impl.ElnCrossTemplatePublishValidator;
import org.junit.Test;
 
import java.util.Collections;
import java.util.Arrays;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
public class ElnCrossTemplateLifecycleTest {
 
    @Test
    public void ignoresRulesWhoseTargetBindingIsNoLongerActive() {
        ElnTemplateMapper templates = mock(ElnTemplateMapper.class);
        ElnTemplateBindingMapper bindings = mock(ElnTemplateBindingMapper.class);
        ElnTemplateCrossReferenceMapper references = mock(ElnTemplateCrossReferenceMapper.class);
        ElnFieldDefMapper fields = mock(ElnFieldDefMapper.class);
        ElnFieldVersionMapper versions = mock(ElnFieldVersionMapper.class);
        ElnTemplateVersionEntity target = new ElnTemplateVersionEntity();
        target.setId("target-template");
        target.setMubanBianhao("TARGET");
        ElnTemplateBindingEntity cancelled = new ElnTemplateBindingEntity();
        cancelled.setId("target-binding");
        cancelled.setMubanId("target-template");
        cancelled.setMubanBianhao("TARGET");
        cancelled.setBangdingZhuangtai("CANCELLED");
        cancelled.setKongjianJuese("PRIMARY");
        ElnTemplateCrossReferenceEntity rule = new ElnTemplateCrossReferenceEntity();
        rule.setMubanBangdingId("target-binding");
        when(templates.selectOne(any(Wrapper.class))).thenReturn(target);
        when(bindings.selectList(any(Wrapper.class)))
                .thenReturn(Collections.singletonList(cancelled));
        when(references.selectList(any(Wrapper.class)))
                .thenReturn(Collections.singletonList(rule));
 
        assertTrue(new ElnCrossTemplatePublishValidator(
                templates, bindings, references, fields, versions)
                .validate("tenant-1", "target-template").isEmpty());
    }
 
    @Test
    public void softDeletesCrossTemplateRuleWhenBindingTerminates() {
        ElnTemplateCrossReferenceMapper references = mock(ElnTemplateCrossReferenceMapper.class);
 
        new ElnCrossTemplateBindingLifecycleParticipant(references)
                .bindingTerminated("tenant-1", "target-binding", "user-1");
 
        verify(references).update(isNull(), any(Wrapper.class));
    }
 
    @Test
    public void rejectsPublishWhenReservedSourceTemplateIsDisabled() {
        SourceEligibilityFixture fixture = new SourceEligibilityFixture();
        fixture.sourceTemplate.setBizEnabled("disabled");
 
        assertEquals("CROSS_TEMPLATE_SOURCE_INVALID", fixture.validateCode());
    }
 
    @Test
    public void rejectsPublishWhenReservedSourceFieldIsDisabled() {
        SourceEligibilityFixture fixture = new SourceEligibilityFixture();
        fixture.sourceField.setBizEnabled("disabled");
 
        assertEquals("CROSS_TEMPLATE_SOURCE_INVALID", fixture.validateCode());
    }
 
    @Test
    public void rejectsPublishWhenReservedSourceFieldIsNotGlobal() {
        SourceEligibilityFixture fixture = new SourceEligibilityFixture();
        fixture.sourceField.setZuoyongFanwei("TEMPLATE");
 
        assertEquals("CROSS_TEMPLATE_SOURCE_INVALID", fixture.validateCode());
    }
 
    @Test
    public void rejectsPublishWhenReservedSourceVersionIsNotPublished() {
        SourceEligibilityFixture fixture = new SourceEligibilityFixture();
        fixture.sourceVersion.setBizStatus("DRAFT");
 
        assertEquals("CROSS_TEMPLATE_SOURCE_INVALID", fixture.validateCode());
    }
 
    @Test
    public void rejectsPublishWhenReservedSourceBindingIsNoLongerActive() {
        SourceEligibilityFixture fixture = new SourceEligibilityFixture();
        fixture.sourceBinding.setBangdingZhuangtai("CANCELLED");
 
        assertEquals("CROSS_TEMPLATE_SOURCE_INVALID", fixture.validateCode());
    }
 
    private static final class SourceEligibilityFixture {
        ElnTemplateMapper templates = mock(ElnTemplateMapper.class);
        ElnTemplateBindingMapper bindings = mock(ElnTemplateBindingMapper.class);
        ElnTemplateCrossReferenceMapper references = mock(ElnTemplateCrossReferenceMapper.class);
        ElnFieldDefMapper fields = mock(ElnFieldDefMapper.class);
        ElnFieldVersionMapper versions = mock(ElnFieldVersionMapper.class);
        ElnTemplateVersionEntity target = template("target-template", "TARGET");
        ElnTemplateVersionEntity sourceTemplate = template("source-template", "SOURCE");
        ElnTemplateBindingEntity targetBinding = binding(
                "target-binding", "target-template", "target-field", "target-version");
        ElnTemplateBindingEntity sourceBinding = binding(
                "source-binding", "source-template", "source-field", "source-version");
        ElnTemplateCrossReferenceEntity rule = new ElnTemplateCrossReferenceEntity();
        ElnFieldDefEntity sourceField = new ElnFieldDefEntity();
        ElnFieldVersionEntity sourceVersion = new ElnFieldVersionEntity();
 
        private SourceEligibilityFixture() {
            sourceTemplate.setBizStatus("PUBLISHED");
            sourceTemplate.setBizEnabled("enabled");
            rule.setMubanBangdingId("target-binding");
            rule.setLaiyuanMubanId("source-template");
            rule.setLaiyuanMubanBianhao("SOURCE");
            rule.setLaiyuanZiduanDingyiId("source-field");
            rule.setLaiyuanZiduanBanbenId("source-version");
            sourceField.setId("source-field");
            sourceField.setBizEnabled("enabled");
            sourceField.setZuoyongFanwei("GLOBAL");
            sourceVersion.setId("source-version");
            sourceVersion.setZiduanDingyiId("source-field");
            sourceVersion.setBizStatus("PUBLISHED");
            sourceVersion.setShujuLeixing("TEXT");
            when(templates.selectOne(any(Wrapper.class))).thenReturn(target, sourceTemplate);
            when(bindings.selectList(any(Wrapper.class)))
                    .thenReturn(Arrays.asList(targetBinding, sourceBinding));
            when(references.selectList(any(Wrapper.class)))
                    .thenReturn(Collections.singletonList(rule));
            when(fields.selectOne(any(Wrapper.class))).thenReturn(sourceField);
            when(versions.selectOne(any(Wrapper.class))).thenReturn(sourceVersion);
        }
 
        private String validateCode() {
            return new ElnCrossTemplatePublishValidator(
                    templates, bindings, references, fields, versions)
                    .validate("tenant-1", "target-template").get(0).getCode();
        }
    }
 
    private static ElnTemplateVersionEntity template(String id, String code) {
        ElnTemplateVersionEntity value = new ElnTemplateVersionEntity();
        value.setId(id);
        value.setMubanBianhao(code);
        return value;
    }
 
    private static ElnTemplateBindingEntity binding(
            String id, String templateId, String fieldId, String versionId) {
        ElnTemplateBindingEntity value = new ElnTemplateBindingEntity();
        value.setId(id);
        value.setMubanId(templateId);
        value.setZiduanDingyiId(fieldId);
        value.setZiduanBanbenId(versionId);
        value.setBangdingZhuangtai("ACTIVE");
        value.setKongjianJuese("PRIMARY");
        return value;
    }
}