### Question:
SortDescription { public void sort(GraphTraversal<Vertex, Vertex> searchResult, List<SortParameter> sortParameters) { if (sortParameters.isEmpty()) { return; } List<SortTraversal> traversals = collectTraversals(sortParameters); if (traversals.isEmpty()) { return; } searchResult.order(); for (SortTraversal sortTraversal : traversals) { for (GraphTraversal<Object, Object> traversal : sortTraversal.traversals) { searchResult.by(traversal, sortTraversal.order); } } } SortDescription(List<SortFieldDescription> sortFieldDescriptions); void sort(GraphTraversal<Vertex, Vertex> searchResult, List<SortParameter> sortParameters);  }### Answer:
@Test @SuppressWarnings("unchecked") public void sortAddsASortParametersThatSortsTheVertices() { GraphTraversal<Vertex, Vertex> traversal = newGraph() .withVertex(v -> v.withTimId("id1").withProperty(PROPERTY_1, "value1.2")) .withVertex(v -> v.withTimId("id2").withProperty(PROPERTY_1, "value1.1")) .withVertex(v -> v.withTimId("id3").withProperty(PROPERTY_1, "value1")) .build() .traversal() .V(); List<SortParameter> sortParameters = Lists.newArrayList(new SortParameter(SORT_FIELD_1, asc)); instance.sort(traversal, sortParameters); List<Vertex> actual = traversal.toList(); assertThat(actual, contains( likeVertex().withTimId("id3"), likeVertex().withTimId("id2"), likeVertex().withTimId("id1"))); }
@Test @SuppressWarnings("unchecked") public void sortAddsASortParameterToTheSearchResultsForEachOfTheSortParameters() { GraphTraversal<Vertex, Vertex> traversal = newGraph() .withVertex(v -> v.withTimId("id1") .withProperty(PROPERTY_1, "value1.2") .withProperty(PROPERTY_2, "value2")) .withVertex(v -> v.withTimId("id2") .withProperty(PROPERTY_1, "value1") .withProperty(PROPERTY_2, "value2.1")) .withVertex(v -> v.withTimId("id3") .withProperty(PROPERTY_1, "value1") .withProperty(PROPERTY_2, "value2")) .build() .traversal() .V(); List<SortParameter> sortParameters = Lists.newArrayList( new SortParameter(SORT_FIELD_1, asc), new SortParameter(SORT_FIELD_2, asc)); instance.sort(traversal, sortParameters); List<Vertex> actual = traversal.toList(); assertThat(actual, contains( likeVertex().withTimId("id3"), likeVertex().withTimId("id2"), likeVertex().withTimId("id1"))); }
@Test @SuppressWarnings("unchecked") public void sortDoesNothingWhenTheSortParametersAreEmpty() { GraphTraversal<Vertex, Vertex> traversal = mock(GraphTraversal.class); List<SortParameter> sortParameters = Lists.newArrayList(); instance.sort(traversal, sortParameters); verifyZeroInteractions(traversal); }
@Test @SuppressWarnings("unchecked") public void sortIgnoresUnknownSortParameters() { GraphTraversal<Vertex, Vertex> traversal = mock(GraphTraversal.class); List<SortParameter> sortParameters = Lists.newArrayList(new SortParameter("unknownField", asc)); instance.sort(traversal, sortParameters); verifyZeroInteractions(traversal); }