LCOV - code coverage report
Current view: top level - acceptance - TestOnStoreSubmitRequirementResultModifier.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 33 35 94.3 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2022 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.acceptance;
      16             : 
      17             : import com.google.common.collect.ImmutableList;
      18             : import com.google.gerrit.entities.SubmitRequirement;
      19             : import com.google.gerrit.entities.SubmitRequirementExpressionResult;
      20             : import com.google.gerrit.entities.SubmitRequirementResult;
      21             : import com.google.gerrit.server.project.OnStoreSubmitRequirementResultModifier;
      22             : import com.google.gerrit.server.query.change.ChangeData;
      23             : import com.google.gerrit.server.update.ChangeContext;
      24             : import java.util.Optional;
      25             : 
      26             : /** Implementation of {@link OnStoreSubmitRequirementResultModifier} that is used in tests. */
      27           1 : public class TestOnStoreSubmitRequirementResultModifier
      28             :     implements OnStoreSubmitRequirementResultModifier {
      29             : 
      30           1 :   private ModificationStrategy modificationStrategy = ModificationStrategy.KEEP;
      31             : 
      32           1 :   private boolean hide = false;
      33             : 
      34             :   /**
      35             :    * The strategy, used by this modifier to transform {@link SubmitRequirementResult} on {@link
      36             :    * OnStoreSubmitRequirementResultModifier#modifyResultOnStore} invocations.
      37             :    */
      38           1 :   public enum ModificationStrategy {
      39           1 :     KEEP,
      40           1 :     FAIL,
      41           1 :     PASS,
      42           1 :     OVERRIDE
      43             :   }
      44             : 
      45             :   public void setModificationStrategy(ModificationStrategy modificationStrategy) {
      46           1 :     this.modificationStrategy = modificationStrategy;
      47           1 :   }
      48             : 
      49             :   public void hide(boolean hide) {
      50           1 :     this.hide = hide;
      51           1 :   }
      52             : 
      53             :   @Override
      54             :   public SubmitRequirementResult modifyResultOnStore(
      55             :       SubmitRequirement submitRequirement,
      56             :       SubmitRequirementResult result,
      57             :       ChangeData cd,
      58             :       ChangeContext ctx) {
      59           1 :     if (modificationStrategy.equals(ModificationStrategy.KEEP)) {
      60           0 :       return result;
      61             :     }
      62           1 :     SubmitRequirementResult.Builder srResultBuilder = result.toBuilder().hidden(Optional.of(hide));
      63           1 :     if (modificationStrategy.equals(ModificationStrategy.OVERRIDE)) {
      64           1 :       return srResultBuilder
      65           1 :           .overrideExpressionResult(
      66           1 :               Optional.of(
      67           1 :                   SubmitRequirementExpressionResult.create(
      68           1 :                       submitRequirement.submittabilityExpression(),
      69             :                       SubmitRequirementExpressionResult.Status.PASS,
      70           1 :                       ImmutableList.of(),
      71           1 :                       ImmutableList.of(
      72           1 :                           submitRequirement.submittabilityExpression().expressionString()))))
      73           1 :           .build();
      74             :     }
      75           1 :     return srResultBuilder
      76           1 :         .submittabilityExpressionResult(
      77           1 :             SubmitRequirementExpressionResult.create(
      78           1 :                 submitRequirement.submittabilityExpression(),
      79           1 :                 modificationStrategy.equals(ModificationStrategy.FAIL)
      80           1 :                     ? SubmitRequirementExpressionResult.Status.FAIL
      81           0 :                     : SubmitRequirementExpressionResult.Status.PASS,
      82           1 :                 ImmutableList.of(),
      83           1 :                 ImmutableList.of(submitRequirement.submittabilityExpression().expressionString())))
      84           1 :         .build();
      85             :   }
      86             : }

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