use of java.util.function.Predicate in project sling by apache.
the class ValidationServiceImplTest method testValidateResourceRecursively.
@Test()
public void testValidateResourceRecursively() throws Exception {
modelBuilder.resourceProperty(propertyBuilder.build("field1"));
final ValidationModel vm1 = modelBuilder.build("resourcetype1", "some source");
modelBuilder = new ValidationModelBuilder();
modelBuilder.resourceProperty(propertyBuilder.build("field2"));
final ValidationModel vm2 = modelBuilder.build("resourcetype2", "some source");
// set model retriever
validationService.modelRetriever = new ValidationModelRetriever() {
@Override
@CheckForNull
public ValidationModel getValidationModel(@Nonnull String resourceType, String resourcePath, boolean considerResourceSuperTypeModels) {
if (resourceType.equals("resourcetype1")) {
return vm1;
} else if (resourceType.equals("resourcetype2")) {
return vm2;
} else {
return null;
}
}
};
ResourceResolver rr = context.resourceResolver();
// resource is lacking the required field (is invalid)
Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", "resourcetype1", JcrConstants.NT_UNSTRUCTURED, true);
// child1 is valid
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype2");
properties.put("field2", "test");
rr.create(testResource, "child1", properties);
// child2 is invalid
properties.clear();
properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype2");
rr.create(testResource, "child2", properties);
// child3 has no model (but its resource type is ignored)
properties.clear();
properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype3");
rr.create(testResource, "child3", properties);
final Predicate<Resource> ignoreResourceType3Filter = new Predicate<Resource>() {
@Override
public boolean test(final Resource resource) {
return !"resourcetype3".equals(resource.getResourceType());
}
};
ValidationResult vr = validationService.validateResourceRecursively(testResource, true, ignoreResourceType3Filter, false);
Assert.assertFalse("resource should have been considered invalid", vr.isValid());
Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"), new DefaultValidationFailure("child2", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field2")));
}
use of java.util.function.Predicate in project helios by spotify.
the class HostsResource method list.
/**
* Returns the list of hostnames of known hosts/agents.
* @return The list of hostnames.
*/
@GET
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public List<String> list(@QueryParam("namePattern") final String namePattern, @QueryParam("selector") final List<String> hostSelectors) {
List<String> hosts = model.listHosts();
if (namePattern != null) {
final Predicate<String> matchesPattern = Pattern.compile(namePattern).asPredicate();
hosts = hosts.stream().filter(matchesPattern).collect(Collectors.toList());
}
if (!hostSelectors.isEmpty()) {
// check that all supplied selectors are parseable/valid
final List<HostSelector> selectors = hostSelectors.stream().map(selectorStr -> {
final HostSelector parsed = HostSelector.parse(selectorStr);
if (parsed == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Invalid host selector: " + selectorStr).build());
}
return parsed;
}).collect(Collectors.toList());
final Map<String, Map<String, String>> hostsAndLabels = getLabels(hosts);
final HostMatcher matcher = new HostMatcher(hostsAndLabels);
hosts = matcher.getMatchingHosts(selectors);
}
return hosts;
}
use of java.util.function.Predicate in project ddf by codice.
the class SubjectUtilsTest method testFilterDNDropTwo.
@Test
public void testFilterDNDropTwo() {
Predicate<RDN> predicate = rdn -> !ImmutableSet.of(BCStyle.C, BCStyle.ST).contains(rdn.getTypesAndValues()[0].getType());
String baseDN = SubjectUtils.filterDN(dnPrincipal, predicate);
assertThat(baseDN, is("CN=Foo,OU=Engineering,OU=Dev,O=DDF"));
}
use of java.util.function.Predicate in project ddf by codice.
the class SubjectUtilsTest method testFilterDNRemoveAll.
@Test
public void testFilterDNRemoveAll() {
Predicate<RDN> predicate = rdn -> !ImmutableSet.of(BCStyle.OU, BCStyle.CN, BCStyle.O, BCStyle.ST, BCStyle.C).contains(rdn.getTypesAndValues()[0].getType());
String baseDN = SubjectUtils.filterDN(dnPrincipal, predicate);
assertThat(baseDN, is(""));
}
use of java.util.function.Predicate in project Gargoyle by callakrsos.
the class FilteredTreeItemExam method start.
@Override
public void start(Stage primaryStage) throws Exception {
FilterableTreeItem<String> filterableTreeItem = new FilterableTreeItem<>("");
filterableTreeItem.setExpanded(true);
TreeView<String> treeView = new TreeView<>(filterableTreeItem);
treeView.setShowRoot(false);
treeView.setRoot(filterableTreeItem);
BorderPane borderPane = new BorderPane(treeView);
TextField value = new TextField();
value.textProperty().addListener((oba, oldval, newval) -> {
Callable<TreeItemPredicate<String>> func = () -> {
Predicate<String> predicate = str -> str.indexOf(newval) >= 0;
return TreeItemPredicate.<String>create(predicate);
};
ObjectBinding<TreeItemPredicate<String>> createObjectBinding = Bindings.createObjectBinding(func, hide);
filterableTreeItem.predicateProperty().bind(createObjectBinding);
});
borderPane.setTop(value);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
FilterableTreeItem<String> e = new FilterableTreeItem<>("ABC");
// e.getChildren().add(new FilterableTreeItem<>("DEF"));
// e.getChildren().add(new FilterableTreeItem<>("김aa"));
// e.getChildren().add(new FilterableTreeItem<>("김bb"));
// e.getChildren().add(new FilterableTreeItem<>("김cc"));
// filterableTreeItem.getChildren().add(e);
// filterableTreeItem.getChildren().add(new FilterableTreeItem<>("DEF"));
// filterableTreeItem.getChildren().add(new FilterableTreeItem<>("김DD"));
//
e.getSourceChildren().add(new FilterableTreeItem<>("DEF"));
e.getSourceChildren().add(new FilterableTreeItem<>("김aa"));
e.getSourceChildren().add(new FilterableTreeItem<>("김bb"));
e.getSourceChildren().add(new FilterableTreeItem<>("김cc"));
filterableTreeItem.getSourceChildren().add(e);
filterableTreeItem.getSourceChildren().add(new FilterableTreeItem<>("DEF"));
filterableTreeItem.getSourceChildren().add(new FilterableTreeItem<>("김DD"));
// filterableTreeItem.predicateProperty()
// .bind(Bindings.createObjectBinding(() ->
// TreeItemPredicate<String>.create(str -> str.indexOf("김") >= 0),
// hide));
}
Aggregations