PortfolioService { @Transactional public Order addOrder(Order order) { logger.debug("Adding order: " + order); if (order.getOrderFee() == null) { order.setOrderFee(Order.DEFAULT_ORDER_FEE); logger.debug("Adding Fee to order: " + order); } if (order.getOrderType() == OrderType.BUY) { double amount = order.getQuantity() * order.getPrice().doubleValue() + order.getOrderFee().doubleValue(); ResponseEntity<Double> result = restTemplate.getForEntity("http: + accountsService + "/accounts/{userid}/decreaseBalance/{amount}", Double.class, order.getAccountId(), amount); if (result.getStatusCode() == HttpStatus.OK) { logger.info(String .format("Account funds updated successfully for account: %s and new funds are: %s", order.getAccountId(), result.getBody())); return repository.save(order); } else { logger.warn("PortfolioService:addOrder - decresing balance HTTP not ok: "); return null; } } else { double amount = order.getQuantity() * order.getPrice().doubleValue() - order.getOrderFee().doubleValue(); ResponseEntity<Double> result = restTemplate.getForEntity("http: + accountsService + "/accounts/{userid}/increaseBalance/{amount}", Double.class, order.getAccountId(), amount); if (result.getStatusCode() == HttpStatus.OK) { logger.info(String .format("Account funds updated successfully for account: %s and new funds are: %s", order.getAccountId(), result.getBody())); return repository.save(order); } else { logger.warn("PortfolioService:addOrder - increasing balance HTTP not ok: "); return null; } } }  Portfolio getPortfolio(String accountId); @Transactional Order addOrder(Order order);  }
@Test public void doSaveOrder() { Order returnOrder = ServiceTestConfiguration.order(); returnOrder.setOrderId(1); double amount = ServiceTestConfiguration.order().getQuantity()*ServiceTestConfiguration.order().getPrice().doubleValue()+ServiceTestConfiguration.order().getOrderFee().doubleValue(); ResponseEntity<Double> response = new ResponseEntity<Double>(100d, HttpStatus.OK); when(restTemplate.getForEntity("http: when(repo.save(ServiceTestConfiguration.order())).thenReturn(returnOrder); Order order = service.addOrder(ServiceTestConfiguration.order()); assertEquals(order, returnOrder); }
@Test public void doSaveOrderNullOrderFee() { Order returnOrder = ServiceTestConfiguration.order(); returnOrder.setOrderId(1); double amount = returnOrder.getQuantity()*returnOrder.getPrice().doubleValue()+returnOrder.getOrderFee().doubleValue(); ResponseEntity<Double> response = new ResponseEntity<Double>(100d, HttpStatus.OK); when(restTemplate.getForEntity(any(), eq(Double.class), any(), any())).thenReturn(response); when(repo.save(isA(Order.class))).thenReturn(returnOrder); Order requestOrder = ServiceTestConfiguration.order(); requestOrder.setOrderFee(null); Order order = service.addOrder(requestOrder); assertEquals(order.getOrderFee(), ServiceTestConfiguration.order().getOrderFee()); }
@Test public void doSaveOrderSellOrder() { Order returnOrder = ServiceTestConfiguration.sellOrder(); returnOrder.setOrderId(1); double amount = ServiceTestConfiguration.sellOrder().getQuantity()*ServiceTestConfiguration.sellOrder().getPrice().doubleValue()-ServiceTestConfiguration.sellOrder().getOrderFee().doubleValue(); ResponseEntity<Double> response = new ResponseEntity<Double>(100d, HttpStatus.OK); when(restTemplate.getForEntity("http: when(repo.save(ServiceTestConfiguration.sellOrder())).thenReturn(returnOrder); Order order = service.addOrder(ServiceTestConfiguration.sellOrder()); assertEquals(order, returnOrder); }