package jnpf.dmsService; import com.fasterxml.jackson.databind.ObjectMapper; import jnpf.dmsEntity.DmsJiluDengjiPeizhi; import jnpf.dmsMapper.DmsJiluDengjiPeizhiMapper; import jnpf.dmsPermission.DmsCurrentIdentity; import jnpf.exception.DataException; import org.junit.jupiter.api.Test; import java.util.List; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; class DmsJiluShuxingServiceTest { private final DmsJiluDengjiPeizhiMapper mapper = mock(DmsJiluDengjiPeizhiMapper.class); private final DmsCurrentIdentity identity = mock(DmsCurrentIdentity.class); private final DmsJiluShuxingService service = new DmsJiluShuxingService(mapper, identity, new ObjectMapper()); private DmsJiluDengjiPeizhi config(String id, String grades, String control) { DmsJiluDengjiPeizhi config = new DmsJiluDengjiPeizhi(); config.setId(id); config.setVersion(1); config.setJiluLeixing("benJilu"); config.setJiluDengji(grades); config.setShifouShoukong(control); return config; } private void rows(DmsJiluDengjiPeizhi... configs) { when(identity.tenantId()).thenReturn("tenant-a"); when(mapper.selectEnabledByType("tenant-a", "benJilu")).thenReturn(List.of(configs)); } @Test void exactGradeAndTrustedTenantDetermineConfiguration() { rows(config("a", "[\"A\"]", "yes"), config("b", "[\"B\",\"C\"]", "no")); assertEquals("no", service.resolve("benJilu", "B").getShifouShoukong()); assertEquals("no", service.resolve("benJilu", "NA").getShifouShoukong()); verify(mapper, times(2)).selectEnabledByType("tenant-a", "benJilu"); } @Test void overlappingMatchesAreRejectedEvenWithSameResult() { rows(config("a", "[\"A\"]", "yes"), config("b", "[\"A\",\"B\"]", "yes")); assertThrows(DataException.class, () -> service.resolve("benJilu", "A")); } @Test void missingGradeDefaultsToUncontrolledWithoutInventingConfiguration() { when(identity.tenantId()).thenReturn("tenant-a"); when(mapper.selectEnabledByType("tenant-a", "pishengchanJilu")) .thenReturn(List.of(config("a", "[\"A\"]", "yes"))); DmsJiluDengjiPeizhi result = service.resolve("pishengchanJilu", "B"); assertEquals("no", result.getShifouShoukong()); assertEquals("pishengchanJilu", result.getJiluLeixing()); assertEquals("[\"B\"]", result.getJiluDengji()); assertNull(result.getId()); assertNull(result.getVersion()); } @Test void missingTypeConfigurationDefaultsButIncompleteSelectionIsRejected() { rows(); assertEquals("no", service.resolve("benJilu", "B").getShifouShoukong()); assertThrows(DataException.class, () -> service.resolve("benJilu", " ")); assertThrows(DataException.class, () -> service.resolve(null, "B")); } @Test void malformedGradesAndControlAreRejected() { rows(config("a", "[\"A\",1]", "yes")); assertThrows(DataException.class, () -> service.resolve("benJilu", "A")); assertThrows(DataException.class, () -> service.resolve("benJilu", "B")); rows(config("a", "[\"A\"]", "invalid")); assertThrows(DataException.class, () -> service.resolve("benJilu", "A")); DmsJiluDengjiPeizhi missingVersion = config("a", "[\"A\"]", "no"); missingVersion.setVersion(null); rows(missingVersion); assertThrows(DataException.class, () -> service.resolve("benJilu", "A")); } }