JcrProperties implements ProgressCheckFactory { @Override public ProgressCheck newInstance(final JsonObject config) { List<Rule> pathScope = Rules.fromJsonArray(arrayOrEmpty(config, keys().scopePaths())); List<String> denyNodeTypes = mapArrayOfStrings(arrayOrEmpty(config, keys().denyNodeTypes())); List<String> nodeTypeScope = mapArrayOfStrings(arrayOrEmpty(config, keys().scopeNodeTypes())); final ResourceBundleHolder resourceBundleHolder = new ResourceBundleHolder(); List<JcrPropertyConstraints> propertyChecks = JcrPropertyConstraints .fromJsonArray(resourceBundleHolder::getResourceBundle, arrayOrEmpty(config, keys().properties())); return new Check(pathScope, denyNodeTypes, nodeTypeScope, propertyChecks, resourceBundleHolder); }  @NotNull static JsonKeys keys(); @Override ProgressCheck newInstance(final JsonObject config); @Deprecated
    static final String CONFIG_SCOPE_PATHS; @Deprecated
    static final String CONFIG_DENY_NODE_TYPES; @Deprecated
    static final String CONFIG_SCOPE_NODE_TYPES; @Deprecated
    static final String CONFIG_PROPERTIES; }
@Test public void testDenyIfAbsent() throws Exception { TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_man").key("denyIfAbsent", false))) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); logViolations("double_man, no scope", report); Assert.assertEquals("no violations", 0, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_man").key("denyIfAbsent", true))) .key("scopePaths", arr() .and(key("pattern", "/tmp/jcr:content").key("type", "allow"))) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("one violation", 1, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_man").key("denyIfAbsent", false))) .key("scopePaths", arr() .and(key("pattern", "/tmp/jcr:content").key("type", "allow"))) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); logViolations("double_man, with scope", report); Assert.assertEquals("no violations", 0, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); }
@Test public void testDenyIfPresent() throws Exception { TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_nan").key("denyIfPresent", false))) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); logViolations("double_nan, no scope", report); Assert.assertEquals("no violations: " + report.getViolations(), 0, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_nan").key("denyIfPresent", true))) .key("scopePaths", arr() .and(key("pattern", "/tmp/jcr:content").key("type", "allow"))) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("one violation: " + report.getViolations(), 1, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_nan").key("denyIfPresent", false))) .key("scopePaths", arr() .and(key("pattern", "/tmp/jcr:content").key("type", "allow"))) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); logViolations("double_nan, with scope", report); Assert.assertEquals("no violations: " + report.getViolations(), 0, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); }
@Test public void testRequireType() throws Exception { TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_nan").key("requireType", "Double")) .and(key("name", "double_neg_inf").key("requireType", "Double")) ) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("no violations: " + report.getViolations(), 0, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_nan").key("requireType", "String")) .and(key("name", "double_neg_inf").key("requireType", "String")) ) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("two violations: " + report.getViolations(), 2, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); }
@Test public void testDenyNodeTypes() throws Exception { TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("denyNodeTypes", arr("sling:Folder")) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("no violations: " + report.getViolations(), 0, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("denyNodeTypes", arr("nt:unstructured")) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("two violations: " + report.getViolations(), 2, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); }
@Test public void testScopeNodeTypes() throws Exception { TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .and(key("name", "double_nan").key("requireType", "String")) .and(key("name", "double_neg_inf").key("requireType", "String")) ) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("two violations: " + report.getViolations(), 2, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("scopeNodeTypes", arr("nt:unstructured")) .key("properties", arr() .and(key("name", "double_nan").key("requireType", "String")) .and(key("name", "double_neg_inf").key("requireType", "String")) ) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("two violations: " + report.getViolations(), 2, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("scopeNodeTypes", arr("sling:Folder")) .key("properties", arr() .and(key("name", "double_nan").key("requireType", "String")) .and(key("name", "double_neg_inf").key("requireType", "String")) ) .get()); CheckReport report = scanWithCheck(check, "double_properties.zip"); Assert.assertEquals("no violations: " + report.getViolations(), 0, report.getViolations().size()); assertTrue("all violations have packageIds", report.getViolations().stream() .allMatch(viol -> !viol.getPackages().isEmpty())); }); }
@Test public void testPropertyValueDeny() throws Exception { File playground = TestPackageUtil.prepareTestPackageFromFolder("playground.zip", new File("src/test/resources/jcr_prop_constraints_playground")); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("scopePaths", arr(new Rule(RuleType.INCLUDE, Pattern.compile("/apps/acme")))) .key("properties", arr() .val(obj() .key("name", "jcr:title") .key("valueRules", arr(new Rule(RuleType.DENY, Pattern.compile("Acme")))) ) ) .get()); CheckReport report = scanWithCheck(check, playground); Assert.assertEquals("one violation: " + report.getViolations(), 1, report.getViolations().size()); }); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .val(obj() .key("name", "multiString") .key("valueRules", arr(new Rule(RuleType.DENY, Pattern.compile("two")))) ) ) .get()); CheckReport report = scanWithCheck(check, playground); Assert.assertEquals("two violation: " + report.getViolations(), 2, report.getViolations().size()); }); }
@Test public void testDenyIfMultivalued() throws Exception { File playground = TestPackageUtil.prepareTestPackageFromFolder("playground.zip", new File("src/test/resources/jcr_prop_constraints_playground")); TestUtil.testBlock(() -> { ProgressCheck check = new JcrProperties().newInstance(obj() .key("properties", arr() .val(obj() .key("name", "singleString") .key("denyIfMultivalued", true) ) ) .get()); CheckReport report = scanWithCheck(check, playground); Assert.assertEquals("one violation: " + report.getViolations(), 1, report.getViolations().size()); }); }