All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.itxiaoer.commons.test.mvc.MockMvcConsumers Maven / Gradle / Ivy

There is a newer version: 2.3.4
Show newest version
package com.itxiaoer.commons.test.mvc;

import com.itxiaoer.commons.core.page.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.function.Consumer;

/**
 * @author : liuyk
 */
@Slf4j
@SuppressWarnings({"unused", "WeakerAccess"})
public final class MockMvcConsumers {

    private static final Consumer DEFAULT_SUCCESS = (actions) -> {
        try {
            actions.andExpect(MockMvcResultMatchers.jsonPath("$.success").value("true"))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(ResponseCode.SUCCESS_CODE));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    };

    private static Consumer DEFAULT_FAIL = (actions) -> {
        try {
            actions.andExpect(MockMvcResultMatchers.jsonPath("$.success").value("false"))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(ResponseCode.SUCCESS_CODE));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    };

    public static Consumer ok() {
        return DEFAULT_SUCCESS;
    }

    public static  Consumer ok(T t) {
        return DEFAULT_SUCCESS.andThen((actions) -> {
            try {
                actions.andExpect(MockMvcResultMatchers.jsonPath("$.data").value(t));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });

    }

    public static Consumer fail() {
        return DEFAULT_FAIL;
    }

    public static Consumer fail(String code) {
        return (actions) -> {
            try {
                actions.andExpect(MockMvcResultMatchers.jsonPath("$.success").value("false"))
                        .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(code));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy