ConditionMatcher 처리 변경
This commit is contained in:
@@ -9,4 +9,6 @@ public interface ConditionMatcher {
|
||||
boolean matches(MockCondition condition, MockRequest request, Map<String, String> pathVariables);
|
||||
|
||||
String getType();
|
||||
|
||||
String getDisplayName();
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.eactive.httpmockserver.api.condition;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service(value = "ConditionMatcherService")
|
||||
public class ConditionMatcherService {
|
||||
|
||||
private final Map<String, ConditionMatcher> matchers;
|
||||
|
||||
private final List<ConditionMatcher> matchersList;
|
||||
|
||||
public ConditionMatcherService(List<ConditionMatcher> matchersList) {
|
||||
this.matchersList = matchersList;
|
||||
this.matchers = this.matchersList.stream().collect(Collectors.toMap(ConditionMatcher::getType, Function.identity()));
|
||||
}
|
||||
|
||||
public ConditionMatcher getMatcher(String key) {
|
||||
return matchers.get(key);
|
||||
}
|
||||
|
||||
public List<ConditionMatcher> getMatchersList() {
|
||||
return matchersList;
|
||||
}
|
||||
}
|
||||
@@ -17,4 +17,9 @@ public class HeaderMatcher implements ConditionMatcher {
|
||||
public String getType() {
|
||||
return "header";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return "Header";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,4 +25,9 @@ public class JsonPathMatcher implements ConditionMatcher {
|
||||
public String getType() {
|
||||
return "jsonpath";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return "JSON Path";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,4 +17,10 @@ public class PathMatcher implements ConditionMatcher {
|
||||
public String getType() {
|
||||
return "path";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return "Path";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,4 +18,9 @@ public class QueryMatcher implements ConditionMatcher {
|
||||
public String getType() {
|
||||
return "query";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return "Query";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.eactive.httpmockserver.api.controller;
|
||||
|
||||
import com.eactive.httpmockserver.api.condition.*;
|
||||
import com.eactive.httpmockserver.api.condition.ConditionMatcher;
|
||||
import com.eactive.httpmockserver.api.condition.ConditionMatcherService;
|
||||
import com.eactive.httpmockserver.api.dto.MockRequest;
|
||||
import com.eactive.httpmockserver.api.entity.MockCondition;
|
||||
import com.eactive.httpmockserver.api.entity.MockResponse;
|
||||
@@ -18,19 +19,18 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
public class MockApiController {
|
||||
|
||||
private final MockRouteService mockRouteService;
|
||||
private final Map<String, ConditionMatcher> matchers;
|
||||
|
||||
public MockApiController(MockRouteService mockRouteService, List<ConditionMatcher> matchersList) {
|
||||
private final ConditionMatcherService conditionMatcherService;
|
||||
|
||||
public MockApiController(MockRouteService mockRouteService, ConditionMatcherService conditionMatcherService) {
|
||||
this.mockRouteService = mockRouteService;
|
||||
this.matchers = matchersList.stream().collect(Collectors.toMap(ConditionMatcher::getType, Function.identity()));
|
||||
this.conditionMatcherService = conditionMatcherService;
|
||||
}
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MockApiController.class);
|
||||
@@ -107,7 +107,7 @@ public class MockApiController {
|
||||
}
|
||||
|
||||
for (MockCondition condition : conditions) {
|
||||
ConditionMatcher matcher = matchers.get(condition.getType().toLowerCase());
|
||||
ConditionMatcher matcher = conditionMatcherService.getMatcher(condition.getType().toLowerCase());
|
||||
if (matcher != null && !matcher.matches(condition, request, pathVariables)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -58,11 +58,8 @@
|
||||
<input type="hidden" th:field="*{responses[__${status.index}__].defaultResponse}" />
|
||||
<ul class="response_conditions">
|
||||
<li th:each="condition, status: *{responses[__${status.index}__].conditions}">
|
||||
<select>
|
||||
<option value="query" th:selected="${condition.type == 'query'}">Query</option>
|
||||
<option value="header" th:selected="${condition.type == 'header'}">Header</option>
|
||||
<option value="path" th:selected="${condition.type == 'path'}">Path</option>
|
||||
<option value="jsonpath" th:selected="${condition.type == 'jsonpath'}">JSON Path</option>
|
||||
<select class="form-select" th:with="matchers1=${@ConditionMatcherService.getMatchersList()}">
|
||||
<option th:each="item : ${matchers1}" th:value="${item.getType()}" th:text="${item.getDisplayName()}" th:selected="${condition.type == item.getType()}"></option>
|
||||
</select>
|
||||
<input type="text" name="key" th:value="${condition.key}"/>
|
||||
<input type="text" name="value" th:value="${condition.value}"/>
|
||||
@@ -164,11 +161,8 @@
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
<select class="form-select">
|
||||
<option value="query">Query</option>
|
||||
<option value="header">Header</option>
|
||||
<option value="path">Path Variable</option>
|
||||
<option value="jsonpath">JSON Path</option>
|
||||
<select class="form-select" th:with="matchers=${@ConditionMatcherService.getMatchersList()}">
|
||||
<option th:each="item : ${matchers}" th:value="${item.getType()}" th:text="${item.getDisplayName()}"></option>
|
||||
</select>
|
||||
</td>
|
||||
<td>
|
||||
|
||||
Reference in New Issue
Block a user