package com.malk.benteler.service;

import com.alibaba.fastjson.JSON;
import com.malk.benteler.config.BentelerYidaConf;
import com.malk.benteler.dto.EiamTestResult;
import com.malk.server.aliwork.YDAuth;
import com.malk.server.aliwork.YDConf;
import com.malk.service.aliwork.YDClient_Form;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * {@link BentelerYidaTestService} 宜搭测试回写验证。
 */
public class BentelerYidaTestServiceTest {

    private YDClient_Form ydClientForm;
    private BentelerYidaConf conf;
    private BentelerYidaTestService service;

    @Before
    public void setUp() {
        ydClientForm = mock(YDClient_Form.class);
        conf = TestBentelerYidaConf.create();
        YDConf ydConf = new YDConf();
        ydConf.setAppType("APP_TEST");
        ydConf.setSystemToken("TOKEN_TEST");
        service = new BentelerYidaTestService(ydClientForm, ydConf, conf);
    }

    @Test
    @SuppressWarnings("unchecked")
    public void markResultSuccess_readsOriginalValueAndWritesSuccess() {
        Map<String, Object> formData = new HashMap<>();
        formData.put(conf.getTestResultFieldId(), "处理中");
        Map<String, Object> instance = new HashMap<>();
        instance.put("modelUuid", conf.getTestFormUuid());
        instance.put("formData", formData);
        when(ydClientForm.getForm(any(YDAuth.class), anyString(), isNull()))
                .thenReturn(instance);
        when(ydClientForm.updateForm(any(YDAuth.class), anyString(), anyString(), anyMap()))
                .thenReturn(Collections.emptyMap());

        EiamTestResult result = service.markResultSuccess("FINST_TEST");

        assertEquals("处理中", result.getOriginalResult());
        assertEquals("成功", result.getUpdatedResult());
        ArgumentCaptor<String> jsonCaptor = ArgumentCaptor.forClass(String.class);
        verify(ydClientForm).updateForm(any(YDAuth.class), anyString(),
                jsonCaptor.capture(), anyMap());
        Map<String, Object> update = JSON.parseObject(jsonCaptor.getValue(), Map.class);
        assertEquals("成功", update.get(conf.getTestResultFieldId()));
    }

    @Test
    public void markResultSuccess_allowsMissingModelUuid() {
        Map<String, Object> formData = new HashMap<>();
        formData.put(conf.getTestResultFieldId(), "待处理");
        Map<String, Object> instance = new HashMap<>();
        instance.put("formData", formData);
        when(ydClientForm.getForm(any(YDAuth.class), anyString(), isNull()))
                .thenReturn(instance);
        when(ydClientForm.updateForm(any(YDAuth.class), anyString(), anyString(), anyMap()))
                .thenReturn(Collections.emptyMap());

        EiamTestResult result = service.markResultSuccess("FINST_TEST");

        assertEquals("待处理", result.getOriginalResult());
        assertEquals("成功", result.getUpdatedResult());
    }
}
