package com.jay.monitor.data.server;

import com.alipay.remoting.InvokeContext;
import com.alipay.remoting.rpc.RpcClient;
import com.jay.monitor.data.core.enums.MsgTypeEnums;
import com.jay.monitor.data.core.model.serializable.SQLMonitorDTO;
import com.jay.monitor.data.core.model.serializable.URLDataDTO;
import com.jay.monitor.data.core.model.serializable.base.BaseDTO;
import com.jay.monitor.data.server.model.TestDataRequest;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MonitorDataServerTest {

    private RpcClient rpcClient;

    private String addr = "127.0.0.1:9420";

    @Before
    public void init() {
        rpcClient = new RpcClient();
        rpcClient.startup();

//        MonitorDataServer monitorDataServer = new MonitorDataServer(9420);
//        monitorDataServer.registerUserProcessor(new SQLUserProcess());
//        monitorDataServer.registerUserProcessor(new URLUserProcess());
//        monitorDataServer.registerUserProcessor(new BatchUserProcess());
//        monitorDataServer.start();
    }

    @Test
    public void start() throws Exception {
        while (true) {
            TestDataRequest testDataRequest = new TestDataRequest("lkx", "abc");
            rpcClient.invokeSync(addr, testDataRequest, new InvokeContext(), 3000);
            Thread.sleep(5000);
        }
    }

    @Test
    public void testBaseDTO() throws Exception {
        while (true) {
            BaseDTO baseDTO = new BaseDTO(MsgTypeEnums.URL);
//            monitorDTO.setSqlId("abcds");

            SQLMonitorDTO monitorDTO = new SQLMonitorDTO();
            monitorDTO.setSqlId("asdasdf");
            // Arrays.asList(monitorDTO)
            List<BaseDTO> dtos = new ArrayList<>();
            dtos.add(monitorDTO);
            dtos.add(baseDTO);
            rpcClient.oneway(addr, dtos, new InvokeContext());
            Thread.sleep(5000);
        }
    }

    @Test
    public void testURLDTO() throws Exception {
        URLDataDTO dto = new URLDataDTO();
        dto.setApplicationName("elab-markting-test");
        dto.setIp("0:0:0:0");
        dto.setGroupName("A");
        dto.setDataId("123123");
        dto.setCreateDate(new Date());
        rpcClient.oneway(addr, dto, new InvokeContext());
        Thread.sleep(5000);
    }
}
