CommonUtils { public String[] getPoliciesAsArray(ObjectMapper objMapper, String policyJson) throws JsonProcessingException, IOException{ ArrayList<String> policies = new ArrayList<String>(); JsonNode policiesNode = objMapper.readTree(policyJson).get("policies"); if (policiesNode.isContainerNode()) { Iterator<JsonNode> elementsIterator = policiesNode.elements(); while (elementsIterator.hasNext()) { JsonNode element = elementsIterator.next(); policies.add(element.asText()); } } else { policies.add(policiesNode.asText()); } JsonNode identityPoliciesNode = objMapper.readTree(policyJson).get("identity_policies"); if (identityPoliciesNode.isContainerNode()) { Iterator<JsonNode> elementsIterator = identityPoliciesNode.elements(); while (elementsIterator.hasNext()) { JsonNode element = elementsIterator.next(); policies.add(element.asText()); } } else { policies.add(identityPoliciesNode.asText()); } return policies.toArray(new String[policies.size()]); } CommonUtils(); String[] getPoliciesAsArray(ObjectMapper objMapper, String policyJson);  }
@Test public void test_getPoliciesAsArray() throws Exception { ObjectMapper objMapper = new ObjectMapper(); String jsonStr = "{\"id\":\"1JRRXfKHYuN2zbl1gkRM12zA\",\"last_renewal_time\":null,\"renewable\":false,\"policies\":[\"normal_user\",\"default\"], \"identity_policies\":[\"default2\"],\"creation_ttl\":1800000,\"username\":\"normaluser\"}"; String[] expectedPolicies = {"approle_normal_user", "default"}; String[] policies = commonUtils.getPoliciesAsArray(objMapper, jsonStr); }
@Test public void test_getPoliciesAsArray_string() throws Exception { ObjectMapper objMapper = new ObjectMapper(); String jsonStr = "{\"id\":\"1JRRXfKHYuN2zbl1gkRM12zA\",\"last_renewal_time\":null,\"renewable\":false,\"policies\":\"approle_normal_user\", \"identity_policies\":[\"default2\"], \"creation_ttl\":1800000,\"username\":\"normaluser\"}"; String[] expectedPolicies = {"approle_normal_user"}; String[] policies = commonUtils.getPoliciesAsArray(objMapper, jsonStr); }