GraphModel { public Graph build(ComponentMap components, FunctionBuilder functionBuilder) { ImmutableMap.Builder<String, Map<Class<?>, InvokableFunction>> portfolioFunctions = ImmutableMap.builder(); for (Map.Entry<String, Map<Class<?>, FunctionModel>> entry : _portfolioFunctionModels.entrySet()) { Map<Class<?>, FunctionModel> functionsByTargetId = entry.getValue(); ImmutableMap.Builder<Class<?>, InvokableFunction> columnBuilder = ImmutableMap.builder(); for (Map.Entry<Class<?>, FunctionModel> columnEntry : functionsByTargetId.entrySet()) { Class<?> inputType = columnEntry.getKey(); FunctionModel functionModel = columnEntry.getValue(); if (functionModel.isValid()) { columnBuilder.put(inputType, functionModel.build(functionBuilder, components)); } else { s_logger.warn("Can't build invalid function model{}", functionModel.prettyPrint()); FunctionModel noOutputFunctionModel = FunctionModel.forFunction(ConfigurationErrorFunction.METADATA); columnBuilder.put(inputType, noOutputFunctionModel.build(functionBuilder, components)); } } String columnName = entry.getKey(); portfolioFunctions.put(columnName, columnBuilder.build()); } ImmutableMap.Builder<String, InvokableFunction> nonPortfolioFunctions = ImmutableMap.builder(); for (Map.Entry<String, FunctionModel> entry : _nonPortfolioFunctionModels.entrySet()) { String name = entry.getKey(); FunctionModel functionModel = entry.getValue(); if (functionModel.isValid()) { nonPortfolioFunctions.put(name, functionModel.build(functionBuilder, components)); } else { s_logger.warn("Can't build invalid function model{}", functionModel.prettyPrint()); FunctionModel noOutputFunctionModel = FunctionModel.forFunction(ConfigurationErrorFunction.METADATA); nonPortfolioFunctions.put(name, noOutputFunctionModel.build(functionBuilder, components)); } } return new Graph(portfolioFunctions.build(), nonPortfolioFunctions.build()); } GraphModel(Map<String, Map<Class<?>, FunctionModel>> portfolioFunctionModels,
                           Map<String, FunctionModel> nonPortfolioFunctionModels); Graph build(ComponentMap components, FunctionBuilder functionBuilder); FunctionModel getFunctionModel(String columnName, Class<?> inputType); FunctionModel getFunctionModel(String outputName);  }
@Test public void invalidPortfolioFunction() { FunctionMetadata metadata = EngineUtils.createMetadata(PortfolioFn.class, "foo"); FunctionModel functionModel = FunctionModel.forFunction(metadata); Map<Class<?>, FunctionModel> fnMap = ImmutableMap.<Class<?>, FunctionModel>of(FXForwardSecurity.class, functionModel); String columnName = "col name"; Map<String, Map<Class<?>, FunctionModel>> colMap = ImmutableMap.of(columnName, fnMap); GraphModel graphModel = new GraphModel(colMap, Collections.<String, FunctionModel>emptyMap()); Graph graph = graphModel.build(ComponentMap.EMPTY, new FunctionBuilder()); Map<Class<?>, InvokableFunction> functionsForColumn = graph.getFunctionsForColumn(columnName); InvokableFunction invokableFunction = functionsForColumn.get(FXForwardSecurity.class); assertNotNull(invokableFunction); SimpleEnvironment env = new SimpleEnvironment(ZonedDateTime.now(), mock(MarketDataBundle.class)); Result<?> result = (Result<?>) invokableFunction.invoke(env, null, FunctionArguments.EMPTY); assertFalse(result.isSuccess()); assertEquals(FailureStatus.ERROR, result.getStatus()); assertEquals(ConfigurationErrorFunction.CONFIG_ERROR, result.getFailureMessage()); }
@Test public void invalidNonPortfolioFunction() { FunctionMetadata metadata = EngineUtils.createMetadata(NonPortfolioFn.class, "foo"); FunctionModel functionModel = FunctionModel.forFunction(metadata); Map<String, Map<Class<?>, FunctionModel>> portfolioFunctionModels = Collections.emptyMap(); String outputName = "output name"; GraphModel graphModel = new GraphModel(portfolioFunctionModels, ImmutableMap.of(outputName, functionModel)); Graph graph = graphModel.build(ComponentMap.EMPTY, new FunctionBuilder()); InvokableFunction invokableFunction = graph.getNonPortfolioFunction(outputName); assertNotNull(invokableFunction); SimpleEnvironment env = new SimpleEnvironment(ZonedDateTime.now(), mock(MarketDataBundle.class)); Result<?> result = (Result<?>) invokableFunction.invoke(env, null, FunctionArguments.EMPTY); assertFalse(result.isSuccess()); assertEquals(FailureStatus.ERROR, result.getStatus()); assertEquals(ConfigurationErrorFunction.CONFIG_ERROR, result.getFailureMessage()); }