LCOV - code coverage report
Current view: top level - server/project - BooleanProjectConfigTransformations.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 43 45 95.6 %
Date: 2022-11-19 15:00:39 Functions: 32 33 97.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 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.ImmutableMap;
      18             : import com.google.common.collect.Sets;
      19             : import com.google.gerrit.entities.BooleanProjectConfig;
      20             : import com.google.gerrit.extensions.api.projects.ConfigInfo;
      21             : import com.google.gerrit.extensions.api.projects.ConfigInfo.InheritedBooleanInfo;
      22             : import com.google.gerrit.extensions.api.projects.ConfigInput;
      23             : import com.google.gerrit.extensions.client.InheritableBoolean;
      24             : import java.util.Arrays;
      25             : import java.util.HashSet;
      26             : 
      27             : /** Provides transformations to get and set BooleanProjectConfigs from the API. */
      28           0 : public class BooleanProjectConfigTransformations {
      29             : 
      30             :   private static final ImmutableMap<BooleanProjectConfig, Mapper> MAPPER =
      31          22 :       ImmutableMap.<BooleanProjectConfig, Mapper>builder()
      32          22 :           .put(
      33             :               BooleanProjectConfig.USE_CONTRIBUTOR_AGREEMENTS,
      34          22 :               new Mapper(i -> i.useContributorAgreements, (i, v) -> i.useContributorAgreements = v))
      35          22 :           .put(
      36             :               BooleanProjectConfig.USE_SIGNED_OFF_BY,
      37          22 :               new Mapper(i -> i.useSignedOffBy, (i, v) -> i.useSignedOffBy = v))
      38          22 :           .put(
      39             :               BooleanProjectConfig.USE_CONTENT_MERGE,
      40          22 :               new Mapper(i -> i.useContentMerge, (i, v) -> i.useContentMerge = v))
      41          22 :           .put(
      42             :               BooleanProjectConfig.REQUIRE_CHANGE_ID,
      43          22 :               new Mapper(i -> i.requireChangeId, (i, v) -> i.requireChangeId = v))
      44          22 :           .put(
      45             :               BooleanProjectConfig.CREATE_NEW_CHANGE_FOR_ALL_NOT_IN_TARGET,
      46             :               new Mapper(
      47          22 :                   i -> i.createNewChangeForAllNotInTarget,
      48          22 :                   (i, v) -> i.createNewChangeForAllNotInTarget = v))
      49          22 :           .put(
      50             :               BooleanProjectConfig.ENABLE_SIGNED_PUSH,
      51          22 :               new Mapper(i -> i.enableSignedPush, (i, v) -> i.enableSignedPush = v))
      52          22 :           .put(
      53             :               BooleanProjectConfig.REQUIRE_SIGNED_PUSH,
      54          22 :               new Mapper(i -> i.requireSignedPush, (i, v) -> i.requireSignedPush = v))
      55          22 :           .put(
      56             :               BooleanProjectConfig.REJECT_IMPLICIT_MERGES,
      57          22 :               new Mapper(i -> i.rejectImplicitMerges, (i, v) -> i.rejectImplicitMerges = v))
      58          22 :           .put(
      59             :               BooleanProjectConfig.PRIVATE_BY_DEFAULT,
      60          22 :               new Mapper(i -> i.privateByDefault, (i, v) -> i.privateByDefault = v))
      61          22 :           .put(
      62             :               BooleanProjectConfig.ENABLE_REVIEWER_BY_EMAIL,
      63          22 :               new Mapper(i -> i.enableReviewerByEmail, (i, v) -> i.enableReviewerByEmail = v))
      64          22 :           .put(
      65             :               BooleanProjectConfig.MATCH_AUTHOR_TO_COMMITTER_DATE,
      66             :               new Mapper(
      67          22 :                   i -> i.matchAuthorToCommitterDate, (i, v) -> i.matchAuthorToCommitterDate = v))
      68          22 :           .put(
      69             :               BooleanProjectConfig.REJECT_EMPTY_COMMIT,
      70          22 :               new Mapper(i -> i.rejectEmptyCommit, (i, v) -> i.rejectEmptyCommit = v))
      71          22 :           .put(
      72             :               BooleanProjectConfig.WORK_IN_PROGRESS_BY_DEFAULT,
      73          22 :               new Mapper(i -> i.workInProgressByDefault, (i, v) -> i.workInProgressByDefault = v))
      74          22 :           .build();
      75             : 
      76             :   static {
      77             :     // Verify that each BooleanProjectConfig has to/from API mappers in
      78             :     // BooleanProjectConfigTransformations
      79          22 :     if (!Sets.symmetricDifference(
      80          22 :             MAPPER.keySet(), new HashSet<>(Arrays.asList(BooleanProjectConfig.values())))
      81          22 :         .isEmpty()) {
      82           0 :       throw new IllegalStateException(
      83             :           "All values of BooleanProjectConfig must have transformations associated with them");
      84             :     }
      85          22 :   }
      86             : 
      87             :   @FunctionalInterface
      88             :   private interface ToApi {
      89             :     void apply(ConfigInfo info, InheritedBooleanInfo val);
      90             :   }
      91             : 
      92             :   @FunctionalInterface
      93             :   private interface FromApi {
      94             :     InheritableBoolean apply(ConfigInput input);
      95             :   }
      96             : 
      97             :   public static void set(BooleanProjectConfig cfg, ConfigInfo info, InheritedBooleanInfo val) {
      98          22 :     MAPPER.get(cfg).set(info, val);
      99          22 :   }
     100             : 
     101             :   public static InheritableBoolean get(BooleanProjectConfig cfg, ConfigInput input) {
     102          22 :     return MAPPER.get(cfg).get(input);
     103             :   }
     104             : 
     105             :   private static class Mapper {
     106             :     private final FromApi fromApi;
     107             :     private final ToApi toApi;
     108             : 
     109          22 :     private Mapper(FromApi fromApi, ToApi toApi) {
     110          22 :       this.fromApi = fromApi;
     111          22 :       this.toApi = toApi;
     112          22 :     }
     113             : 
     114             :     public void set(ConfigInfo info, InheritedBooleanInfo val) {
     115          22 :       toApi.apply(info, val);
     116          22 :     }
     117             : 
     118             :     public InheritableBoolean get(ConfigInput input) {
     119          22 :       return fromApi.apply(input);
     120             :     }
     121             :   }
     122             : }

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