PartitionPathSupplier implements Supplier<Path>, AutoCloseable { @Override public Path get() { return Optional .of(roadModelSupplier.get().path("partitionPath")) .filter(path -> !path.isMissingNode()) .filter(path -> !path.isNull()) .map(JsonNode::asText) .filter(text -> !text.isEmpty()) .map(KeyPathParser::parse) .orElse(null); }  @Override Path get(); @Override void close();  }
@Test public void nullOnMissingPath() throws Exception { JsonNode model = new ObjectMapper().readTree("{}"); @SuppressWarnings("resource") Supplier<Path> pathSupplier = new PartitionPathSupplier(() -> model); assertThat(pathSupplier.get(), is(nullValue())); }
@Test public void nullOnNullPathValue() throws Exception { JsonNode model = new ObjectMapper().readTree("{\"partitionPath\":null}"); @SuppressWarnings("resource") Supplier<Path> pathSupplier = new PartitionPathSupplier(() -> model); assertThat(pathSupplier.get(), is(nullValue())); }
@Test public void nullOnEmptyPath() throws Exception { JsonNode model = new ObjectMapper().readTree("{\"partitionPath\":\"\"}"); @SuppressWarnings("resource") Supplier<Path> pathSupplier = new PartitionPathSupplier(() -> model); assertThat(pathSupplier.get(), is(nullValue())); }
@Test public void path() throws Exception { JsonNode model = new ObjectMapper().readTree("{\"partitionPath\":\"$.a\"}"); @SuppressWarnings("resource") Supplier<Path> pathSupplier = new PartitionPathSupplier(() -> model); assertThat(pathSupplier.get(), is(KeyPathParser.parse("$.a"))); }