PreCalibratedMulticurveFn implements DiscountingMulticurveCombinerFn,
    ScenarioFunction<PreCalibratedMulticurveArguments, PreCalibratedMulticurveFn> { @Override public Result<MulticurveBundle> getMulticurveBundle(Environment env, TradeWrapper<?> trade) { List<PreCalibratedMulticurveArguments> scenarioArguments = env.getScenarioArguments(this); if (scenarioArguments.size() != 1) { return Result.failure(FailureStatus.MISSING_DATA, "One instance of PreCalibratedMulticurveArguments expected " + "in the scenario arguments, found {}", scenarioArguments.size()); } List<MulticurveProviderDiscount> multicurves = new ArrayList<>(); List<CurveBuildingBlockBundle> blockBundles = new ArrayList<>(); List<Result<?>> failures = new ArrayList<>(); Set<String> curveNames = _curveSelector.getMulticurveNames(trade.getTrade()); if (curveNames.isEmpty()) { return Result.failure(FailureStatus.MISSING_DATA, "No matching curves found for trade: {}", trade); } if (curveNames.size() == 1) { return scenarioArguments.get(0).getMulticurveBundle(curveNames.iterator().next()); } for (String curveName : curveNames) { Result<MulticurveBundle> multicurve = scenarioArguments.get(0).getMulticurveBundle(curveName); if (multicurve.isSuccess()) { multicurves.add(multicurve.getValue().getMulticurveProvider()); blockBundles.add(multicurve.getValue().getCurveBuildingBlockBundle()); } else { failures.add(multicurve); } } if (!failures.isEmpty()) { return Result.failure(failures); } MulticurveProviderDiscount multicurve = ProviderUtils.mergeDiscountingProviders(multicurves); CurveBuildingBlockBundle mergedBlockBundle = new CurveBuildingBlockBundle(); for (CurveBuildingBlockBundle blockBundle : blockBundles) { mergedBlockBundle.addAll(blockBundle); } return Result.success(new MulticurveBundle(multicurve, mergedBlockBundle)); } PreCalibratedMulticurveFn(CurveSelector curveSelector); @Override Result<Pair<MulticurveProviderDiscount, CurveBuildingBlockBundle>> createMergedMulticurveBundle(
      Environment env, TradeWrapper trade, FXMatrix fxMatrix); @Override Result<MulticurveBundle> getMulticurveBundle(Environment env, TradeWrapper<?> trade); @Override Class<PreCalibratedMulticurveArguments> getArgumentType();  }
