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.
-
Create a new interface named
CurrencyConversionService
-
Add the following method to the interface:
int convertToBritishPound(int amount);
-
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¶
-
Open the
CoffeeOrderApiController
class -
Inject the
CurrencyConversionService
interface into the existing constructor as another parameter -
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¶
-
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 thecurrency
variable.The
defaultValue="usd"
means that if no query parameter exists,currency
will default to"usd"
.
Failing Tests¶
-
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 thecoffeeOrderInfo
method. -
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"); }
-
This test should fail, but all other tests should still pass.
Fix the Test¶
-
Open the
CoffeeOrderApiController
class -
In the
coffeeOrderInfo
method, ifcurrency
parameter is"gbp"
, then:-
Take the
price
from the coffee order (retrieved from the repository) -
Use the
CurrencyConversionService
to convert the price into Pounds -
Store the converted price into the
CoffeeOrderResponse
as the price.
-
-
Once you implement that properly, all tests should now pass.
-
Try it out using
curl
or Postman, both with and without the currency parameter ofgbp
.
Once you've completed the above steps,
check in with the instructor to review your code.