package com.njcn.gather.tool.parsepqdif.service.impl;

import com.njcn.gather.tool.parsepqdif.mapper.CsPqdifPathMapper;
import com.njcn.gather.tool.parsepqdif.pojo.param.CsPqdifPathParam;
import com.njcn.gather.tool.parsepqdif.pojo.po.CsPqdifPathPO;
import com.njcn.gather.tool.parsepqdif.pojo.vo.CsPqdifPathDetailVO;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class CsPqdifPathServiceImplTest {

    @Test
    public void addPqdifPathShouldPersistFileName() {
        CsPqdifPathMapper mapper = mock(CsPqdifPathMapper.class);
        when(mapper.insert(any(CsPqdifPathPO.class))).thenReturn(1);
        CsPqdifPathServiceImpl service = new CsPqdifPathServiceImpl(mapper);

        CsPqdifPathParam param = new CsPqdifPathParam();
        param.setName("sample");
        param.setFilePath("D:/files/pqdif-files/uuid_sample.pqd");
        param.setFileName("sample.pqd");

        boolean result = service.addPqdifPath(param);

        Assert.assertTrue(result);
        ArgumentCaptor<CsPqdifPathPO> captor = ArgumentCaptor.forClass(CsPqdifPathPO.class);
        verify(mapper).insert(captor.capture());
        Assert.assertEquals("sample.pqd", captor.getValue().getFileName());
    }

    @Test
    public void updatePqdifPathShouldPersistFileName() {
        CsPqdifPathMapper mapper = mock(CsPqdifPathMapper.class);
        CsPqdifPathDetailVO detail = new CsPqdifPathDetailVO();
        detail.setId("pqdif-id");
        when(mapper.selectPqdifPathDetailById("pqdif-id")).thenReturn(detail);
        when(mapper.updateById(any(CsPqdifPathPO.class))).thenReturn(1);
        CsPqdifPathServiceImpl service = new CsPqdifPathServiceImpl(mapper);

        CsPqdifPathParam.UpdateParam param = new CsPqdifPathParam.UpdateParam();
        param.setId("pqdif-id");
        param.setName("sample");
        param.setFilePath("D:/files/pqdif-files/uuid_sample_new.pqd");
        param.setFileName("sample-new.pqd");

        boolean result = service.updatePqdifPath(param);

        Assert.assertTrue(result);
        ArgumentCaptor<CsPqdifPathPO> captor = ArgumentCaptor.forClass(CsPqdifPathPO.class);
        verify(mapper).updateById(captor.capture());
        Assert.assertEquals("sample-new.pqd", captor.getValue().getFileName());
        verify(mapper).selectPqdifPathDetailById(eq("pqdif-id"));
    }
}
