RestxSpecRepository { 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: .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; } RestxSpecRepository(RestxSpecLoader specLoader); Iterable<String> findAll(); Optional<RestxSpec> findSpecById(String id); Iterable<String> findSpecsByOperation(String httpMethod, String path); Iterable<WhenHttpRequest> findSpecsByRequest(RestxRequest request);  }
@Test public void should_find_specs_for_request() throws Exception { RestxSpecRepository resource = getRestxSpecRepository(); WhenHttpRequest when1; WhenHttpRequest when2; WhenHttpRequest when3; ImmutableMap<String, RestxSpec> allSpecs = ImmutableMap.of( "cases/test/test.spec.yaml", spec("should say hello on path", when1 = when("GET", "/messages/xavier")), "cases/test/test2.spec.yaml", spec("should say hello w/ query param", when2 = when("GET", "/messages?who=xavier")), "cases/test/test3.spec.yaml", spec("should say hello w/ other query param", when3 = when("GET", "/messages?who=jules")) ); assertThat(resource.findWhensMatchingRequest(allSpecs, request("GET", "/messages/xavier"))) .isNotEmpty().containsExactly(when1); assertThat(resource.findWhensMatchingRequest(allSpecs, request("GET", "/messages?who=xavier"))) .isNotEmpty().containsExactly(when2); assertThat(resource.findWhensMatchingRequest(allSpecs, request("GET", "/messages?who=xavier&test=anything"))) .isNotEmpty().containsExactly(when2); assertThat(resource.findWhensMatchingRequest(allSpecs, request("GET", "/messages?who=jules"))) .isNotEmpty().containsExactly(when3); }