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

          Line data    Source code
       1             : // Copyright (C) 2020 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.cache.serialize.entities;
      16             : 
      17             : import static com.google.common.base.Strings.emptyToNull;
      18             : import static com.google.common.base.Strings.nullToEmpty;
      19             : import static com.google.common.collect.ImmutableSet.toImmutableSet;
      20             : 
      21             : import com.google.common.base.Converter;
      22             : import com.google.common.base.Enums;
      23             : import com.google.gerrit.entities.BooleanProjectConfig;
      24             : import com.google.gerrit.entities.Project;
      25             : import com.google.gerrit.extensions.client.InheritableBoolean;
      26             : import com.google.gerrit.extensions.client.ProjectState;
      27             : import com.google.gerrit.extensions.client.SubmitType;
      28             : import com.google.gerrit.server.cache.proto.Cache;
      29             : import java.util.Arrays;
      30             : import java.util.Set;
      31             : 
      32             : /** Helper to (de)serialize values for caches. */
      33             : public class ProjectSerializer {
      34          16 :   private static final Converter<String, ProjectState> PROJECT_STATE_CONVERTER =
      35          16 :       Enums.stringConverter(ProjectState.class);
      36          16 :   private static final Converter<String, SubmitType> SUBMIT_TYPE_CONVERTER =
      37          16 :       Enums.stringConverter(SubmitType.class);
      38             : 
      39             :   public static Project deserialize(Cache.ProjectProto proto) {
      40          16 :     Project.Builder builder =
      41          16 :         Project.builder(Project.nameKey(proto.getName()))
      42          16 :             .setSubmitType(SUBMIT_TYPE_CONVERTER.convert(proto.getSubmitType()))
      43          16 :             .setState(PROJECT_STATE_CONVERTER.convert(proto.getState()))
      44          16 :             .setDescription(emptyToNull(proto.getDescription()))
      45          16 :             .setParent(emptyToNull(proto.getParent()))
      46          16 :             .setMaxObjectSizeLimit(emptyToNull(proto.getMaxObjectSizeLimit()))
      47          16 :             .setDefaultDashboard(emptyToNull(proto.getDefaultDashboard()))
      48          16 :             .setLocalDefaultDashboard(emptyToNull(proto.getLocalDefaultDashboard()))
      49          16 :             .setConfigRefState(emptyToNull(proto.getConfigRefState()));
      50             : 
      51             :     Set<String> configs =
      52          16 :         Arrays.stream(BooleanProjectConfig.values())
      53          16 :             .map(BooleanProjectConfig::name)
      54          16 :             .collect(toImmutableSet());
      55          16 :     proto
      56          16 :         .getBooleanConfigsMap()
      57          16 :         .entrySet()
      58          16 :         .forEach(
      59             :             configEntry -> {
      60          16 :               if (configs.contains(configEntry.getKey())) {
      61          16 :                 builder.setBooleanConfig(
      62          16 :                     BooleanProjectConfig.valueOf(configEntry.getKey()),
      63          16 :                     InheritableBoolean.valueOf(configEntry.getValue()));
      64             :               }
      65          16 :             });
      66             : 
      67          16 :     return builder.build();
      68             :   }
      69             : 
      70             :   public static Cache.ProjectProto serialize(Project autoValue) {
      71             :     Cache.ProjectProto.Builder builder =
      72          16 :         Cache.ProjectProto.newBuilder()
      73          16 :             .setName(autoValue.getName())
      74          16 :             .setSubmitType(SUBMIT_TYPE_CONVERTER.reverse().convert(autoValue.getSubmitType()))
      75          16 :             .setState(PROJECT_STATE_CONVERTER.reverse().convert(autoValue.getState()))
      76          16 :             .setDescription(nullToEmpty(autoValue.getDescription()))
      77          16 :             .setParent(nullToEmpty(autoValue.getParentName()))
      78          16 :             .setMaxObjectSizeLimit(nullToEmpty(autoValue.getMaxObjectSizeLimit()))
      79          16 :             .setDefaultDashboard(nullToEmpty(autoValue.getDefaultDashboard()))
      80          16 :             .setLocalDefaultDashboard(nullToEmpty(autoValue.getLocalDefaultDashboard()))
      81          16 :             .setConfigRefState(nullToEmpty(autoValue.getConfigRefState()));
      82             : 
      83          16 :     autoValue
      84          16 :         .getBooleanConfigs()
      85          16 :         .entrySet()
      86          16 :         .forEach(
      87             :             configEntry -> {
      88          16 :               builder.putBooleanConfigs(configEntry.getKey().name(), configEntry.getValue().name());
      89          16 :             });
      90             : 
      91          16 :     return builder.build();
      92             :   }
      93             : 
      94             :   private ProjectSerializer() {}
      95             : }

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