LCOV - code coverage report
Current view: top level - server/project - SubmitRequirementExpressionsValidator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 27 27 100.0 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2021 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.project;
      16             : 
      17             : import com.google.common.collect.ImmutableList;
      18             : import com.google.gerrit.entities.SubmitRequirement;
      19             : import com.google.gerrit.entities.SubmitRequirementExpression;
      20             : import com.google.gerrit.index.query.QueryParseException;
      21             : import com.google.inject.Inject;
      22             : import com.google.inject.Singleton;
      23             : import java.util.ArrayList;
      24             : import java.util.List;
      25             : 
      26             : @Singleton
      27             : public class SubmitRequirementExpressionsValidator {
      28             :   private final SubmitRequirementsEvaluator submitRequirementsEvaluator;
      29             : 
      30             :   @Inject
      31         146 :   SubmitRequirementExpressionsValidator(SubmitRequirementsEvaluator submitRequirementsEvaluator) {
      32         146 :     this.submitRequirementsEvaluator = submitRequirementsEvaluator;
      33         146 :   }
      34             : 
      35             :   /**
      36             :    * Validates the query expressions on the input {@code submitRequirement}.
      37             :    *
      38             :    * @return list of string containing the error messages resulting from the validation. The list is
      39             :    *     empty if the "submit requirement" is valid.
      40             :    */
      41             :   public ImmutableList<String> validateExpressions(SubmitRequirement submitRequirement) {
      42           4 :     List<String> validationMessages = new ArrayList<>();
      43           4 :     validateSubmitRequirementExpression(
      44             :         validationMessages,
      45             :         submitRequirement,
      46           4 :         submitRequirement.submittabilityExpression(),
      47             :         ProjectConfig.KEY_SR_SUBMITTABILITY_EXPRESSION);
      48           4 :     submitRequirement
      49           4 :         .applicabilityExpression()
      50           4 :         .ifPresent(
      51             :             expression ->
      52           2 :                 validateSubmitRequirementExpression(
      53             :                     validationMessages,
      54             :                     submitRequirement,
      55             :                     expression,
      56             :                     ProjectConfig.KEY_SR_APPLICABILITY_EXPRESSION));
      57           4 :     submitRequirement
      58           4 :         .overrideExpression()
      59           4 :         .ifPresent(
      60             :             expression ->
      61           2 :                 validateSubmitRequirementExpression(
      62             :                     validationMessages,
      63             :                     submitRequirement,
      64             :                     expression,
      65             :                     ProjectConfig.KEY_SR_OVERRIDE_EXPRESSION));
      66           4 :     return ImmutableList.copyOf(validationMessages);
      67             :   }
      68             : 
      69             :   private void validateSubmitRequirementExpression(
      70             :       List<String> validationMessages,
      71             :       SubmitRequirement submitRequirement,
      72             :       SubmitRequirementExpression expression,
      73             :       String configKey) {
      74             :     try {
      75           4 :       submitRequirementsEvaluator.validateExpression(expression);
      76           2 :     } catch (QueryParseException e) {
      77           2 :       if (validationMessages.isEmpty()) {
      78           2 :         validationMessages.add("Invalid project configuration");
      79             :       }
      80           2 :       validationMessages.add(
      81           2 :           String.format(
      82             :               "  %s: Expression '%s' of submit requirement '%s' (parameter %s.%s.%s) is"
      83             :                   + " invalid: %s",
      84             :               ProjectConfig.PROJECT_CONFIG,
      85           2 :               expression.expressionString(),
      86           2 :               submitRequirement.name(),
      87             :               ProjectConfig.SUBMIT_REQUIREMENT,
      88           2 :               submitRequirement.name(),
      89             :               configKey,
      90           2 :               e.getMessage()));
      91           4 :     }
      92           4 :   }
      93             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750