### Question:
PayloadDeserializer extends StdDeserializer<Payload> { List<String> getStringOrArray(Map<String, JsonNode> tree, String claimName) throws JWTDecodeException { JsonNode node = tree.get(claimName); if (node == null || node.isNull() || !(node.isArray() || node.isTextual())) { return null; } if (node.isTextual() && !node.asText().isEmpty()) { return Collections.singletonList(node.asText()); } List<String> list = new ArrayList<>(node.size()); for (int i = 0; i < node.size(); i++) { try { list.add(objectReader.treeToValue(node.get(i), String.class)); } catch (JsonProcessingException e) { throw new JWTDecodeException("Couldn't map the Claim's array contents to String", e); } } return list; } PayloadDeserializer(ObjectReader reader); private  PayloadDeserializer(Class<?> vc, ObjectReader reader); @Override Payload deserialize(JsonParser p, DeserializationContext ctxt);  }### Answer:
@Test public void shouldThrowWhenParsingArrayWithObjectValue() throws Exception { exception.expect(JWTDecodeException.class); exception.expectMessage("Couldn't map the Claim's array contents to String"); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree("{\"some\" : \"random\", \"properties\" : \"inside\"}"); Map<String, JsonNode> tree = new HashMap<>(); List<JsonNode> subNodes = new ArrayList<>(); subNodes.add(jsonNode); ArrayNode arrNode = new ArrayNode(JsonNodeFactory.instance, subNodes); tree.put("key", arrNode); deserializer.getStringOrArray(tree, "key"); }
@Test public void shouldGetStringArrayWhenParsingArrayNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); List<JsonNode> subNodes = new ArrayList<>(); TextNode textNode1 = new TextNode("one"); TextNode textNode2 = new TextNode("two"); subNodes.add(textNode1); subNodes.add(textNode2); ArrayNode arrNode = new ArrayNode(JsonNodeFactory.instance, subNodes); tree.put("key", arrNode); List<String> values = deserializer.getStringOrArray(tree, "key"); assertThat(values, is(notNullValue())); assertThat(values, is(IsCollectionWithSize.hasSize(2))); assertThat(values, is(IsCollectionContaining.hasItems("one", "two"))); }
@Test public void shouldGetStringArrayWhenParsingTextNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); TextNode textNode = new TextNode("something"); tree.put("key", textNode); List<String> values = deserializer.getStringOrArray(tree, "key"); assertThat(values, is(notNullValue())); assertThat(values, is(IsCollectionWithSize.hasSize(1))); assertThat(values, is(IsCollectionContaining.hasItems("something"))); }
@Test public void shouldGetEmptyStringArrayWhenParsingEmptyTextNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); TextNode textNode = new TextNode(""); tree.put("key", textNode); List<String> values = deserializer.getStringOrArray(tree, "key"); assertThat(values, is(notNullValue())); assertThat(values, is(IsEmptyCollection.empty())); }
@Test public void shouldGetNullArrayWhenParsingNullNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); NullNode node = NullNode.getInstance(); tree.put("key", node); List<String> values = deserializer.getStringOrArray(tree, "key"); assertThat(values, is(nullValue())); }
@Test public void shouldGetNullArrayWhenParsingNullNodeValue() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); tree.put("key", null); List<String> values = deserializer.getStringOrArray(tree, "key"); assertThat(values, is(nullValue())); }
@Test public void shouldGetNullArrayWhenParsingNonArrayOrTextNode() throws Exception { Map<String, JsonNode> tree = new HashMap<>(); IntNode node = new IntNode(456789); tree.put("key", node); List<String> values = deserializer.getStringOrArray(tree, "key"); assertThat(values, is(nullValue())); }