ServiceInfo { @VisibleForTesting static Set<MethodInfo> mergeEndpoints(Iterable<MethodInfo> methodInfos) { final Map<List<Object>, MethodInfo> methodInfoMap = new HashMap<>(); for (MethodInfo methodInfo : methodInfos) { final List<Object> mergeKey = ImmutableList.of(methodInfo.name(), methodInfo.httpMethod()); methodInfoMap.compute(mergeKey, (key, value) -> { if (value == null) { return methodInfo; } else { final Set<EndpointInfo> endpointInfos = Sets.union(value.endpoints(), methodInfo.endpoints()); return new MethodInfo(value.name(), value.returnTypeSignature(), value.parameters(), value.exceptionTypeSignatures(), endpointInfos, value.exampleHeaders(), value.exampleRequests(), value.examplePaths(), value.exampleQueries(), value.httpMethod(), value.docString()); } }); } return ImmutableSortedSet .orderedBy(comparing(MethodInfo::name).thenComparing(MethodInfo::httpMethod)) .addAll(methodInfoMap.values()) .build(); } ServiceInfo(String name,
                       Iterable<MethodInfo> methods);  ServiceInfo(String name,
                       Iterable<MethodInfo> methods,
                       @Nullable String docString);  ServiceInfo(String name,
                       Iterable<MethodInfo> methods,
                       Iterable<HttpHeaders> exampleHeaders,
                       @Nullable String docString); @JsonProperty String name(); @JsonProperty Set<MethodInfo> methods(); Set<TypeSignature> findNamedTypes(); @JsonProperty @JsonInclude(Include.NON_NULL) @Nullable String docString(); @JsonProperty List<HttpHeaders> exampleHeaders(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); @Override String toString();  }
@Test void testCollectMethodsWithDifferentMethods() { final MethodInfo barMethodInfo = createMethodInfo("bar", HttpMethod.GET, "exact:/bar"); final MethodInfo fooMethodInfo = createMethodInfo("foo", HttpMethod.GET, "exact:/foo"); final List<MethodInfo> inputMethodInfos = ImmutableList.of(barMethodInfo, fooMethodInfo); assertThat(ServiceInfo.mergeEndpoints(inputMethodInfos)).isEqualTo(inputMethodInfos); }
@Test void testCollectMethodGrouping() { final MethodInfo barGet1 = createMethodInfo("bar", HttpMethod.GET, "exact:/bar1"); final MethodInfo barGet2 = createMethodInfo("bar", HttpMethod.GET, "exact:/bar2"); final MethodInfo barPost3 = createMethodInfo("bar", HttpMethod.POST, "exact:/bar3"); final MethodInfo barPost4 = createMethodInfo("bar", HttpMethod.POST, "exact:/bar4"); final MethodInfo fooGet1 = createMethodInfo("foo", HttpMethod.GET, "exact:/foo1"); final MethodInfo fooGet2 = createMethodInfo("foo", HttpMethod.GET, "exact:/foo2"); final MethodInfo fooPost3 = createMethodInfo("foo", HttpMethod.POST, "exact:/foo3"); final MethodInfo fooPost4 = createMethodInfo("foo", HttpMethod.POST, "exact:/foo4"); final List<MethodInfo> inputMethodInfos = ImmutableList.of(barGet1, barGet2, barPost3, barPost4, fooGet1, fooGet2, fooPost3, fooPost4); final List<MethodInfo> collectMethods = ImmutableList.copyOf(ServiceInfo.mergeEndpoints(inputMethodInfos)); assertThat(collectMethods).hasSize(4); final Function<MethodInfo, Set<String>> getPaths = methodInfo -> methodInfo.endpoints().stream().map(EndpointInfo::pathMapping).collect(Collectors.toSet()); assertThat(getPaths.apply(collectMethods.get(0))).containsExactly("exact:/bar1", "exact:/bar2"); assertThat(getPaths.apply(collectMethods.get(1))).containsExactly("exact:/bar3", "exact:/bar4"); assertThat(getPaths.apply(collectMethods.get(2))).containsExactly("exact:/foo1", "exact:/foo2"); assertThat(getPaths.apply(collectMethods.get(3))).containsExactly("exact:/foo3", "exact:/foo4"); }