Skip to content

Lab09 convert currency stub

In this lab, you'll have the GET method for your API return the price in GBP (which is the abbreviation for British Pound Sterling) instead of US Dollars (well, really cents), if a query parameter specifies it. For example:

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

Would return the price of the coffee order in GBP (actually, pennies) instead of cents.

A. Create Stub Currency Conversion Service

You'll create a Stub version of the currency conversion service. Remember that a Stub returns a hard-coded value.

  1. Create a new interface named CurrencyConversionService

  2. Add the following method to the interface:

    int convertToBritishPound(int amount);
    
  3. Create a stub class that will implement the interface named StubCurrencyConversionService

    @Service
    public class StubCurrencyConversionService implements CurrencyConversionService {
      public int convertToBritishPound(int amount) {
        return 1234;
      }
    }
    

B. Use Service in API Controller

Injection

  1. Open the CoffeeOrderApiController class

  2. Inject the CurrencyConversionService interface into the existing constructor as another parameter

  3. Run your tests and you might see some that are broken (no longer compile), so fix it by using the StubCurrencyConversionService implementation.

Currency Query Parameter

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

    public ResponseEntity<CoffeeOrderResponse> coffeeOrderInfo(
        @PathVariable("id") String coffeeOrderId,
        @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 Tests

  1. This will cause tests in CoffeeOrderApiControllerTest to fail to compile, so fix them by adding "usd" as the second parameter to the tests that call the coffeeOrderInfo method.

  2. Add this test to the CoffeeOrderApiControllerTest:

    @Test
    public void getWithPoundQueryParamConvertsPriceToPounds() throws Exception {
      CoffeeOrderRepository coffeeOrderRepository = new FakeCoffeeOrderRepository();
      CoffeeOrder coffeeOrder = new CoffeeOrder();
      CoffeeOrder savedCoffeeOrder = coffeeOrderRepository.save(coffeeOrder);
    
      CoffeeOrderApiController controller =
          new CoffeeOrderApiController(coffeeOrderRepository, new StubCurrencyConversionService());
    
      // When: we do a GET with "gbp" currency
      CoffeeOrderResponse response = controller.coffeeOrderInfo(
          String.valueOf(savedCoffeeOrder.getId()), "gbp").getBody();
    
      // Then: price will always be 1234
      assertThat(response.getPrice())
          .isEqualTo("1234");
    }
    
  3. This test should fail, but all other tests should still pass.

Fix the Test

  1. Open the CoffeeOrderApiController class

  2. In the coffeeOrderInfo method, if currency parameter is "gbp", then:

    1. Take the price from the coffee order (retrieved from the repository)

    2. Use the CurrencyConversionService to convert the price into Pounds

    3. Store the converted price into the CoffeeOrderResponse as the price.

  3. Once you implement that properly, all tests should now pass.

  4. Try it out using curl or Postman, both with and without the currency parameter of gbp.


Once you've completed the above steps,
check in with the instructor to review your code.