PolicyEngineConfigurator { public static PolicyEngine configure(final Collection<String> rulesetRefs) throws IllegalArgumentException { if (rulesetRefs == null || rulesetRefs.isEmpty()) { throw new IllegalArgumentException("Configuration Error: No rule set reference given"); } final Map<String, Set<Rule>> executorToRuleMapping = rulesetRefs.stream() .map(PolicyEngineConfigurator::createRuleset) .map(Ruleset::getRules) .flatMap(Collection::stream) .map(PolicyEngineConfigurator::mapRuleToExecutor) .collect(Collectors.groupingBy(RuleToExecutor::getExecutorClass, Collectors.mapping(RuleToExecutor::getRule, Collectors.toSet()))); PolicyEngine resultEngine = new PolicyEngine(createExecutors(executorToRuleMapping)); return resultEngine; }  static PolicyEngine configure(final Collection<String> rulesetRefs);  }
@Test public void testProperConfiguration() throws NoSuchFieldException, IllegalAccessException { PolicyEngine testResult = PolicyEngineConfigurator.configure(PolicyEngineTestdata.RULESET_CONFIG); assertThat(testResult).isNotNull(); Collection<RuleExecutor> createdExecutor = extractExecutor(testResult); assertThat(createdExecutor.size()).isEqualTo(1); assertThat(identifyExecutors(createdExecutor, SingleArtifactExecutor.class.getName())).isTrue(); assertThat(identifyExecutors(createdExecutor, CompareArtifactExecutor.class.getName())).isFalse(); Collection<Rule> createdRules = extractRules(createdExecutor); assertThat(createdRules.size()).isEqualTo(2); createdRules.forEach(rule -> assertThat(rule).isInstanceOf(SingleArtifactRule.class)); }
@Test(expected = IllegalArgumentException.class) public void testWrongClassname() { Collection<String> rulesets = new ArrayList<>(); rulesets.addAll(PolicyEngineTestdata.RULESET_CONFIG); rulesets.add("org.eclipse.sw360.antenna.policy.engine.DummyClass"); PolicyEngineConfigurator.configure(rulesets); }
@Test(expected = IllegalStateException.class) public void testRuleWithoutExecutor() { PolicyEngineConfigurator.configure(PolicyEngineTestdata.FAILING_RULESET_CONFIG); }
@Test(expected = IllegalArgumentException.class) public void testNoRulesets() { PolicyEngineConfigurator.configure(new ArrayList<>()); }
@Test(expected = IllegalArgumentException.class) public void testNullParam() { PolicyEngineConfigurator.configure(null); }
@Test(expected = IllegalArgumentException.class) public void testNotRulesetClass() { PolicyEngineConfigurator.configure(PolicyEngineTestdata.NO_RULESET_CLASS_CONFIG); }