Skip to content

Commit

Permalink
Add support for expression groups
Browse files Browse the repository at this point in the history
The provider can be called with -DgroupsExpression=and(group1,group2) for which the grammar would support and(GROUP...), or(GROUP...) and not(GROUP)

Signed-off-by: Alex ROBUCHON <alex.robuchon@forgerock.com>
  • Loading branch information
Alex ROBUCHON committed Feb 16, 2018
1 parent a3f9d90 commit 87cd3f2
Show file tree
Hide file tree
Showing 18 changed files with 881 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

package org.forgerock.cuppa.maven.surefire;

import static java.util.Collections.emptySet;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -52,43 +53,55 @@ public final class CuppaSurefireProvider extends AbstractProvider {
public CuppaSurefireProvider(ProviderParameters parameters) {
this.providerParameters = parameters;
Map<String, String> properties = parameters.getProviderProperties();
tags = new Tags(getTags(properties), getExcludedTags(properties));
tags = new Tags(getIncludedTags(properties), getExcludedTags(properties),
getExpressionTags(properties));

if (!tags.expressionTags.isEmpty() && (!tags.tags.isEmpty() || !tags.excludedTags.isEmpty())) {
throw new RuntimeException("Use of groupsExpression/tagsExpression cannot be used with "
+ "excludedGroups/excludedTags or groups/tags");
}
}

private String getExpressionTags(Map<String, String> properties) {
return getTagsFromPropertiesOrSystem("groupsExpression", "tagsExpression", properties);
}

private Set<String> getIncludedTags(Map<String, String> properties) {
return split(getTagsFromPropertiesOrSystem("groups", "tags", properties));
}

private Set<String> getTags(Map<String, String> properties) {
String groups = properties.get("groups");
private Set<String> getExcludedTags(Map<String, String> properties) {
return split(getTagsFromPropertiesOrSystem("excludedGroups", "excludedTags", properties));
}

private String getTagsFromPropertiesOrSystem(String groupName, String tagName,
Map<String, String> properties) {
String groups = properties.get(groupName);
if (groups == null) {
groups = System.getProperty("groups");
groups = System.getProperty(groupName);
}
String tags = properties.get("tags");
String overrideTags = System.getProperty("tags");
String tags = properties.get(tagName);
String overrideTags = System.getProperty(tagName);
return getTags(groups, overrideTags == null ? tags : overrideTags);
}

private Set<String> getTags(String groups, String tags) {
private String getTags(String groups, String tags) {
if (groups != null && tags != null) {
throw new RuntimeException("Use of 'groups/excludedGroups' and 'tags/excludedTags' "
+ "are mutually exclusive.");
throw new RuntimeException("Use of 'groups/excludedGroups/groupsExpression' and"
+ " 'tags/excludedTags/tagsExpression are mutually exclusive.");
} else if (groups != null) {
return split(groups);
return groups;
} else if (tags != null) {
return split(tags);
return tags;
} else {
return Collections.emptySet();
return "";
}
}

private Set<String> getExcludedTags(Map<String, String> properties) {
String excludedGroups = properties.get("excludedGroups");
if (excludedGroups == null) {
excludedGroups = System.getProperty("excludedGroups");
}
String excludedTags = properties.get("excludedTags");
String overrideExcludedTags = System.getProperty("excludedTags");
return getTags(excludedGroups, overrideExcludedTags == null ? excludedTags : overrideExcludedTags);
}

private Set<String> split(String s) {
if (s.isEmpty()) {
return emptySet();
}
return Arrays.stream(s.split(",")).map(String::trim).collect(Collectors.toSet());
}

Expand Down
5 changes: 3 additions & 2 deletions cuppa/src/main/java/org/forgerock/cuppa/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.forgerock.cuppa.internal.filters.EmptyTestBlockFilter;
import org.forgerock.cuppa.internal.filters.OnlyTestBlockFilter;
import org.forgerock.cuppa.internal.filters.TagTestBlockFilter;
import org.forgerock.cuppa.internal.filters.expression.ExpressionTagTestBlockFilter;
import org.forgerock.cuppa.model.Tags;
import org.forgerock.cuppa.model.TestBlock;
import org.forgerock.cuppa.model.TestBlockBuilder;
Expand Down Expand Up @@ -77,8 +78,8 @@ public Runner(Tags runTags) {
* @param configuration Cuppa configuration to control the behaviour of the runner.
*/
public Runner(Tags runTags, Configuration configuration) {
coreTestTransforms = Arrays.asList(new OnlyTestBlockFilter(), new TagTestBlockFilter(runTags),
new EmptyTestBlockFilter());
coreTestTransforms = Arrays.asList(new OnlyTestBlockFilter(), new ExpressionTagTestBlockFilter(runTags),
new TagTestBlockFilter(runTags), new EmptyTestBlockFilter());
this.configuration = configuration;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2018 ForgeRock AS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.forgerock.cuppa.internal.filters.expression;

import java.util.Collection;
import java.util.Collections;

/**
* A condition that composes other conditions with a logical AND.
*/
class AndCondition extends ConditionWrapper {

public static final AndCondition EMPTY = new AndCondition(Collections.emptyList());
private final Collection<Condition> conditions;

/**
* Constructor.
*
* @param conditions a list of condition to compose.
*/
AndCondition(Collection<Condition> conditions) {
this.conditions = Collections.unmodifiableCollection(conditions);
}

@Override
public boolean shouldRun(Collection<String> tags) {
return conditions.stream().allMatch(c -> c.shouldRun(tags));
}

@Override
public ConditionWrapper setConditions(Collection<Condition> conditions) {
return new AndCondition(conditions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2018 ForgeRock AS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.forgerock.cuppa.internal.filters.expression;

import java.util.Collection;

/**
* A condition used by {@link ExpressionTagTestBlockFilter}.
*/
@FunctionalInterface
public interface Condition {

/**
* Check if the list of tags is compliant with the condition.
*
* @param tags The collection of tags.
* @return true if the condition complies with the tags supplied, false otherwise.
*/
boolean shouldRun(Collection<String> tags);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.forgerock.cuppa.internal.filters.expression;

import java.util.Arrays;
import java.util.List;

/**
* A factory to creates {@link ConditionWrapper}.
*/
final class ConditionFactory {

/**
* link the operator to an instance of {@link Condition}.
*/
enum ConditionEnum {
AND("and", AndCondition.EMPTY),
OR("or", OrCondition.EMPTY),
NOT("not", NotCondition.EMPTY);

private String operator;
private ConditionWrapper condition;

ConditionEnum(String operator, ConditionWrapper condition) {
this.operator = operator;
this.condition = condition;
}

/**
* Get a ConditionEnum fron an string operator.
* @param operator The operator we want to get the associated enum
* @return The ConditionEnum
*/
static ConditionEnum getFromOperator(String operator) {
return Arrays.stream(ConditionEnum.values())
.filter(c -> c.operator.equals(operator))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(operator + " is not supported. The list of "
+ "supported operator is and, or, not"));
}

Condition getCondition(List<Condition> tags) {
return condition.setConditions(tags);
}
}

private ConditionFactory() {
}

/**
* Create a {@link Condition}.
* @param operator The operator we want to create a condition for.
* @param tags The list of conditions that {@link ConditionWrapper} will include.
* @return A condition
*/
static Condition get(String operator, List<Condition> tags) {
return ConditionEnum.getFromOperator(operator.trim().toLowerCase())
.getCondition(tags);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.forgerock.cuppa.internal.filters.expression;

import java.util.Collection;

/**
* A condition wrapper for Condition that needs to wrap other conditions.
*/
abstract class ConditionWrapper implements Condition {

/**
* Create a new Condition with the given conditions.
* @param conditions the collection of conditions
* @return a new condition
*/
abstract ConditionWrapper setConditions(Collection<Condition> conditions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2018 ForgeRock AS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.forgerock.cuppa.internal.filters.expression;

import java.util.Collection;

/**
* A condition that checks if a tag is contains in a collection of tags.
*/
class ContainsCondition implements Condition {

private String tag;

/**
* Constructor.
*
* @param tag A group/tag we want to search for.
*/
ContainsCondition(String tag) {
this.tag = tag;
}

@Override
public boolean shouldRun(Collection<String> tags) {
return tags.contains(tag);
}
}
Loading

0 comments on commit 87cd3f2

Please sign in to comment.