@Test public void testGetMissingFields_WithAnys_EmptyCursor() { Path path = mock(Path.class); when(path.nAnys()).thenReturn(1); when(path.prefix(-1)).thenReturn(mock(Path.class)); when(path.tail(0)).thenReturn("FAKE_FIELD_NAME"); @SuppressWarnings("unchecked") KeyValueCursor<Path, JsonNode> kvCursor = mock(KeyValueCursor.class); when(kvCursor.hasNext()).thenReturn(false); JsonDoc doc = mock(JsonDoc.class); when(doc.get(path)).thenReturn(mock(JsonNode.class)); when(doc.getAllNodes(any(Path.class))).thenReturn(kvCursor); List<Path> results = RequiredChecker.getMissingFields(path, doc); assertNotNull(results); assertTrue(results.isEmpty()); }
public static List<Path> getMissingFields(Path fieldMetadataPath, JsonDoc doc) { LOGGER.debug("Checking {}", fieldMetadataPath); int nAnys = fieldMetadataPath.nAnys(); List<Path> errors = new ArrayList<>(); if (nAnys == 0) { JsonNode fieldNode = doc.get(fieldMetadataPath); if (fieldNode == null) { if (fieldMetadataPath.numSegments() > 1) { JsonNode parent = doc.get(fieldMetadataPath.prefix(-1)); if (parent == null || parent instanceof NullNode) { ; } else { errors.add(fieldMetadataPath); } } else { errors.add(fieldMetadataPath); } } else if (fieldNode instanceof NullNode) { errors.add(fieldMetadataPath); } } else { Path parent = fieldMetadataPath.prefix(-1); String fieldName = fieldMetadataPath.tail(0); LOGGER.debug("Checking {} under {}", fieldName, parent); KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(parent); while (cursor.hasNext()) { cursor.next(); JsonNode parentObject = cursor.getCurrentValue(); if (!(parentObject instanceof NullNode)) { LOGGER.debug("Checking {}", cursor.getCurrentKey()); JsonNode fieldNode = parentObject.get(fieldName); if (fieldNode == null || fieldNode instanceof NullNode) { errors.add(new Path(cursor.getCurrentKey() + "." + fieldName)); } } } } LOGGER.debug("Errors:{}", errors); return errors; }
RequiredChecker implements FieldConstraintDocChecker { public static List<Path> getMissingFields(Path fieldMetadataPath, JsonDoc doc) { LOGGER.debug("Checking {}", fieldMetadataPath); int nAnys = fieldMetadataPath.nAnys(); List<Path> errors = new ArrayList<>(); if (nAnys == 0) { JsonNode fieldNode = doc.get(fieldMetadataPath); if (fieldNode == null) { if (fieldMetadataPath.numSegments() > 1) { JsonNode parent = doc.get(fieldMetadataPath.prefix(-1)); if (parent == null || parent instanceof NullNode) { ; } else { errors.add(fieldMetadataPath); } } else { errors.add(fieldMetadataPath); } } else if (fieldNode instanceof NullNode) { errors.add(fieldMetadataPath); } } else { Path parent = fieldMetadataPath.prefix(-1); String fieldName = fieldMetadataPath.tail(0); LOGGER.debug("Checking {} under {}", fieldName, parent); KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(parent); while (cursor.hasNext()) { cursor.next(); JsonNode parentObject = cursor.getCurrentValue(); if (!(parentObject instanceof NullNode)) { LOGGER.debug("Checking {}", cursor.getCurrentKey()); JsonNode fieldNode = parentObject.get(fieldName); if (fieldNode == null || fieldNode instanceof NullNode) { errors.add(new Path(cursor.getCurrentKey() + "." + fieldName)); } } } } LOGGER.debug("Errors:{}", errors); return errors; } }
RequiredChecker implements FieldConstraintDocChecker { public static List<Path> getMissingFields(Path fieldMetadataPath, JsonDoc doc) { LOGGER.debug("Checking {}", fieldMetadataPath); int nAnys = fieldMetadataPath.nAnys(); List<Path> errors = new ArrayList<>(); if (nAnys == 0) { JsonNode fieldNode = doc.get(fieldMetadataPath); if (fieldNode == null) { if (fieldMetadataPath.numSegments() > 1) { JsonNode parent = doc.get(fieldMetadataPath.prefix(-1)); if (parent == null || parent instanceof NullNode) { ; } else { errors.add(fieldMetadataPath); } } else { errors.add(fieldMetadataPath); } } else if (fieldNode instanceof NullNode) { errors.add(fieldMetadataPath); } } else { Path parent = fieldMetadataPath.prefix(-1); String fieldName = fieldMetadataPath.tail(0); LOGGER.debug("Checking {} under {}", fieldName, parent); KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(parent); while (cursor.hasNext()) { cursor.next(); JsonNode parentObject = cursor.getCurrentValue(); if (!(parentObject instanceof NullNode)) { LOGGER.debug("Checking {}", cursor.getCurrentKey()); JsonNode fieldNode = parentObject.get(fieldName); if (fieldNode == null || fieldNode instanceof NullNode) { errors.add(new Path(cursor.getCurrentKey() + "." + fieldName)); } } } } LOGGER.debug("Errors:{}", errors); return errors; }  }
RequiredChecker implements FieldConstraintDocChecker { public static List<Path> getMissingFields(Path fieldMetadataPath, JsonDoc doc) { LOGGER.debug("Checking {}", fieldMetadataPath); int nAnys = fieldMetadataPath.nAnys(); List<Path> errors = new ArrayList<>(); if (nAnys == 0) { JsonNode fieldNode = doc.get(fieldMetadataPath); if (fieldNode == null) { if (fieldMetadataPath.numSegments() > 1) { JsonNode parent = doc.get(fieldMetadataPath.prefix(-1)); if (parent == null || parent instanceof NullNode) { ; } else { errors.add(fieldMetadataPath); } } else { errors.add(fieldMetadataPath); } } else if (fieldNode instanceof NullNode) { errors.add(fieldMetadataPath); } } else { Path parent = fieldMetadataPath.prefix(-1); String fieldName = fieldMetadataPath.tail(0); LOGGER.debug("Checking {} under {}", fieldName, parent); KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(parent); while (cursor.hasNext()) { cursor.next(); JsonNode parentObject = cursor.getCurrentValue(); if (!(parentObject instanceof NullNode)) { LOGGER.debug("Checking {}", cursor.getCurrentKey()); JsonNode fieldNode = parentObject.get(fieldName); if (fieldNode == null || fieldNode instanceof NullNode) { errors.add(new Path(cursor.getCurrentKey() + "." + fieldName)); } } } } LOGGER.debug("Errors:{}", errors); return errors; }  @Override void checkConstraint(ConstraintValidator validator,
                                FieldTreeNode fieldMetadata,
                                Path fieldMetadataPath,
                                FieldConstraint constraint,
                                JsonDoc doc); static List<Path> getMissingFields(Path fieldMetadataPath,
                                              JsonDoc doc); }
