package com.friday.tooldriver.service;

import com.friday.tooldriver.ServiceUTBase;
import com.friday.base.common.response.DubboResult;
import com.friday.operation.api.tooldriver.dto.common.DriverBase;
import com.friday.operation.api.tooldriver.dto.rpa.CodePackageDto;
import com.friday.operation.api.tooldriver.dto.rpa.GroupDto;
import com.friday.operation.api.tooldriver.dto.rpa.ReleaseDto;
import com.friday.operation.api.tooldriver.dto.rpa.RobotDto;
import com.friday.tooldriver.service.rpa.driverimpl.UIPATHDriverServiceImpl;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import java.util.ArrayList;

@FixMethodOrder(MethodSorters.JVM)
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class UIPathDriverServiceImplTest extends ServiceUTBase {
    @Autowired
    private UIPATHDriverServiceImpl UiPathDriverService;

    @Test
    public void testCreateGroup() {
        GroupDto group = GroupDto.builder()
                .name("ParentGroup1")
                .description("ParentGroupDescription1")
                .build();
        DubboResult<GroupDto> result = UiPathDriverService.createGroup(new DriverBase(),  group);
        Assert.assertTrue(result.success());
        Assert.assertNotNull(result.getObject().getKey());
        GroupDto subGroup = GroupDto.builder()
                .name("SubGroup1")
                .description("SubGroupDescription1")
                .parentKey(result.getObject().getKey())
                .build();
        DubboResult<GroupDto> result2 = UiPathDriverService.createGroup(new DriverBase(),  subGroup);
        Assert.assertTrue(result2.success());
        Assert.assertNotNull(result2.getObject().getKey());
    }

    @Test
    public void testQueryGroups() {
        DubboResult<ArrayList<GroupDto>> groups = UiPathDriverService.queryGroups(new DriverBase(), null);
        Assert.assertTrue(groups.success());
        Assert.assertEquals(groups.getObject().size(), 2);
    }

    @Test
    public void testQueryGroupById() {
        DubboResult<GroupDto> group = UiPathDriverService.queryGroupByKey(new DriverBase(), "265");
        Assert.assertTrue(group.success());
        Assert.assertEquals(group.getObject().getKey(), "265");
    }

    @Test
    public void testDeleteGroupById() {
        DubboResult result = UiPathDriverService.deleteGroupByKey(new DriverBase(), "267");
        Assert.assertTrue(result.success());
    }

    @Test
    public void testUploadPackage() {
        CodePackageDto pkg = CodePackageDto.builder().filePath("D:\\Liz\\UIPATH\\smoke_pro_test.1.0.2.nupkg").build();
        DubboResult<CodePackageDto> result = UiPathDriverService.uploadPackage(new DriverBase(), pkg);
        Assert.assertTrue(result.success() && result.getObject().getKey().equals("smoke_pro_test:1.0.2"));
    }

    @Test
    public void testDeletePackage() {
        DubboResult result = UiPathDriverService.deletePackageByKey(new DriverBase(), "smoke_pro_test:1.0.2");
        Assert.assertTrue(result.success());
    }

    @Test
    public void testDownloadPackage() {
        DubboResult result = UiPathDriverService.downloadPackageByKey(new DriverBase(), "RPA_TEST7:1.0.4", null);
        Assert.assertTrue(result.success());
    }

    @Test
    public void testReleaseProcess() {
        DriverBase base = DriverBase.builder().groupKey("264").build();
        ReleaseDto release = ReleaseDto.builder()
                .name("RPA_TEST1")
                .processVersion("1.0.1")
                .description("Liz的流程")
                .build();
        DubboResult<ReleaseDto> result = UiPathDriverService.releaseProcess(base, release);
        Assert.assertTrue(result.success());
    }

    @Test
    public void testDeleteRelease() {
        DriverBase base = DriverBase.builder().groupKey("264").build();
        DubboResult result = UiPathDriverService.deleteProcessReleaseByKey(base, "10722");
        Assert.assertTrue(result.success());
    }

    @Test
    public void testQueryRelease() {
        DriverBase base = DriverBase.builder().groupKey("265").build();
        DubboResult<ReleaseDto> release = UiPathDriverService.queryProcessReleaseByKey(base, "10724");
        Assert.assertTrue(release.success());
        Assert.assertEquals(release.getObject().getKey(), "10724");
    }

    @Test
    public void testAddRobot() {
        DriverBase base = DriverBase.builder().tenantKey("1").build();
        RobotDto robot = RobotDto.builder().name("Lizilizy").build();
        DubboResult<RobotDto> result = UiPathDriverService.createRobot(base, robot);
        Assert.assertTrue(result.success());
    }

    @Test
    public void testAssignRobot() {
        DriverBase base = DriverBase.builder().tenantKey("1").build();
        DubboResult result = UiPathDriverService.assignRobotToGroup(base, "770", "268");
        Assert.assertTrue(result.success());
    }
}
