ConfigDBInstrumentExposuresProvider implements InstrumentExposuresProvider { @Override public Set<String> getCurveConstructionConfigurationsForConfig(String instrumentExposureConfigurationName, Trade trade) { ArgumentChecker.notNull(instrumentExposureConfigurationName, "instrument exposure configuration name"); ArgumentChecker.notNull(trade, "trade"); final ExposureFunctions exposures = _query.get(instrumentExposureConfigurationName); if (exposures == null) { throw new OpenGammaRuntimeException("Could not get instrument exposure configuration called " + instrumentExposureConfigurationName); } final List<String> exposureFunctionNames = exposures.getExposureFunctions(); List<ExternalId> ids = null; final Set<String> curveConstructionConfigurationNames = new HashSet<>(); Multimap<String, ExternalId> functionToIds = LinkedHashMultimap.create(); for (final String exposureFunctionName : exposureFunctionNames) { final ExposureFunction exposureFunction = ExposureFunctionFactory.getExposureFunction(_securitySource, exposureFunctionName); ids = exposureFunction.getIds(trade); if (ids != null) { final Map<ExternalId, String> idsToNames = exposures.getIdsToNames(); functionToIds.putAll(exposureFunctionName, ids); for (final ExternalId id : ids) { final String name = idsToNames.get(id); if (name == null) { continue; } curveConstructionConfigurationNames.add(name); } if (!curveConstructionConfigurationNames.isEmpty()) { return curveConstructionConfigurationNames; } } } Security security = trade.getSecurity(); throw new OpenGammaRuntimeException("Could not find a matching list of ids for " + security.getClass().getSimpleName() + "/" + security.getExternalIdBundle() + " from ExposureFunctions object '" + instrumentExposureConfigurationName + "'. Ids attempted for referenced functions: " + functionToIds); } @Deprecated  ConfigDBInstrumentExposuresProvider(final ConfigSource configSource, final SecuritySource securitySource);  ConfigDBInstrumentExposuresProvider(final ConfigSource configSource, final SecuritySource securitySource, final VersionCorrection configVersionCorrection); private  ConfigDBInstrumentExposuresProvider(final ConfigSourceQuery<ExposureFunctions> query, final SecuritySource securitySource); static ConfigDBInstrumentExposuresProvider init(final FunctionCompilationContext context, final FunctionDefinition function); @Override Set<String> getCurveConstructionConfigurationsForConfig(String instrumentExposureConfigurationName,
                                                                 Trade trade);  }
@Test public void testEmptyCurveConfigs() { ConfigMaster configMaster = new InMemoryConfigMaster(); ConfigSource configSource = new MasterConfigSource(configMaster); SecuritySource securitySource = new MasterSecuritySource(new InMemorySecurityMaster()); String name = "test"; List<String> exposureFunctions = Lists.newArrayList(CurrencyExposureFunction.NAME); Map<ExternalId, String> idsToNames = Maps.newHashMap(); ExposureFunctions exposures = new ExposureFunctions(name, exposureFunctions, idsToNames); ConfigMasterUtils.storeByName(configMaster, ConfigItem.of(exposures)); ConfigDBInstrumentExposuresProvider provider = new ConfigDBInstrumentExposuresProvider(configSource, securitySource, VersionCorrection.LATEST); FRASecurity security = ExposureFunctionTestHelper.getFRASecurity(); Trade trade = new SimpleTrade(security, BigDecimal.ONE, new SimpleCounterparty(ExternalId.of(Counterparty.DEFAULT_SCHEME, "TEST")), LocalDate.now(), OffsetTime.now()); try { provider.getCurveConstructionConfigurationsForConfig(name, trade); fail("Expected exception for empty curve configs"); } catch (OpenGammaRuntimeException e) { } }
@Test public void testMultipleCurveConfigs() { ConfigMaster configMaster = new InMemoryConfigMaster(); ConfigSource configSource = new MasterConfigSource(configMaster); SecuritySource securitySource = new MasterSecuritySource(new InMemorySecurityMaster()); FRASecurity security = ExposureFunctionTestHelper.getFRASecurity(); String name = "test"; List<String> exposureFunctions = Lists.newArrayList(SecurityTypeExposureFunction.NAME, CurrencyExposureFunction.NAME); Map<ExternalId, String> idsToNames = new HashMap<>(); String securityTypeCurveConfig = "SecurityTypeConfig"; idsToNames.put(ExternalId.of(ExposureFunction.SECURITY_IDENTIFIER, security.getSecurityType()), securityTypeCurveConfig); String currencyCurveConfig = "CurrencyConfig"; idsToNames.put(ExternalId.of(Currency.OBJECT_SCHEME, security.getCurrency().getCode()), currencyCurveConfig); ExposureFunctions exposures = new ExposureFunctions(name, exposureFunctions, idsToNames); ConfigMasterUtils.storeByName(configMaster, ConfigItem.of(exposures)); ConfigDBInstrumentExposuresProvider provider = new ConfigDBInstrumentExposuresProvider(configSource, securitySource, VersionCorrection.LATEST); Trade trade = new SimpleTrade(security, BigDecimal.ONE, new SimpleCounterparty(ExternalId.of(Counterparty.DEFAULT_SCHEME, "TEST")), LocalDate.now(), OffsetTime.now()); Set<String> curveConfigs = provider.getCurveConstructionConfigurationsForConfig(name, trade); assertEquals("Expected single curve config", 1, curveConfigs.size()); assertTrue("Expected configs to contain security type config", curveConfigs.contains(securityTypeCurveConfig)); assertFalse("Expected configs to not contain currency config", curveConfigs.contains(currencyCurveConfig)); }