OrderingHelper { public static void sort( final List<BaseDimensionalItemObject> dimensionalItems, final OrderParams sortingParams ) { if ( sortingParams != null && isNotEmpty( dimensionalItems ) ) { final ComparatorChain<BaseDimensionalItemObject> chainOfComparators = new ComparatorChain<>(); final Set<String> orderingPairs = sortingParams.getOrders(); if ( sortingParams != null && isNotEmpty( orderingPairs ) ) { for ( final String orderingPair : orderingPairs ) { chainOfComparators.addComparator( getComparator( orderingPair ) ); } dimensionalItems.sort( chainOfComparators ); } } }  static void sort( final List<BaseDimensionalItemObject> dimensionalItems, final OrderParams sortingParams );  }
@Test public void sortWhenDimensionalItemsIsEmpty() { final List<BaseDimensionalItemObject> emptyDimensionalItems = emptyList(); final OrderParams orderParams = new OrderParams(); sort( emptyDimensionalItems, orderParams ); assertThat( emptyDimensionalItems, is( emptyList() ) ); }
@Test public void sortWhenOrderParamsIsNull() { final List<BaseDimensionalItemObject> anyDimensionalItems = mockDimensionalItems( 2 ); final List<BaseDimensionalItemObject> unchangedList = mockDimensionalItems( 2 ); final OrderParams nullOrderParams = new OrderParams(); sort( anyDimensionalItems, nullOrderParams ); assertEquals( anyDimensionalItems, unchangedList ); }
@Test public void sortWhenOrderParamsIsAsc() { final Set<String> orderings = new HashSet<>( asList( "name:asc" ) ); final OrderParams orderParams = new OrderParams( orderings ); final List<BaseDimensionalItemObject> anyDimensionalItems = mockDimensionalItems( 2 ); final List<BaseDimensionalItemObject> ascList = mockDimensionalItems( 2 ); Collections.sort( ascList ); sort( anyDimensionalItems, orderParams ); assertEquals( anyDimensionalItems, ascList ); }
@Test public void sortWhenOrderParamsIsDesc() { final Set<String> orderings = new HashSet<>( asList( "name:desc" ) ); final OrderParams orderParams = new OrderParams( orderings ); final List<BaseDimensionalItemObject> anyDimensionalItems = mockDimensionalItems( 2 ); final List<BaseDimensionalItemObject> ascList = mockDimensionalItems( 2 ); Collections.reverse( ascList ); sort( anyDimensionalItems, orderParams ); assertEquals( anyDimensionalItems, ascList ); }
@Test public void sortWhenOrderParamsIsInvalid() { final Set<String> orderingWithNoValue = new HashSet<>( asList( "name:" ) ); final OrderParams orderParams = new OrderParams( orderingWithNoValue ); final List<BaseDimensionalItemObject> anyDimensionalItems = mockDimensionalItems( 2 ); expectedException.expect( IllegalQueryException.class ); expectedException.expectMessage( "Unable to parse order param: `" + "name:" + "`" ); sort( anyDimensionalItems, orderParams ); }