TaxPaymentWebService { public void doWebserviceCallToTaxService(Employee employee, Money taxesToPay) throws TaxWebServiceException { TaxTo taxTo = createWebserviceInput(employee, taxesToPay); try { URI uri = URI.create(taxServiceUrl); ResponseEntity<TaxServiceResponseTo> webserviceResult = restTemplate.postForEntity(uri, taxTo, TaxServiceResponseTo.class); TaxServiceResponseTo taxServiceResponseTo = webserviceResult.getBody(); if (!"OK".equals(taxServiceResponseTo.getStatus())) { throw new TaxWebServiceNonFatalException(employee, taxesToPay, webserviceResult.getStatusCode(), getJson(taxServiceResponseTo), "invalid response from server"); } } catch (HttpClientErrorException e) { throw new TaxWebServiceFatalException(employee, taxesToPay, e.getStatusCode(), e.getResponseBodyAsString(), e); } catch (HttpServerErrorException e) { throw new TaxWebServiceNonFatalException(employee, taxesToPay, e.getStatusCode(), e.getResponseBodyAsString(), e); } catch (ResourceAccessException e) { throw new TaxWebServiceNonFatalException(employee, taxesToPay, HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e); } }  void doWebserviceCallToTaxService(Employee employee, Money taxesToPay);  }
@Test public void testProcessHappyPath_NoExceptionHasBeenThrownAndEmployeeIsReturned() throws Exception { whenCallingTheWebservice().thenReturn(mockedResponse); when(mockedResponse.getBody()).thenReturn(new TaxServiceResponseTo("OK")); when(mockedResponse.getStatusCode()).thenReturn(HttpStatus.OK); Employee employee = new EmployeeTestBuilder().build(); taxPaymentWebService.doWebserviceCallToTaxService(employee, Money.of(CurrencyUnit.EUR, 2000.0)); assertThat("no error has been thrown").isNotEmpty(); }
@Test(expected = TaxWebServiceNonFatalException.class) public void testProcessBadResponse_ExceptionHasBeenThrownForever() throws Exception { whenCallingTheWebservice().thenReturn(mockedResponse); when(mockedResponse.getBody()).thenReturn(new TaxServiceResponseTo("ERROR")); when(mockedResponse.getStatusCode()).thenReturn(HttpStatus.OK); Employee employee = new EmployeeTestBuilder().build(); taxPaymentWebService.doWebserviceCallToTaxService(employee, Money.of(CurrencyUnit.EUR, 2000.0)); }
@Test(expected = TaxWebServiceNonFatalException.class) public void testProcessTimeoutResponse_ExceptionHasBeenThrownForever() throws Exception { whenCallingTheWebservice().thenThrow(aWrappedTimeOutException()); Employee employee = new EmployeeTestBuilder().build(); taxPaymentWebService.doWebserviceCallToTaxService(employee, Money.of(CurrencyUnit.EUR, 2000.0)); }
@Test(expected = TaxWebServiceNonFatalException.class) public void testProcess_UnexpectedServerExceptionOccurs_ExceptionIsRethrown() throws Exception { whenCallingTheWebservice().thenThrow(aServerErrorException()); Employee employee = new EmployeeTestBuilder().build(); taxPaymentWebService.doWebserviceCallToTaxService(employee, Money.of(CurrencyUnit.EUR, 2000.0)); }
@Test(expected = TaxWebServiceFatalException.class) public void testProcess_ClientExceptionOccurs_TaxWebserviceFatalExceptionIsRethrown() throws Exception { whenCallingTheWebservice().thenThrow(aMethodNotAllowedException()); Employee employee = new EmployeeTestBuilder().build(); taxPaymentWebService.doWebserviceCallToTaxService(employee, Money.of(CurrencyUnit.EUR, 2000.0)); }