Iterable<String> filterSpecsByOperation( ImmutableMap<String, RestxSpec> allSpecs, String httpMethod, String path) { StdRestxRequestMatcher matcher = new StdRestxRequestMatcher(httpMethod, path); Collection<String> specs = Lists.newArrayList(); for (Map.Entry<String, RestxSpec> spec : allSpecs.entrySet()) { for (When when : spec.getValue().getWhens()) { if (when instanceof WhenHttpRequest) { WhenHttpRequest request = (WhenHttpRequest) when; String requestPath = request.getPath(); if (!requestPath.startsWith("/")) { requestPath = "/" + requestPath; } if (requestPath.indexOf("?") != -1) { requestPath = requestPath.substring(0, requestPath.indexOf("?")); } Optional<? extends RestxRequestMatch> match = matcher.match(request.getMethod(), requestPath); if (match.isPresent()) { specs.add(spec.getKey()); break; } } } } return specs; }
Iterable<WhenHttpRequest> findWhensMatchingRequest( ImmutableMap<String, RestxSpec> allSpecs, RestxRequest restxRequest) { Collection<WhenHttpRequest> matchingRequestsSpecs = Lists.newArrayList(); for (Map.Entry<String, RestxSpec> spec : allSpecs.entrySet()) { for (When when : spec.getValue().getWhens()) { if (when instanceof WhenHttpRequest) { WhenHttpRequest request = (WhenHttpRequest) when; String requestPath = request.getPath(); if (!requestPath.startsWith("/")) { requestPath = "/" + requestPath; } StdRequest stdRequest = StdRequest.builder() .setBaseUri("http://restx.io") // baseUri is required but we won't use it .setHttpMethod(request.getMethod()) .setFullPath(requestPath) .build(); if (restxRequest.getHttpMethod().equals(stdRequest.getHttpMethod()) && restxRequest.getRestxPath().equals(stdRequest.getRestxPath())) { MapDifference<String, ImmutableList<String>> difference = Maps.difference(restxRequest.getQueryParams(), stdRequest.getQueryParams()); if (difference.entriesOnlyOnRight().isEmpty() && difference.entriesDiffering().isEmpty()) { matchingRequestsSpecs.add(request); break; } } } } } return matchingRequestsSpecs; }
synchronized ImmutableMap<String, RestxSpec> findAllSpecs() { if (allSpecs == null) { allSpecs = ImmutableMap.copyOf(buildSpecsMap(false)); } return allSpecs; }