package com.njcn.gather.source.pojo;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.njcn.gather.source.pojo.param.PqSourceParam;
import com.njcn.gather.source.pojo.po.PqSource;
import org.junit.Assert;
import org.junit.Test;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.DecimalMin;
import java.lang.reflect.Field;
import java.math.BigDecimal;

public class PqSourceCapacityFieldsTest {

    private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    @Test
    public void shouldExposeExplicitCapacityColumnMappings() throws Exception {
        Field maxVoltage = PqSource.class.getDeclaredField("maxVoltage");
        Field maxCurrent = PqSource.class.getDeclaredField("maxCurrent");

        Assert.assertEquals("Max_Voltage", maxVoltage.getAnnotation(TableField.class).value());
        Assert.assertEquals("Max_Current", maxCurrent.getAnnotation(TableField.class).value());
    }

    @Test
    public void shouldExposeNonNegativeCapacityValidation() throws Exception {
        Field maxVoltage = PqSourceParam.class.getDeclaredField("maxVoltage");
        Field maxCurrent = PqSourceParam.class.getDeclaredField("maxCurrent");

        Assert.assertEquals("0", maxVoltage.getAnnotation(DecimalMin.class).value());
        Assert.assertEquals("0", maxCurrent.getAnnotation(DecimalMin.class).value());

        PqSourceParam.UpdateParam param = new PqSourceParam.UpdateParam();
        param.setId("12345678901234567890123456789012");
        param.setPattern("12345678901234567890123456789012");
        param.setType("12345678901234567890123456789012");
        param.setDevType("12345678901234567890123456789012");
        param.setMaxVoltage(new BigDecimal("-1"));

        Assert.assertFalse(validator.validate(param).isEmpty());
    }

    @Test
    public void shouldCopyCapacityFieldsFromRequestToEntity() {
        PqSourceParam.UpdateParam param = new PqSourceParam.UpdateParam();
        param.setMaxVoltage(new BigDecimal("220.00"));
        param.setMaxCurrent(new BigDecimal("5.00"));

        PqSource source = new PqSource();
        BeanUtil.copyProperties(param, source);

        Assert.assertEquals(new BigDecimal("220.00"), source.getMaxVoltage());
        Assert.assertEquals(new BigDecimal("5.00"), source.getMaxCurrent());
    }
}