@Test public void noCurvesForTrade() { CurveSelector curveSelector = mock(CurveSelector.class); when(curveSelector.getMulticurveNames(TRADE.getTrade())).thenReturn(Collections.<String>emptySet()); PreCalibratedMulticurveFn fn = new PreCalibratedMulticurveFn(curveSelector); assertFalse(fn.getMulticurveBundle(ENV, TRADE).isSuccess()); }
@Test public void wrongNumberOfScenarioArgs() { Map<String, MulticurveBundle> emptyCurves = Collections.emptyMap(); PreCalibratedMulticurveArguments args1 = new PreCalibratedMulticurveArguments(emptyCurves); PreCalibratedMulticurveArguments args2 = new PreCalibratedMulticurveArguments(emptyCurves); FilteredScenarioDefinition scenarioDefinition = new FilteredScenarioDefinition(args1, args2); SimpleEnvironment env = new SimpleEnvironment(ZonedDateTime.now(), mock(MarketDataBundle.class), scenarioDefinition); Set<String> curveNames = ImmutableSet.of("a curve"); CurveSelector curveSelector = mock(CurveSelector.class); when(curveSelector.getMulticurveNames(TRADE.getTrade())).thenReturn(curveNames); PreCalibratedMulticurveFn fn = new PreCalibratedMulticurveFn(curveSelector); assertFalse(fn.getMulticurveBundle(env, TRADE).isSuccess()); }
@Test public void expectedCurveNotAvailable() { Map<String, MulticurveBundle> curves = new HashMap<>(); LinkedHashMap<String, Pair<CurveBuildingBlock, DoubleMatrix2D>> linkedMap = new LinkedHashMap<>(); MulticurveBundle multicurveBundle = new MulticurveBundle(new MulticurveProviderDiscount(), new CurveBuildingBlockBundle(linkedMap)); curves.put("a curve", multicurveBundle); PreCalibratedMulticurveArguments args = new PreCalibratedMulticurveArguments(curves); FilteredScenarioDefinition scenarioDefinition = new FilteredScenarioDefinition(args); SimpleEnvironment env = new SimpleEnvironment(ZonedDateTime.now(), mock(MarketDataBundle.class), scenarioDefinition); Set<String> curveNames = ImmutableSet.of("a different curve"); CurveSelector curveSelector = mock(CurveSelector.class); when(curveSelector.getMulticurveNames(TRADE.getTrade())).thenReturn(curveNames); PreCalibratedMulticurveFn fn = new PreCalibratedMulticurveFn(curveSelector); assertFalse(fn.getMulticurveBundle(env, TRADE).isSuccess()); }
@Test public void singleCurveFound() { Map<String, MulticurveBundle> curves = new HashMap<>(); LinkedHashMap<String, Pair<CurveBuildingBlock, DoubleMatrix2D>> linkedMap = new LinkedHashMap<>(); MulticurveBundle multicurveBundle = new MulticurveBundle(new MulticurveProviderDiscount(), new CurveBuildingBlockBundle(linkedMap)); curves.put("a curve", multicurveBundle); PreCalibratedMulticurveArguments args = new PreCalibratedMulticurveArguments(curves); FilteredScenarioDefinition scenarioDefinition = new FilteredScenarioDefinition(args); SimpleEnvironment env = new SimpleEnvironment(ZonedDateTime.now(), mock(MarketDataBundle.class), scenarioDefinition); Set<String> curveNames = ImmutableSet.of("a curve"); CurveSelector curveSelector = mock(CurveSelector.class); when(curveSelector.getMulticurveNames(TRADE.getTrade())).thenReturn(curveNames); PreCalibratedMulticurveFn fn = new PreCalibratedMulticurveFn(curveSelector); assertTrue(fn.getMulticurveBundle(env, TRADE).isSuccess()); }
@Test public void multipleCurvesFound() { Map<String, MulticurveBundle> curves = new HashMap<>(); LinkedHashMap<String, Pair<CurveBuildingBlock, DoubleMatrix2D>> linkedMap1 = new LinkedHashMap<>(); MulticurveBundle multicurveBundle1 = new MulticurveBundle(new MulticurveProviderDiscount(), new CurveBuildingBlockBundle(linkedMap1)); curves.put("curve1", multicurveBundle1); LinkedHashMap<String, Pair<CurveBuildingBlock, DoubleMatrix2D>> linkedMap2 = new LinkedHashMap<>(); MulticurveBundle multicurveBundle2 = new MulticurveBundle(new MulticurveProviderDiscount(), new CurveBuildingBlockBundle(linkedMap2)); curves.put("curve2", multicurveBundle2); PreCalibratedMulticurveArguments args = new PreCalibratedMulticurveArguments(curves); FilteredScenarioDefinition scenarioDefinition = new FilteredScenarioDefinition(args); SimpleEnvironment env = new SimpleEnvironment(ZonedDateTime.now(), mock(MarketDataBundle.class), scenarioDefinition); Set<String> curveNames = ImmutableSet.of("curve1", "curve2"); CurveSelector curveSelector = mock(CurveSelector.class); when(curveSelector.getMulticurveNames(TRADE.getTrade())).thenReturn(curveNames); PreCalibratedMulticurveFn fn = new PreCalibratedMulticurveFn(curveSelector); Result<MulticurveBundle> result = fn.getMulticurveBundle(env, TRADE); assertTrue(result.isSuccess()); }