RequiredChecker implements FieldConstraintDocChecker { public static List<Path> getMissingFields(Path fieldMetadataPath, JsonDoc doc) { LOGGER.debug("Checking {}", fieldMetadataPath); int nAnys = fieldMetadataPath.nAnys(); List<Path> errors = new ArrayList<>(); if (nAnys == 0) { JsonNode fieldNode = doc.get(fieldMetadataPath); if (fieldNode == null) { if (fieldMetadataPath.numSegments() > 1) { JsonNode parent = doc.get(fieldMetadataPath.prefix(-1)); if (parent == null || parent instanceof NullNode) { ; } else { errors.add(fieldMetadataPath); } } else { errors.add(fieldMetadataPath); } } else if (fieldNode instanceof NullNode) { errors.add(fieldMetadataPath); } } else { Path parent = fieldMetadataPath.prefix(-1); String fieldName = fieldMetadataPath.tail(0); LOGGER.debug("Checking {} under {}", fieldName, parent); KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(parent); while (cursor.hasNext()) { cursor.next(); JsonNode parentObject = cursor.getCurrentValue(); if (!(parentObject instanceof NullNode)) { LOGGER.debug("Checking {}", cursor.getCurrentKey()); JsonNode fieldNode = parentObject.get(fieldName); if (fieldNode == null || fieldNode instanceof NullNode) { errors.add(new Path(cursor.getCurrentKey() + "." + fieldName)); } } } } LOGGER.debug("Errors:{}", errors); return errors; }  @Override void checkConstraint(ConstraintValidator validator,
                                FieldTreeNode fieldMetadata,
                                Path fieldMetadataPath,
                                FieldConstraint constraint,
                                JsonDoc doc); static List<Path> getMissingFields(Path fieldMetadataPath,
                                              JsonDoc doc);  }