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

Right behavior of getByTags(Set<String> tags) method #1413 #1456

Merged
merged 1 commit into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -782,20 +782,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