package jnpf.elnService.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import jnpf.elnEntity.ElnMubanFileVo; import jnpf.elnMapper.ElnDianziJiluMubanMapper; import jnpf.exception.DataException; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import java.lang.reflect.Constructor; import java.util.Arrays; import java.util.concurrent.atomic.AtomicReference; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; public class ElnDianziJiluMubanServiceImplTest { @Test public void marksProductionConstructorForSpringInjection() throws Exception { Constructor constructor = ElnDianziJiluMubanServiceImpl.class.getConstructor(ElnDianziJiluMubanMapper.class); Assert.assertNotNull(constructor.getAnnotation(Autowired.class)); } @Test public void returnsFirstFileForCurrentTenant() { AtomicReference tenant = new AtomicReference<>(); AtomicReference item = new AtomicReference<>(); ElnDianziJiluMubanMapper mapper = (requestPage, tenantId, jianyanXiangmu) -> { assertEquals(1L, requestPage.getCurrent()); assertEquals(1L, requestPage.getSize()); Assert.assertFalse(requestPage.searchCount()); tenant.set(tenantId); item.set(jianyanXiangmu); return resultPage( "[{\"fileId\":\"file-1.docx\",\"name\":\"记录模板.docx\"," + "\"url\":\"/api/file/Image/annex/file-1.docx\",\"fileSize\":128," + "\"fileExtension\":\"docx\",\"fileVersionId\":null}]"); }; ElnMubanFileVo result = new ElnDianziJiluMubanServiceImpl(mapper, () -> "tenant-1") .getFile("item-1"); assertEquals("tenant-1", tenant.get()); assertEquals("item-1", item.get()); assertEquals("file-1.docx", result.getFileId()); assertEquals("记录模板.docx", result.getName()); assertEquals(Long.valueOf(128), result.getFileSize()); } @Test public void returnsNullWhenTemplateOrArrayIsEmpty() { assertNull(new ElnDianziJiluMubanServiceImpl( (page, tenant, item) -> resultPage(), () -> "tenant-1").getFile("item-1")); assertNull(new ElnDianziJiluMubanServiceImpl( (page, tenant, item) -> resultPage("[]"), () -> "tenant-1").getFile("item-1")); } @Test(expected = DataException.class) public void rejectsMalformedFileJson() { new ElnDianziJiluMubanServiceImpl( (page, tenant, item) -> resultPage("not-json"), () -> "tenant-1").getFile("item-1"); } @Test(expected = DataException.class) public void rejectsJsonNull() { new ElnDianziJiluMubanServiceImpl( (page, tenant, item) -> resultPage("null"), () -> "tenant-1").getFile("item-1"); } @Test(expected = DataException.class) public void rejectsNullFirstFile() { new ElnDianziJiluMubanServiceImpl( (page, tenant, item) -> resultPage("[null]"), () -> "tenant-1").getFile("item-1"); } private static IPage resultPage(String... records) { return new Page(1, 1, false).setRecords(Arrays.asList(records)); } }