From: Simon Brandhof Date: Mon, 28 May 2012 21:10:10 +0000 (+0200) Subject: Extract beta api from review workflow X-Git-Tag: 3.1~41 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bd839a5cd75785456330f6c282a42ed2c3dc6725;p=sonarqube.git Extract beta api from review workflow --- diff --git a/sonar-core/src/main/java/org/sonar/core/review/package-info.java b/sonar-core/src/main/java/org/sonar/core/review/package-info.java index 8e8e350ae15..b9180bc8ce9 100644 --- a/sonar-core/src/main/java/org/sonar/core/review/package-info.java +++ b/sonar-core/src/main/java/org/sonar/core/review/package-info.java @@ -17,5 +17,7 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ +@ParametersAreNonnullByDefault package org.sonar.core.review; +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/ReviewDatabaseStore.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/ReviewDatabaseStore.java deleted file mode 100644 index 9dd425e38bc..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/ReviewDatabaseStore.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import com.google.common.annotations.VisibleForTesting; -import org.apache.ibatis.session.SqlSession; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.ServerComponent; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.core.persistence.MyBatis; -import org.sonar.core.properties.PropertiesMapper; -import org.sonar.core.properties.PropertyDto; -import org.sonar.core.review.ReviewCommentDto; -import org.sonar.core.review.ReviewCommentMapper; -import org.sonar.core.review.ReviewDto; -import org.sonar.core.review.ReviewMapper; -import org.sonar.core.review.workflow.review.Comment; -import org.sonar.core.review.workflow.review.DefaultReview; -import java.util.Date; -import java.util.List; - -public class ReviewDatabaseStore implements ReviewStore, ServerComponent { - private static final Logger LOG = LoggerFactory.getLogger(ReviewDatabaseStore.class); - - private MyBatis mybatis; - - public ReviewDatabaseStore(MyBatis mb) { - this.mybatis = mb; - } - - public void store(DefaultReview review) { - store(review, new Date()); - } - - @VisibleForTesting - void store(DefaultReview review, Date now) { - if (review.getReviewId() == null) { - LOG.error("Review has no id. Violation id is: " + review.getViolationId()); - return; - } - - SqlSession session = mybatis.openSession(); - ReviewMapper mapper = session.getMapper(ReviewMapper.class); - ReviewCommentMapper commentMapper = session.getMapper(ReviewCommentMapper.class); - try { - ReviewDto dto = mapper.findById(review.getReviewId()); - dto.setResolution(review.getResolution()); - dto.setStatus(review.getStatus()); - dto.setData(KeyValueFormat.format(review.getProperties())); - dto.setUpdatedAt(now); - mapper.update(dto); - - for (Comment comment : review.getNewComments()) { - ReviewCommentDto commentDto = new ReviewCommentDto(); - commentDto.setReviewId(dto.getId()); - commentDto.setText(comment.getMarkdownText()); - commentDto.setCreatedAt(now); - commentDto.setUpdatedAt(now); - commentDto.setUserId(comment.getUserId()); - commentMapper.insert(commentDto); - } - session.commit(); - - } finally { - MyBatis.closeQuietly(session); - } - } - - public void completeProjectSettings(Long projectId, Settings settings, List propertyKeys) { - if (propertyKeys.isEmpty()) { - return; - } - - SqlSession session = mybatis.openSession(); - PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); - try { - List dtos = mapper.selectSetOfResourceProperties(projectId, propertyKeys); - for (PropertyDto dto : dtos) { - settings.setProperty(dto.getKey(), dto.getValue()); - } - - } finally { - MyBatis.closeQuietly(session); - } - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/ReviewStore.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/ReviewStore.java deleted file mode 100644 index 36540973903..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/ReviewStore.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import org.sonar.api.config.Settings; -import org.sonar.core.review.workflow.review.DefaultReview; -import java.util.List; - -public interface ReviewStore { - void store(DefaultReview review); - - void completeProjectSettings(Long projectId, Settings settings, List propertyKeys); -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/SampleWorkflowBuilder.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/SampleWorkflowBuilder.java deleted file mode 100644 index a6a89ccd2c6..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/SampleWorkflowBuilder.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import org.sonar.api.ServerExtension; -import org.sonar.core.review.workflow.function.CommentFunction; -import org.sonar.core.review.workflow.screen.CommentScreen; - -/** - * Sample of workflow customization. Not used. - */ -public class SampleWorkflowBuilder implements ServerExtension { - - private final Workflow workflow; - - public SampleWorkflowBuilder(Workflow workflow) { - this.workflow = workflow; - } - - public void start() { - workflow.addCommand("comment"); - workflow.setScreen("comment", new CommentScreen()); - workflow.addFunction("comment", new CommentFunction()); - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/Workflow.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/Workflow.java deleted file mode 100644 index 3a4004e5b40..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/Workflow.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.common.collect.*; -import org.sonar.api.ServerComponent; -import org.sonar.core.review.workflow.condition.Condition; -import org.sonar.core.review.workflow.condition.ProjectPropertyCondition; -import org.sonar.core.review.workflow.function.Function; -import org.sonar.core.review.workflow.screen.Screen; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -public class Workflow implements ServerComponent { - - private Set commands = Sets.newLinkedHashSet(); - private ListMultimap conditionsByCommand = ArrayListMultimap.create(); - private ListMultimap functionsByCommand = ArrayListMultimap.create(); - private Map screensByCommand = Maps.newLinkedHashMap(); - - /** - * Keys of all the properties that are required by conditions (see {@link org.sonar.core.review.workflow.condition.ProjectPropertyCondition} - */ - private List projectPropertyKeys = Lists.newArrayList(); - - /** - * Optimization: fast way to get all context conditions - */ - private ListMultimap contextConditionsByCommand = ArrayListMultimap.create(); - - /** - * Optimization: fast way to get all review conditions - */ - private ListMultimap reviewConditionsByCommand = ArrayListMultimap.create(); - - - public Workflow addCommand(String key) { - Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Empty command key"); - commands.add(key); - return this; - } - - public Set getCommands() { - return commands; - } - - public boolean hasCommand(String key) { - return commands.contains(key); - } - - List getProjectPropertyKeys() { - return projectPropertyKeys; - } - - /** - * Shortcut for: getReviewConditions(commandKey) + getContextConditions(commandKey) - */ - public List getConditions(String commandKey) { - return conditionsByCommand.get(commandKey); - } - - public List getReviewConditions(String commandKey) { - return reviewConditionsByCommand.get(commandKey); - } - - public List getContextConditions(String commandKey) { - return contextConditionsByCommand.get(commandKey); - } - - public Workflow addCondition(String commandKey, Condition condition) { - Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey); - Preconditions.checkNotNull(condition); - conditionsByCommand.put(commandKey, condition); - if (condition instanceof ProjectPropertyCondition) { - projectPropertyKeys.add(((ProjectPropertyCondition) condition).getPropertyKey()); - } - if (condition.isOnContext()) { - contextConditionsByCommand.put(commandKey, condition); - } else { - reviewConditionsByCommand.put(commandKey, condition); - } - return this; - } - - public List getFunctions(String commandKey) { - return functionsByCommand.get(commandKey); - } - - public Workflow addFunction(String commandKey, Function function) { - Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey); - Preconditions.checkNotNull(function); - functionsByCommand.put(commandKey, function); - return this; - } - - public Screen getScreen(String commandKey) { - return screensByCommand.get(commandKey); - } - - public Workflow setScreen(String commandKey, Screen screen) { - Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey); - Preconditions.checkNotNull(screen); - Preconditions.checkState(Strings.isNullOrEmpty(screen.getCommandKey()), "Screen is already associated with command: " + screen.getCommandKey()); - screen.setCommandKey(commandKey); - screensByCommand.put(commandKey, screen); - return this; - } - - Map getScreensByCommand() { - return screensByCommand; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/WorkflowEngine.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/WorkflowEngine.java deleted file mode 100644 index a73d42cadf1..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/WorkflowEngine.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ListMultimap; -import com.google.common.collect.Lists; -import org.sonar.api.ServerComponent; -import org.sonar.api.config.Settings; -import org.sonar.core.review.workflow.condition.Condition; -import org.sonar.core.review.workflow.function.Function; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; -import org.sonar.core.review.workflow.screen.Screen; - -import javax.annotation.Nullable; - -import java.util.List; -import java.util.Map; - -public class WorkflowEngine implements ServerComponent { - - private final Workflow workflow; - private final ReviewStore store; - private final Settings settings; - - public WorkflowEngine(Workflow workflow, ReviewStore store, Settings settings) { - this.workflow = workflow; - this.store = store; - this.settings = settings; - } - - /** - * @return non-null list of screens per review#violationId - */ - public ListMultimap listAvailableScreens(DefaultReview[] reviews, DefaultWorkflowContext context, boolean verifyConditions) { - ListMultimap result = ArrayListMultimap.create(); - - completeProjectSettings(context); - - for (Map.Entry entry : workflow.getScreensByCommand().entrySet()) { - String commandKey = entry.getKey(); - if (!verifyConditions || verifyConditionsQuietly(null, context, workflow.getContextConditions(commandKey))) { - for (DefaultReview review : reviews) { - if (!verifyConditions || verifyConditionsQuietly(review, context, workflow.getReviewConditions(commandKey))) { - result.put(review.getViolationId(), entry.getValue()); - } - } - } - } - return result; - } - - public List listAvailableScreens(Review review, DefaultWorkflowContext context, boolean verifyConditions) { - List result = Lists.newArrayList(); - completeProjectSettings(context); - for (Map.Entry entry : workflow.getScreensByCommand().entrySet()) { - String commandKey = entry.getKey(); - if (!verifyConditions || verifyConditionsQuietly(review, context, workflow.getConditions(commandKey))) { - result.add(entry.getValue()); - - } - } - return result; - } - - /** - * @return the optional (nullable) screen associated to the command - */ - public Screen getScreen(String commandKey) { - return workflow.getScreen(commandKey); - } - - public void execute(String commandKey, DefaultReview review, DefaultWorkflowContext context, Map parameters) { - Preconditions.checkArgument(!Strings.isNullOrEmpty(commandKey), "Missing command"); - Preconditions.checkArgument(workflow.hasCommand(commandKey), "Unknown command: " + commandKey); - - completeProjectSettings(context); - - verifyConditions(review, context, workflow.getConditions(commandKey)); - - Map immutableParameters = ImmutableMap.copyOf(parameters); - - // TODO execute functions are change state before functions that consume state (like "create-jira-issue") - Review initialReview = review.cloneImmutable(); - for (Function function : workflow.getFunctions(commandKey)) { - function.doExecute(review, initialReview, context, immutableParameters); - } - - // should it be extracted to a core function ? - store.store(review); - - // TODO notify listeners - } - - private boolean verifyConditionsQuietly(@Nullable Review review, WorkflowContext context, List conditions) { - for (Condition condition : conditions) { - if (!condition.doVerify(review, context)) { - return false; - } - } - return true; - } - - private void verifyConditions(@Nullable Review review, WorkflowContext context, List conditions) { - for (Condition condition : conditions) { - if (!condition.doVerify(review, context)) { - throw new IllegalStateException("Condition is not respected: " + condition.toString()); - } - } - } - - private void completeProjectSettings(DefaultWorkflowContext context) { - Settings projectSettings = new Settings(settings); - List propertyKeys = workflow.getProjectPropertyKeys(); - store.completeProjectSettings(context.getProjectId(), projectSettings, propertyKeys); - context.setSettings(projectSettings); - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/AdminRoleCondition.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/AdminRoleCondition.java deleted file mode 100644 index 6868361fde2..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/AdminRoleCondition.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.annotations.Beta; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -/** - * Checks that user has admin rights on project. - * - * @since 3.1 - */ -@Beta -public final class AdminRoleCondition extends Condition { - - public AdminRoleCondition() { - super(true); - } - - @Override - public boolean doVerify(Review review, WorkflowContext context) { - return context.isAdmin(); - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/Condition.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/Condition.java deleted file mode 100644 index f5386879bf4..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/Condition.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.annotations.Beta; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import javax.annotation.Nullable; - -/** - * @since 3.1 - */ -@Beta -public abstract class Condition { - - private final boolean onContext; - - protected Condition(boolean onContext) { - this.onContext = onContext; - } - - public final boolean isOnContext() { - return onContext; - } - - /** - * @param review the review on "review conditions" like StatusCondition, null on "context conditions" like AdminRoleCondition or ProjectPropertyCondition - * @param context - * @return is the condition verified ? - */ - public abstract boolean doVerify(@Nullable Review review, WorkflowContext context); - -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/Conditions.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/Conditions.java deleted file mode 100644 index 2e3c1a1a564..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/Conditions.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.annotations.Beta; - -/** - * Static utility methods pertaining to {@link Condition} instances. - * - * @since 3.1 - */ -@Beta -public final class Conditions { - - private Conditions() { - } - - public static Condition not(Condition c) { - return new NotCondition(c); - } - - public static Condition hasReviewProperty(String propertyKey) { - return new HasReviewPropertyCondition(propertyKey); - } - - public static Condition hasProjectProperty(String propertyKey) { - return new HasProjectPropertyCondition(propertyKey); - } - - public static Condition hasAdminRole() { - return new AdminRoleCondition(); - } - - public static Condition statuses(String... statuses) { - return new StatusCondition(statuses); - } - - public static Condition resolutions(String... resolutions) { - return new ResolutionCondition(resolutions); - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/HasProjectPropertyCondition.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/HasProjectPropertyCondition.java deleted file mode 100644 index 046aedb799b..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/HasProjectPropertyCondition.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.annotations.Beta; -import org.sonar.api.config.Settings; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -/** - * Checks that a project property is set, whatever its value. - * - * @since 3.1 - */ -@Beta -public final class HasProjectPropertyCondition extends ProjectPropertyCondition { - - public HasProjectPropertyCondition(String propertyKey) { - super(propertyKey); - } - - @Override - public boolean doVerify(Review review, WorkflowContext context) { - Settings settings = context.getProjectSettings(); - return settings.hasKey(getPropertyKey()) || settings.getDefaultValue(getPropertyKey()) != null; - } - - @Override - public String toString() { - return "Property " + getPropertyKey() + " must be set"; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/HasReviewPropertyCondition.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/HasReviewPropertyCondition.java deleted file mode 100644 index a9922a98b9d..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/HasReviewPropertyCondition.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.annotations.Beta; -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import javax.annotation.Nullable; - -/** - * @since 3.1 - */ -@Beta -public final class HasReviewPropertyCondition extends Condition { - - private final String propertyKey; - - public HasReviewPropertyCondition(String propertyKey) { - super(false); - Preconditions.checkArgument(!Strings.isNullOrEmpty(propertyKey)); - this.propertyKey = propertyKey; - } - - public String getPropertyKey() { - return propertyKey; - } - - @Override - public boolean doVerify(@Nullable Review review, WorkflowContext context) { - return review != null && !Strings.isNullOrEmpty(review.getProperties().get(propertyKey)); - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/NotCondition.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/NotCondition.java deleted file mode 100644 index bdaba1bdc89..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/NotCondition.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.annotations.VisibleForTesting; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -public final class NotCondition extends Condition { - - private Condition condition; - - public NotCondition(Condition c) { - super(c.isOnContext()); - this.condition = c; - } - - @Override - public boolean doVerify(Review review, WorkflowContext context) { - return !condition.doVerify(review, context); - } - - @VisibleForTesting - Condition getCondition() { - return condition; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/ProjectPropertyCondition.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/ProjectPropertyCondition.java deleted file mode 100644 index ae0e148b26d..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/ProjectPropertyCondition.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; - - -public abstract class ProjectPropertyCondition extends Condition { - private final String propertyKey; - - protected ProjectPropertyCondition(String propertyKey) { - super(true); - Preconditions.checkArgument(!Strings.isNullOrEmpty(propertyKey)); - this.propertyKey = propertyKey; - } - - public final String getPropertyKey() { - return propertyKey; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/ResolutionCondition.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/ResolutionCondition.java deleted file mode 100644 index b7750faa027..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/ResolutionCondition.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import java.util.Arrays; -import java.util.Set; - -public final class ResolutionCondition extends Condition { - private final Set resolutions; - - public ResolutionCondition(Set resolutions) { - super(false); - Preconditions.checkNotNull(resolutions); - Preconditions.checkArgument(!resolutions.isEmpty(), "No resolutions defined"); - this.resolutions = resolutions; - } - - public ResolutionCondition(String... resolutions) { - this(Sets.newLinkedHashSet(Arrays.asList(resolutions))); - } - - @Override - public boolean doVerify(Review review, WorkflowContext context) { - return resolutions.contains(review.getResolution()); - } - - @VisibleForTesting - Set getResolutions() { - return ImmutableSet.copyOf(resolutions); - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/StatusCondition.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/StatusCondition.java deleted file mode 100644 index 5b443ee9f38..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/StatusCondition.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import java.util.Arrays; -import java.util.Set; - -public final class StatusCondition extends Condition { - private final Set statuses; - - public StatusCondition(Set statuses) { - super(false); - Preconditions.checkNotNull(statuses); - Preconditions.checkArgument(!statuses.isEmpty(), "No statuses defined"); - this.statuses = statuses; - } - - public StatusCondition(String... statuses) { - this(Sets.newLinkedHashSet(Arrays.asList(statuses))); - } - - @Override - public boolean doVerify(Review review, WorkflowContext context) { - return statuses.contains(review.getStatus()); - } - - @VisibleForTesting - Set getStatuses() { - return ImmutableSet.copyOf(statuses); - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/package-info.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/package-info.java deleted file mode 100644 index 06cd5142ac8..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/condition/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -@ParametersAreNonnullByDefault -package org.sonar.core.review.workflow.condition; - -import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/function/CommentFunction.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/function/CommentFunction.java deleted file mode 100644 index f9131b71f8c..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/function/CommentFunction.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.function; - -import org.sonar.core.review.workflow.review.Comment; -import org.sonar.core.review.workflow.review.MutableReview; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import java.util.Map; - -public final class CommentFunction extends Function { - - @Override - public void doExecute(MutableReview review, Review initialReview, WorkflowContext context, Map parameters) { - Comment comment = review.createComment(); - comment.setMarkdownText(parameters.get("text")); - comment.setUserId(context.getUserId()); - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/function/Function.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/function/Function.java deleted file mode 100644 index 1f941d4f1dc..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/function/Function.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.function; - -import com.google.common.annotations.Beta; -import org.sonar.core.review.workflow.review.MutableReview; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import java.util.Map; - -/** - * @since 3.1 - */ -@Beta -public abstract class Function { - - /** - * This method is executed when all the conditions pass. - * - * @param review the review that can be changed - * @param initialReview the read-only review as stated before execution of functions - * @param context information about the user who executed the command and about project - * @param parameters the command parameters sent by end user, generally from forms displayed in screens - */ - public abstract void doExecute(MutableReview review, Review initialReview, WorkflowContext context, Map parameters); - -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/function/package-info.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/function/package-info.java deleted file mode 100644 index 5c50525b275..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/function/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -@ParametersAreNonnullByDefault -package org.sonar.core.review.workflow.function; - -import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/package-info.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/package-info.java deleted file mode 100644 index 27652dae71c..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -@ParametersAreNonnullByDefault -package org.sonar.core.review.workflow; - -import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/Comment.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/Comment.java deleted file mode 100644 index c648471d8dc..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/Comment.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.review; - -public interface Comment { - String getMarkdownText(); - - Long getUserId(); - - Comment setMarkdownText(String s); - - Comment setUserId(Long l); -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultComment.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultComment.java deleted file mode 100644 index 47026442e9a..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultComment.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.review; - -import org.apache.commons.lang.builder.ReflectionToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; - -public final class DefaultComment implements Comment, Cloneable { - private String markdownText; - private Long userId; - - DefaultComment() { - } - - public String getMarkdownText() { - return markdownText; - } - - public DefaultComment setMarkdownText(String s) { - this.markdownText = s; - return this; - } - - public Long getUserId() { - return userId; - } - - public DefaultComment setUserId(Long l) { - this.userId = l; - return this; - } - - @Override - public String toString() { - return new ReflectionToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).toString(); - } - - @Override - public Comment clone() { - return new DefaultComment().setMarkdownText(markdownText).setUserId(userId); - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultReview.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultReview.java deleted file mode 100644 index 68989f0921e..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultReview.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.review; - -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import org.apache.commons.lang.builder.ReflectionToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.utils.KeyValueFormat; - -import javax.annotation.Nullable; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -public final class DefaultReview implements MutableReview { - - private Long violationId; - private Long reviewId; - private String ruleRepositoryKey; - private String ruleKey; - private String ruleName; - private Long line; - private boolean switchedOff = false; - private boolean manual = false; - private String message; - private String status; - private String resolution; - private String severity; - private Map properties; - private List newComments; - - public Long getViolationId() { - return violationId; - } - - public DefaultReview setViolationId(Long violationId) { - this.violationId = violationId; - return this; - } - - public Long getReviewId() { - return reviewId; - } - - public DefaultReview setReviewId(Long reviewId) { - this.reviewId = reviewId; - return this; - } - - public String getRuleRepositoryKey() { - return ruleRepositoryKey; - } - - public DefaultReview setRuleRepositoryKey(String s) { - this.ruleRepositoryKey = s; - return this; - } - - public String getRuleKey() { - return ruleKey; - } - - public DefaultReview setRuleKey(String s) { - this.ruleKey = s; - return this; - } - - public String getRuleName() { - return ruleName; - } - - public DefaultReview setRuleName(String s) { - this.ruleName = s; - return this; - } - - public Long getLine() { - return line; - } - - public DefaultReview setLine(Long line) { - this.line = line; - return this; - } - - public boolean isSwitchedOff() { - return switchedOff; - } - - public DefaultReview setSwitchedOff(boolean b) { - this.switchedOff = b; - return this; - } - - public boolean isManual() { - return manual; - } - - public DefaultReview setManual(boolean manual) { - this.manual = manual; - return this; - } - - public String getMessage() { - return message; - } - - public DefaultReview setMessage(String message) { - this.message = message; - return this; - } - - public String getStatus() { - return status; - } - - public DefaultReview setStatus(String s) { - Preconditions.checkArgument(!Strings.isNullOrEmpty(s)); - this.status = s; - return this; - } - - public String getResolution() { - return resolution; - } - - public DefaultReview setResolution(@Nullable String s) { - this.resolution = s; - return this; - } - - public String getSeverity() { - return severity; - } - - public DefaultReview setSeverity(String s) { - Preconditions.checkArgument(!Strings.isNullOrEmpty(s)); - this.severity = s; - return this; - } - - public Map getProperties() { - if (properties == null) { - return Collections.emptyMap(); - } - return properties; - } - - public DefaultReview setProperties(Map properties) { - this.properties = properties; - return this; - } - - public DefaultReview setPropertiesAsString(@Nullable String s) { - this.properties = (s == null ? null : KeyValueFormat.parse(s)); - return this; - } - - public Comment createComment() { - if (newComments == null) { - newComments = Lists.newArrayList(); - } - Comment comment = new DefaultComment(); - newComments.add(comment); - return comment; - } - - public List getNewComments() { - if (newComments == null) { - return Collections.emptyList(); - } - return newComments; - } - - public DefaultReview setProperty(String key, @Nullable String value) { - if (properties == null) { - // keeping entries ordered by key allows to have consistent behavior in unit tests - properties = Maps.newLinkedHashMap(); - } - properties.put(key, value); - return this; - } - - @Override - public String toString() { - return new ReflectionToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).toString(); - } - - /** - * Note : implementation is still mutable. - */ - public ImmutableReview cloneImmutable() { - ImmutableReview clone = new ImmutableReview(); - clone.setLine(line); - clone.setManual(manual); - clone.setMessage(message); - clone.setProperties(ImmutableMap.copyOf(getProperties())); - clone.setResolution(resolution); - clone.setReviewId(reviewId); - clone.setRuleKey(ruleKey); - clone.setRuleRepositoryKey(ruleRepositoryKey); - clone.setRuleName(ruleName); - clone.setSeverity(severity); - clone.setStatus(status); - clone.setSwitchedOff(switchedOff); - clone.setViolationId(violationId); - clone.setProperties(ImmutableMap.copyOf(getProperties())); - return clone; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultWorkflowContext.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultWorkflowContext.java deleted file mode 100644 index e8a1e3ec2e8..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/DefaultWorkflowContext.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.review; - -import org.apache.commons.lang.builder.ReflectionToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.config.Settings; - -public final class DefaultWorkflowContext implements WorkflowContext { - - private Long userId; - private String userLogin; - private String userName; - private String userEmail; - private boolean isAdmin = false; - private Long projectId; - private Settings settings; - - public Long getUserId() { - return userId; - } - - public DefaultWorkflowContext setUserId(Long l) { - this.userId = l; - return this; - } - - public String getUserLogin() { - return userLogin; - } - - public DefaultWorkflowContext setUserLogin(String s) { - this.userLogin = s; - return this; - } - - public String getUserName() { - return userName; - } - - public DefaultWorkflowContext setUserName(String s) { - this.userName = s; - return this; - } - - public String getUserEmail() { - return userEmail; - } - - public DefaultWorkflowContext setUserEmail(String userEmail) { - this.userEmail = userEmail; - return this; - } - - public boolean isAdmin() { - return isAdmin; - } - - public DefaultWorkflowContext setIsAdmin(boolean b) { - isAdmin = b; - return this; - } - - public Long getProjectId() { - return projectId; - } - - public DefaultWorkflowContext setProjectId(Long l) { - this.projectId = l; - return this; - } - - public Settings getProjectSettings() { - return settings; - } - - public DefaultWorkflowContext setSettings(Settings s) { - this.settings = s; - return this; - } - - @Override - public String toString() { - return new ReflectionToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).toString(); - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/ImmutableReview.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/ImmutableReview.java deleted file mode 100644 index 35c8f01a470..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/ImmutableReview.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.review; - -import java.util.Map; - -public class ImmutableReview implements Review { - private Long violationId; - private Long reviewId; - private String ruleRepositoryKey; - private String ruleKey; - private String ruleName; - private Long line; - private boolean switchedOff = false; - private boolean manual = false; - private String message; - private String status; - private String resolution; - private String severity; - private Map properties; - - public Long getViolationId() { - return violationId; - } - - void setViolationId(Long violationId) { - this.violationId = violationId; - } - - public Long getReviewId() { - return reviewId; - } - - void setReviewId(Long reviewId) { - this.reviewId = reviewId; - } - - public String getRuleName() { - return ruleName; - } - - void setRuleName(String s) { - this.ruleName = s; - } - - public String getRuleRepositoryKey() { - return ruleRepositoryKey; - } - - void setRuleRepositoryKey(String ruleRepositoryKey) { - this.ruleRepositoryKey = ruleRepositoryKey; - } - - public String getRuleKey() { - return ruleKey; - } - - void setRuleKey(String ruleKey) { - this.ruleKey = ruleKey; - } - - public Long getLine() { - return line; - } - - void setLine(Long line) { - this.line = line; - } - - public boolean isSwitchedOff() { - return switchedOff; - } - - void setSwitchedOff(boolean switchedOff) { - this.switchedOff = switchedOff; - } - - public boolean isManual() { - return manual; - } - - void setManual(boolean manual) { - this.manual = manual; - } - - public String getMessage() { - return message; - } - - void setMessage(String message) { - this.message = message; - } - - public String getStatus() { - return status; - } - - void setStatus(String status) { - this.status = status; - } - - public String getResolution() { - return resolution; - } - - void setResolution(String resolution) { - this.resolution = resolution; - } - - public String getSeverity() { - return severity; - } - - void setSeverity(String severity) { - this.severity = severity; - } - - public Map getProperties() { - return properties; - } - - void setProperties(Map properties) { - this.properties = properties; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/MutableReview.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/MutableReview.java deleted file mode 100644 index 286254dcdb8..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/MutableReview.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.review; - -import javax.annotation.Nullable; -import java.util.List; - -public interface MutableReview extends Review { - - MutableReview setStatus(String s); - - MutableReview setResolution(@Nullable String resolution); - - MutableReview setProperty(String key, @Nullable String value); - - Comment createComment(); - - List getNewComments(); -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/Review.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/Review.java deleted file mode 100644 index 7cca22da27b..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/Review.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.review; - -import java.util.Map; - -public interface Review { - - Long getReviewId(); - - String getRuleRepositoryKey(); - - String getRuleKey(); - - String getRuleName(); - - boolean isSwitchedOff(); - - String getMessage(); - - Map getProperties(); - - String getStatus(); - - String getResolution(); - - String getSeverity(); - - Long getLine(); - - boolean isManual(); -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/WorkflowContext.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/WorkflowContext.java deleted file mode 100644 index 7c0f2faa17c..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/WorkflowContext.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.review; - -import org.sonar.api.config.Settings; - -public interface WorkflowContext { - Long getProjectId(); - - Long getUserId(); - - String getUserLogin(); - - String getUserName(); - - String getUserEmail(); - - boolean isAdmin(); - - Settings getProjectSettings(); -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/package-info.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/review/package-info.java deleted file mode 100644 index 0e49d65ce22..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/review/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -@ParametersAreNonnullByDefault -package org.sonar.core.review.workflow.review; - -import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/CommentScreen.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/CommentScreen.java deleted file mode 100644 index 6bb253c1762..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/CommentScreen.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.screen; - -/** - * Form with only a textarea field to type a comment. - */ -public final class CommentScreen extends Screen { - - public CommentScreen() { - super("comment"); - } - -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/Screen.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/Screen.java deleted file mode 100644 index b37dbb3cb04..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/Screen.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.screen; - -/** - *

Localization

- *

At least two buttons must have labels :

- *
    - *
  • the button in the violation toolbar that displays the form screen. Key is 'reviews.command..button'.
  • - *
  • the button in the form screen that submits the command. Key is 'reviews.command..submit'.
  • - *
- */ -public abstract class Screen { - private final String key; - private String commandKey; - - protected Screen(String key) { - this.key = key; - } - - public final String getKey() { - return key; - } - - public final String getCommandKey() { - return commandKey; - } - - public final Screen setCommandKey(String commandKey) { - this.commandKey = commandKey; - return this; - } -} diff --git a/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/package-info.java b/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/package-info.java deleted file mode 100644 index 25a1028fd3c..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/review/workflow/screen/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -@ParametersAreNonnullByDefault -package org.sonar.core.review.workflow.screen; - -import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-core/src/main/java/org/sonar/core/workflow/ImmutableReview.java b/sonar-core/src/main/java/org/sonar/core/workflow/ImmutableReview.java new file mode 100644 index 00000000000..2194b535e7e --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/workflow/ImmutableReview.java @@ -0,0 +1,113 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.workflow; + +import com.google.common.collect.ImmutableMap; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.internal.DefaultReview; + +import java.util.Map; + +public final class ImmutableReview implements Review { + private final Long violationId; + private final Long reviewId; + private final String ruleRepositoryKey; + private final String ruleKey; + private final String ruleName; + private final Long line; + private final boolean switchedOff; + private final boolean manual; + private final String message; + private final String status; + private final String resolution; + private final String severity; + private final Map properties; + + /** + * Warning : implementation is still mutable. + */ + public ImmutableReview(DefaultReview review) { + this.line = review.getLine(); + this.manual = review.isManual(); + this.message = review.getMessage(); + this.properties = ImmutableMap.copyOf(review.getProperties()); + this.resolution = review.getResolution(); + this.reviewId = review.getReviewId(); + this.ruleKey = review.getRuleKey(); + this.ruleRepositoryKey = review.getRuleRepositoryKey(); + this.ruleName = review.getRuleName(); + this.severity = review.getSeverity(); + this.status = review.getStatus(); + this.switchedOff = review.isSwitchedOff(); + this.violationId = review.getViolationId(); + } + + public Long getViolationId() { + return violationId; + } + + public Long getReviewId() { + return reviewId; + } + + public String getRuleName() { + return ruleName; + } + + public String getRuleRepositoryKey() { + return ruleRepositoryKey; + } + + public String getRuleKey() { + return ruleKey; + } + + public Long getLine() { + return line; + } + + public boolean isSwitchedOff() { + return switchedOff; + } + + public boolean isManual() { + return manual; + } + + public String getMessage() { + return message; + } + + public String getStatus() { + return status; + } + + public String getResolution() { + return resolution; + } + + public String getSeverity() { + return severity; + } + + public Map getProperties() { + return properties; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/workflow/ReviewDatabaseStore.java b/sonar-core/src/main/java/org/sonar/core/workflow/ReviewDatabaseStore.java new file mode 100644 index 00000000000..81c6d0b3a71 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/workflow/ReviewDatabaseStore.java @@ -0,0 +1,105 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.workflow; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.ibatis.session.SqlSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.ServerComponent; +import org.sonar.api.config.Settings; +import org.sonar.api.utils.KeyValueFormat; +import org.sonar.core.persistence.MyBatis; +import org.sonar.core.properties.PropertiesMapper; +import org.sonar.core.properties.PropertyDto; +import org.sonar.core.review.ReviewCommentDto; +import org.sonar.core.review.ReviewCommentMapper; +import org.sonar.core.review.ReviewDto; +import org.sonar.core.review.ReviewMapper; +import org.sonar.api.workflow.Comment; +import org.sonar.api.workflow.internal.DefaultReview; +import java.util.Date; +import java.util.List; + +public class ReviewDatabaseStore implements ReviewStore, ServerComponent { + private static final Logger LOG = LoggerFactory.getLogger(ReviewDatabaseStore.class); + + private MyBatis mybatis; + + public ReviewDatabaseStore(MyBatis mb) { + this.mybatis = mb; + } + + public void store(DefaultReview review) { + store(review, new Date()); + } + + @VisibleForTesting + void store(DefaultReview review, Date now) { + if (review.getReviewId() == null) { + LOG.error("Review has no id. Violation id is: " + review.getViolationId()); + return; + } + + SqlSession session = mybatis.openSession(); + ReviewMapper mapper = session.getMapper(ReviewMapper.class); + ReviewCommentMapper commentMapper = session.getMapper(ReviewCommentMapper.class); + try { + ReviewDto dto = mapper.findById(review.getReviewId()); + dto.setResolution(review.getResolution()); + dto.setStatus(review.getStatus()); + dto.setData(KeyValueFormat.format(review.getProperties())); + dto.setUpdatedAt(now); + mapper.update(dto); + + for (Comment comment : review.getNewComments()) { + ReviewCommentDto commentDto = new ReviewCommentDto(); + commentDto.setReviewId(dto.getId()); + commentDto.setText(comment.getMarkdownText()); + commentDto.setCreatedAt(now); + commentDto.setUpdatedAt(now); + commentDto.setUserId(comment.getUserId()); + commentMapper.insert(commentDto); + } + session.commit(); + + } finally { + MyBatis.closeQuietly(session); + } + } + + public void completeProjectSettings(Long projectId, Settings settings, List propertyKeys) { + if (propertyKeys.isEmpty()) { + return; + } + + SqlSession session = mybatis.openSession(); + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + try { + List dtos = mapper.selectSetOfResourceProperties(projectId, propertyKeys); + for (PropertyDto dto : dtos) { + settings.setProperty(dto.getKey(), dto.getValue()); + } + + } finally { + MyBatis.closeQuietly(session); + } + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/workflow/ReviewStore.java b/sonar-core/src/main/java/org/sonar/core/workflow/ReviewStore.java new file mode 100644 index 00000000000..f5a67470f68 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/workflow/ReviewStore.java @@ -0,0 +1,30 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.workflow; + +import org.sonar.api.config.Settings; +import org.sonar.api.workflow.internal.DefaultReview; +import java.util.List; + +public interface ReviewStore { + void store(DefaultReview review); + + void completeProjectSettings(Long projectId, Settings settings, List propertyKeys); +} diff --git a/sonar-core/src/main/java/org/sonar/core/workflow/WorkflowEngine.java b/sonar-core/src/main/java/org/sonar/core/workflow/WorkflowEngine.java new file mode 100644 index 00000000000..450c949756e --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/workflow/WorkflowEngine.java @@ -0,0 +1,141 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.workflow; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ListMultimap; +import com.google.common.collect.Lists; +import org.sonar.api.ServerComponent; +import org.sonar.api.config.Settings; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; +import org.sonar.api.workflow.condition.Condition; +import org.sonar.api.workflow.function.Function; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflow; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; +import org.sonar.api.workflow.screen.Screen; + +import javax.annotation.Nullable; +import java.util.List; +import java.util.Map; + +public class WorkflowEngine implements ServerComponent { + + private final DefaultWorkflow workflow; + private final ReviewStore store; + private final Settings settings; + + public WorkflowEngine(DefaultWorkflow workflow, ReviewStore store, Settings settings) { + this.workflow = workflow; + this.store = store; + this.settings = settings; + } + + /** + * @return non-null list of screens per review#violationId + */ + public ListMultimap listAvailableScreens(DefaultReview[] reviews, DefaultWorkflowContext context, boolean verifyConditions) { + ListMultimap result = ArrayListMultimap.create(); + + completeProjectSettings(context); + + for (Map.Entry entry : workflow.getScreensByCommand().entrySet()) { + String commandKey = entry.getKey(); + if (!verifyConditions || verifyConditionsQuietly(null, context, workflow.getContextConditions(commandKey))) { + for (DefaultReview review : reviews) { + if (!verifyConditions || verifyConditionsQuietly(review, context, workflow.getReviewConditions(commandKey))) { + result.put(review.getViolationId(), entry.getValue()); + } + } + } + } + return result; + } + + public List listAvailableScreens(Review review, DefaultWorkflowContext context, boolean verifyConditions) { + List result = Lists.newArrayList(); + completeProjectSettings(context); + for (Map.Entry entry : workflow.getScreensByCommand().entrySet()) { + String commandKey = entry.getKey(); + if (!verifyConditions || verifyConditionsQuietly(review, context, workflow.getConditions(commandKey))) { + result.add(entry.getValue()); + + } + } + return result; + } + + /** + * @return the optional (nullable) screen associated to the command + */ + public Screen getScreen(String commandKey) { + return workflow.getScreen(commandKey); + } + + public void execute(String commandKey, DefaultReview review, DefaultWorkflowContext context, Map parameters) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(commandKey), "Missing command"); + Preconditions.checkArgument(workflow.hasCommand(commandKey), "Unknown command: " + commandKey); + + completeProjectSettings(context); + + verifyConditions(review, context, workflow.getConditions(commandKey)); + + Map immutableParameters = ImmutableMap.copyOf(parameters); + + // TODO execute functions are change state before functions that consume state (like "create-jira-issue") + Review initialReview = new ImmutableReview(review); + for (Function function : workflow.getFunctions(commandKey)) { + function.doExecute(review, initialReview, context, immutableParameters); + } + + // should it be extracted to a core function ? + store.store(review); + + // TODO notify listeners + } + + private boolean verifyConditionsQuietly(@Nullable Review review, WorkflowContext context, List conditions) { + for (Condition condition : conditions) { + if (!condition.doVerify(review, context)) { + return false; + } + } + return true; + } + + private void verifyConditions(@Nullable Review review, WorkflowContext context, List conditions) { + for (Condition condition : conditions) { + if (!condition.doVerify(review, context)) { + throw new IllegalStateException("Condition is not respected: " + condition.toString()); + } + } + } + + private void completeProjectSettings(DefaultWorkflowContext context) { + Settings projectSettings = new Settings(settings); + List propertyKeys = workflow.getProjectPropertyKeys(); + store.completeProjectSettings(context.getProjectId(), projectSettings, propertyKeys); + context.setSettings(projectSettings); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/workflow/package-info.java b/sonar-core/src/main/java/org/sonar/core/workflow/package-info.java new file mode 100644 index 00000000000..c3d167a10b6 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/workflow/package-info.java @@ -0,0 +1,23 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +@ParametersAreNonnullByDefault +package org.sonar.core.workflow; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/ReviewDatabaseStoreTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/ReviewDatabaseStoreTest.java deleted file mode 100644 index 0dbfa6a6d3b..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/ReviewDatabaseStoreTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import org.junit.Test; -import org.sonar.api.utils.DateUtils; -import org.sonar.core.persistence.DaoTestCase; -import org.sonar.core.review.workflow.review.Comment; -import org.sonar.core.review.workflow.review.DefaultReview; -import java.util.Date; - -public class ReviewDatabaseStoreTest extends DaoTestCase { - - @Test - public void store() { - setupData("store"); - ReviewDatabaseStore store = new ReviewDatabaseStore(getMyBatis()); - DefaultReview review = new DefaultReview().setReviewId(1234L); - review.setStatus("CLOSED"); - review.setResolution("RESOLVED"); - review.setProperty("who", "me"); - review.setProperty("why", "because"); - Comment comment = review.createComment(); - comment.setMarkdownText("this is a comment"); - comment.setUserId(555L); - - Date now = DateUtils.parseDate("2012-05-18"); - store.store(review, now); - - checkTables("store", "reviews"); - checkTables("store", new String[]{"id"}, "review_comments"); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/SampleWorkflowBuilderTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/SampleWorkflowBuilderTest.java deleted file mode 100644 index 285396cbfd7..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/SampleWorkflowBuilderTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import org.junit.Test; - -import static org.fest.assertions.Assertions.assertThat; - -public class SampleWorkflowBuilderTest { - @Test - public void completeWorkflowAtStartup() { - Workflow workflow = new Workflow(); - - new SampleWorkflowBuilder(workflow).start(); - - assertThat(workflow.getCommands()).containsOnly("comment"); - assertThat(workflow.getScreen("comment")).isNotNull(); - assertThat(workflow.getFunctions("comment")).hasSize(1); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/WorkflowEngineTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/WorkflowEngineTest.java deleted file mode 100644 index 29f86a9b1be..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/WorkflowEngineTest.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import com.google.common.collect.ListMultimap; -import com.google.common.collect.Maps; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.config.Settings; -import org.sonar.core.review.workflow.condition.Condition; -import org.sonar.core.review.workflow.condition.HasProjectPropertyCondition; -import org.sonar.core.review.workflow.function.Function; -import org.sonar.core.review.workflow.review.*; -import org.sonar.core.review.workflow.screen.CommentScreen; -import org.sonar.core.review.workflow.screen.Screen; - -import java.util.List; -import java.util.Map; - -import static org.fest.assertions.Assertions.assertThat; -import static org.junit.matchers.JUnitMatchers.hasItem; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; - -public class WorkflowEngineTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void listAvailableScreensForReview_empty() { - WorkflowEngine engine = new WorkflowEngine(new Workflow(), mock(ReviewStore.class), new Settings()); - List screens = engine.listAvailableScreens(new DefaultReview(), new DefaultWorkflowContext(), true); - assertThat(screens).isEmpty(); - } - - @Test - public void listAvailableScreensForReview() { - Workflow workflow = new Workflow(); - workflow.addCommand("command-without-screen"); - workflow.addCommand("resolve"); - CommentScreen screen = new CommentScreen(); - workflow.setScreen("resolve", screen); - - WorkflowEngine engine = new WorkflowEngine(workflow, mock(ReviewStore.class), new Settings()); - List screens = engine.listAvailableScreens(new DefaultReview(), new DefaultWorkflowContext(), true); - assertThat(screens).containsExactly(screen); - } - - @Test - public void listAvailableScreensForReview_verify_conditions() { - Workflow workflow = new Workflow(); - workflow.addCommand("resolve"); - Condition condition = mock(Condition.class); - when(condition.doVerify(any(Review.class), any(WorkflowContext.class))).thenReturn(false); - workflow.addCondition("resolve", condition); - workflow.setScreen("resolve", new CommentScreen()); - - WorkflowEngine engine = new WorkflowEngine(workflow, mock(ReviewStore.class), new Settings()); - DefaultReview review = new DefaultReview(); - DefaultWorkflowContext context = new DefaultWorkflowContext(); - assertThat(engine.listAvailableScreens(review, context, true)).isEmpty(); - - verify(condition).doVerify(review, context); - } - - @Test - public void listAvailableScreensForReviews_empty() { - WorkflowEngine engine = new WorkflowEngine(new Workflow(), mock(ReviewStore.class), new Settings()); - ListMultimap screens = engine.listAvailableScreens( - new DefaultReview[]{new DefaultReview().setViolationId(1000L), new DefaultReview().setViolationId(2000L)}, - new DefaultWorkflowContext(), true); - assertThat(screens.size()).isEqualTo(0); - } - - @Test - public void listAvailableScreensForReviews() { - Workflow workflow = new Workflow(); - workflow.addCommand("command-without-screen"); - workflow.addCommand("resolve"); - CommentScreen screen = new CommentScreen(); - workflow.setScreen("resolve", screen); - WorkflowEngine engine = new WorkflowEngine(workflow, mock(ReviewStore.class), new Settings()); - ListMultimap screens = engine.listAvailableScreens( - new DefaultReview[]{new DefaultReview().setViolationId(1000L), new DefaultReview().setViolationId(2000L)}, - new DefaultWorkflowContext(), true); - assertThat(screens.size()).isEqualTo(2); - assertThat(screens.get(1000L)).containsExactly(screen); - assertThat(screens.get(2000L)).containsExactly(screen); - } - - @Test - public void listAvailableScreensForReviews_load_project_properties() { - Workflow workflow = new Workflow(); - workflow.addCommand("resolve"); - workflow.addCondition("resolve", new HasProjectPropertyCondition("foo")); - - ReviewStore store = mock(ReviewStore.class); - WorkflowEngine engine = new WorkflowEngine(workflow, store, new Settings()); - - engine.listAvailableScreens( - new DefaultReview[]{new DefaultReview().setViolationId(1000L), new DefaultReview().setViolationId(2000L)}, - new DefaultWorkflowContext().setProjectId(300L), - true); - - verify(store).completeProjectSettings(eq(300L), any(Settings.class), (List) argThat(hasItem("foo"))); - } - - @Test - public void execute_conditions_pass() { - Workflow workflow = new Workflow(); - workflow.addCommand("resolve"); - workflow.addCondition("resolve", new HasProjectPropertyCondition("foo")); - Function function = mock(Function.class); - workflow.addFunction("resolve", function); - - ReviewStore store = mock(ReviewStore.class); - Settings settings = new Settings(); - settings.setProperty("foo", "bar"); - WorkflowEngine engine = new WorkflowEngine(workflow, store, settings); - - DefaultReview review = new DefaultReview().setViolationId(1000L); - Map parameters = Maps.newHashMap(); - DefaultWorkflowContext context = new DefaultWorkflowContext().setProjectId(300L); - - engine.execute("resolve", review, context, parameters); - - verify(store).completeProjectSettings(eq(300L), any(Settings.class), (List) argThat(hasItem("foo"))); - verify(function).doExecute(eq(review), any(ImmutableReview.class), eq(context), eq(parameters)); - } - - @Test - public void execute_fail_if_conditions_dont_pass() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Condition is not respected: Property foo must be set"); - - Workflow workflow = new Workflow(); - workflow.addCommand("resolve"); - workflow.addCondition("resolve", new HasProjectPropertyCondition("foo")); - Function function = mock(Function.class); - workflow.addFunction("resolve", function); - - ReviewStore store = mock(ReviewStore.class); - Settings settings = new Settings();// missing property 'foo' - WorkflowEngine engine = new WorkflowEngine(workflow, store, settings); - - DefaultReview review = new DefaultReview().setViolationId(1000L); - Map parameters = Maps.newHashMap(); - DefaultWorkflowContext context = new DefaultWorkflowContext().setProjectId(300L); - - engine.execute("resolve", review, context, parameters); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/WorkflowTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/WorkflowTest.java deleted file mode 100644 index 00502cd6148..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/WorkflowTest.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow; - -import org.fest.assertions.MapAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.core.review.workflow.condition.Condition; -import org.sonar.core.review.workflow.condition.HasProjectPropertyCondition; -import org.sonar.core.review.workflow.condition.StatusCondition; -import org.sonar.core.review.workflow.function.CommentFunction; -import org.sonar.core.review.workflow.function.Function; -import org.sonar.core.review.workflow.screen.CommentScreen; - -import static org.fest.assertions.Assertions.assertThat; - -public class WorkflowTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void addCommand() { - Workflow workflow = new Workflow(); - assertThat(workflow.getCommands()).isEmpty(); - - assertThat(workflow.addCommand("resolve")).isSameAs(workflow); - assertThat(workflow.getCommands()).containsOnly("resolve"); - assertThat(workflow.hasCommand("resolve")).isTrue(); - } - - @Test - public void addCommand_does_not_accept_blank() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Empty command key"); - - Workflow workflow = new Workflow(); - workflow.addCommand(""); - } - - @Test - public void addSeveralTimesTheSameCommand() { - Workflow workflow = new Workflow(); - workflow.addCommand("resolve"); - workflow.addCommand("resolve"); - assertThat(workflow.getCommands()).containsOnly("resolve"); - assertThat(workflow.getCommands()).hasSize(1); - } - - @Test - public void addCondition_fail_if_unknown_command() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Unknown command: resolve"); - - Workflow workflow = new Workflow(); - workflow.addCondition("resolve", new StatusCondition("OPEN")); - } - - @Test - public void addCondition() { - Workflow workflow = new Workflow(); - Condition condition = new StatusCondition("OPEN"); - workflow.addCommand("resolve"); - - workflow.addCondition("resolve", condition); - - assertThat(workflow.getConditions("resolve")).containsExactly(condition); - } - - @Test - public void getConditions_empty() { - Workflow workflow = new Workflow(); - assertThat(workflow.getConditions("resolve")).isEmpty(); - } - - @Test - public void keepCacheOfProjectPropertiesRequiredByConditions() { - Workflow workflow = new Workflow(); - Condition condition1 = new HasProjectPropertyCondition("jira.url"); - Condition condition2 = new HasProjectPropertyCondition("jira.login"); - workflow.addCommand("create-jira-issue"); - workflow.addCondition("create-jira-issue", condition1); - workflow.addCondition("create-jira-issue", condition2); - - assertThat(workflow.getProjectPropertyKeys()).containsExactly("jira.url", "jira.login"); - } - - @Test - public void cacheOfProjectPropertiesIsNotNull() { - Workflow workflow = new Workflow(); - - assertThat(workflow.getProjectPropertyKeys()).isEmpty(); - } - - @Test - public void keepFastLinksToReviewAndContextConditions() { - Workflow workflow = new Workflow(); - workflow.addCommand("create-jira-issue"); - Condition contextCondition = new HasProjectPropertyCondition("jira.url"); - workflow.addCondition("create-jira-issue", contextCondition); - Condition reviewCondition = new StatusCondition("OPEN"); - workflow.addCondition("create-jira-issue", reviewCondition); - - assertThat(workflow.getContextConditions("create-jira-issue")).containsExactly(contextCondition); - assertThat(workflow.getReviewConditions("create-jira-issue")).containsExactly(reviewCondition); - } - - @Test - public void addFunction() { - Workflow workflow = new Workflow(); - workflow.addCommand("resolve"); - - Function function = new CommentFunction(); - workflow.addFunction("resolve", function); - - assertThat(workflow.getFunctions("resolve")).containsExactly(function); - } - - @Test - public void getFunctions_empty() { - Workflow workflow = new Workflow(); - assertThat(workflow.getFunctions("resolve")).isEmpty(); - } - - @Test - public void addFunction_fail_if_unknown_command() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Unknown command: resolve"); - - Workflow workflow = new Workflow(); - workflow.addFunction("resolve", new CommentFunction()); - } - - @Test - public void setScreen_fail_if_unknown_command() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Unknown command: resolve"); - - Workflow workflow = new Workflow(); - workflow.setScreen("resolve", new CommentScreen()); - } - - @Test - public void setScreen() { - Workflow workflow = new Workflow(); - workflow.addCommand("resolve"); - CommentScreen screen = new CommentScreen(); - workflow.setScreen("resolve", screen); - - assertThat(workflow.getScreen("resolve")).isSameAs(screen); - assertThat(workflow.getScreensByCommand()).includes(MapAssert.entry("resolve", screen)); - assertThat(workflow.getScreensByCommand()).hasSize(1); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/AdminRoleConditionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/AdminRoleConditionTest.java deleted file mode 100644 index f72c31ce0cd..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/AdminRoleConditionTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Test; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; - -import static org.fest.assertions.Assertions.assertThat; - -public class AdminRoleConditionTest { - @Test - public void verifiedIfAdminRole() { - AdminRoleCondition condition = new AdminRoleCondition(); - DefaultWorkflowContext context = new DefaultWorkflowContext(); - context.setIsAdmin(true); - assertThat(condition.doVerify(new DefaultReview(), context)).isTrue(); - } - - @Test - public void failIfNotAdminRole() { - AdminRoleCondition condition = new AdminRoleCondition(); - DefaultWorkflowContext context = new DefaultWorkflowContext(); - context.setIsAdmin(false); - assertThat(condition.doVerify(new DefaultReview(), context)).isFalse(); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ConditionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ConditionTest.java deleted file mode 100644 index 9a3e74a4557..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ConditionTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Test; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import static org.fest.assertions.Assertions.assertThat; - -public class ConditionTest { - @Test - public void checkedOncePerGroupOfReviews() { - Condition condition = new Condition(true) { - @Override - public boolean doVerify(Review review, WorkflowContext context) { - return false; - } - }; - assertThat(condition.isOnContext()).isTrue(); - } - - @Test - public void checkedForEveryReview() { - Condition condition = new Condition(false) { - @Override - public boolean doVerify(Review review, WorkflowContext context) { - return false; - } - }; - assertThat(condition.isOnContext()).isFalse(); - } - -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ConditionsTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ConditionsTest.java deleted file mode 100644 index a0ad857405b..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ConditionsTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Test; - -import static org.fest.assertions.Assertions.assertThat; - -public class ConditionsTest { - @Test - public void not() { - StatusCondition target = new StatusCondition("OPEN"); - Condition not = Conditions.not(target); - assertThat(not).isInstanceOf(NotCondition.class); - assertThat(((NotCondition) not).getCondition()).isSameAs(target); - } - - @Test - public void hasReviewProperty() { - Condition condition = Conditions.hasReviewProperty("foo"); - assertThat(condition).isInstanceOf(HasReviewPropertyCondition.class); - assertThat(((HasReviewPropertyCondition) condition).getPropertyKey()).isEqualTo("foo"); - } - - @Test - public void hasProjectProperty() { - Condition condition = Conditions.hasProjectProperty("foo"); - assertThat(condition).isInstanceOf(HasProjectPropertyCondition.class); - assertThat(((HasProjectPropertyCondition) condition).getPropertyKey()).isEqualTo("foo"); - } - - @Test - public void hasAdminRole() { - Condition condition = Conditions.hasAdminRole(); - assertThat(condition).isInstanceOf(AdminRoleCondition.class); - } - - @Test - public void statuses() { - Condition condition = Conditions.statuses("OPEN", "CLOSED"); - assertThat(condition).isInstanceOf(StatusCondition.class); - assertThat(((StatusCondition) condition).getStatuses()).containsOnly("OPEN", "CLOSED"); - } - - @Test - public void resolutions() { - Condition condition = Conditions.resolutions("", "RESOLVED"); - assertThat(condition).isInstanceOf(ResolutionCondition.class); - assertThat(((ResolutionCondition) condition).getResolutions()).containsOnly("", "RESOLVED"); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/HasProjectPropertyConditionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/HasProjectPropertyConditionTest.java deleted file mode 100644 index 541188cb47b..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/HasProjectPropertyConditionTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Test; -import org.sonar.api.Properties; -import org.sonar.api.Property; -import org.sonar.api.config.PropertyDefinitions; -import org.sonar.api.config.Settings; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; - -import static org.fest.assertions.Assertions.assertThat; - -public class HasProjectPropertyConditionTest { - @Test - public void doVerify() { - HasProjectPropertyCondition condition = new HasProjectPropertyCondition("jira.url"); - DefaultWorkflowContext context = new DefaultWorkflowContext(); - context.setSettings(new Settings().setProperty("jira.url", "http://jira")); - assertThat(condition.doVerify(new DefaultReview(), context)).isTrue(); - } - - @Test - public void missingProperty() { - HasProjectPropertyCondition condition = new HasProjectPropertyCondition("jira.url"); - DefaultWorkflowContext context = new DefaultWorkflowContext(); - context.setSettings(new Settings()); - assertThat(condition.doVerify(new DefaultReview(), context)).isFalse(); - } - - @Test - public void returnTrueIfDefaultValue() { - HasProjectPropertyCondition condition = new HasProjectPropertyCondition("jira.url"); - DefaultWorkflowContext context = new DefaultWorkflowContext(); - context.setSettings(new Settings(new PropertyDefinitions().addComponent(WithDefaultValue.class))); - assertThat(condition.doVerify(new DefaultReview(), context)).isTrue(); - } - - @Properties({ - @Property(key = "jira.url", name = "JIRA URL", defaultValue = "http://jira.com") - }) - private static class WithDefaultValue { - - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/HasReviewPropertyConditionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/HasReviewPropertyConditionTest.java deleted file mode 100644 index ebc0fb7d042..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/HasReviewPropertyConditionTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; - -import static org.fest.assertions.Assertions.assertThat; - -public class HasReviewPropertyConditionTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void doVerify() { - HasReviewPropertyCondition condition = new HasReviewPropertyCondition("foo"); - - DefaultWorkflowContext context = new DefaultWorkflowContext(); - assertThat(condition.doVerify(new DefaultReview(), context)).isFalse(); - assertThat(condition.doVerify(new DefaultReview().setProperty("foo", ""), context)).isFalse(); - assertThat(condition.doVerify(new DefaultReview().setProperty("foo", "bar"), context)).isTrue(); - } - - @Test - public void getPropertyKey() { - HasReviewPropertyCondition condition = new HasReviewPropertyCondition("foo"); - assertThat(condition.getPropertyKey()).isEqualTo("foo"); - } - - @Test - public void failIfNullProperty() { - thrown.expect(IllegalArgumentException.class); - new HasReviewPropertyCondition(null); - } - - @Test - public void failIfEmptyProperty() { - thrown.expect(IllegalArgumentException.class); - new HasReviewPropertyCondition(""); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/NotConditionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/NotConditionTest.java deleted file mode 100644 index 46ac81f5acd..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/NotConditionTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Test; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import static org.fest.assertions.Assertions.assertThat; - -public class NotConditionTest { - @Test - public void doVerifyInverse() { - Condition target = new TargetCondition(true); - assertThat(new NotCondition(target).doVerify(new DefaultReview(), new DefaultWorkflowContext())).isFalse(); - - target = new TargetCondition(false); - assertThat(new NotCondition(target).doVerify(new DefaultReview(), new DefaultWorkflowContext())).isTrue(); - } - - private static class TargetCondition extends Condition { - private boolean returns; - - private TargetCondition(boolean returns) { - super(false); - this.returns = returns; - } - - @Override - public boolean doVerify(Review review, WorkflowContext context) { - return returns; - } - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ProjectPropertyConditionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ProjectPropertyConditionTest.java deleted file mode 100644 index 86d7c279b31..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ProjectPropertyConditionTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Test; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.review.WorkflowContext; - -import static org.fest.assertions.Assertions.assertThat; - -public class ProjectPropertyConditionTest { - @Test - public void getPropertyKey() { - ProjectPropertyCondition condition = new ProjectPropertyCondition("foo") { - @Override - public boolean doVerify(Review review, WorkflowContext context) { - return false; - } - }; - assertThat(condition.getPropertyKey()).isEqualTo("foo"); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ResolutionConditionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ResolutionConditionTest.java deleted file mode 100644 index caa79784e3c..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/ResolutionConditionTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; -import org.sonar.core.review.workflow.review.Review; - -import static org.fest.assertions.Assertions.assertThat; - -public class ResolutionConditionTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void failIfNoResolution() { - thrown.expect(IllegalArgumentException.class); - new ResolutionCondition(); - } - - @Test - public void getResolutions() { - ResolutionCondition condition = new ResolutionCondition("", "RESOLVED"); - assertThat(condition.getResolutions()).containsOnly("", "RESOLVED"); - } - - @Test - public void doVerify_review_has_resolution() { - Condition condition = new ResolutionCondition("", "RESOLVED"); - Review review = new DefaultReview().setResolution(""); - assertThat(condition.doVerify(review, new DefaultWorkflowContext())).isTrue(); - } - - @Test - public void doVerify_review_does_not_have_resolution() { - Condition condition = new ResolutionCondition("", "RESOLVED"); - Review review = new DefaultReview().setResolution("OTHER"); - assertThat(condition.doVerify(review, new DefaultWorkflowContext())).isFalse(); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/StatusConditionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/StatusConditionTest.java deleted file mode 100644 index 46c2a2c5ed5..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/condition/StatusConditionTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.condition; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; -import org.sonar.core.review.workflow.review.Review; - -import static org.fest.assertions.Assertions.assertThat; - -public class StatusConditionTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void failIfNoStatus() { - thrown.expect(IllegalArgumentException.class); - new StatusCondition(); - } - - - @Test - public void getStatuses() { - StatusCondition condition = new StatusCondition("OPEN", "CLOSED"); - assertThat(condition.getStatuses()).containsOnly("OPEN", "CLOSED"); - } - - @Test - public void doVerify_review_has_status() { - Condition condition = new StatusCondition("OPEN", "CLOSED"); - Review review = new DefaultReview().setStatus("CLOSED"); - assertThat(condition.doVerify(review, new DefaultWorkflowContext())).isTrue(); - } - - @Test - public void doVerify_review_does_not_have_status() { - Condition condition = new StatusCondition("OPEN", "CLOSED"); - Review review = new DefaultReview().setStatus("OTHER"); - assertThat(condition.doVerify(review, new DefaultWorkflowContext())).isFalse(); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/function/CommentFunctionTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/function/CommentFunctionTest.java deleted file mode 100644 index bfb464298ce..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/function/CommentFunctionTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.function; - -import com.google.common.collect.Maps; -import org.junit.Test; -import org.sonar.core.review.workflow.review.Comment; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; - -import java.util.List; -import java.util.Map; - -import static org.fest.assertions.Assertions.assertThat; - -public class CommentFunctionTest { - @Test - public void setTextAndUserId() { - CommentFunction function = new CommentFunction(); - Map parameters = Maps.newHashMap(); - parameters.put("text", "foo"); - DefaultReview review = new DefaultReview(); - DefaultWorkflowContext context = new DefaultWorkflowContext(); - context.setUserId(1234L); - - function.doExecute(review, new DefaultReview(), context, parameters); - - List newComments = review.getNewComments(); - assertThat(newComments).hasSize(1); - assertThat(newComments.get(0).getMarkdownText()).isEqualTo("foo"); - assertThat(newComments.get(0).getUserId()).isEqualTo(1234L); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/review/workflow/screen/CommentScreenTest.java b/sonar-core/src/test/java/org/sonar/core/review/workflow/screen/CommentScreenTest.java deleted file mode 100644 index db448b37d7a..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/review/workflow/screen/CommentScreenTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.core.review.workflow.screen; - -import org.junit.Test; - -import static org.fest.assertions.Assertions.assertThat; - -public class CommentScreenTest { - @Test - public void testCommentScreen() { - CommentScreen screen = new CommentScreen(); - - assertThat(screen.getKey()).isEqualTo("comment"); - - assertThat(screen.getCommandKey()).isNull(); - assertThat(screen.setCommandKey("create-jira-issue")); - assertThat(screen.getCommandKey()).isEqualTo("create-jira-issue"); - } -} diff --git a/sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java b/sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java new file mode 100644 index 00000000000..8294d3ce7bf --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/workflow/ReviewDatabaseStoreTest.java @@ -0,0 +1,52 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.workflow; + +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.core.persistence.DaoTestCase; +import org.sonar.api.workflow.Comment; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.core.workflow.ReviewDatabaseStore; + +import java.util.Date; + +public class ReviewDatabaseStoreTest extends DaoTestCase { + + @Test + public void store() { + setupData("store"); + ReviewDatabaseStore store = new ReviewDatabaseStore(getMyBatis()); + DefaultReview review = new DefaultReview().setReviewId(1234L); + review.setStatus("CLOSED"); + review.setResolution("RESOLVED"); + review.setProperty("who", "me"); + review.setProperty("why", "because"); + Comment comment = review.createComment(); + comment.setMarkdownText("this is a comment"); + comment.setUserId(555L); + + Date now = DateUtils.parseDate("2012-05-18"); + store.store(review, now); + + checkTables("store", "reviews"); + checkTables("store", new String[]{"id"}, "review_comments"); + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/workflow/WorkflowEngineTest.java b/sonar-core/src/test/java/org/sonar/core/workflow/WorkflowEngineTest.java new file mode 100644 index 00000000000..b38a4a8793e --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/workflow/WorkflowEngineTest.java @@ -0,0 +1,178 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.workflow; + +import com.google.common.collect.ListMultimap; +import com.google.common.collect.Maps; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.config.Settings; +import org.sonar.api.workflow.internal.DefaultWorkflow; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; +import org.sonar.api.workflow.condition.Condition; +import org.sonar.api.workflow.condition.HasProjectPropertyCondition; +import org.sonar.api.workflow.function.Function; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; +import org.sonar.api.workflow.screen.CommentScreen; +import org.sonar.api.workflow.screen.Screen; +import org.sonar.core.workflow.ImmutableReview; +import org.sonar.core.workflow.ReviewStore; +import org.sonar.core.workflow.WorkflowEngine; + +import java.util.List; +import java.util.Map; + +import static org.fest.assertions.Assertions.assertThat; +import static org.junit.matchers.JUnitMatchers.hasItem; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class WorkflowEngineTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void listAvailableScreensForReview_empty() { + WorkflowEngine engine = new WorkflowEngine(new DefaultWorkflow(), mock(ReviewStore.class), new Settings()); + List screens = engine.listAvailableScreens(new DefaultReview(), new DefaultWorkflowContext(), true); + assertThat(screens).isEmpty(); + } + + @Test + public void listAvailableScreensForReview() { + DefaultWorkflow workflow = new DefaultWorkflow(); + workflow.addCommand("command-without-screen"); + workflow.addCommand("resolve"); + CommentScreen screen = new CommentScreen(); + workflow.setScreen("resolve", screen); + + WorkflowEngine engine = new WorkflowEngine(workflow, mock(ReviewStore.class), new Settings()); + List screens = engine.listAvailableScreens(new DefaultReview(), new DefaultWorkflowContext(), true); + assertThat(screens).containsExactly(screen); + } + + @Test + public void listAvailableScreensForReview_verify_conditions() { + DefaultWorkflow workflow = new DefaultWorkflow(); + workflow.addCommand("resolve"); + Condition condition = mock(Condition.class); + when(condition.doVerify(any(Review.class), any(WorkflowContext.class))).thenReturn(false); + workflow.addCondition("resolve", condition); + workflow.setScreen("resolve", new CommentScreen()); + + WorkflowEngine engine = new WorkflowEngine(workflow, mock(ReviewStore.class), new Settings()); + DefaultReview review = new DefaultReview(); + DefaultWorkflowContext context = new DefaultWorkflowContext(); + assertThat(engine.listAvailableScreens(review, context, true)).isEmpty(); + + verify(condition).doVerify(review, context); + } + + @Test + public void listAvailableScreensForReviews_empty() { + WorkflowEngine engine = new WorkflowEngine(new DefaultWorkflow(), mock(ReviewStore.class), new Settings()); + ListMultimap screens = engine.listAvailableScreens( + new DefaultReview[]{new DefaultReview().setViolationId(1000L), new DefaultReview().setViolationId(2000L)}, + new DefaultWorkflowContext(), true); + assertThat(screens.size()).isEqualTo(0); + } + + @Test + public void listAvailableScreensForReviews() { + DefaultWorkflow workflow = new DefaultWorkflow(); + workflow.addCommand("command-without-screen"); + workflow.addCommand("resolve"); + CommentScreen screen = new CommentScreen(); + workflow.setScreen("resolve", screen); + WorkflowEngine engine = new WorkflowEngine(workflow, mock(ReviewStore.class), new Settings()); + ListMultimap screens = engine.listAvailableScreens( + new DefaultReview[]{new DefaultReview().setViolationId(1000L), new DefaultReview().setViolationId(2000L)}, + new DefaultWorkflowContext(), true); + assertThat(screens.size()).isEqualTo(2); + assertThat(screens.get(1000L)).containsExactly(screen); + assertThat(screens.get(2000L)).containsExactly(screen); + } + + @Test + public void listAvailableScreensForReviews_load_project_properties() { + DefaultWorkflow workflow = new DefaultWorkflow(); + workflow.addCommand("resolve"); + workflow.addCondition("resolve", new HasProjectPropertyCondition("foo")); + + ReviewStore store = mock(ReviewStore.class); + WorkflowEngine engine = new WorkflowEngine(workflow, store, new Settings()); + + engine.listAvailableScreens( + new DefaultReview[]{new DefaultReview().setViolationId(1000L), new DefaultReview().setViolationId(2000L)}, + new DefaultWorkflowContext().setProjectId(300L), + true); + + verify(store).completeProjectSettings(eq(300L), any(Settings.class), (List) argThat(hasItem("foo"))); + } + + @Test + public void execute_conditions_pass() { + DefaultWorkflow workflow = new DefaultWorkflow(); + workflow.addCommand("resolve"); + workflow.addCondition("resolve", new HasProjectPropertyCondition("foo")); + Function function = mock(Function.class); + workflow.addFunction("resolve", function); + + ReviewStore store = mock(ReviewStore.class); + Settings settings = new Settings(); + settings.setProperty("foo", "bar"); + WorkflowEngine engine = new WorkflowEngine(workflow, store, settings); + + DefaultReview review = new DefaultReview().setViolationId(1000L); + Map parameters = Maps.newHashMap(); + DefaultWorkflowContext context = new DefaultWorkflowContext().setProjectId(300L); + + engine.execute("resolve", review, context, parameters); + + verify(store).completeProjectSettings(eq(300L), any(Settings.class), (List) argThat(hasItem("foo"))); + verify(function).doExecute(eq(review), any(ImmutableReview.class), eq(context), eq(parameters)); + } + + @Test + public void execute_fail_if_conditions_dont_pass() { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Condition is not respected: Property foo must be set"); + + DefaultWorkflow workflow = new DefaultWorkflow(); + workflow.addCommand("resolve"); + workflow.addCondition("resolve", new HasProjectPropertyCondition("foo")); + Function function = mock(Function.class); + workflow.addFunction("resolve", function); + + ReviewStore store = mock(ReviewStore.class); + Settings settings = new Settings();// missing property 'foo' + WorkflowEngine engine = new WorkflowEngine(workflow, store, settings); + + DefaultReview review = new DefaultReview().setViolationId(1000L); + Map parameters = Maps.newHashMap(); + DefaultWorkflowContext context = new DefaultWorkflowContext().setProjectId(300L); + + engine.execute("resolve", review, context, parameters); + } +} diff --git a/sonar-core/src/test/resources/org/sonar/core/review/workflow/ReviewDatabaseStoreTest/store-result.xml b/sonar-core/src/test/resources/org/sonar/core/review/workflow/ReviewDatabaseStoreTest/store-result.xml deleted file mode 100644 index a389f7ff88c..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/review/workflow/ReviewDatabaseStoreTest/store-result.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/review/workflow/ReviewDatabaseStoreTest/store.xml b/sonar-core/src/test/resources/org/sonar/core/review/workflow/ReviewDatabaseStoreTest/store.xml deleted file mode 100644 index 8de7eda9f7c..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/review/workflow/ReviewDatabaseStoreTest/store.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - diff --git a/sonar-core/src/test/resources/org/sonar/core/workflow/ReviewDatabaseStoreTest/store-result.xml b/sonar-core/src/test/resources/org/sonar/core/workflow/ReviewDatabaseStoreTest/store-result.xml new file mode 100644 index 00000000000..a389f7ff88c --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/workflow/ReviewDatabaseStoreTest/store-result.xml @@ -0,0 +1,31 @@ + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/workflow/ReviewDatabaseStoreTest/store.xml b/sonar-core/src/test/resources/org/sonar/core/workflow/ReviewDatabaseStoreTest/store.xml new file mode 100644 index 00000000000..8de7eda9f7c --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/workflow/ReviewDatabaseStoreTest/store.xml @@ -0,0 +1,24 @@ + + + + + diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/FilterColumn.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/FilterColumn.java index d5e8e323cd6..fb8516ec9f7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/FilterColumn.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/FilterColumn.java @@ -91,7 +91,7 @@ public final class FilterColumn { } /** - * A column can be based on the varation of a value rather than on the value itself. + * A column can be based on the variation of a value rather than on the value itself. * * @return true when the variation is used rather than the value */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Comment.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Comment.java new file mode 100644 index 00000000000..c478ad6f355 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Comment.java @@ -0,0 +1,36 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow; + +import com.google.common.annotations.Beta; + +/** + * @since 3.1 + */ +@Beta +public interface Comment { + String getMarkdownText(); + + Long getUserId(); + + Comment setMarkdownText(String s); + + Comment setUserId(Long l); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/MutableReview.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/MutableReview.java new file mode 100644 index 00000000000..5d1e3316029 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/MutableReview.java @@ -0,0 +1,45 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow; + +import com.google.common.annotations.Beta; + +import javax.annotation.Nullable; +import java.util.List; + +/** + * Review that can be changed by functions. It does not support (yet) changes + * on creation date, author, severity, existing comments or switched-off attribute. + * + * @since 3.1 + */ +@Beta +public interface MutableReview extends Review { + + MutableReview setStatus(String s); + + MutableReview setResolution(@Nullable String resolution); + + MutableReview setProperty(String key, @Nullable String value); + + Comment createComment(); + + List getNewComments(); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Review.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Review.java new file mode 100644 index 00000000000..9ff51f00b4c --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Review.java @@ -0,0 +1,83 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow; + +import com.google.common.annotations.Beta; + +import java.util.Map; + +/** + * @since 3.1 + */ +@Beta +public interface Review { + + /** + * This method will probably be removed in order to decrease + * coupling with database. + * + * @return not-null review id (primary key of the table REVIEWS). + */ + Long getReviewId(); + + /** + * @return not-null rule repository, for example "checkstyle" + */ + String getRuleRepositoryKey(); + + /** + * @return not-null rule key + */ + String getRuleKey(); + + /** + * @return not-null rule name, in English. + */ + String getRuleName(); + + boolean isSwitchedOff(); + + String getMessage(); + + /** + * @return not-null properties + */ + Map getProperties(); + + String getStatus(); + + String getResolution(); + + /** + * @return not-null severity, from INFO to BLOCKER + */ + String getSeverity(); + + /** + * @return optional line, starting from 1 + */ + Long getLine(); + + /** + * @return true if the violation has been created by an automated rule engine, + * false if created by an end-user. + */ + boolean isManual(); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Workflow.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Workflow.java new file mode 100644 index 00000000000..0e3d4e78a7d --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/Workflow.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow; + +import com.google.common.annotations.Beta; +import org.sonar.api.ServerComponent; +import org.sonar.api.workflow.condition.Condition; +import org.sonar.api.workflow.function.Function; +import org.sonar.api.workflow.screen.Screen; + +import java.util.List; +import java.util.Set; + +/** + * Experimental component to customize the actions that can be + * executed on reviews. + * + * @since 3.1 + */ +@Beta +public interface Workflow extends ServerComponent { + Workflow addCommand(String key); + + Set getCommands(); + + List getConditions(String commandKey); + + Workflow addCondition(String commandKey, Condition condition); + + List getFunctions(String commandKey); + + Workflow addFunction(String commandKey, Function function); + + Screen getScreen(String commandKey); + + Workflow setScreen(String commandKey, Screen screen); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/WorkflowContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/WorkflowContext.java new file mode 100644 index 00000000000..693e43a70e2 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/WorkflowContext.java @@ -0,0 +1,48 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow; + +import com.google.common.annotations.Beta; +import org.sonar.api.config.Settings; + +/** + * @since 3.1 + */ +@Beta +public interface WorkflowContext { + + /** + * TODO : to be replaced by getProjectKey() + */ + Long getProjectId(); + + Long getUserId(); + + String getUserLogin(); + + String getUserName(); + + String getUserEmail(); + + boolean isAdmin(); + + Settings getProjectSettings(); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/AdminRoleCondition.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/AdminRoleCondition.java new file mode 100644 index 00000000000..ee0595c8c0e --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/AdminRoleCondition.java @@ -0,0 +1,44 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import javax.annotation.Nullable; + +/** + * Checks that user has admin rights on project. + * + * @since 3.1 + */ +@Beta +public final class AdminRoleCondition extends Condition { + + public AdminRoleCondition() { + super(true); + } + + @Override + public boolean doVerify(@Nullable Review review, WorkflowContext context) { + return context.isAdmin(); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/Condition.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/Condition.java new file mode 100644 index 00000000000..985f51b03e6 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/Condition.java @@ -0,0 +1,59 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import javax.annotation.Nullable; + +/** + * Conditions control who can perform a command (i.e. who can see the screen + * associated to the command). + * + * @since 3.1 + */ +@Beta +public abstract class Condition { + + private final boolean onContext; + + protected Condition(boolean onContext) { + this.onContext = onContext; + } + + /** + * @return true if the condition relates to a review, false if it relates to the resource + * context (selected file, end-user, ...) + */ + public final boolean isOnContext() { + return onContext; + } + + /** + * @param review the review on "review conditions" like StatusCondition, null on "context conditions" + * like AdminRoleCondition or ProjectPropertyCondition + * @param context + * @return is the condition verified ? + */ + public abstract boolean doVerify(@Nullable Review review, WorkflowContext context); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/Conditions.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/Conditions.java new file mode 100644 index 00000000000..ac8985125b4 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/Conditions.java @@ -0,0 +1,59 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; + +/** + * Static utility methods pertaining to {@link Condition} instances. + * + * @since 3.1 + */ +@Beta +public final class Conditions { + + private Conditions() { + } + + public static Condition not(Condition c) { + return new NotCondition(c); + } + + public static Condition hasReviewProperty(String propertyKey) { + return new HasReviewPropertyCondition(propertyKey); + } + + public static Condition hasProjectProperty(String propertyKey) { + return new HasProjectPropertyCondition(propertyKey); + } + + public static Condition hasAdminRole() { + return new AdminRoleCondition(); + } + + public static Condition statuses(String... statuses) { + return new StatusCondition(statuses); + } + + public static Condition resolutions(String... resolutions) { + return new ResolutionCondition(resolutions); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/HasProjectPropertyCondition.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/HasProjectPropertyCondition.java new file mode 100644 index 00000000000..df9ce0dac62 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/HasProjectPropertyCondition.java @@ -0,0 +1,51 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; +import org.sonar.api.config.Settings; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import javax.annotation.Nullable; + +/** + * Checks that a project property is set, whatever its value. + * + * @since 3.1 + */ +@Beta +public final class HasProjectPropertyCondition extends ProjectPropertyCondition { + + public HasProjectPropertyCondition(String propertyKey) { + super(propertyKey); + } + + @Override + public boolean doVerify(@Nullable Review review, WorkflowContext context) { + Settings settings = context.getProjectSettings(); + return settings.hasKey(getPropertyKey()) || settings.getDefaultValue(getPropertyKey()) != null; + } + + @Override + public String toString() { + return "Property " + getPropertyKey() + " must be set"; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/HasReviewPropertyCondition.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/HasReviewPropertyCondition.java new file mode 100644 index 00000000000..09afc8cc4d8 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/HasReviewPropertyCondition.java @@ -0,0 +1,52 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import javax.annotation.Nullable; + +/** + * @since 3.1 + */ +@Beta +public final class HasReviewPropertyCondition extends Condition { + + private final String propertyKey; + + public HasReviewPropertyCondition(String propertyKey) { + super(false); + Preconditions.checkArgument(!Strings.isNullOrEmpty(propertyKey)); + this.propertyKey = propertyKey; + } + + public String getPropertyKey() { + return propertyKey; + } + + @Override + public boolean doVerify(@Nullable Review review, WorkflowContext context) { + return review != null && !Strings.isNullOrEmpty(review.getProperties().get(propertyKey)); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/NotCondition.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/NotCondition.java new file mode 100644 index 00000000000..3eabc3f575b --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/NotCondition.java @@ -0,0 +1,51 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; +import com.google.common.annotations.VisibleForTesting; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import javax.annotation.Nullable; + +/** + * @since 3.1 + */ +@Beta +public final class NotCondition extends Condition { + + private Condition condition; + + public NotCondition(Condition c) { + super(c.isOnContext()); + this.condition = c; + } + + @Override + public boolean doVerify(@Nullable Review review, WorkflowContext context) { + return !condition.doVerify(review, context); + } + + @VisibleForTesting + Condition getCondition() { + return condition; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/ProjectPropertyCondition.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/ProjectPropertyCondition.java new file mode 100644 index 00000000000..9c19004dc8b --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/ProjectPropertyCondition.java @@ -0,0 +1,42 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; + +/** + * @since 3.1 + */ +@Beta +public abstract class ProjectPropertyCondition extends Condition { + private final String propertyKey; + + protected ProjectPropertyCondition(String propertyKey) { + super(true); + Preconditions.checkArgument(!Strings.isNullOrEmpty(propertyKey)); + this.propertyKey = propertyKey; + } + + public final String getPropertyKey() { + return propertyKey; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/ResolutionCondition.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/ResolutionCondition.java new file mode 100644 index 00000000000..080df298700 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/ResolutionCondition.java @@ -0,0 +1,61 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.Set; + +/** + * @since 3.1 + */ +@Beta +public final class ResolutionCondition extends Condition { + private final Set resolutions; + + public ResolutionCondition(Set resolutions) { + super(false); + Preconditions.checkNotNull(resolutions); + Preconditions.checkArgument(!resolutions.isEmpty(), "No resolutions defined"); + this.resolutions = resolutions; + } + + public ResolutionCondition(String... resolutions) { + this(Sets.newLinkedHashSet(Arrays.asList(resolutions))); + } + + @Override + public boolean doVerify(@Nullable Review review, WorkflowContext context) { + return review != null && resolutions.contains(review.getResolution()); + } + + @VisibleForTesting + Set getResolutions() { + return ImmutableSet.copyOf(resolutions); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/StatusCondition.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/StatusCondition.java new file mode 100644 index 00000000000..f27709fec3a --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/StatusCondition.java @@ -0,0 +1,61 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import com.google.common.annotations.Beta; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.Set; + +/** + * @since 3.1 + */ +@Beta +public final class StatusCondition extends Condition { + private final Set statuses; + + public StatusCondition(Set statuses) { + super(false); + Preconditions.checkNotNull(statuses); + Preconditions.checkArgument(!statuses.isEmpty(), "No statuses defined"); + this.statuses = statuses; + } + + public StatusCondition(String... statuses) { + this(Sets.newLinkedHashSet(Arrays.asList(statuses))); + } + + @Override + public boolean doVerify(@Nullable Review review, WorkflowContext context) { + return review != null && statuses.contains(review.getStatus()); + } + + @VisibleForTesting + Set getStatuses() { + return ImmutableSet.copyOf(statuses); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/package-info.java new file mode 100644 index 00000000000..f8b3ec06f20 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/condition/package-info.java @@ -0,0 +1,23 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +@ParametersAreNonnullByDefault +package org.sonar.api.workflow.condition; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/CommentFunction.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/CommentFunction.java new file mode 100644 index 00000000000..c75e358c8c8 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/CommentFunction.java @@ -0,0 +1,43 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.function; + +import com.google.common.annotations.Beta; +import org.sonar.api.workflow.Comment; +import org.sonar.api.workflow.MutableReview; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import java.util.Map; + +/** + * @since 3.1 + */ +@Beta +public final class CommentFunction extends Function { + + @Override + public void doExecute(MutableReview review, Review initialReview, WorkflowContext context, Map parameters) { + Comment comment = review.createComment(); + comment.setMarkdownText(parameters.get("text")); + comment.setUserId(context.getUserId()); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/Function.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/Function.java new file mode 100644 index 00000000000..11ad2362e3a --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/Function.java @@ -0,0 +1,53 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.function; + +import com.google.common.annotations.Beta; +import org.sonar.api.workflow.MutableReview; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import java.util.Map; + +/** + * Functions perform actions when the command is executed, e.g.: + * + *
    + *
  • Assign the issue to a particular user (not yet implemented)
  • + *
  • Add a comment
  • + *
  • Set a review property
  • + *
+ * + * @since 3.1 + */ +@Beta +public abstract class Function { + + /** + * This method is executed when all the conditions pass. + * + * @param review the review that can be changed + * @param initialReview the read-only review as stated before execution of functions + * @param context information about the user who executed the command and about project + * @param parameters the command parameters sent by end user, generally from forms displayed in screens + */ + public abstract void doExecute(MutableReview review, Review initialReview, WorkflowContext context, Map parameters); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/package-info.java new file mode 100644 index 00000000000..a6ac723b57d --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/package-info.java @@ -0,0 +1,23 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +@ParametersAreNonnullByDefault +package org.sonar.api.workflow.function; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultComment.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultComment.java new file mode 100644 index 00000000000..1887b202f46 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultComment.java @@ -0,0 +1,60 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.internal; + +import com.google.common.annotations.Beta; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; +import org.sonar.api.workflow.Comment; + +/** + * @since 3.1 + */ +@Beta +public final class DefaultComment implements Comment { + private String markdownText; + private Long userId; + + DefaultComment() { + } + + public String getMarkdownText() { + return markdownText; + } + + public DefaultComment setMarkdownText(String s) { + this.markdownText = s; + return this; + } + + public Long getUserId() { + return userId; + } + + public DefaultComment setUserId(Long l) { + this.userId = l; + return this; + } + + @Override + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).toString(); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultReview.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultReview.java new file mode 100644 index 00000000000..019463e8828 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultReview.java @@ -0,0 +1,215 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.internal; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; +import org.sonar.api.workflow.Comment; +import org.sonar.api.workflow.MutableReview; +import org.sonar.api.utils.KeyValueFormat; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * @since 3.1 + */ +@Beta +public final class DefaultReview implements MutableReview { + + private Long violationId; + private Long reviewId; + private String ruleRepositoryKey; + private String ruleKey; + private String ruleName; + private Long line; + private boolean switchedOff = false; + private boolean manual = false; + private String message; + private String status; + private String resolution; + private String severity; + private Map properties; + private List newComments; + + public Long getViolationId() { + return violationId; + } + + public DefaultReview setViolationId(Long violationId) { + this.violationId = violationId; + return this; + } + + public Long getReviewId() { + return reviewId; + } + + public DefaultReview setReviewId(Long reviewId) { + this.reviewId = reviewId; + return this; + } + + public String getRuleRepositoryKey() { + return ruleRepositoryKey; + } + + public DefaultReview setRuleRepositoryKey(String s) { + this.ruleRepositoryKey = s; + return this; + } + + public String getRuleKey() { + return ruleKey; + } + + public DefaultReview setRuleKey(String s) { + this.ruleKey = s; + return this; + } + + public String getRuleName() { + return ruleName; + } + + public DefaultReview setRuleName(String s) { + this.ruleName = s; + return this; + } + + public Long getLine() { + return line; + } + + public DefaultReview setLine(Long line) { + this.line = line; + return this; + } + + public boolean isSwitchedOff() { + return switchedOff; + } + + public DefaultReview setSwitchedOff(boolean b) { + this.switchedOff = b; + return this; + } + + public boolean isManual() { + return manual; + } + + public DefaultReview setManual(boolean manual) { + this.manual = manual; + return this; + } + + public String getMessage() { + return message; + } + + public DefaultReview setMessage(String message) { + this.message = message; + return this; + } + + public String getStatus() { + return status; + } + + public DefaultReview setStatus(String s) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(s)); + this.status = s; + return this; + } + + public String getResolution() { + return resolution; + } + + public DefaultReview setResolution(@Nullable String s) { + this.resolution = s; + return this; + } + + public String getSeverity() { + return severity; + } + + public DefaultReview setSeverity(String s) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(s)); + this.severity = s; + return this; + } + + public Map getProperties() { + if (properties == null) { + return Collections.emptyMap(); + } + return properties; + } + + public DefaultReview setProperties(Map properties) { + this.properties = properties; + return this; + } + + public DefaultReview setPropertiesAsString(@Nullable String s) { + this.properties = (s == null ? null : KeyValueFormat.parse(s)); + return this; + } + + public Comment createComment() { + if (newComments == null) { + newComments = Lists.newArrayList(); + } + Comment comment = new DefaultComment(); + newComments.add(comment); + return comment; + } + + public List getNewComments() { + if (newComments == null) { + return Collections.emptyList(); + } + return newComments; + } + + public DefaultReview setProperty(String key, @Nullable String value) { + if (properties == null) { + // keeping entries ordered by key allows to have consistent behavior in unit tests + properties = Maps.newLinkedHashMap(); + } + properties.put(key, value); + return this; + } + + @Override + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).toString(); + } +} \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultWorkflow.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultWorkflow.java new file mode 100644 index 00000000000..e6a0c87abd3 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultWorkflow.java @@ -0,0 +1,138 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.internal; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import com.google.common.collect.*; +import org.sonar.api.workflow.Workflow; +import org.sonar.api.workflow.condition.Condition; +import org.sonar.api.workflow.condition.ProjectPropertyCondition; +import org.sonar.api.workflow.function.Function; +import org.sonar.api.workflow.screen.Screen; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * @since 3.1 + */ +@Beta +public final class DefaultWorkflow implements Workflow { + + private Set commands = Sets.newLinkedHashSet(); + private ListMultimap conditionsByCommand = ArrayListMultimap.create(); + private ListMultimap functionsByCommand = ArrayListMultimap.create(); + private Map screensByCommand = Maps.newLinkedHashMap(); + + /** + * Keys of all the properties that are required by conditions (see {@link org.sonar.api.workflow.condition.ProjectPropertyCondition} + */ + private List projectPropertyKeys = Lists.newArrayList(); + + /** + * Optimization: fast way to get all context conditions + */ + private ListMultimap contextConditionsByCommand = ArrayListMultimap.create(); + + /** + * Optimization: fast way to get all review conditions + */ + private ListMultimap reviewConditionsByCommand = ArrayListMultimap.create(); + + + public Workflow addCommand(String key) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Empty command key"); + commands.add(key); + return this; + } + + public Set getCommands() { + return commands; + } + + public boolean hasCommand(String key) { + return commands.contains(key); + } + + public List getProjectPropertyKeys() { + return projectPropertyKeys; + } + + /** + * Shortcut for: getReviewConditions(commandKey) + getContextConditions(commandKey) + */ + public List getConditions(String commandKey) { + return conditionsByCommand.get(commandKey); + } + + public List getReviewConditions(String commandKey) { + return reviewConditionsByCommand.get(commandKey); + } + + public List getContextConditions(String commandKey) { + return contextConditionsByCommand.get(commandKey); + } + + public Workflow addCondition(String commandKey, Condition condition) { + Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey); + Preconditions.checkNotNull(condition); + conditionsByCommand.put(commandKey, condition); + if (condition instanceof ProjectPropertyCondition) { + projectPropertyKeys.add(((ProjectPropertyCondition) condition).getPropertyKey()); + } + if (condition.isOnContext()) { + contextConditionsByCommand.put(commandKey, condition); + } else { + reviewConditionsByCommand.put(commandKey, condition); + } + return this; + } + + public List getFunctions(String commandKey) { + return functionsByCommand.get(commandKey); + } + + public Workflow addFunction(String commandKey, Function function) { + Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey); + Preconditions.checkNotNull(function); + functionsByCommand.put(commandKey, function); + return this; + } + + public Screen getScreen(String commandKey) { + return screensByCommand.get(commandKey); + } + + public Workflow setScreen(String commandKey, Screen screen) { + Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey); + Preconditions.checkNotNull(screen); + Preconditions.checkState(Strings.isNullOrEmpty(screen.getCommandKey()), "Screen is already associated with command: " + screen.getCommandKey()); + screen.setCommandKey(commandKey); + screensByCommand.put(commandKey, screen); + return this; + } + + public Map getScreensByCommand() { + return screensByCommand; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultWorkflowContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultWorkflowContext.java new file mode 100644 index 00000000000..3101d431e29 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/DefaultWorkflowContext.java @@ -0,0 +1,109 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.internal; + +import com.google.common.annotations.Beta; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; +import org.sonar.api.config.Settings; +import org.sonar.api.workflow.WorkflowContext; + +/** + * @since 3.1 + */ +@Beta +public final class DefaultWorkflowContext implements WorkflowContext { + + private Long userId; + private String userLogin; + private String userName; + private String userEmail; + private boolean isAdmin = false; + private Long projectId; + private Settings settings; + + public Long getUserId() { + return userId; + } + + public DefaultWorkflowContext setUserId(Long l) { + this.userId = l; + return this; + } + + public String getUserLogin() { + return userLogin; + } + + public DefaultWorkflowContext setUserLogin(String s) { + this.userLogin = s; + return this; + } + + public String getUserName() { + return userName; + } + + public DefaultWorkflowContext setUserName(String s) { + this.userName = s; + return this; + } + + public String getUserEmail() { + return userEmail; + } + + public DefaultWorkflowContext setUserEmail(String userEmail) { + this.userEmail = userEmail; + return this; + } + + public boolean isAdmin() { + return isAdmin; + } + + public DefaultWorkflowContext setIsAdmin(boolean b) { + isAdmin = b; + return this; + } + + public Long getProjectId() { + return projectId; + } + + public DefaultWorkflowContext setProjectId(Long l) { + this.projectId = l; + return this; + } + + public Settings getProjectSettings() { + return settings; + } + + public DefaultWorkflowContext setSettings(Settings s) { + this.settings = s; + return this; + } + + @Override + public String toString() { + return new ReflectionToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).toString(); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/package-info.java new file mode 100644 index 00000000000..4ac8f86d8aa --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/internal/package-info.java @@ -0,0 +1,31 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ + +/** + This package is not considered as API and future versions can break backward-compatibility. +

+ It provides some classes that can be helpful for unit tests but must + ABSOLUTELY NOT be used by plugins production code. +

+ */ +@ParametersAreNonnullByDefault +package org.sonar.api.workflow.internal; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/package-info.java new file mode 100644 index 00000000000..919b1aa45fc --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/package-info.java @@ -0,0 +1,24 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ + +@ParametersAreNonnullByDefault +package org.sonar.api.workflow; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/CommentScreen.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/CommentScreen.java new file mode 100644 index 00000000000..aff9df57e36 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/CommentScreen.java @@ -0,0 +1,38 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.screen; + +/** + * Form with only a textarea field to type a comment. + */ + +import com.google.common.annotations.Beta; + +/** + * @since 3.1 + */ +@Beta +public final class CommentScreen extends Screen { + + public CommentScreen() { + super("comment"); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/Screen.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/Screen.java new file mode 100644 index 00000000000..853a29a60d7 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/Screen.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.screen; + +import com.google.common.annotations.Beta; + +/** + *

Localization

+ *

At least two buttons must have labels :

+ *
    + *
  • the button in the violation toolbar that displays the form screen. Key is 'reviews.command..button'.
  • + *
  • the button in the form screen that submits the command. Key is 'reviews.command..submit'.
  • + *
+ * @since 3.1 + */ +@Beta +public abstract class Screen { + private final String key; + private String commandKey; + + protected Screen(String key) { + this.key = key; + } + + public final String getKey() { + return key; + } + + public final String getCommandKey() { + return commandKey; + } + + public final Screen setCommandKey(String commandKey) { + this.commandKey = commandKey; + return this; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/package-info.java new file mode 100644 index 00000000000..f8f7e605f9c --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/screen/package-info.java @@ -0,0 +1,23 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +@ParametersAreNonnullByDefault +package org.sonar.api.workflow.screen; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/AdminRoleConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/AdminRoleConditionTest.java new file mode 100644 index 00000000000..9a6d9fe33b4 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/AdminRoleConditionTest.java @@ -0,0 +1,44 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Test; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; + +import static org.fest.assertions.Assertions.assertThat; + +public class AdminRoleConditionTest { + @Test + public void verifiedIfAdminRole() { + AdminRoleCondition condition = new AdminRoleCondition(); + DefaultWorkflowContext context = new DefaultWorkflowContext(); + context.setIsAdmin(true); + assertThat(condition.doVerify(new DefaultReview(), context)).isTrue(); + } + + @Test + public void failIfNotAdminRole() { + AdminRoleCondition condition = new AdminRoleCondition(); + DefaultWorkflowContext context = new DefaultWorkflowContext(); + context.setIsAdmin(false); + assertThat(condition.doVerify(new DefaultReview(), context)).isFalse(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ConditionTest.java new file mode 100644 index 00000000000..5fbb7d2d26a --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ConditionTest.java @@ -0,0 +1,51 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Test; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import static org.fest.assertions.Assertions.assertThat; + +public class ConditionTest { + @Test + public void checkedOncePerGroupOfReviews() { + Condition condition = new Condition(true) { + @Override + public boolean doVerify(Review review, WorkflowContext context) { + return false; + } + }; + assertThat(condition.isOnContext()).isTrue(); + } + + @Test + public void checkedForEveryReview() { + Condition condition = new Condition(false) { + @Override + public boolean doVerify(Review review, WorkflowContext context) { + return false; + } + }; + assertThat(condition.isOnContext()).isFalse(); + } + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ConditionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ConditionsTest.java new file mode 100644 index 00000000000..2c913d4752b --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ConditionsTest.java @@ -0,0 +1,68 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class ConditionsTest { + @Test + public void not() { + StatusCondition target = new StatusCondition("OPEN"); + Condition not = Conditions.not(target); + assertThat(not).isInstanceOf(NotCondition.class); + assertThat(((NotCondition) not).getCondition()).isSameAs(target); + } + + @Test + public void hasReviewProperty() { + Condition condition = Conditions.hasReviewProperty("foo"); + assertThat(condition).isInstanceOf(HasReviewPropertyCondition.class); + assertThat(((HasReviewPropertyCondition) condition).getPropertyKey()).isEqualTo("foo"); + } + + @Test + public void hasProjectProperty() { + Condition condition = Conditions.hasProjectProperty("foo"); + assertThat(condition).isInstanceOf(HasProjectPropertyCondition.class); + assertThat(((HasProjectPropertyCondition) condition).getPropertyKey()).isEqualTo("foo"); + } + + @Test + public void hasAdminRole() { + Condition condition = Conditions.hasAdminRole(); + assertThat(condition).isInstanceOf(AdminRoleCondition.class); + } + + @Test + public void statuses() { + Condition condition = Conditions.statuses("OPEN", "CLOSED"); + assertThat(condition).isInstanceOf(StatusCondition.class); + assertThat(((StatusCondition) condition).getStatuses()).containsOnly("OPEN", "CLOSED"); + } + + @Test + public void resolutions() { + Condition condition = Conditions.resolutions("", "RESOLVED"); + assertThat(condition).isInstanceOf(ResolutionCondition.class); + assertThat(((ResolutionCondition) condition).getResolutions()).containsOnly("", "RESOLVED"); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasProjectPropertyConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasProjectPropertyConditionTest.java new file mode 100644 index 00000000000..87c3a4fe70b --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasProjectPropertyConditionTest.java @@ -0,0 +1,64 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Test; +import org.sonar.api.Properties; +import org.sonar.api.Property; +import org.sonar.api.config.PropertyDefinitions; +import org.sonar.api.config.Settings; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; + + +import static org.fest.assertions.Assertions.assertThat; + +public class HasProjectPropertyConditionTest { + @Test + public void doVerify() { + HasProjectPropertyCondition condition = new HasProjectPropertyCondition("jira.url"); + DefaultWorkflowContext context = new DefaultWorkflowContext(); + context.setSettings(new Settings().setProperty("jira.url", "http://jira")); + assertThat(condition.doVerify(new DefaultReview(), context)).isTrue(); + } + + @Test + public void missingProperty() { + HasProjectPropertyCondition condition = new HasProjectPropertyCondition("jira.url"); + DefaultWorkflowContext context = new DefaultWorkflowContext(); + context.setSettings(new Settings()); + assertThat(condition.doVerify(new DefaultReview(), context)).isFalse(); + } + + @Test + public void returnTrueIfDefaultValue() { + HasProjectPropertyCondition condition = new HasProjectPropertyCondition("jira.url"); + DefaultWorkflowContext context = new DefaultWorkflowContext(); + context.setSettings(new Settings(new PropertyDefinitions().addComponent(WithDefaultValue.class))); + assertThat(condition.doVerify(new DefaultReview(), context)).isTrue(); + } + + @Properties({ + @Property(key = "jira.url", name = "JIRA URL", defaultValue = "http://jira.com") + }) + private static class WithDefaultValue { + + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasReviewPropertyConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasReviewPropertyConditionTest.java new file mode 100644 index 00000000000..1f578549024 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasReviewPropertyConditionTest.java @@ -0,0 +1,62 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; + +import static org.fest.assertions.Assertions.assertThat; + +public class HasReviewPropertyConditionTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void doVerify() { + HasReviewPropertyCondition condition = new HasReviewPropertyCondition("foo"); + + DefaultWorkflowContext context = new DefaultWorkflowContext(); + assertThat(condition.doVerify(new DefaultReview(), context)).isFalse(); + assertThat(condition.doVerify(new DefaultReview().setProperty("foo", ""), context)).isFalse(); + assertThat(condition.doVerify(new DefaultReview().setProperty("foo", "bar"), context)).isTrue(); + } + + @Test + public void getPropertyKey() { + HasReviewPropertyCondition condition = new HasReviewPropertyCondition("foo"); + assertThat(condition.getPropertyKey()).isEqualTo("foo"); + } + + @Test + public void failIfNullProperty() { + thrown.expect(IllegalArgumentException.class); + new HasReviewPropertyCondition(null); + } + + @Test + public void failIfEmptyProperty() { + thrown.expect(IllegalArgumentException.class); + new HasReviewPropertyCondition(""); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/NotConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/NotConditionTest.java new file mode 100644 index 00000000000..1b73fc7723c --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/NotConditionTest.java @@ -0,0 +1,53 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Test; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import static org.fest.assertions.Assertions.assertThat; + +public class NotConditionTest { + @Test + public void doVerifyInverse() { + Condition target = new TargetCondition(true); + assertThat(new NotCondition(target).doVerify(new DefaultReview(), new DefaultWorkflowContext())).isFalse(); + + target = new TargetCondition(false); + assertThat(new NotCondition(target).doVerify(new DefaultReview(), new DefaultWorkflowContext())).isTrue(); + } + + private static class TargetCondition extends Condition { + private boolean returns; + + private TargetCondition(boolean returns) { + super(false); + this.returns = returns; + } + + @Override + public boolean doVerify(Review review, WorkflowContext context) { + return returns; + } + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ProjectPropertyConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ProjectPropertyConditionTest.java new file mode 100644 index 00000000000..7e3344d528a --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ProjectPropertyConditionTest.java @@ -0,0 +1,39 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Test; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.WorkflowContext; + +import static org.fest.assertions.Assertions.assertThat; + +public class ProjectPropertyConditionTest { + @Test + public void getPropertyKey() { + ProjectPropertyCondition condition = new ProjectPropertyCondition("foo") { + @Override + public boolean doVerify(Review review, WorkflowContext context) { + return false; + } + }; + assertThat(condition.getPropertyKey()).isEqualTo("foo"); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ResolutionConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ResolutionConditionTest.java new file mode 100644 index 00000000000..17b44272931 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/ResolutionConditionTest.java @@ -0,0 +1,61 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; + +import static org.fest.assertions.Assertions.assertThat; + +public class ResolutionConditionTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void failIfNoResolution() { + thrown.expect(IllegalArgumentException.class); + new ResolutionCondition(); + } + + @Test + public void getResolutions() { + ResolutionCondition condition = new ResolutionCondition("", "RESOLVED"); + assertThat(condition.getResolutions()).containsOnly("", "RESOLVED"); + } + + @Test + public void doVerify_review_has_resolution() { + Condition condition = new ResolutionCondition("", "RESOLVED"); + Review review = new DefaultReview().setResolution(""); + assertThat(condition.doVerify(review, new DefaultWorkflowContext())).isTrue(); + } + + @Test + public void doVerify_review_does_not_have_resolution() { + Condition condition = new ResolutionCondition("", "RESOLVED"); + Review review = new DefaultReview().setResolution("OTHER"); + assertThat(condition.doVerify(review, new DefaultWorkflowContext())).isFalse(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/StatusConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/StatusConditionTest.java new file mode 100644 index 00000000000..2fcc17a49ed --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/StatusConditionTest.java @@ -0,0 +1,62 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.condition; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; + +import static org.fest.assertions.Assertions.assertThat; + +public class StatusConditionTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void failIfNoStatus() { + thrown.expect(IllegalArgumentException.class); + new StatusCondition(); + } + + + @Test + public void getStatuses() { + StatusCondition condition = new StatusCondition("OPEN", "CLOSED"); + assertThat(condition.getStatuses()).containsOnly("OPEN", "CLOSED"); + } + + @Test + public void doVerify_review_has_status() { + Condition condition = new StatusCondition("OPEN", "CLOSED"); + Review review = new DefaultReview().setStatus("CLOSED"); + assertThat(condition.doVerify(review, new DefaultWorkflowContext())).isTrue(); + } + + @Test + public void doVerify_review_does_not_have_status() { + Condition condition = new StatusCondition("OPEN", "CLOSED"); + Review review = new DefaultReview().setStatus("OTHER"); + assertThat(condition.doVerify(review, new DefaultWorkflowContext())).isFalse(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/function/CommentFunctionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/function/CommentFunctionTest.java new file mode 100644 index 00000000000..3970f22b809 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/function/CommentFunctionTest.java @@ -0,0 +1,50 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.function; + +import com.google.common.collect.Maps; +import org.junit.Test; +import org.sonar.api.workflow.Comment; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; + +import java.util.List; +import java.util.Map; + +import static org.fest.assertions.Assertions.assertThat; + +public class CommentFunctionTest { + @Test + public void setTextAndUserId() { + CommentFunction function = new CommentFunction(); + Map parameters = Maps.newHashMap(); + parameters.put("text", "foo"); + DefaultReview review = new DefaultReview(); + DefaultWorkflowContext context = new DefaultWorkflowContext(); + context.setUserId(1234L); + + function.doExecute(review, new DefaultReview(), context, parameters); + + List newComments = review.getNewComments(); + assertThat(newComments).hasSize(1); + assertThat(newComments.get(0).getMarkdownText()).isEqualTo("foo"); + assertThat(newComments.get(0).getUserId()).isEqualTo(1234L); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/internal/DefaultWorkflowTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/internal/DefaultWorkflowTest.java new file mode 100644 index 00000000000..69bd7711d23 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/internal/DefaultWorkflowTest.java @@ -0,0 +1,173 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.internal; + +import org.fest.assertions.MapAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.workflow.Workflow; +import org.sonar.api.workflow.condition.Condition; +import org.sonar.api.workflow.condition.HasProjectPropertyCondition; +import org.sonar.api.workflow.condition.StatusCondition; +import org.sonar.api.workflow.function.CommentFunction; +import org.sonar.api.workflow.function.Function; +import org.sonar.api.workflow.screen.CommentScreen; + +import static org.fest.assertions.Assertions.assertThat; + +public class DefaultWorkflowTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void addCommand() { + DefaultWorkflow workflow = new DefaultWorkflow(); + assertThat(workflow.getCommands()).isEmpty(); + + assertThat(workflow.addCommand("resolve")).isSameAs(workflow); + assertThat(workflow.getCommands()).containsOnly("resolve"); + assertThat(workflow.hasCommand("resolve")).isTrue(); + } + + @Test + public void addCommand_does_not_accept_blank() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Empty command key"); + + Workflow workflow = new DefaultWorkflow(); + workflow.addCommand(""); + } + + @Test + public void addSeveralTimesTheSameCommand() { + Workflow workflow = new DefaultWorkflow(); + workflow.addCommand("resolve"); + workflow.addCommand("resolve"); + assertThat(workflow.getCommands()).containsOnly("resolve"); + assertThat(workflow.getCommands()).hasSize(1); + } + + @Test + public void addCondition_fail_if_unknown_command() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Unknown command: resolve"); + + Workflow workflow = new DefaultWorkflow(); + workflow.addCondition("resolve", new StatusCondition("OPEN")); + } + + @Test + public void addCondition() { + Workflow workflow = new DefaultWorkflow(); + Condition condition = new StatusCondition("OPEN"); + workflow.addCommand("resolve"); + + workflow.addCondition("resolve", condition); + + assertThat(workflow.getConditions("resolve")).containsExactly(condition); + } + + @Test + public void getConditions_empty() { + Workflow workflow = new DefaultWorkflow(); + assertThat(workflow.getConditions("resolve")).isEmpty(); + } + + @Test + public void keepCacheOfProjectPropertiesRequiredByConditions() { + DefaultWorkflow workflow = new DefaultWorkflow(); + Condition condition1 = new HasProjectPropertyCondition("jira.url"); + Condition condition2 = new HasProjectPropertyCondition("jira.login"); + workflow.addCommand("create-jira-issue"); + workflow.addCondition("create-jira-issue", condition1); + workflow.addCondition("create-jira-issue", condition2); + + assertThat(workflow.getProjectPropertyKeys()).containsExactly("jira.url", "jira.login"); + } + + @Test + public void cacheOfProjectPropertiesIsNotNull() { + DefaultWorkflow workflow = new DefaultWorkflow(); + + assertThat(workflow.getProjectPropertyKeys()).isEmpty(); + } + + @Test + public void keepFastLinksToReviewAndContextConditions() { + DefaultWorkflow workflow = new DefaultWorkflow(); + workflow.addCommand("create-jira-issue"); + Condition contextCondition = new HasProjectPropertyCondition("jira.url"); + workflow.addCondition("create-jira-issue", contextCondition); + Condition reviewCondition = new StatusCondition("OPEN"); + workflow.addCondition("create-jira-issue", reviewCondition); + + assertThat(workflow.getContextConditions("create-jira-issue")).containsExactly(contextCondition); + assertThat(workflow.getReviewConditions("create-jira-issue")).containsExactly(reviewCondition); + } + + @Test + public void addFunction() { + Workflow workflow = new DefaultWorkflow(); + workflow.addCommand("resolve"); + + Function function = new CommentFunction(); + workflow.addFunction("resolve", function); + + assertThat(workflow.getFunctions("resolve")).containsExactly(function); + } + + @Test + public void getFunctions_empty() { + Workflow workflow = new DefaultWorkflow(); + assertThat(workflow.getFunctions("resolve")).isEmpty(); + } + + @Test + public void addFunction_fail_if_unknown_command() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Unknown command: resolve"); + + Workflow workflow = new DefaultWorkflow(); + workflow.addFunction("resolve", new CommentFunction()); + } + + @Test + public void setScreen_fail_if_unknown_command() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Unknown command: resolve"); + + Workflow workflow = new DefaultWorkflow(); + workflow.setScreen("resolve", new CommentScreen()); + } + + @Test + public void setScreen() { + DefaultWorkflow workflow = new DefaultWorkflow(); + workflow.addCommand("resolve"); + CommentScreen screen = new CommentScreen(); + workflow.setScreen("resolve", screen); + + assertThat(workflow.getScreen("resolve")).isSameAs(screen); + assertThat(workflow.getScreensByCommand()).includes(MapAssert.entry("resolve", screen)); + assertThat(workflow.getScreensByCommand()).hasSize(1); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/screen/CommentScreenTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/screen/CommentScreenTest.java new file mode 100644 index 00000000000..1404bfc6de0 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/screen/CommentScreenTest.java @@ -0,0 +1,37 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.workflow.screen; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class CommentScreenTest { + @Test + public void testCommentScreen() { + CommentScreen screen = new CommentScreen(); + + assertThat(screen.getKey()).isEqualTo("comment"); + + assertThat(screen.getCommandKey()).isNull(); + assertThat(screen.setCommandKey("create-jira-issue")); + assertThat(screen.getCommandKey()).isEqualTo("create-jira-issue"); + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 1542bb10aaa..26981427754 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -19,6 +19,7 @@ */ package org.sonar.server.platform; +import org.sonar.api.workflow.internal.DefaultWorkflow; import org.sonar.server.startup.RegisterNewFilters; import org.apache.commons.configuration.BaseConfiguration; @@ -44,9 +45,8 @@ import org.sonar.core.metric.DefaultMetricFinder; import org.sonar.core.notification.DefaultNotificationManager; import org.sonar.core.persistence.*; import org.sonar.core.qualitymodel.DefaultModelFinder; -import org.sonar.core.review.workflow.ReviewDatabaseStore; -import org.sonar.core.review.workflow.WorkflowEngine; -import org.sonar.core.review.workflow.Workflow; +import org.sonar.core.workflow.ReviewDatabaseStore; +import org.sonar.core.workflow.WorkflowEngine; import org.sonar.core.rule.DefaultRuleFinder; import org.sonar.core.user.DefaultUserFinder; import org.sonar.jpa.dao.MeasuresDao; @@ -176,7 +176,7 @@ public final class Platform { ServerExtensionInstaller extensionRegistrar = servicesContainer.getComponentByType(ServerExtensionInstaller.class); extensionRegistrar.registerExtensions(servicesContainer); - servicesContainer.addSingleton(Workflow.class); + servicesContainer.addSingleton(DefaultWorkflow.class); servicesContainer.addSingleton(ReviewDatabaseStore.class); servicesContainer.addSingleton(WorkflowEngine.class); diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index b639339f1bf..a2a73f25d9c 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -43,11 +43,11 @@ import org.sonar.core.persistence.Database; import org.sonar.core.persistence.DatabaseMigrator; import org.sonar.core.purge.PurgeDao; import org.sonar.core.resource.ResourceIndexerDao; -import org.sonar.core.review.workflow.WorkflowEngine; -import org.sonar.core.review.workflow.review.DefaultReview; -import org.sonar.core.review.workflow.review.DefaultWorkflowContext; -import org.sonar.core.review.workflow.review.Review; -import org.sonar.core.review.workflow.screen.Screen; +import org.sonar.core.workflow.WorkflowEngine; +import org.sonar.api.workflow.internal.DefaultReview; +import org.sonar.api.workflow.internal.DefaultWorkflowContext; +import org.sonar.api.workflow.Review; +import org.sonar.api.workflow.screen.Screen; import org.sonar.markdown.Markdown; import org.sonar.server.configuration.Backup; import org.sonar.server.configuration.ProfilesManager; diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/api/review_context.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/api/review_context.rb index 54b68249b94..6fd158983ff 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/api/review_context.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/api/review_context.rb @@ -33,7 +33,7 @@ class Api::ReviewContext def initialize(options={}) - @review = options[:review] + @review = options[:workflow] @project = options[:project] @user = options[:user] @params = options[:params] diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb index 6ae29910d0b..72a1d405f72 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb @@ -275,7 +275,7 @@ class RuleFailure < ActiveRecord::Base end def self.to_java_workflow_review(violation) - java_review=Java::OrgSonarCoreReviewWorkflowReview::DefaultReview.new + java_review=Java::OrgSonarApiWorkflowInternal::DefaultReview.new java_review.setViolationId(violation.id) java_review.setSeverity(violation.severity.to_s) java_review.setRuleKey(violation.rule.plugin_rule_key) @@ -299,7 +299,7 @@ class RuleFailure < ActiveRecord::Base end def self.to_java_workflow_context(project, user) - java_context = Java::OrgSonarCoreReviewWorkflowReview::DefaultWorkflowContext.new + java_context = Java::OrgSonarApiWorkflowInternal::DefaultWorkflowContext.new java_context.setUserId(user.id) java_context.setUserLogin(user.login) java_context.setUserName(user.name) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/project_reviews/_view.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/project_reviews/_view.html.erb index 7f52dafd51a..b274fc3672f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/project_reviews/_view.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/project_reviews/_view.html.erb @@ -3,5 +3,5 @@ # hack in case 'error_message' is nil (this should disappear when refactoring the '_view' and '_review' partials) error_message = error_message %> - <%= render :partial => 'project_reviews/review', :locals => {:review => @review, :error_message => error_message} -%> + <%= render :partial => 'project_reviews/review', :locals => {:workflow => @review, :error_message => error_message} -%>