package com.thirdDay;

import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/20 0020.
 */
public class JsonArrayTest {
    //顺序变化的时候
    @Test
    public void sortArray() {
        String result = "{\n" +
                "    \"code\": \"success\",\n" +
                "    \"success\": true,\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"age\": 20,\n" +
                "            \"name\": \"long20\",\n" +
                "            \"eamil\": \"long20@test.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"age\": 28,\n" +
                "            \"name\": \"long28\",\n" +
                "            \"eamil\": \"long28@test.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"age\": 100,\n" +
                "            \"name\": \"long100\",\n" +
                "            \"eamil\": \"long100@test.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"age\": 150,\n" +
                "            \"name\": \"long150\",\n" +
                "            \"eamil\": \"long150@test.com\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"msg\": \"获取用户成功\"\n" +
                "}\n";
        String returnBody = "{\n" +
                "    \"code\": \"success\",\n" +
                "    \"success\": true,\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"age\": 28,\n" +
                "            \"name\": \"long28\",\n" +
                "            \"eamil\": \"long28@test.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"age\": 20,\n" +
                "            \"name\": \"long20\",\n" +
                "            \"eamil\": \"long20@test.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"age\": 150,\n" +
                "            \"name\": \"long150\",\n" +
                "            \"eamil\": \"long150@test.com\"\n" +
                "        },\n" +

                "    ],\n" +
                "    \"msg\": \"获取用户成功\"\n" +
                "}\n";


        if (!result.equalsIgnoreCase(returnBody)) {
            if (sortResult(result, returnBody)) {
                System.out.println("Pass");
            } else {
                System.out.println("@@@@@@Fail");
            }
        } else {
            System.out.println("Pass");
        }
         if(sortResult(result,returnBody)){
            System.out.print("Pass");
        }else{
            System.out.print("Fail");
        }
    }

    public boolean sortResult(String result, String returnBody) { //测试返回数据顺序不一致
        boolean f = false;
        //returnBody  实际结果,是否是JSON对象
        if (!returnBody.startsWith("{") && !returnBody.endsWith("}")) {
            return f;
        }
        try {
            //o1 是预期结果的Map对象,将预期结果的JSON转换成Map对象
            Map o1 = com.alibaba.fastjson.JSON.parseObject(result, Map.class);
            //o2 实际结果的Map对象,将实际结果的JSON转换成Map对象
            Map o2 = com.alibaba.fastjson.JSON.parseObject(returnBody, Map.class);

            //转换成Map后,可以拿到Map下面的所有的属性,通过遍历Map
            int s = 0; //记录A 对象的JSON数量
            int k = 0; //记录B 和A 相同的JSON数量
            int listSize = 0;
            for (Object o : o1.keySet()) {
                //拿到的对象 拿到4个
                Object valueObj = o1.get(o);

                //判断是否是 list 类型,如果是List的类型,则可能是无序的
                if (valueObj instanceof List) {
                    listSize += 1;
                    //拿出实际结果的list 对象 程序中是不知道
                    Object valueObj2 = o2.get(o);

                    //预期结果 result  里面的list 数量
                    s += ((List) valueObj).size();
                    //首先要校验是否为null
                    if (null != valueObj2) {
                        //拿到实际结果的List 数量 和预期结果的数量 要 比较 是否相等
                        if (((List) valueObj).size() != ((List) valueObj2).size()) {
                            f = false;
                            break;
                        }
                        //遍历每一对象
                        for (Object o3 : (List) valueObj) {
                            String o3str = com.alibaba.fastjson.JSON.toJSONString(o3);
                            for (Object o4 : (List) valueObj2) {
                                String o4str = com.alibaba.fastjson.JSON.toJSONString(o4);
                                if (o3str.equalsIgnoreCase(o4str)) {
                                    k += 1;
                                }
                            }
                        }
                    } else {
                        f = false;
                        break;
                    }
                }
            }
            //如果listSize>0 说明第二层对象是有LIST的 所以 进行属性的校验
            if (listSize > 0) {
                //如果总数的 JSON数量 和 对比后的JSON数量相等说明只是顺序变了 值是相等的
                if (s == k) {
                    f = true;
                }
            }
        } catch (Exception e) {
            f = false;
            e.printStackTrace();
        }
        return f;
    }
}