@Test public void whenSaveCalledWithKnownIdThenReturnUpdatedStrategyConfig() { expect( ConfigurationManager.loadConfig( eq(StrategiesType.class), eq(STRATEGIES_CONFIG_YAML_FILENAME))) .andReturn(allTheInternalStrategiesConfig()); ConfigurationManager.saveConfig( eq(StrategiesType.class), anyObject(StrategiesType.class), eq(STRATEGIES_CONFIG_YAML_FILENAME)); expect( ConfigurationManager.loadConfig( eq(StrategiesType.class), eq(STRATEGIES_CONFIG_YAML_FILENAME))) .andReturn(allTheInternalStrategiesConfig()); PowerMock.replayAll(); final StrategyConfigRepository strategyConfigRepository = new StrategyConfigYamlRepository(); final StrategyConfig strategyConfig = strategyConfigRepository.save(someExternalStrategyConfig()); assertThat(strategyConfig.getId()).isEqualTo(STRAT_ID_1); assertThat(strategyConfig.getName()).isEqualTo(STRAT_NAME_1); assertThat(strategyConfig.getDescription()).isEqualTo(STRAT_DESCRIPTION_1); assertThat(strategyConfig.getClassName()).isEqualTo(STRAT_CLASSNAME_1); assertThat(strategyConfig.getConfigItems().containsKey(BUY_PRICE_CONFIG_ITEM_KEY)).isTrue(); assertThat(strategyConfig.getConfigItems().containsValue(BUY_PRICE_CONFIG_ITEM_VALUE)).isTrue(); assertThat(strategyConfig.getConfigItems().containsKey(AMOUNT_TO_BUY_CONFIG_ITEM_KEY)).isTrue(); assertThat(strategyConfig.getConfigItems().containsValue(AMOUNT_TO_BUY_CONFIG_ITEM_VALUE)) .isTrue(); PowerMock.verifyAll(); }
@Override public StrategyConfig save(StrategyConfig config) { final StrategiesType strategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); final List<StrategyConfig> strategyConfigs = strategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList()); if (config.getId() == null || config.getId().isEmpty()) { LOG.info(() -> "About to create StrategyConfig: " + config); if (strategyConfigs.isEmpty()) { final StrategyConfig newStrategyConfig = new StrategyConfig(config); newStrategyConfig.setId(generateUuid()); strategiesType.getStrategies().add(newStrategyConfig); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedInternalStrategiesConfig = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedInternalStrategiesConfig.getStrategies().stream() .filter(item -> item.getId().equals(newStrategyConfig.getId())) .distinct() .collect(Collectors.toList())); } else { throw new IllegalStateException( "Trying to create new StrategyConfig but null/empty id already exists. " + "StrategyConfig: " + config + " Existing StrategyConfigs: " + strategiesType.getStrategies()); } } else { LOG.info(() -> "About to update StrategyConfig: " + config); if (!strategyConfigs.isEmpty()) { strategiesType .getStrategies() .remove(strategyConfigs.get(0)); strategiesType.getStrategies().add(config); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedStrategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedStrategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList())); } else { LOG.warn( () -> "Trying to update StrategyConfig but id does not exist StrategyConfig: " + config + " Existing StrategyConfig: " + strategiesType.getStrategies()); return null; } } }
StrategyConfigYamlRepository implements StrategyConfigRepository { @Override public StrategyConfig save(StrategyConfig config) { final StrategiesType strategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); final List<StrategyConfig> strategyConfigs = strategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList()); if (config.getId() == null || config.getId().isEmpty()) { LOG.info(() -> "About to create StrategyConfig: " + config); if (strategyConfigs.isEmpty()) { final StrategyConfig newStrategyConfig = new StrategyConfig(config); newStrategyConfig.setId(generateUuid()); strategiesType.getStrategies().add(newStrategyConfig); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedInternalStrategiesConfig = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedInternalStrategiesConfig.getStrategies().stream() .filter(item -> item.getId().equals(newStrategyConfig.getId())) .distinct() .collect(Collectors.toList())); } else { throw new IllegalStateException( "Trying to create new StrategyConfig but null/empty id already exists. " + "StrategyConfig: " + config + " Existing StrategyConfigs: " + strategiesType.getStrategies()); } } else { LOG.info(() -> "About to update StrategyConfig: " + config); if (!strategyConfigs.isEmpty()) { strategiesType .getStrategies() .remove(strategyConfigs.get(0)); strategiesType.getStrategies().add(config); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedStrategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedStrategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList())); } else { LOG.warn( () -> "Trying to update StrategyConfig but id does not exist StrategyConfig: " + config + " Existing StrategyConfig: " + strategiesType.getStrategies()); return null; } } } }
StrategyConfigYamlRepository implements StrategyConfigRepository { @Override public StrategyConfig save(StrategyConfig config) { final StrategiesType strategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); final List<StrategyConfig> strategyConfigs = strategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList()); if (config.getId() == null || config.getId().isEmpty()) { LOG.info(() -> "About to create StrategyConfig: " + config); if (strategyConfigs.isEmpty()) { final StrategyConfig newStrategyConfig = new StrategyConfig(config); newStrategyConfig.setId(generateUuid()); strategiesType.getStrategies().add(newStrategyConfig); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedInternalStrategiesConfig = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedInternalStrategiesConfig.getStrategies().stream() .filter(item -> item.getId().equals(newStrategyConfig.getId())) .distinct() .collect(Collectors.toList())); } else { throw new IllegalStateException( "Trying to create new StrategyConfig but null/empty id already exists. " + "StrategyConfig: " + config + " Existing StrategyConfigs: " + strategiesType.getStrategies()); } } else { LOG.info(() -> "About to update StrategyConfig: " + config); if (!strategyConfigs.isEmpty()) { strategiesType .getStrategies() .remove(strategyConfigs.get(0)); strategiesType.getStrategies().add(config); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedStrategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedStrategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList())); } else { LOG.warn( () -> "Trying to update StrategyConfig but id does not exist StrategyConfig: " + config + " Existing StrategyConfig: " + strategiesType.getStrategies()); return null; } } }  }
StrategyConfigYamlRepository implements StrategyConfigRepository { @Override public StrategyConfig save(StrategyConfig config) { final StrategiesType strategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); final List<StrategyConfig> strategyConfigs = strategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList()); if (config.getId() == null || config.getId().isEmpty()) { LOG.info(() -> "About to create StrategyConfig: " + config); if (strategyConfigs.isEmpty()) { final StrategyConfig newStrategyConfig = new StrategyConfig(config); newStrategyConfig.setId(generateUuid()); strategiesType.getStrategies().add(newStrategyConfig); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedInternalStrategiesConfig = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedInternalStrategiesConfig.getStrategies().stream() .filter(item -> item.getId().equals(newStrategyConfig.getId())) .distinct() .collect(Collectors.toList())); } else { throw new IllegalStateException( "Trying to create new StrategyConfig but null/empty id already exists. " + "StrategyConfig: " + config + " Existing StrategyConfigs: " + strategiesType.getStrategies()); } } else { LOG.info(() -> "About to update StrategyConfig: " + config); if (!strategyConfigs.isEmpty()) { strategiesType .getStrategies() .remove(strategyConfigs.get(0)); strategiesType.getStrategies().add(config); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedStrategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedStrategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList())); } else { LOG.warn( () -> "Trying to update StrategyConfig but id does not exist StrategyConfig: " + config + " Existing StrategyConfig: " + strategiesType.getStrategies()); return null; } } }  @Override List<StrategyConfig> findAll(); @Override StrategyConfig findById(String id); @Override StrategyConfig save(StrategyConfig config); @Override StrategyConfig delete(String id); }
StrategyConfigYamlRepository implements StrategyConfigRepository { @Override public StrategyConfig save(StrategyConfig config) { final StrategiesType strategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); final List<StrategyConfig> strategyConfigs = strategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList()); if (config.getId() == null || config.getId().isEmpty()) { LOG.info(() -> "About to create StrategyConfig: " + config); if (strategyConfigs.isEmpty()) { final StrategyConfig newStrategyConfig = new StrategyConfig(config); newStrategyConfig.setId(generateUuid()); strategiesType.getStrategies().add(newStrategyConfig); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedInternalStrategiesConfig = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedInternalStrategiesConfig.getStrategies().stream() .filter(item -> item.getId().equals(newStrategyConfig.getId())) .distinct() .collect(Collectors.toList())); } else { throw new IllegalStateException( "Trying to create new StrategyConfig but null/empty id already exists. " + "StrategyConfig: " + config + " Existing StrategyConfigs: " + strategiesType.getStrategies()); } } else { LOG.info(() -> "About to update StrategyConfig: " + config); if (!strategyConfigs.isEmpty()) { strategiesType .getStrategies() .remove(strategyConfigs.get(0)); strategiesType.getStrategies().add(config); ConfigurationManager.saveConfig( StrategiesType.class, strategiesType, STRATEGIES_CONFIG_YAML_FILENAME); final StrategiesType updatedStrategiesType = ConfigurationManager.loadConfig(StrategiesType.class, STRATEGIES_CONFIG_YAML_FILENAME); return adaptInternalToExternalConfig( updatedStrategiesType.getStrategies().stream() .filter(item -> item.getId().equals(config.getId())) .distinct() .collect(Collectors.toList())); } else { LOG.warn( () -> "Trying to update StrategyConfig but id does not exist StrategyConfig: " + config + " Existing StrategyConfig: " + strategiesType.getStrategies()); return null; } } }  @Override List<StrategyConfig> findAll(); @Override StrategyConfig findById(String id); @Override StrategyConfig save(StrategyConfig config); @Override StrategyConfig delete(String id);  }