88import org .junit .jupiter .api .extension .ExtendWith ;
99import org .springframework .beans .factory .annotation .Autowired ;
1010import org .springframework .boot .test .context .SpringBootTest ;
11+ import org .springframework .cloud .openfeign .FeignClientProperties ;
1112import org .springframework .http .HttpStatus ;
13+ import org .springframework .test .context .TestPropertySource ;
1214import org .springframework .test .context .junit .jupiter .SpringExtension ;
1315
1416import java .util .concurrent .ExecutionException ;
17+ import java .util .concurrent .TimeoutException ;
1518
1619import static com .github .tomakehurst .wiremock .client .WireMock .*;
17- import static org .junit .Assert .assertNull ;
18- import static org .junit .Assert .assertThrows ;
20+ import static org .junit .Assert .*;
1921import static org .mockito .ArgumentMatchers .any ;
2022
2123@ ExtendWith (SpringExtension .class )
2224@ SpringBootTest (classes = ExampleApplication .class )
25+ @ TestPropertySource (locations = "classpath:application-integration_test.properties" )
2326class PurchaseServiceIntegrationTest {
2427
2528 @ Autowired
@@ -33,11 +36,18 @@ class PurchaseServiceIntegrationTest {
3336
3437 private WireMockServer wireMockServer ;
3538
39+ private Purchase purchase ;
40+
3641 @ BeforeEach
3742 public void startWireMockServer () {
3843 wireMockServer = new WireMockServer (8083 );
3944 configureFor ("localhost" , 8083 );
4045 wireMockServer .start ();
46+
47+ stubFor (post (urlEqualTo ("/reports" ))
48+ .willReturn (aResponse ().withStatus (HttpStatus .OK .value ())));
49+
50+ purchase = new Purchase ("BR" );
4151 }
4252
4353 @ AfterEach
@@ -46,17 +56,46 @@ public void stopWireMockServer() {
4656 }
4757
4858 @ Test
49- void executePurchase () throws ExecutionException , InterruptedException {
59+ void givenRestCalls_whenBothReturnsOk_thenReturnCorrectResult ()
60+ throws ExecutionException , InterruptedException {
5061 stubFor (post (urlEqualTo ("/purchase?site_id=BR" ))
51- .willReturn (aResponse ().withStatus (HttpStatus .NOT_FOUND .value ())));
62+ .willReturn (aResponse ().withStatus (HttpStatus .OK .value ()). withBody ( "credit_card" )));
5263
53- stubFor (post (urlEqualTo ("/reports" ))
54- .willReturn (aResponse ().withStatus (HttpStatus .OK .value ())));
64+ String result = purchaseService .executePurchase (purchase );
5565
56- Purchase purchase = new Purchase ("BR" );
66+ assertNotNull (result );
67+ assertEquals ("Purchase executed with payment method credit_card" , result );
68+ }
69+
70+ @ Test
71+ void givenRestCalls_whenPurchaseReturns404_thenReturnDefault ()
72+ throws ExecutionException , InterruptedException {
73+ stubFor (post (urlEqualTo ("/purchase?site_id=BR" ))
74+ .willReturn (aResponse ().withStatus (HttpStatus .NOT_FOUND .value ())));
5775
5876 String result = purchaseService .executePurchase (purchase );
5977
60- assertNull (result );
78+ assertNotNull (result );
79+ assertEquals ("Purchase executed with payment method account_money" , result );
80+ }
81+
82+ @ Test
83+ void givenRestCalls_whenPurchaseCompletableFutureTimeout_thenReturnDefault () {
84+ stubFor (post (urlEqualTo ("/purchase?site_id=BR" ))
85+ .willReturn (aResponse ().withFixedDelay (450 )));
86+
87+ Throwable error = assertThrows (ExecutionException .class , () -> purchaseService .executePurchase (purchase ));
88+
89+ assertEquals ("java.lang.RuntimeException: Thread timeout!" , error .getMessage ());
6190 }
91+
92+ // @Test
93+ // void givenRestCalls_whenPurchaseRequestWebTimeout_thenReturnDefault() {
94+ // stubFor(post(urlEqualTo("/purchase?site_id=BR"))
95+ // .willReturn(aResponse().withFixedDelay(250)));
96+ //
97+ // Throwable error = assertThrows(ExecutionException.class, () -> purchaseService.executePurchase(purchase));
98+ //
99+ // assertEquals("REST call network timeout!", error.getMessage());
100+ // }
62101}
0 commit comments