|
| 1 | +package com.baeldung.client; |
| 2 | + |
| 3 | +import com.baeldung.api.Booking; |
| 4 | +import com.baeldung.api.BookingException; |
| 5 | +import com.baeldung.api.CabBookingService; |
| 6 | +import com.baeldung.server.Server; |
| 7 | +import org.junit.AfterClass; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.test.context.junit4.SpringRunner; |
| 16 | + |
| 17 | +import static java.lang.Thread.sleep; |
| 18 | + |
| 19 | +@SpringBootTest(classes = {BurlapClient.class, HessianClient.class}) |
| 20 | +@RunWith(SpringRunner.class) |
| 21 | +public class CabBookingServiceTest { |
| 22 | + |
| 23 | + static Logger log = LoggerFactory.getLogger(CabBookingServiceTest.class); |
| 24 | + @Autowired @Qualifier("burlapInvoker") CabBookingService burlapClient; |
| 25 | + @Autowired @Qualifier("hessianInvoker") CabBookingService hessianClient; |
| 26 | + static Thread serverThread; |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void startServer() throws InterruptedException { |
| 30 | + serverThread = serverThread(); |
| 31 | + log.info("Starting server."); |
| 32 | + serverThread.start(); |
| 33 | + sleep(4000); |
| 34 | + } |
| 35 | + |
| 36 | + @org.junit.Test |
| 37 | + public void bookACabWithBurlapClient() throws InterruptedException { |
| 38 | + bookACab(this.burlapClient); |
| 39 | + } |
| 40 | + |
| 41 | + @org.junit.Test |
| 42 | + public void bookACabWithHessianClient() throws InterruptedException { |
| 43 | + bookACab(this.hessianClient); |
| 44 | + } |
| 45 | + |
| 46 | + private void bookACab(CabBookingService burlapClient) { |
| 47 | + Booking booking; |
| 48 | + try { |
| 49 | + booking = burlapClient.bookRide("Duomo place"); |
| 50 | + log.info("Booking success: {}", booking); |
| 51 | + } catch (BookingException e) { |
| 52 | + log.info("Booking failed: {}", e.getMessage()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @AfterClass |
| 57 | + public static void stopServer() throws InterruptedException { |
| 58 | + serverThread.interrupt(); |
| 59 | + serverThread.join(); |
| 60 | + log.info("Server terminated."); |
| 61 | + } |
| 62 | + |
| 63 | + static Thread serverThread() { |
| 64 | + Thread serverThread = new Thread(()-> { |
| 65 | + log.info("Starting Burlap and Hessian server"); |
| 66 | + Server.main(new String[]{}); |
| 67 | + log.info("Burlap and Hessian server terminated"); |
| 68 | + }); |
| 69 | + serverThread.setDaemon(true); |
| 70 | + return serverThread; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments