LCOV - code coverage report
Current view: top level - extensions/common - GroupAuditEventInfo.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 20 31 64.5 %
Date: 2022-11-19 15:00:39 Functions: 8 13 61.5 %

          Line data    Source code
       1             : // Copyright (C) 2015 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.extensions.common;
      16             : 
      17             : import com.google.gerrit.common.Nullable;
      18             : import java.sql.Timestamp;
      19             : import java.time.Instant;
      20             : import java.util.Optional;
      21             : 
      22             : public abstract class GroupAuditEventInfo {
      23           2 :   public enum Type {
      24           2 :     ADD_USER,
      25           2 :     REMOVE_USER,
      26           2 :     ADD_GROUP,
      27           2 :     REMOVE_GROUP
      28             :   }
      29             : 
      30             :   public Type type;
      31             :   public AccountInfo user;
      32             : 
      33             :   // TODO(issue-15508): Migrate timestamp fields in *Info/*Input classes from type Timestamp to
      34             :   // Instant
      35             :   public Timestamp date;
      36             : 
      37             :   // TODO(issue-15508): Migrate timestamp fields in *Info/*Input classes from type Timestamp to
      38             :   // Instant
      39             :   @SuppressWarnings("JdkObsolete")
      40             :   public static UserMemberAuditEventInfo createAddUserEvent(
      41             :       AccountInfo user, Timestamp date, AccountInfo member) {
      42           0 :     return new UserMemberAuditEventInfo(Type.ADD_USER, user, date.toInstant(), member);
      43             :   }
      44             : 
      45             :   public static UserMemberAuditEventInfo createAddUserEvent(
      46             :       AccountInfo user, Instant date, AccountInfo member) {
      47           2 :     return new UserMemberAuditEventInfo(Type.ADD_USER, user, date, member);
      48             :   }
      49             : 
      50             :   // TODO(issue-15508): Migrate timestamp fields in *Info/*Input classes from type Timestamp to
      51             :   // Instant
      52             :   @SuppressWarnings("JdkObsolete")
      53             :   public static UserMemberAuditEventInfo createRemoveUserEvent(
      54             :       AccountInfo user, Optional<Timestamp> date, AccountInfo member) {
      55           0 :     return new UserMemberAuditEventInfo(
      56           0 :         Type.REMOVE_USER, user, date.map(Timestamp::toInstant).orElse(null), member);
      57             :   }
      58             : 
      59             :   public static UserMemberAuditEventInfo createRemoveUserEvent(
      60             :       AccountInfo user, @Nullable Instant date, AccountInfo member) {
      61           1 :     return new UserMemberAuditEventInfo(Type.REMOVE_USER, user, date, member);
      62             :   }
      63             : 
      64             :   // TODO(issue-15508): Migrate timestamp fields in *Info/*Input classes from type Timestamp to
      65             :   // Instant
      66             :   @SuppressWarnings("JdkObsolete")
      67             :   public static GroupMemberAuditEventInfo createAddGroupEvent(
      68             :       AccountInfo user, Timestamp date, GroupInfo member) {
      69           0 :     return new GroupMemberAuditEventInfo(Type.ADD_GROUP, user, date.toInstant(), member);
      70             :   }
      71             : 
      72             :   public static GroupMemberAuditEventInfo createAddGroupEvent(
      73             :       AccountInfo user, Instant date, GroupInfo member) {
      74           1 :     return new GroupMemberAuditEventInfo(Type.ADD_GROUP, user, date, member);
      75             :   }
      76             : 
      77             :   // TODO(issue-15508): Migrate timestamp fields in *Info/*Input classes from type Timestamp to
      78             :   // Instant
      79             :   @SuppressWarnings("JdkObsolete")
      80             :   public static GroupMemberAuditEventInfo createRemoveGroupEvent(
      81             :       AccountInfo user, Optional<Timestamp> date, GroupInfo member) {
      82           0 :     return new GroupMemberAuditEventInfo(
      83           0 :         Type.REMOVE_GROUP, user, date.map(Timestamp::toInstant).orElse(null), member);
      84             :   }
      85             : 
      86             :   public static GroupMemberAuditEventInfo createRemoveGroupEvent(
      87             :       AccountInfo user, @Nullable Instant date, GroupInfo member) {
      88           1 :     return new GroupMemberAuditEventInfo(Type.REMOVE_GROUP, user, date, member);
      89             :   }
      90             : 
      91           0 :   protected GroupAuditEventInfo(Type type, AccountInfo user, Optional<Timestamp> date) {
      92           0 :     this.type = type;
      93           0 :     this.user = user;
      94           0 :     this.date = date.orElse(null);
      95           0 :   }
      96             : 
      97             :   // TODO(issue-15508): Migrate timestamp fields in *Info/*Input classes from type Timestamp to
      98             :   // Instant
      99             :   @SuppressWarnings("JdkObsolete")
     100           2 :   protected GroupAuditEventInfo(Type type, AccountInfo user, @Nullable Instant date) {
     101           2 :     this.type = type;
     102           2 :     this.user = user;
     103           2 :     this.date = date != null ? Timestamp.from(date) : null;
     104           2 :   }
     105             : 
     106             :   public static class UserMemberAuditEventInfo extends GroupAuditEventInfo {
     107             :     public AccountInfo member;
     108             : 
     109             :     private UserMemberAuditEventInfo(
     110             :         Type type, AccountInfo user, @Nullable Instant date, AccountInfo member) {
     111           2 :       super(type, user, date);
     112           2 :       this.member = member;
     113           2 :     }
     114             :   }
     115             : 
     116             :   public static class GroupMemberAuditEventInfo extends GroupAuditEventInfo {
     117             :     public GroupInfo member;
     118             : 
     119             :     private GroupMemberAuditEventInfo(
     120             :         Type type, AccountInfo user, @Nullable Instant date, GroupInfo member) {
     121           1 :       super(type, user, date);
     122           1 :       this.member = member;
     123           1 :     }
     124             :   }
     125             : }

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