package com.usoft.credit.grpc.test;

import com.usoft.credit.grpc.api.protobuf.*;
import com.usoft.credit.grpc.api.query.entity.EnterpriseQuery;
import com.usoft.credit.grpc.api.query.entity.PersonQuery;
import com.usoft.credit.grpc.api.result.entity.EnterpriseJson;
import com.usoft.credit.grpc.api.result.entity.PersonJson;
import com.usoft.credit.grpc.api.result.entity.User;
import com.usoft.credit.util.ProtoBufUtil;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.junit.Test;

import java.util.concurrent.ExecutionException;

/**
 * @author liuam
 * @since 2018/8/7 0007 上午 9:52
 */

public class CreditGrpcTestClient {

    private ManagedChannel channel = ManagedChannelBuilder.forAddress("192.168.253.6", 36047).usePlaintext().build();


    @Test
    public void testQueryEn() throws ExecutionException, InterruptedException {

        // 构建 request
        EnterpriseQueryRequest.Builder request = EnterpriseQueryRequest.newBuilder();
        // 构建 查询条件
        EnterpriseQuery.Builder enterpriseQuery = EnterpriseQuery.newBuilder();
        // 初始化 查询条件
        enterpriseQuery.setEnname("中国邮政集团公司安庆市雷池路邮政所")
                .setRegisterNo("441234123456789")
                .setCreditCode("914403001922038216");
        request.setEnterpriseQuery(enterpriseQuery);

        User.Builder user = User.newBuilder();
        user.setEnuu(123);
        user.setPassword("123");
        user.setUseruu(123);
        request.setUser(user);

        // 远程调用得到结果
        EnterpriseJsonResponse response =
                GrpcQueryServiceGrpc.newFutureStub(channel).queryEn(request.build()).get();
        EnterpriseJson enterpriseJson = response.getEnterpriseJson();
        String result = ProtoBufUtil.toJSONHasTryCatch(enterpriseJson);
        System.out.println(result);
    }


    @Test
    public void testQueryPe() throws ExecutionException, InterruptedException {
        PersonQueryRequest.Builder request = PersonQueryRequest.newBuilder();

        PersonQuery.Builder personQuery = PersonQuery.newBuilder();

        personQuery.setDocumentNo("110000199001011112").setName("测试一");

        request.setPersonQuery(personQuery);

        PersonJsonResponse response =
                GrpcQueryServiceGrpc.newFutureStub(channel).queryPerson(request.build()).get();

        PersonJson personJson = response.getPersonJson();
        String result = ProtoBufUtil.toJSONHasTryCatch(personJson);

        System.out.println(result);

    }

}
