package cd.casic.server;

import cd.casic.ci.process.dto.req.target.TargetManagerCreateReq;
import cd.casic.ci.process.dto.req.target.TargetManagerUpdateReq;
import cd.casic.ci.process.process.converter.TargetConverter;
import cd.casic.ci.process.process.dao.pipeline.TargetManagerDao;
import cd.casic.ci.process.process.dao.pipeline.TargetVersionDao;
import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
import cd.casic.ci.process.process.dataObject.target.TargetManager;
import cd.casic.ci.process.process.dataObject.target.TargetVersion;
import cd.casic.ci.process.process.service.target.impl.TargetManagerServiceImpl;
import cd.casic.ci.process.process.service.target.impl.TargetVersionServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@SpringBootTest(classes = {OpsServerApplication.class})
@ActiveProfiles("local")
@Slf4j
public class TargetTest {
    @Autowired
    private TargetManagerServiceImpl targetManagerService;

    @MockBean
    private TargetManagerDao targetManagerDao;

    @MockBean
    private TargetVersionDao targetVersionDao;

    @MockBean
    private TargetConverter targetConverter;

    @MockBean
    private TargetVersionServiceImpl targetVersionService;


    @Test
    void testCreateTarget() {
        TargetManagerCreateReq targetManagerCreateReq = new TargetManagerCreateReq();
        targetManagerCreateReq.setTargetName("1");
        targetManagerCreateReq.setFilePath("1");


        targetManagerService.createTarget(targetManagerCreateReq);

        verify(targetManagerDao, times(1)).insert(any(TargetManager.class));
        verify(targetVersionService, times(1)).save(any(TargetVersion.class));
    }

    @Test
    void testUpdateTarget() {
        TargetManagerUpdateReq targetManagerUpdateReq = new TargetManagerUpdateReq();
        targetManagerUpdateReq.setId("1");

        targetManagerService.updateTarget(targetManagerUpdateReq);

        verify(targetManagerDao, times(1)).updateById(any(TargetManager.class));
        verify(targetVersionService, times(1)).save(any(TargetVersion.class));
    }
//
    @Test
    void testDeleteTarget() {
        BaseIdReq baseIdReq = new BaseIdReq();
        baseIdReq.setId("1");

        targetManagerService.deleteTarget(baseIdReq);

        verify(targetManagerDao, times(1)).deleteById("1");
        verify(targetVersionDao, times(1)).deleteByIds(anyList());
    }

}
