Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Right behavior of getByTags(Set<String> tags) method #1413 (#1456)
Browse files Browse the repository at this point in the history
Signed-off-by: Yordan Mihaylov <j.mihailov@prosyst.com>
  • Loading branch information
Yordan Mihaylov authored and kaikreuzer committed May 4, 2016
1 parent e423ad0 commit 1cc7715
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ public void testRuleTags() {
Assert.assertTrue("Missing tag in rule", rule2GetTags.contains("tag2"));
}

/**
* test get rules by tags
*/
@Test
public void testGetRuleByTags() {
RuleEngine ruleEngine = createRuleEngine();

Rule rule1 = new Rule("rule1", null, null, null, null, null);
Set<String> ruleTags = new LinkedHashSet<String>();
ruleTags.add("tag1");
rule1.setTags(ruleTags);
ruleEngine.addRule(rule1, true);

Rule rule2 = new Rule("rule2", null, null, null, null, null);
Set<String> ruleTags2 = new LinkedHashSet<String>();
ruleTags2.add("tag1");
ruleTags2.add("tag2");
rule2.setTags(ruleTags2);
ruleEngine.addRule(rule2, true);

Collection<Rule> rules = ruleEngine.getRulesByTags(null);
Assert.assertNotNull("Cannot find rule by set of tags: <tag1> ", rules);
Assert.assertEquals("Collection of rules doesn't contains both rules.", 2, rules.size());

rules = ruleEngine.getRulesByTags(ruleTags);
Assert.assertNotNull("Cannot find rule by set of tags: <tag1> ", rules);
Assert.assertEquals("Collection of rules doesn't contains both rules.", 2, rules.size());

rules = ruleEngine.getRulesByTags(ruleTags2);
Assert.assertNotNull("Cannot find rule by set of tags: <tag1, tag2> ", rules);
Assert.assertEquals("Collection of rules has to contain only rule2.", 1, rules.size());
}

/**
* test rule configurations with null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,20 +765,17 @@ public synchronized Collection<Rule> getRulesByTags(Set<String> tags) {
RuntimeRule r = it.next();
if (tags != null) {
Set<String> rTags = r.getTags();
if (tags != null) {
for (Iterator<String> i = rTags.iterator(); i.hasNext();) {
String tag = i.next();
if (tags.contains(tag)) {
result.add(r.getRuleCopy());
break;
}
if (rTags != null) {
if (rTags.containsAll(tags)) {
result.add(r.getRuleCopy());
}
}
} else {
result.add(r.getRuleCopy());
}
}
return result;

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,10 @@ public <T extends Template> Collection<T> getByTags(Set<String> tags, Locale loc
for (Iterator<T> it = templates.iterator(); it.hasNext();) {
T t = it.next();
if (tags != null) {
Collection<String> rTags = t.getTags();
if (rTags != null) {
for (Iterator<String> itt = rTags.iterator(); itt.hasNext();) {
String tag = itt.next();
if (tags.contains(tag)) {
result.add(t);
break;
}
Collection<String> tTags = t.getTags();
if (tTags != null) {
if (tTags.containsAll(tags)) {
result.add(t);
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,9 @@ public <T extends ModuleType> Collection<T> getByTags(Set<String> tags, Locale l
for (Iterator<ModuleType> it = moduleTypes.iterator(); it.hasNext();) {
ModuleType mt = it.next();
if (tags != null) {
Collection<String> rTags = mt.getTags();
for (Iterator<String> itt = rTags.iterator(); itt.hasNext();) {
String tag = itt.next();
if (tags.contains(tag)) {
result.add((T) createCopy(mt));
break;
}
Collection<String> mtTags = mt.getTags();
if (mtTags.containsAll(tags)) {
result.add((T) createCopy(mt));
}
} else {
result.add((T) createCopy(mt));
Expand Down

0 comments on commit 1cc7715

Please sign in to comment.