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; } }