<%
var parentPkgName=GetGParamValue('EZGEN_ROOT_PKGNAME'); //上级包名
if(!parentPkgName)parentPkgName='com.ezdml.codegen.ezdmlcodegendemo';

var tb=curTable; //当前表
var ClsName=AutoCapProc(tb.name,'ClassName'); //类名（驼峰命名，首字母大写）
var tbName=AutoCapProc(tb.name,'FieldName'); //字段名（驼峰命名，首字母小写）
var dispName=tb.UIDisplayName; //逻辑显示名
var pkField="id"; //主键
var idTp="Long";
var idValExp="id";
var idV1="1L";
var idV2="2L";
if(tb.getPrimaryKeyField()){
  pkField=AutoCapProc(tb.getPrimaryKeyField().name,'FieldName');
  if(tb.getPrimaryKeyField().dataType=='cfdtString'){
    idValExp="Integer.toString(id)";
    idV1='"1"';
    idV2='"2"';
  }
}
%>package ${parentPkgName}.${AutoCapProc(curModel.name,'JavaPackageName')}.${AutoCapProc(tb.name,'JavaPackageName')};

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class ${ClsName}ServiceTest {

    protected Log logger= LogFactory.getLog(${ClsName}ServiceTest.class);

    @Mock
    private ${ClsName}Repository ${tbName}Repository;
     
    @InjectMocks
    private ${ClsName}ServiceImpl ${tbName}Service;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    public void wLogOk(String msg){
        logger.info(">>>>>>> "+msg);
    }

    public ${ClsName} createNewItem(${idTp} ${idValExp}){
      ${ClsName} res = new ${ClsName}();
      res.set${AutoCapProc(pkField,'ClassName')}(${idValExp});
      return res;
    }

    @Test
    public void testFindAll() throws Exception {
        long startTime = System.currentTimeMillis();

        ${ClsName} ${tbName}1 = createNewItem(${idV1});
        ${ClsName} ${tbName}2 = createNewItem(${idV2});
        List<${ClsName}> ${tbName}s = Arrays.asList(${tbName}1, ${tbName}2);

        when(${tbName}Repository.findAll()).thenReturn(Arrays.asList(${tbName}1, ${tbName}2));

        List<${ClsName}> result = ${tbName}Service.findAll();

        assertEquals(2, result.size());
        verify(${tbName}Repository, times(1)).findAll();

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testFindAll executed successfully in " + (endTime - startTime) + " ms");
    }

    @Test
    public void testGetByIds() throws Exception {
        long startTime = System.currentTimeMillis();

        ${ClsName} ${tbName}1 = createNewItem(${idV1});
        ${ClsName} ${tbName}2 = createNewItem(${idV2});
        List<${idTp}> ids = Arrays.asList(${idV1}, ${idV2});

        when(${tbName}Repository.findAllById(ids)).thenReturn(Arrays.asList(${tbName}1, ${tbName}2));

        List<${ClsName}> result = ${tbName}Service.findByIds(ids);

        assertEquals(2, result.size());
        verify(${tbName}Repository, times(1)).findAllById(ids);

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testGetById executed successfully in " + (endTime - startTime) + " ms");
    }

    @Test
    public void testGetById() throws Exception {
        long startTime = System.currentTimeMillis();

        ${ClsName} ${tbName} = createNewItem(${idV1});

        when(${tbName}Repository.getById(${idV1})).thenReturn(${tbName});

        ${ClsName} result = ${tbName}Service.findById(${idV1});

        assertNotNull(result);
        verify(${tbName}Repository, times(1)).getById(${idV1});

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testGetById executed successfully in " + (endTime - startTime) + " ms");
    }

    @Test
    public void testCount() throws Exception {
        long startTime = System.currentTimeMillis();

        when(${tbName}Repository.count()).thenReturn(10L);

        long result = ${tbName}Service.count();

        assertEquals(10L, result);
        verify(${tbName}Repository, times(1)).count();

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testCount executed successfully in " + (endTime - startTime) + " ms");
    }

    @Test
    public void testAdd() throws Exception {
        long startTime = System.currentTimeMillis();
                                                  
        ${ClsName} ${tbName} = createNewItem(${idV1});
        when(${tbName}Repository.save(${tbName})).thenReturn(${tbName});

        ${ClsName} result = ${tbName}Service.add(${tbName});

        assertNotNull(result);
        verify(${tbName}Repository, times(1)).save(${tbName});

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testAdd executed successfully in " + (endTime - startTime) + " ms");
    }

    @Test
    public void testUpdate() throws Exception {
        long startTime = System.currentTimeMillis();
                     
        ${ClsName} ${tbName} = createNewItem(${idV1});
        when(${tbName}Repository.save(${tbName})).thenReturn(${tbName});

        ${ClsName} result = ${tbName}Service.update(${tbName});

        assertNotNull(result);
        verify(${tbName}Repository, times(1)).save(${tbName});

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testUpdate executed successfully in " + (endTime - startTime) + " ms");
    }

    @Test
    public void testDelete() throws Exception {
        long startTime = System.currentTimeMillis();

        ${ClsName} ${tbName} = createNewItem(${idV1});

        ${tbName}Service.delete(${tbName});

        verify(${tbName}Repository, times(1)).delete(${tbName});

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testDelete executed successfully in " + (endTime - startTime) + " ms");
    } 

    @Test
    public void testDeleteById() throws Exception {
        long startTime = System.currentTimeMillis();

        ${tbName}Service.deleteById(${idV1});

        verify(${tbName}Repository, times(1)).deleteById(${idV1});

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testDeleteById executed successfully in " + (endTime - startTime) + " ms");
    }

    @Test
    public void testDeleteByIds() throws Exception {
        long startTime = System.currentTimeMillis();
                                             
        List<${idTp}> ids = Arrays.asList(${idV1}, ${idV2});

        ${tbName}Service.deleteByIds(ids);

        verify(${tbName}Repository, times(1)).deleteAllById(ids);

        long endTime = System.currentTimeMillis();
        wLogOk("${ClsName}Service testDeleteByIds executed successfully in " + (endTime - startTime) + " ms");
    }
}