@Test public void saveHoldingsTest() throws IOException { Client client = mock(Client.class); User user = mock(User.class); UserProfile userProfile = mock(UserProfile.class); Principal principal = mock(Principal.class); String username = "joe.coder@example.com"; when(principal.getName()).thenReturn(username); when(client.getUser(username)).thenReturn(user); when(user.getProfile()).thenReturn(userProfile); Holding[] inputHoldings = new Holding[] { new Holding() .setCrypto("crypto1") .setCurrency("currency1") .setAmount("amount1"), new Holding() .setCrypto("crypto2") .setCurrency("currency2") .setAmount("amount2") }; HoldingsController holdingsController = new HoldingsController(client); Holding[] outputHoldings = holdingsController.saveHoldings(inputHoldings, principal); assertThat(outputHoldings, is(inputHoldings)); ArgumentCaptor<String> holdingsJsonCaptor = ArgumentCaptor.forClass(String.class); verify(userProfile).put(eq("holdings"), holdingsJsonCaptor.capture()); verify(user).update(); JsonNode holdingsParsed = new ObjectMapper().readTree(holdingsJsonCaptor.getValue()); assertThat(holdingsParsed, jsonArray(contains( jsonObject() .where("crypto", jsonText("crypto1")) .where("currency", jsonText("currency1")) .where("amount", jsonText("amount1")), jsonObject() .where("crypto", jsonText("crypto2")) .where("currency", jsonText("currency2")) .where("amount", jsonText("amount2")) ))); }
@PostMapping public Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal) { User user = getUser(principal); try { String json = mapper.writeValueAsString(holdings); user.getProfile().put(HOLDINGS_ATTRIBUTE_NAME, json); user.update(); } catch (JsonProcessingException e) { logger.error("Error saving Okta custom data: " + e.getMessage()); e.printStackTrace(); } return holdings; }
HoldingsController { @PostMapping public Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal) { User user = getUser(principal); try { String json = mapper.writeValueAsString(holdings); user.getProfile().put(HOLDINGS_ATTRIBUTE_NAME, json); user.update(); } catch (JsonProcessingException e) { logger.error("Error saving Okta custom data: " + e.getMessage()); e.printStackTrace(); } return holdings; } }
HoldingsController { @PostMapping public Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal) { User user = getUser(principal); try { String json = mapper.writeValueAsString(holdings); user.getProfile().put(HOLDINGS_ATTRIBUTE_NAME, json); user.update(); } catch (JsonProcessingException e) { logger.error("Error saving Okta custom data: " + e.getMessage()); e.printStackTrace(); } return holdings; } HoldingsController(Client client); }
HoldingsController { @PostMapping public Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal) { User user = getUser(principal); try { String json = mapper.writeValueAsString(holdings); user.getProfile().put(HOLDINGS_ATTRIBUTE_NAME, json); user.update(); } catch (JsonProcessingException e) { logger.error("Error saving Okta custom data: " + e.getMessage()); e.printStackTrace(); } return holdings; } HoldingsController(Client client); @GetMapping List<Holding> getHoldings(Principal principal); @PostMapping Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal); }
HoldingsController { @PostMapping public Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal) { User user = getUser(principal); try { String json = mapper.writeValueAsString(holdings); user.getProfile().put(HOLDINGS_ATTRIBUTE_NAME, json); user.update(); } catch (JsonProcessingException e) { logger.error("Error saving Okta custom data: " + e.getMessage()); e.printStackTrace(); } return holdings; } HoldingsController(Client client); @GetMapping List<Holding> getHoldings(Principal principal); @PostMapping Holding[] saveHoldings(@RequestBody Holding[] holdings, Principal principal);  }