SymbolService { public Symbol addSymbol(String code, String name, String countryFlag, String currencyCode) { requireNonNull(code, "code is null"); requireNonNull(name, "name is null"); if(symbolRepo.existsById(code)) { throw SymbolException.alreadyExists(code); } SymbolEntity se = new SymbolEntity(code); se.setName(name); se.setCountryFlag(countryFlag); if(currencyCode != null) { se.setCurrency(new SymbolEntity(currencyCode)); } return symbolMapper.mapToSymbol(symbolRepo.save(se)); }  Symbol addSymbol(String code, String name, String countryFlag, String currencyCode); Optional<Symbol> getForCode(String code); Optional<Symbol> getForCodeWithNoCurrency(String code); void addFavoriteSymbol(String fromSymbolCode, String toSymbolCode, String username); void deleteFavoriteSymbol(String fromSymbolCode, String toSymbolCode, String username);  }
@Test public void testAddSymbol_notExist() { String code = USD; String name = "US Dollar"; String countryFlag = "us"; Symbol result = symbolService.addSymbol(code, name, countryFlag, null); assertThat(result).isEqualToComparingFieldByField(new SymbolEntity(code, name, countryFlag, null)); assertThat(result).isEqualToComparingFieldByField(symbolRepo.findById(code).get()); }
@Sql("/sql/insert_data.sql") @Test public void testAddSymbol_NotExistWithExistingCurrency() { String code = "TOTO"; String name = "toto"; var flag = "flag"; String currencyCode = USD; Symbol result = symbolService.addSymbol(code, name, flag, currencyCode); assertThat(result).isEqualToIgnoringGivenFields(new Symbol(code, name, flag, new Symbol(USD, "US Dollar", "us", null))); var savedSymbol = symbolRepo.findById(code).get(); assertThat(savedSymbol.getCode()).isEqualTo(code); assertThat(savedSymbol.getName()).isEqualTo(name); assertThat(savedSymbol.getCountryFlag()).isEqualTo(flag); assertThat(savedSymbol.getCurrency().getCode()).isEqualTo(currencyCode); }
@Test public void testAddSymbol_NotExistWithNotExistingCurrency() { String code = "TOTO"; String name = "toto"; String currencyCode = "XXX"; assertThatThrownBy(() -> symbolService.addSymbol(code, name, null, currencyCode)) .hasRootCauseInstanceOf(EntityNotFoundException.class) .hasMessageContaining("Unable to find fr.ymanvieu.trading.common.symbol.entity.SymbolEntity with id XXX"); }
@Sql("/sql/insert_data.sql") @Test public void testAddSymbol_AlreadyExist() { String code = USD; String name = "US Dollar"; String countryFlag = "us"; assertThatThrownBy(() -> symbolService.addSymbol(code, name, countryFlag, null)) .isInstanceOf(SymbolException.class) .hasMessage("symbols.error.already_exists: [USD]"); }