FunctionsFunction implements Function<DataContainer> { @Override public DataContainer apply(List<Object> objects) { Optional<String> functionName = objects.size() == 1 && objects.get(0) != null ? Optional.of(String.valueOf(objects.get(0))) : Optional.empty(); List<Function<?>> functions = new ArrayList<>(); if (functionName.isPresent()) { Optional<Function<?>> functionCandidate = functionsAware.getFunction(functionName.get()); if (!functionCandidate.isPresent()) { throw new IllegalArgumentException(String.format("Function %s does not exist", functionName)); } functions.add(functionCandidate.get()); } else { functions.addAll(functionsAware.getAllFunctions()); } DataContainer dataContainer = new DataContainer(Arrays.asList(FUNCTION_NAME, DESCRIPTION)); functions.forEach(f -> { dataContainer.addRow(Arrays.asList( f.getSignature(), f.getDescription() )); }); return dataContainer; } @Autowired  FunctionsFunction(FunctionsAware functionsAware); @Override Set<String> validateInput(List<Object> args); @Override Class<DataContainer> getReturnType(); @Override FunctionName getName(); @Override DataContainer apply(List<Object> objects); @Override String getSignature(); @Override String getDescription();  }
@Test public void testListAllFunctions() { DataContainer data = function.apply(Collections.emptyList()); assertThat(data.getColumnNames(), contains("function_name", "description")); assertThat(data.getRows(), hasSize(FunctionName.values().length)); }
@Test public void testOneFunction() { DataContainer data = function.apply(Collections.singletonList(ABS.name())); assertThat(data.getColumnNames(), contains("function_name", "description")); assertThat(data.getRows(), hasSize(1)); }
@Test(expected = IllegalArgumentException.class) public void testMissingFunction() { function.apply(Collections.singletonList("missing_function")); }