YahooQuoteToStockProductConverter implements Converter<YahooQuote, StockProduct> { @Override public StockProduct convert(YahooQuote yahooQuote) { StockProduct stockProduct = stockProductRepository.findOne(yahooQuote.getId()); if(stockProduct == null){ stockProduct = new StockProduct(); stockProduct.setId(yahooQuote.getId()); } stockProduct.setName(yahooQuote.getName()); stockProduct.setHigh(BigDecimal.valueOf(yahooQuote.getHigh())); stockProduct.setLow(BigDecimal.valueOf(yahooQuote.getLow())); stockProduct.setDailyLatestChange(BigDecimal.valueOf(yahooQuote.getLastChange())); stockProduct.setDailyLatestChangePercent(BigDecimal.valueOf(yahooQuote.getLastChangePercent())); stockProduct.setDailyLatestValue(BigDecimal.valueOf(yahooQuote.getLast())); stockProduct.setPreviousClose(BigDecimal.valueOf(yahooQuote.getPreviousClose())); stockProduct.setOpen(BigDecimal.valueOf(yahooQuote.getOpen())); if(!StringUtils.isEmpty(yahooQuote.getExchange())){ stockProduct.setExchange(exchangeService.get(yahooQuote.getExchange())); } if(!StringUtils.isEmpty(yahooQuote.getCurrency())){ stockProduct.setCurrency(yahooQuote.getCurrency()); } return stockProduct; }  @Override StockProduct convert(YahooQuote yahooQuote);  }
@Test public void essentialDataTransfered(){ when(stockProductRepository.findOne(any(String.class))).thenReturn(new StockProduct(ID_VALUE)); when(exchangeService.get(EXCHANGE_VALUE)).thenReturn(new Exchange(EXCHANGE_VALUE)); validateResult(converter.convert(buildYahooQuoteInstance())); verify(stockProductRepository, times(1)).findOne(any(String.class)); verify(exchangeService, times(1)).get(any(String.class)); }
@Test public void producesNewStockProductIfNoneFound(){ when(stockProductRepository.findOne(any(String.class))).thenReturn(null); when(exchangeService.get(EXCHANGE_VALUE)).thenReturn(new Exchange(EXCHANGE_VALUE)); validateResult(converter.convert(buildYahooQuoteInstance())); verify(stockProductRepository, times(1)).findOne(any(String.class)); verify(exchangeService, times(1)).get(any(String.class)); }