RestxSpecRepository { 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; } 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_operation() throws Exception { RestxSpecRepository resource = getRestxSpecRepository(); ImmutableMap<String, RestxSpec> allSpecs = ImmutableMap.of( "cases/test/test.spec.yaml", spec("should say hello on path", when("GET", "/messages/xavier")), "cases/test/test2.spec.yaml", spec("should say hello w/ query param", when("GET", "/messages?who=xavier"))); assertThat(resource.filterSpecsByOperation(allSpecs, "GET", "/messages/{id}")) .isNotEmpty().containsExactly("cases/test/test.spec.yaml"); assertThat(resource.filterSpecsByOperation(allSpecs, "GET", "/messages")) .isNotEmpty().containsExactly("cases/test/test2.spec.yaml"); }