ConfigJsonBuilder { public Map<String, Object> getConfigPageModel(String columnName, FunctionModelConfig config, @Nullable Class<?> inputType, @Nullable OutputName outputName, @Nullable FunctionModel model) { ArgumentChecker.notEmpty(columnName, "columnName"); List<Map<String, Object>> inputTypeList = new ArrayList<>(); for (Class<?> type : _availableOutputs.getInputTypes()) { inputTypeList.add(typeMap(type)); } Collections.sort(inputTypeList, TypeMapComparator.INSTANCE); Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put(COL_NAME, columnName); jsonMap.put(INPUT_TYPES, inputTypeList); if (inputType != null) { Set<OutputName> availableOutputs = _availableOutputs.getAvailableOutputs(inputType); List<String> outputNames = new ArrayList<>(availableOutputs.size()); for (OutputName availableOutput : availableOutputs) { outputNames.add(availableOutput.getName()); } Collections.sort(outputNames); jsonMap.put(INPUT_TYPE, typeMap(inputType)); jsonMap.put(OUTPUT_NAMES, outputNames); if (outputName != null && availableOutputs.contains(outputName)) { jsonMap.put(OUTPUT_NAME, outputName.getName()); } } List<Map<String, Object>> functions = getFunctions(config, model); if (!functions.isEmpty()) { jsonMap.put(FUNCTIONS, functions); } return jsonMap; } ConfigJsonBuilder(AvailableOutputs availableOutputs,
                    AvailableImplementations availableImplementations,
                    ConfigMaster configMaster,
                    ArgumentConverter argumentConverter); @SuppressWarnings("unchecked") FunctionModelConfig getConfigFromJson(Map<String, Object> json); Map<String, Object> getJsonFromConfig(FunctionModelConfig config); Map<String, Object> getConfigPageModel(String columnName,
                                                FunctionModelConfig config,
                                                @Nullable Class<?> inputType,
                                                @Nullable OutputName outputName,
                                                @Nullable FunctionModel model);  }
