### Question:
JpaSortBuilder { public List<Order> buildSortOrders(SortRequest sortRequest, JpaPredicateVisitor<T> visitor) { if (null == sortRequest || null == sortRequest.getProperties()) { return Collections.emptyList(); } CriteriaBuilder builder = visitor.getCriteriaBuilder(); List<SortRequestProperty> sortProperties = sortRequest.getProperties(); List<Order> sortOrders = new ArrayList<>(sortProperties.size()); for (SortRequestProperty sort : sortProperties) { String propertyId = sort.getPropertyId(); List<? extends SingularAttribute<?, ?>> singularAttributes = visitor.getPredicateMapping(propertyId); if (null == singularAttributes || singularAttributes.size() == 0) { continue; } Path<?> path = null; for (SingularAttribute<?, ?> singularAttribute : singularAttributes) { if (null == path) { CriteriaQuery<T> query = visitor.getCriteriaQuery(); Set<Root<?>> roots = query.getRoots(); if (null != roots && !roots.isEmpty()) { Iterator<Root<?>> iterator = roots.iterator(); while (iterator.hasNext()) { Root<?> root = iterator.next(); Class<?> visitorEntityClass = visitor.getEntityClass(); if (ObjectUtils.equals(visitorEntityClass, root.getJavaType()) || ObjectUtils.equals(visitorEntityClass, root.getModel().getJavaType())) { path = root.get(singularAttribute.getName()); break; } } } if (null == path) { path = query.from(visitor.getEntityClass()).get(singularAttribute.getName()); } } else { path = path.get(singularAttribute.getName()); } } Order sortOrder = null; if (sort.getOrder() == org.apache.ambari.server.controller.spi.SortRequest.Order.ASC) { sortOrder = builder.asc(path); } else { sortOrder = builder.desc(path); } sortOrders.add(sortOrder); } return sortOrders; } JpaSortBuilder(); List<Order> buildSortOrders(SortRequest sortRequest,
      JpaPredicateVisitor<T> visitor);  }### Answer:
@Test public void testSortDoesNotAddExtraRootPaths() throws Exception { List<SortRequestProperty> sortRequestProperties = new ArrayList<>(); sortRequestProperties.add( new SortRequestProperty(AlertHistoryResourceProvider.ALERT_HISTORY_TIMESTAMP, org.apache.ambari.server.controller.spi.SortRequest.Order.ASC)); SortRequest sortRequest = new SortRequestImpl(sortRequestProperties); Predicate predicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_DEFINITION_NAME).equals("foo").toPredicate(); MockAlertHistoryredicateVisitor visitor = new MockAlertHistoryredicateVisitor(); PredicateHelper.visit(predicate, visitor); JpaSortBuilder<AlertHistoryEntity> sortBuilder = new JpaSortBuilder<>(); List<Order> sortOrders = sortBuilder.buildSortOrders(sortRequest, visitor); Assert.assertEquals(sortOrders.size(), 1); CriteriaQuery<AlertHistoryEntity> query = visitor.getCriteriaQuery(); Set<Root<?>> roots = query.getRoots(); Assert.assertEquals(1, roots.size()); }