TaxPaymentWebServiceFacade { public TaxWebserviceCallResult callTaxService(TaxCalculation taxCalculation, Callable<Void> callable) throws Exception { TaxWebserviceCallResult previousResult = taxWebserviceCallResultRepository.findSuccessfulByTaxCalculation(taxCalculation); if (previousResult != null) { return previousResult; } try { callable.call(); TaxWebserviceCallResult successTaxWebserviceCallResult = TaxWebserviceCallResult.callSucceeded(taxCalculation); saveTaxServiceCallResult(successTaxWebserviceCallResult); return successTaxWebserviceCallResult; } catch (TaxWebServiceNonFatalException e) { TaxWebserviceCallResult failedTaxWebserviceCallResult = TaxWebserviceCallResult.callFailed(taxCalculation, e); saveTaxServiceCallResult(failedTaxWebserviceCallResult); throw e; } }  TaxWebserviceCallResult callTaxService(TaxCalculation taxCalculation, Callable<Void> callable);  }
@Test public void whenPreviousCallResultIsFailed_andCallReturnsValid_callResultIsReturned() throws Exception { when(taxWebserviceCallResultRepository.findSuccessfulByTaxCalculation(taxCalculation)).thenReturn(null); when(taxServiceCallResultCallable.call()).thenReturn(null); TaxWebserviceCallResult actualTaxWebserviceCallResult = taxPaymentWebServiceFacade.callTaxService(taxCalculation, taxServiceCallResultCallable); assertThat(actualTaxWebserviceCallResult.isSuccessfulResponse()).isTrue(); }
@Test public void whenPreviousCallResultIsFailed_andCallReturnsException_exceptionIsReturned() throws Exception { when(taxWebserviceCallResultRepository.findSuccessfulByTaxCalculation(taxCalculation)).thenReturn(null); when(taxServiceCallResultCallable.call()).thenThrow(new TaxWebServiceNonFatalException(new EmployeeTestBuilder().build(), Money.of(CurrencyUnit.EUR, 10), HttpStatus.INTERNAL_SERVER_ERROR, null, "boe")); expectExceptionWithMessage(TaxWebServiceNonFatalException.class, "Paying the taxes for employee with id null with amount EUR 10.00 failed because of boe"); taxPaymentWebServiceFacade.callTaxService(taxCalculation, taxServiceCallResultCallable); }
@Test public void whenPreviousCallResultIsValid_thenPreviousCallResultIsReturned() throws Exception { when(taxWebserviceCallResultRepository.findSuccessfulByTaxCalculation(taxCalculation)).thenReturn(taxWebserviceCallResultValid); verify(taxServiceCallResultCallable, never()).call(); assertThat(taxPaymentWebServiceFacade.callTaxService(taxCalculation, taxServiceCallResultCallable)) .isEqualTo(taxWebserviceCallResultValid); }