@Test public void fnWithMultipleImplsNoImplSelected() throws JSONException { AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableImplementations.register(ImplNoArgs.class, ImplWithArgs.class); FunctionModel model = FunctionModel.forFunction(FOO_META); ConfigJsonBuilder builder = new ConfigJsonBuilder(mock(AvailableOutputs.class), availableImplementations, mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, FunctionModelConfig.EMPTY, null, null, model); checkJson(json, "fnWithMultipleImplsNoImplSelected"); }
@Test public void fnWithMultipleImplsSelectedImplHasArgs() { AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableImplementations.register(ImplNoArgs.class, ImplWithArgs.class); FunctionModelConfig config = config( implementations(Fn.class, ImplWithArgs.class), arguments( function( ImplWithArgs.class, argument("arg1", "value1"), argument("arg2", ImmutableList.of(1, 2, 3))))); FunctionModel model = FunctionModel.forFunction(FOO_META, config); ConfigJsonBuilder builder = new ConfigJsonBuilder(mock(AvailableOutputs.class), availableImplementations, mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, config, null, null, model); checkJson(json, "fnWithMultipleImplsSelectedImplHasArgs"); }
@Test public void fnWithMultipleImplsSelectedImplHasNoArgs() { AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableImplementations.register(ImplNoArgs.class, ImplWithArgs.class); FunctionModelConfig config = config(implementations(Fn.class, ImplNoArgs.class)); FunctionModel model = FunctionModel.forFunction(FOO_META, config); ConfigJsonBuilder builder = new ConfigJsonBuilder(mock(AvailableOutputs.class), availableImplementations, mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, config, null, null, model); checkJson(json, "fnWithMultipleImplsSelectedImplHasNoArgs"); }
@Test public void concreteFunctionNoArgs() { FunctionMetadata metadata = EngineUtils.createMetadata(ConcreteNoArgs.class, "bar"); FunctionModel model = FunctionModel.forFunction(metadata); ConfigJsonBuilder builder = new ConfigJsonBuilder(mock(AvailableOutputs.class), new AvailableImplementationsImpl(), mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, FunctionModelConfig.EMPTY, null, null, model); checkJson(json, "concreteFunctionNoArgs"); }
@Test public void concreteFunctionWithArgs() { FunctionMetadata metadata = EngineUtils.createMetadata(ConcreteWithArgs.class, "baz"); FunctionModelConfig config = config( arguments( function( ConcreteWithArgs.class, argument("arg1", "value1"), argument("arg2", ImmutableList.of(1, 2, 3))))); FunctionModel model = FunctionModel.forFunction(metadata, config); ConfigJsonBuilder builder = new ConfigJsonBuilder(mock(AvailableOutputs.class), new AvailableImplementationsImpl(), mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, config, null, null, model); checkJson(json, "concreteFunctionWithArgs"); }
@Test public void missingArgument() { AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableImplementations.register(ImplNoArgs.class, ImplWithArgs.class); FunctionModelConfig config = config(implementations(Fn.class, ImplWithArgs.class), arguments(function(ImplWithArgs.class, argument("arg2", ImmutableList.of(1, 2, 3))))); FunctionModel model = FunctionModel.forFunction(FOO_META, config); ConfigJsonBuilder builder = new ConfigJsonBuilder(mock(AvailableOutputs.class), availableImplementations, mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, config, null, null, model); checkJson(json, "missingArgument"); }
@Test public void columnJsonNoInputOrOutput() { AvailableOutputs availableOutputs = new AvailableOutputsImpl(); AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableOutputs.register(Fn.class, Fn2.class, ConcreteNoArgs.class, ConcreteWithArgs.class); availableImplementations.register(ImplNoArgs.class, ImplWithArgs.class); ConfigJsonBuilder builder = new ConfigJsonBuilder(availableOutputs, availableImplementations, mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, FunctionModelConfig.EMPTY, null, null, null); checkJson(json, "columnJsonNoInputOrOutput"); }
@Test public void columnJsonInputNoOutput() { AvailableOutputs availableOutputs = new AvailableOutputsImpl(); AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableOutputs.register(Fn.class, Fn2.class, ConcreteNoArgs.class, ConcreteWithArgs.class); availableImplementations.register(ImplNoArgs.class, ImplWithArgs.class); ConfigJsonBuilder builder = new ConfigJsonBuilder(availableOutputs, availableImplementations, mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, FunctionModelConfig.EMPTY, Trade.class, null, null); checkJson(json, "columnJsonInputNoOutput"); }
@Test public void columnJsonInputAndOutput() { AvailableOutputs availableOutputs = new AvailableOutputsImpl(); AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableOutputs.register(Fn.class, Fn2.class, ConcreteNoArgs.class, ConcreteWithArgs.class); availableImplementations.register(ImplNoArgs.class, ImplWithArgs.class); ConfigJsonBuilder builder = new ConfigJsonBuilder(availableOutputs, availableImplementations, mock(ConfigMaster.class), new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, FunctionModelConfig.EMPTY, Trade.class, OutputName.of("Foo2"), null); checkJson(json, "columnJsonInputAndOutput"); }
@Test public void configArgNoSelection() { AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableImplementations.register(UsesConfig.class); FunctionModelConfig config = new FunctionModelConfig(availableImplementations.getDefaultImplementations()); FunctionModel model = FunctionModel.forFunction(FOO_META, config); ConfigMaster configMaster = mock(ConfigMaster.class); ConfigSearchRequest<?> searchRequest = new ConfigSearchRequest<>(); searchRequest.setType(ConfigObject.class); List<ConfigDocument> configs = Lists.newArrayList(configDocument("foo"), configDocument("bar"), configDocument("baz")); when(configMaster.search(searchRequest)).thenAnswer(new Returns(new ConfigSearchResult<>(configs))); ConfigJsonBuilder builder = new ConfigJsonBuilder(mock(AvailableOutputs.class), availableImplementations, configMaster, new DefaultArgumentConverter()); Map<String, Object> functionJson = builder.getConfigPageModel(COLUMN_NAME, config, null, null, model); checkJson(functionJson, "configArgNoSelection"); }
@Test(enabled = false) public void configArgValueSelected() { AvailableImplementations availableImplementations = new AvailableImplementationsImpl(); availableImplementations.register(UsesConfig.class); ConfigLink<ConfigObject> configLink = ConfigLink.resolvable("bar", ConfigObject.class); FunctionModelConfig config = config(implementations(Fn.class, UsesConfig.class), arguments(function(UsesConfig.class, argument("configArg", configLink)))); FunctionModel model = FunctionModel.forFunction(FOO_META, config); ConfigMaster configMaster = mock(ConfigMaster.class); ConfigSearchRequest<?> searchRequest = new ConfigSearchRequest<>(); searchRequest.setType(ConfigObject.class); List<ConfigDocument> configs = Lists.newArrayList(configDocument("foo"), configDocument("bar"), configDocument("baz")); when(configMaster.search(searchRequest)).thenAnswer(new Returns(new ConfigSearchResult<>(configs))); ConfigJsonBuilder builder = new ConfigJsonBuilder(mock(AvailableOutputs.class), availableImplementations, configMaster, new DefaultArgumentConverter()); Map<String, Object> json = builder.getConfigPageModel(COLUMN_NAME, config, null, null, model); checkJson(json, "configArgNoSelection"); }