package com.eactive.httpmockserver.api.condition;

import com.eactive.httpmockserver.api.dto.MockRequest;
import com.eactive.httpmockserver.api.entity.MockCondition;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class JsonPathMatcher implements ConditionMatcher {
    @Override
    public boolean matches(MockCondition condition, MockRequest request, Map<String, String> pathVariables) {
        try {
            Object value = JsonPath.read(request.getBody(), condition.getKey());
            return value.toString().equals(condition.getValue());
        } catch (PathNotFoundException e) {
            return false;
        }
    }

    @Override
    public String getType() {
        return "jsonpath";
    }

    @Override
    public String getDisplayName() {
        return "JSON Path";
    }
}
