MarketConfigYamlRepository implements MarketConfigRepository { @Override public MarketConfig save(MarketConfig config) { final MarketsType marketsType = ConfigurationManager.loadConfig(MarketsType.class, MARKETS_CONFIG_YAML_FILENAME); final List<MarketConfig> marketConfigs = marketsType.getMarkets().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList()); if (config.getId() == null || config.getId().isEmpty()) { LOG.info(() -> "About to create MarketConfig: " + config); if (marketConfigs.isEmpty()) { final MarketConfig newMarketConfig = new MarketConfig(config); newMarketConfig.setId(generateUuid()); marketsType.getMarkets().add(newMarketConfig); ConfigurationManager.saveConfig( MarketsType.class, marketsType, MARKETS_CONFIG_YAML_FILENAME); final MarketsType updatedInternalMarketsConfig = ConfigurationManager.loadConfig(MarketsType.class, MARKETS_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedInternalMarketsConfig.getMarkets().stream() .filter(item -> item.getId().equals(newMarketConfig.getId())) .distinct() .collect(Collectors.toList())); } else { throw new IllegalStateException( "Trying to create new MarketConfig but null/empty id already exists. " + "MarketConfig: " + config + EXISTING_MARKET_CONFIG + marketsType.getMarkets()); } } else { LOG.info(() -> "About to update MarketConfig: " + config); if (!marketConfigs.isEmpty()) { marketsType.getMarkets().remove(marketConfigs.get(0)); marketsType.getMarkets().add(config); ConfigurationManager.saveConfig( MarketsType.class, marketsType, MARKETS_CONFIG_YAML_FILENAME); final MarketsType updatedMarketsType = ConfigurationManager.loadConfig(MarketsType.class, MARKETS_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedMarketsType.getMarkets().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList())); } else { LOG.warn( () -> "Trying to update MarketConfig but id does not exist MarketConfig: " + config + EXISTING_MARKET_CONFIG + marketsType.getMarkets()); return null; } } }  @Override List<MarketConfig> findAll(); @Override MarketConfig findById(String id); @Override MarketConfig save(MarketConfig config); @Override MarketConfig delete(String id);  }
@Test public void whenSaveCalledWithKnownIdThenReturnUpdatedMarketConfig() { expect(ConfigurationManager.loadConfig(eq(MarketsType.class), eq(MARKETS_CONFIG_YAML_FILENAME))) .andReturn(allTheInternalMarketsConfig()); ConfigurationManager.saveConfig( eq(MarketsType.class), anyObject(MarketsType.class), eq(MARKETS_CONFIG_YAML_FILENAME)); expect(ConfigurationManager.loadConfig(eq(MarketsType.class), eq(MARKETS_CONFIG_YAML_FILENAME))) .andReturn(allTheInternalMarketsConfig()); PowerMock.replayAll(); final MarketConfigRepository marketConfigRepository = new MarketConfigYamlRepository(); final MarketConfig marketConfig = marketConfigRepository.save(someExternalMarketConfig()); assertThat(marketConfig.getId()).isEqualTo(MARKET_1_ID); assertThat(marketConfig.getName()).isEqualTo(MARKET_1_NAME); assertThat(marketConfig.isEnabled()).isEqualTo(MARKET_1_IS_ENABLED); assertThat(marketConfig.getBaseCurrency()).isEqualTo(MARKET_1_BASE_CURRENCY); assertThat(marketConfig.getCounterCurrency()).isEqualTo(MARKET_1_COUNTER_CURRENCY); assertThat(marketConfig.getTradingStrategyId()).isEqualTo(MARKET_1_TRADING_STRATEGY_ID); PowerMock.verifyAll(); }
@Test public void whenSaveCalledWithUnknownIdThenReturnEmptyMarketConfig() { expect(ConfigurationManager.loadConfig(eq(MarketsType.class), eq(MARKETS_CONFIG_YAML_FILENAME))) .andReturn(allTheInternalMarketsConfig()); PowerMock.replayAll(); final MarketConfigRepository marketConfigRepository = new MarketConfigYamlRepository(); final MarketConfig marketConfig = marketConfigRepository.save(someExternalMarketConfigWithUnknownId()); assertThat(marketConfig).isEqualTo(null); PowerMock.verifyAll(); }
@Test public void whenSaveCalledWithEmptyIdThenExpectCreatedMarketConfigToBeReturned() throws Exception { expect(ConfigurationManager.loadConfig(eq(MarketsType.class), eq(MARKETS_CONFIG_YAML_FILENAME))) .andReturn(allTheInternalMarketsConfig()); ConfigurationManager.saveConfig( eq(MarketsType.class), anyObject(MarketsType.class), eq(MARKETS_CONFIG_YAML_FILENAME)); expect(ConfigurationManager.loadConfig(eq(MarketsType.class), eq(MARKETS_CONFIG_YAML_FILENAME))) .andReturn(allTheInternalMarketsConfigPlusNewOne()); final MarketConfigRepository marketConfigRepository = PowerMock.createPartialMock(MarketConfigYamlRepository.class, MOCKED_GENERATE_UUID_METHOD); PowerMock.expectPrivate(marketConfigRepository, MOCKED_GENERATE_UUID_METHOD) .andReturn(GENERATED_MARKET_ID); PowerMock.replayAll(); final MarketConfig marketConfig = marketConfigRepository.save(someNewExternalMarketConfig()); assertThat(marketConfig.getId()).isEqualTo(GENERATED_MARKET_ID); assertThat(marketConfig.getName()).isEqualTo(NEW_MARKET_NAME); assertThat(marketConfig.isEnabled()).isEqualTo(NEW_MARKET_IS_ENABLED); assertThat(marketConfig.getBaseCurrency()).isEqualTo(NEW_MARKET_BASE_CURRENCY); assertThat(marketConfig.getCounterCurrency()).isEqualTo(NEW_MARKET_COUNTER_CURRENCY); assertThat(marketConfig.getTradingStrategyId()).isEqualTo(NEW_MARKET_TRADING_STRATEGY_ID); PowerMock.verifyAll(); }