@Test public void getHoldingsTest() { String username = "joe.coder@example.com"; Client client = mock(Client.class); User user = mock(User.class); UserProfile userProfile = mock(UserProfile.class); Principal principal = mock(Principal.class); when(principal.getName()).thenReturn(username); when(client.getUser(username)).thenReturn(user); when(user.getProfile()).thenReturn(userProfile); when(userProfile.get("holdings")).thenReturn( "[" + "{\"crypto\": \"crypto-value1\", \"currency\": \"currency-value1\", \"amount\": \"amount-value1\"}," + "{\"crypto\": \"crypto-value2\", \"currency\": \"currency-value2\", \"amount\": \"amount-value2\"}" + "]"); HoldingsController holdingsController = new HoldingsController(client); List<Holding> holdings = holdingsController.getHoldings(principal); assertThat(holdings, contains( pojo(Holding.class) .withProperty("crypto", is("crypto-value1")) .withProperty("currency", is("currency-value1")) .withProperty("amount", is("amount-value1")), pojo(Holding.class) .withProperty("crypto", is("crypto-value2")) .withProperty("currency", is("currency-value2")) .withProperty("amount", is("amount-value2")) )); }
@GetMapping public List<Holding> getHoldings(Principal principal) { User user = getUser(principal); String holdingsFromOkta = (String) user.getProfile().get(HOLDINGS_ATTRIBUTE_NAME); List<Holding> holdings = new LinkedList<>(); if (holdingsFromOkta != null) { try { holdings = mapper.readValue(holdingsFromOkta, new TypeReference<List<Holding>>() {}); } catch (IOException io) { logger.error("Error marshalling Okta custom data: " + io.getMessage()); io.printStackTrace(); } } return holdings; }
HoldingsController { @GetMapping public List<Holding> getHoldings(Principal principal) { User user = getUser(principal); String holdingsFromOkta = (String) user.getProfile().get(HOLDINGS_ATTRIBUTE_NAME); List<Holding> holdings = new LinkedList<>(); if (holdingsFromOkta != null) { try { holdings = mapper.readValue(holdingsFromOkta, new TypeReference<List<Holding>>() {}); } catch (IOException io) { logger.error("Error marshalling Okta custom data: " + io.getMessage()); io.printStackTrace(); } } return holdings; } }
HoldingsController { @GetMapping public List<Holding> getHoldings(Principal principal) { User user = getUser(principal); String holdingsFromOkta = (String) user.getProfile().get(HOLDINGS_ATTRIBUTE_NAME); List<Holding> holdings = new LinkedList<>(); if (holdingsFromOkta != null) { try { holdings = mapper.readValue(holdingsFromOkta, new TypeReference<List<Holding>>() {}); } catch (IOException io) { logger.error("Error marshalling Okta custom data: " + io.getMessage()); io.printStackTrace(); } } return holdings; } HoldingsController(Client client); }
HoldingsController { @GetMapping public List<Holding> getHoldings(Principal principal) { User user = getUser(principal); String holdingsFromOkta = (String) user.getProfile().get(HOLDINGS_ATTRIBUTE_NAME); List<Holding> holdings = new LinkedList<>(); if (holdingsFromOkta != null) { try { holdings = mapper.readValue(holdingsFromOkta, new TypeReference<List<Holding>>() {}); } catch (IOException io) { logger.error("Error marshalling Okta custom data: " + io.getMessage()); io.printStackTrace(); } } return holdings; } HoldingsController(Client client); @GetMapping List<Holding> getHoldings(Principal principal); @PostMapping Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal); }
HoldingsController { @GetMapping public List<Holding> getHoldings(Principal principal) { User user = getUser(principal); String holdingsFromOkta = (String) user.getProfile().get(HOLDINGS_ATTRIBUTE_NAME); List<Holding> holdings = new LinkedList<>(); if (holdingsFromOkta != null) { try { holdings = mapper.readValue(holdingsFromOkta, new TypeReference<List<Holding>>() {}); } catch (IOException io) { logger.error("Error marshalling Okta custom data: " + io.getMessage()); io.printStackTrace(); } } return holdings; } HoldingsController(Client client); @GetMapping List<Holding> getHoldings(Principal principal); @PostMapping Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal);  }