Skip to content

Lab10 convert currency

In this lab, you'll have the GET method for your API return the price in GBP (Great British Pounds) instead of US Dollars, if a query parameter specifies it. For example:

http://localhost:8080/api/mealorder/1?currency=gbp

A. Create a Fake Currency Conversion Service

  1. Create a new interface
    • Name: CurrencyConversionService
    • Package: com.welltestedlearning.mealkiosk.domain
  2. Add the following method to the interface:
    int convertToGbp(int amount);
    
  3. Create a new class that will implement the interface:

    • Name: FakeCurrencyConversionService
    • Package: com.welltestedlearning.mealkiosk.adapter
    @Service
    public class FakeCurrencyConversionService implements CurrencyConversionService {
      public int convertToGbp(int amount) {
        return (int) (amount * 0.75);
      }
    }
    

B. Use CurrencyService in API Controller

Injection

  1. Open the MealOrderApiController class
  2. Inject the CurrencyService interface into the constructor
    • Remember, you don't create a new constructor, you add another parameter to the existing constructor.
    • This will cause the MealOrderApiControllerTest tests to no longer compile, so fix them by passing in a new FakeCurrencyService() instance.

Currency Query Parameter

  1. Change the findMealOrder() method to accept an optional Query Parameter by changing the method signature to:

    public MealOrderResponse findMealOrder(@PathVariable("id") Long id,
                   @RequestParam(value = "currency", defaultValue="usd") String currency) {
    
    This will assign any incoming Query Parameter (e.g., ?currency=gbp) to the currency variable.

    The defaultValue="usd" means that if no query parameter exists, currency will default to "usd".

Failing Test

  1. This will cause tests in MealOrderApiControllerTest to fail, so fix them by adding "usd" as the second parameter to the tests that call findMealOrder.
  2. Add this test to the MealOrderApiControllerTest:
    @Test
    public void getWithGbpQueryParamConvertsPriceToGbp() throws Exception {
      MealOrderRepository mealOrderRepository = new MealOrderRepository();
      MealOrder mealOrder = MealBuilder.builder().burger("cheese").build();
      MealOrder savedMealOrder = mealOrderRepository.save(mealOrder);
    
      MealOrderApiController controller =
          new MealOrderApiController(mealOrderRepository, DUMMY_KITCHEN_SERVICE, new FakeCurrencyService());
    
      // When: we do a GET for the ID
      MealOrderResponse response = controller.findMealOrder(savedMealOrder.getId(), "gbp");
    
      assertThat(response.getPrice())
          .isEqualTo(4); // US$6 = GBP4
    }
    
  3. This test should fail.

Fix the Test

  1. Add code near the end of the findMealOrder method: If the currency string is "gbp", then:
    • Use the currency converter service to convert the price to GBP
    • Store that converted value into the meal order response (i.e., setPrice()).
  2. Once you implement that properly, the test should now pass.