_batchMaster.addValuesToMarketData(ObjectId.of("nonexistent", "nonexistent"), ImmutableSet.of(new MarketDataValue()));
}
@Test
public void addValuesToIncompleteSnapshot() {
final MarketData marketData = _batchWriter.createOrGetMarketDataInTransaction(_cycleMetadataStub.getMarketDataSnapshotId());
_batchMaster.addValuesToMarketData(marketData.getObjectId(), Collections.<MarketDataValue>emptySet());
List<MarketDataValue> marketDataValues = _batchMaster.getMarketDataValues(marketData.getObjectId(), PagingRequest.ALL).getFirst();
assertNotNull(marketDataValues);
assertEquals(0, marketDataValues.size());
final Set<ComputationTargetSpecification> specs = Sets.newHashSet();
specs.add(ComputationTargetSpecification.of(UniqueId.of("BUID", "EQ12345", null)));
specs.add(ComputationTargetSpecification.of(UniqueId.of("BUID", "EQ12346", "1")));
specs.add(ComputationTargetSpecification.of(UniqueId.of("BUID", "EQ12347", "2")));
final Map<ComputationTargetSpecification, Long> compTargetSpecIdx = new HashMap<ComputationTargetSpecification, Long>();
final Map<Long, ComputationTargetSpecification> reversedCompTargetSpecIdx = new HashMap<Long, ComputationTargetSpecification>();
_batchMaster.getDbConnector().getTransactionTemplate().execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(final TransactionStatus status) {
// manually populationg db with computation targets
for (final ComputationTargetSpecification spec : specs) {
final HbComputationTargetSpecification hbComputationTargetSpecification = _batchWriter.getOrCreateComputationTargetInTransaction(spec);
compTargetSpecIdx.put(spec, hbComputationTargetSpecification.getId());
reversedCompTargetSpecIdx.put(hbComputationTargetSpecification.getId(), spec);
}
return null;
}
});
Set<MarketDataValue> values = new HashSet<MarketDataValue>();
for (final ComputationTargetSpecification spec : specs) {
values.add(new MarketDataValue(spec, 123.45, "value_name"));
}
_batchMaster.addValuesToMarketData(marketData.getObjectId(), values);
marketDataValues = _batchMaster.getMarketDataValues(marketData.getObjectId(), PagingRequest.ALL).getFirst();
assertEquals(specs.size(), marketDataValues.size());
final Map<Long, MarketDataValue> marketDataValuesMap = newHashMap();
for (final MarketDataValue value : marketDataValues) {
marketDataValuesMap.put(value.getComputationTargetSpecificationId(), value);
}
for (final ComputationTargetSpecification spec : specs) {
final Long targetSpecificationId = compTargetSpecIdx.get(spec);
final MarketDataValue marketDataValue = marketDataValuesMap.get(targetSpecificationId);
assertNotNull(marketDataValue);
assertEquals(spec, reversedCompTargetSpecIdx.get(marketDataValue.getComputationTargetSpecificationId()));
assertEquals("value_name", marketDataValue.getName());
assertEquals(123.45, marketDataValue.getValue(), 0.000001);
}
// should not add anything extra
_batchMaster.addValuesToMarketData(marketData.getObjectId(), values);
marketDataValues = _batchMaster.getMarketDataValues(marketData.getObjectId(), PagingRequest.ALL).getFirst();
assertEquals(3, marketDataValues.size());
// should update 2, add 1
values = new HashSet<MarketDataValue>();
values.add(new MarketDataValue(ComputationTargetSpecification.of(UniqueId.of("BUID", "EQ12345", null)), 123.46, "value_name"));
values.add(new MarketDataValue(ComputationTargetSpecification.of(UniqueId.of("BUID", "EQ12347", "2")), 123.47, "value_name"));
values.add(new MarketDataValue(ComputationTargetSpecification.of(UniqueId.of("BUID", "EQ12348")), 123.45, "value_name"));
_batchMaster.addValuesToMarketData(marketData.getObjectId(), values);
marketDataValues = _batchMaster.getMarketDataValues(marketData.getObjectId(), PagingRequest.ALL).getFirst();
assertEquals(4, marketDataValues.size());
}