응답을 메모리 기반으로 처리
This commit is contained in:
@@ -5,8 +5,11 @@ import com.eactive.httpmockserver.api.entity.MockCondition;
|
||||
import com.eactive.httpmockserver.api.entity.MockResponse;
|
||||
import com.eactive.httpmockserver.api.entity.MockRoute;
|
||||
import com.eactive.httpmockserver.api.repository.MockRouteRepository;
|
||||
import com.eactive.httpmockserver.api.service.MockRouteService;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.jayway.jsonpath.PathNotFoundException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -24,18 +27,23 @@ import java.util.stream.Collectors;
|
||||
public class MockApiController {
|
||||
|
||||
@Autowired
|
||||
private MockRouteRepository mockRouteRepository;
|
||||
private MockRouteService mockRouteService;
|
||||
|
||||
|
||||
//logger
|
||||
private static final Logger logger = LoggerFactory.getLogger(MockApiController.class);
|
||||
|
||||
|
||||
@RequestMapping(value = "/{_:^(?!swagger-ui|mgmt|min-maps|plugins|dist|css|fonts|js|h2-console|img|pages|favicon.ico).*$}/**")
|
||||
public ResponseEntity<String> handleRequest(HttpServletRequest httpRequest) {
|
||||
MockRequest request = new MockRequest(httpRequest);
|
||||
AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
for (MockRoute mockRoute : mockRouteRepository.findAll()) {
|
||||
for (MockRoute mockRoute : mockRouteService.getAllRoutes()) {
|
||||
if (isRouteMatch(mockRoute, request)) {
|
||||
logger.debug(request.getBody());
|
||||
Map<String, String> pathVariables = pathMatcher.extractUriTemplateVariables(mockRoute.getPathPattern(), request.getUri());
|
||||
MockResponse matchingResponse = findMatchingResponse(mockRoute, request, pathVariables);
|
||||
|
||||
if (matchingResponse != null) {
|
||||
if (matchingResponse.getDelay() > 0) {
|
||||
try {
|
||||
|
||||
@@ -16,10 +16,17 @@ import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/mgmt/routes")
|
||||
public class MockRouteMgmtController {
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
mockRouteService.initAllRoutes();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private MockRouteService mockRouteService;
|
||||
|
||||
@@ -78,6 +85,7 @@ public class MockRouteMgmtController {
|
||||
|
||||
MockRoute mockRoute = mockRouteMapper.map(dto);
|
||||
mockRouteService.create(mockRoute);
|
||||
mockRouteService.initAllRoutes();
|
||||
return MOCK_ROUTE_LIST_VIEW;
|
||||
}
|
||||
|
||||
@@ -100,6 +108,7 @@ public class MockRouteMgmtController {
|
||||
|
||||
MockRoute mockRoute = mockRouteMapper.map(dto);
|
||||
mockRouteService.update(Long.parseLong(id), mockRoute);
|
||||
mockRouteService.initAllRoutes();
|
||||
return MOCK_ROUTE_LIST_VIEW;
|
||||
}
|
||||
|
||||
@@ -109,6 +118,7 @@ public class MockRouteMgmtController {
|
||||
modelSearch.getCheckedIdForDel().forEach(id -> {
|
||||
mockRouteService.deleteById(Long.parseLong(id));
|
||||
});
|
||||
mockRouteService.initAllRoutes();
|
||||
return MOCK_ROUTE_LIST_VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,24 @@ import com.eactive.httpmockserver.api.entity.MockResponse;
|
||||
import com.eactive.httpmockserver.api.entity.MockRoute;
|
||||
import com.eactive.httpmockserver.api.repository.MockRouteRepository;
|
||||
import com.eactive.httpmockserver.common.exception.NotFoundException;
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.transaction.Transactional;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class MockRouteService {
|
||||
|
||||
private static final String NOT_FOUND_MESSAGE = "Route Not Found";
|
||||
@@ -19,6 +30,19 @@ public class MockRouteService {
|
||||
@Autowired
|
||||
MockRouteRepository mockRouteRepository;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
private List<MockRoute> allRoutes = new ArrayList<MockRoute>();
|
||||
|
||||
public List<MockRoute> getAllRoutes() {
|
||||
return allRoutes;
|
||||
}
|
||||
|
||||
public void setAllRoutes(List<MockRoute> allRoutes) {
|
||||
this.allRoutes = allRoutes;
|
||||
}
|
||||
|
||||
public Page<MockRoute> findAll(Specification<MockRoute> mockRouteSpecification, Pageable pageable) {
|
||||
return mockRouteRepository.findAll(mockRouteSpecification, pageable);
|
||||
}
|
||||
@@ -29,7 +53,7 @@ public class MockRouteService {
|
||||
|
||||
public void create(MockRoute mockRoute) {
|
||||
|
||||
if (!mockRoute.getPathPattern().startsWith("/")){
|
||||
if (!mockRoute.getPathPattern().startsWith("/")) {
|
||||
mockRoute.setPathPattern("/" + mockRoute.getPathPattern());
|
||||
}
|
||||
|
||||
@@ -58,7 +82,7 @@ public class MockRouteService {
|
||||
route.setDescription(updated.getDescription());
|
||||
route.setMethod(updated.getMethod());
|
||||
route.setPathPattern(updated.getPathPattern());
|
||||
if (!route.getPathPattern().startsWith("/")){
|
||||
if (!route.getPathPattern().startsWith("/")) {
|
||||
route.setPathPattern("/" + route.getPathPattern());
|
||||
}
|
||||
|
||||
@@ -74,5 +98,32 @@ public class MockRouteService {
|
||||
|
||||
}
|
||||
mockRouteRepository.save(route);
|
||||
|
||||
}
|
||||
|
||||
public void initAllRoutes() {
|
||||
allRoutes.clear();
|
||||
List<MockRoute> routes = mockRouteRepository.findAll();
|
||||
for (MockRoute route : routes) {
|
||||
for (MockResponse response : route.getResponses()) {
|
||||
if (response.getConditions() != null) {
|
||||
for (MockCondition condition : response.getConditions()) {
|
||||
Hibernate.initialize(condition.getMockResponse());
|
||||
}
|
||||
}
|
||||
if (response.getHeaders() != null) {
|
||||
Hibernate.initialize(response.getHeaders());
|
||||
}
|
||||
Hibernate.initialize(response);
|
||||
}
|
||||
// entityManager.detach(route);
|
||||
try {
|
||||
MockRoute cloned = (MockRoute) BeanUtils.cloneBean(route);
|
||||
allRoutes.add(cloned);
|
||||
} catch (IllegalAccessException | InstantiationException | InvocationTargetException |
|
||||
NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user