ConfigJsonBuilder { @SuppressWarnings("unchecked") public FunctionModelConfig getConfigFromJson(Map<String, Object> json) { Map<String, String> implsJson = (Map<String, String>) json.get(IMPLS); Map<Class<?>, Class<?>> impls = new HashMap<>(); for (Map.Entry<String, String> entry : implsJson.entrySet()) { String fnType = entry.getKey(); String implType = entry.getValue(); if (!StringUtils.isEmpty(fnType) && !StringUtils.isEmpty(implType)) { try { impls.put(Class.forName(fnType), Class.forName(implType)); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } } } Map<String, Map<String, String>> argsJson = (Map<String, Map<String, String>>) json.get(ARGS); Map<Class<?>, FunctionArguments> args = new HashMap<>(); for (Map.Entry<String, Map<String, String>> entry : argsJson.entrySet()) { String fnTypeName = entry.getKey(); Class<?> fnType; try { fnType = Class.forName(fnTypeName); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } Map<String, String> fnArgStrs = entry.getValue(); Map<String, Object> fnArgs = new HashMap<>(); for (Map.Entry<String, String> fnArgEntry : fnArgStrs.entrySet()) { String paramName = fnArgEntry.getKey(); String paramValueStr = fnArgEntry.getValue(); if (StringUtils.isEmpty(paramValueStr)) { continue; } Parameter parameter = Parameter.named(paramName, fnType); if (EngineUtils.isConfig(parameter.getType())) { fnArgs.put(paramName, ConfigLink.resolvable(paramValueStr, parameter.getType())); } else if (_argumentConverter.isConvertible(parameter.getParameterType())) { fnArgs.put(paramName, _argumentConverter.convertFromString(parameter.getParameterType(), paramValueStr)); } else { throw new IllegalArgumentException("Cannot convert from string to parameter type " + parameter.getParameterType()); } } FunctionArguments simpleFunctionArguments = new FunctionArguments(fnArgs); args.put(fnType, simpleFunctionArguments); } return new FunctionModelConfig(impls, args); } 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);  }
@SuppressWarnings("unchecked") @Test public void createConfig() { Gson gson = new Gson(); JsonElement element = _expected.getAsJsonObject().get("createConfig"); Map<String, Object> map = gson.fromJson(element, Map.class); ConfigJsonBuilder builder = new ConfigJsonBuilder(new AvailableOutputsImpl(), new AvailableImplementationsImpl(), mock(ConfigMaster.class), new DefaultArgumentConverter()); FunctionModelConfig config = builder.getConfigFromJson(map); Map<String, Object> argsMap = ImmutableMap.<String, Object>of("arg1", "value1", "arg2", ImmutableList.of(1, 2, 3)); FunctionArguments simpleFunctionArguments = new FunctionArguments(argsMap); Map<Class<?>, FunctionArguments> fnArgsMap = ImmutableMap.<Class<?>, FunctionArguments>of(ImplWithArgs.class, simpleFunctionArguments); FunctionModelConfig expected = new FunctionModelConfig(ImmutableMap.<Class<?>, Class<?>>of(Fn.class, ImplWithArgs.class), fnArgsMap); assertEquals(expected, config); }
@SuppressWarnings("unchecked") @Test public void createConfigWithConfigArg() { Gson gson = new Gson(); JsonElement element = _expected.getAsJsonObject().get("createConfigWithConfigArg"); Map<String, Object> map = gson.fromJson(element, Map.class); ConfigJsonBuilder builder = new ConfigJsonBuilder(new AvailableOutputsImpl(), new AvailableImplementationsImpl(), mock(ConfigMaster.class), new DefaultArgumentConverter()); FunctionModelConfig config = builder.getConfigFromJson(map); ConfigLink<ConfigObject> configLink = ConfigLink.resolvable("configName", ConfigObject.class); Map<String, Object> argsMap = ImmutableMap.<String, Object>of("configArg", configLink); FunctionArguments simpleFunctionArguments = new FunctionArguments(argsMap); Map<Class<?>, FunctionArguments> fnArgsMap = ImmutableMap.<Class<?>, FunctionArguments>of(UsesConfig.class, simpleFunctionArguments); FunctionModelConfig expected = new FunctionModelConfig(ImmutableMap.<Class<?>, Class<?>>of(Fn.class, UsesConfig.class), fnArgsMap); assertEquals(expected, config); }