package kr.co.zumo.app.lifeplus.model.module;

import com.google.gson.Gson;

import org.junit.Before;
import org.junit.Test;

import kr.co.zumo.app.lifeplus.bean.api.LoginResultBean;
import kr.co.zumo.app.lifeplus.model.LifeplusPreferences;
import kr.co.zumo.app.lifeplus.model.SuperModel;

import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.mock;

/**
 * LoginModuleParserTest
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018-10-11]   [최초 작성]
 * @since 2018-10-11
 */
public class LoginModuleParserTest {

  LifeplusPreferences lifeplusPreferences;

  @Before
  public void setup() {
    lifeplusPreferences = mock(LifeplusPreferences.class);

    SuperModel.getInstance().init(lifeplusPreferences);
    SuperModel.getInstance().setUserId("__test__");
    SuperModel.getInstance().setToken("");
    SuperModel.getInstance().deleteMemberStatus(SuperModel.MEMBER_STATUS_BIT_BLOCK);
    SuperModel.getInstance().deleteMemberStatus(SuperModel.MEMBER_STATUS_BIT_REST);
    SuperModel.getInstance().deleteMemberStatus(SuperModel.MEMBER_STATUS_BIT_OUT);
  }

  @Test
  public void parse() {
    LoginModuleParser parser = new LoginModuleParser();

    String result;
    boolean isSuccess;

    result = "{\n" +
      "  \"data\": [{\"user_no\": \"12345678900\",\n" +
      "            \"user_name\": \"나나나\",\n" +
      "            \"mail_id\": \"naananan@gmail.com\",\n" +
      "            \"memr_stat_cd\": \"000\",\n" +                // 000: 정상, 200:휴면, 탈퇴:900
      "            \"lock_stat_cd\": \"000\", \n" +               // 정상: 000, 잠김 회원: 999
      "            \"ci_value\":\"5555555555\"}],\n" +
      "    \"sp_rtn\": 0\n" +
      " }";

    isSuccess = parser.parse(new Gson().fromJson(result, LoginResultBean.class));
    assertEquals(true, isSuccess);
    assertEquals(false, SuperModel.getInstance().isActiveMember());
    assertEquals(false, SuperModel.getInstance().isBlockMember());
    assertEquals(false, SuperModel.getInstance().isOutMember());
    assertEquals(false, SuperModel.getInstance().isRestMember());
  }

  @Test
  public void parse1() {
    LoginModuleParser parser = new LoginModuleParser();

    String result;
    boolean isSuccess;
    result = "{\n" +
      "  \"data\": [{\"user_no\": \"12345678900\",\n" +
      "            \"user_name\": \"나나나\",\n" +
      "            \"mail_id\": \"naananan@gmail.com\",\n" +
      "            \"memr_stat_cd\": \"200\",\n" +                // 000: 정상, 200:휴면, 탈퇴:900
      "            \"lock_stat_cd\": \"000\", \n" +               // 정상: 000, 잠김 회원: 999
      "            \"ci_value\":\"5555555555\"}],\n" +
      "    \"sp_rtn\": 0\n" +
      " }";

    isSuccess = parser.parse(new Gson().fromJson(result, LoginResultBean.class));
    assertEquals(true, isSuccess);
    assertEquals(false, SuperModel.getInstance().isActiveMember());
    assertEquals(false, SuperModel.getInstance().isBlockMember());
    assertEquals(false, SuperModel.getInstance().isOutMember());
    assertEquals(true, SuperModel.getInstance().isRestMember());

  }

  @Test
  public void parse2() {
    LoginModuleParser parser = new LoginModuleParser();

    String result;
    boolean isSuccess;
    result = "{\n" +
      "  \"data\": [{\"user_no\": \"12345678900\",\n" +
      "            \"user_name\": \"나나나\",\n" +
      "            \"mail_id\": \"naananan@gmail.com\",\n" +
      "            \"memr_stat_cd\": \"900\",\n" +                // 000: 정상, 200:휴면, 탈퇴:900
      "            \"lock_stat_cd\": \"000\", \n" +               // 정상: 000, 잠김 회원: 999
      "            \"ci_value\":\"5555555555\"}],\n" +
      "    \"sp_rtn\": 0\n" +
      " }";

    isSuccess = parser.parse(new Gson().fromJson(result, LoginResultBean.class));
    assertEquals(true, isSuccess);
    assertEquals(false, SuperModel.getInstance().isActiveMember());
    assertEquals(false, SuperModel.getInstance().isBlockMember());
    assertEquals(true, SuperModel.getInstance().isOutMember());
    assertEquals(false, SuperModel.getInstance().isRestMember());

  }

  @Test
  public void parse3() {
    LoginModuleParser parser = new LoginModuleParser();

    String result;
    boolean isSuccess;
    result = "{\n" +
      "  \"data\": [{\"user_no\": \"12345678900\",\n" +
      "            \"user_name\": \"나나나\",\n" +
      "            \"mail_id\": \"naananan@gmail.com\",\n" +
      "            \"memr_stat_cd\": \"000\",\n" +                // 000: 정상, 200:휴면, 탈퇴:900
      "            \"lock_stat_cd\": \"999\", \n" +               // 정상: 000, 잠김 회원: 999
      "            \"ci_value\":\"5555555555\"}],\n" +
      "    \"sp_rtn\": 0\n" +
      " }";

    isSuccess = parser.parse(new Gson().fromJson(result, LoginResultBean.class));
    assertEquals(true, isSuccess);
    assertEquals(false, SuperModel.getInstance().isActiveMember());
    assertEquals(true, SuperModel.getInstance().isBlockMember());
    assertEquals(false, SuperModel.getInstance().isOutMember());
    assertEquals(false, SuperModel.getInstance().isRestMember());

  }

  @Test
  public void parse4() {
    LoginModuleParser parser = new LoginModuleParser();

    String result;
    boolean isSuccess;
    result = "{\n" +
      "  \"data\": [{\"user_no\": \"12345678900\",\n" +
      "            \"user_name\": \"나나나\",\n" +
      "            \"mail_id\": \"naananan@gmail.com\",\n" +
      "            \"memr_stat_cd\": \"000\",\n" +                // 000: 정상, 200:휴면, 탈퇴:900
      "            \"lock_stat_cd\": \"000\", \n" +               // 정상: 000, 잠김 회원: 999
      "            \"ci_value\":\"5555555555\"}],\n" +
      "    \"sp_rtn\": -100\n" +    // temp block: -100, rest: -200
      " }";

    isSuccess = parser.parse(new Gson().fromJson(result, LoginResultBean.class));
    assertEquals(true, isSuccess);
    assertEquals(false, SuperModel.getInstance().isActiveMember());
    assertEquals(true, SuperModel.getInstance().isBlockMember());
    assertEquals(false, SuperModel.getInstance().isOutMember());
    assertEquals(false, SuperModel.getInstance().isRestMember());

  }

  @Test
  public void parse5() {
    LoginModuleParser parser = new LoginModuleParser();

    String result;
    boolean isSuccess;
    result = "{\n" +
      "  \"data\": [{\"user_no\": \"12345678900\",\n" +
      "            \"user_name\": \"나나나\",\n" +
      "            \"mail_id\": \"naananan@gmail.com\",\n" +
      "            \"memr_stat_cd\": \"000\",\n" +                // 000: 정상, 200:휴면, 탈퇴:900
      "            \"lock_stat_cd\": \"000\", \n" +               // 정상: 000, 잠김 회원: 999
      "            \"ci_value\":\"5555555555\"}],\n" +
      "    \"sp_rtn\": -200\n" +    // temp block: -100, rest: -200
      " }";

    isSuccess = parser.parse(new Gson().fromJson(result, LoginResultBean.class));
    assertEquals(true, isSuccess);
    assertEquals(false, SuperModel.getInstance().isActiveMember());
    assertEquals(false, SuperModel.getInstance().isBlockMember());
    assertEquals(false, SuperModel.getInstance().isOutMember());
    assertEquals(true, SuperModel.getInstance().isRestMember());
  }

  @Test
  public void parseFail() {
    LoginModuleParser parser = new LoginModuleParser();

    String result;
    boolean isSuccess;
    result = "{\n" +
      "  \"data\": [{\"user_no\": \"12345678900\",\n" +
      "            \"user_name\": \"나나나\",\n" +
      "            \"mail_id\": \"naananan@gmail.com\",\n" +
      "            \"memr_stat_cd\": \"000\",\n" +                // 000: 정상, 200:휴면, 탈퇴:900
      "            \"lock_stat_cd\": \"000\", \n" +               // 정상: 000, 잠김 회원: 999
      "            \"ci_value\":\"5555555555\"}],\n" +
      "    \"sp_rtn\": -1\n" +    // temp block: -100, rest: -200
      " }";

    isSuccess = parser.parse(new Gson().fromJson(result, LoginResultBean.class));
    assertEquals(false, isSuccess);
    assertEquals(false, SuperModel.getInstance().isActiveMember());
    assertEquals(false, SuperModel.getInstance().isBlockMember());
    assertEquals(false, SuperModel.getInstance().isOutMember());
    assertEquals(false, SuperModel.getInstance().isRestMember());
  }

  @Test
  public void parseFail1() {
    LoginModuleParser parser = new LoginModuleParser();

    boolean isSuccess;
    isSuccess = parser.parse(null);
    assertEquals(false, isSuccess);
    assertEquals(false, SuperModel.getInstance().isActiveMember());
    assertEquals(false, SuperModel.getInstance().isBlockMember());
    assertEquals(false, SuperModel.getInstance().isOutMember());
    assertEquals(false, SuperModel.getInstance().isRestMember());
  }
}