Search in sources :

Example 1 with RoleAuditLog

use of com.yahoo.athenz.zms.RoleAuditLog in project athenz by yahoo.

the class JDBCConnectionTest method testListRoleAuditLogs.

@Test
public void testListRoleAuditLogs() throws SQLException {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Mockito.when(mockResultSet.next()).thenReturn(// domain id success
    true).thenReturn(// role id success
    true).thenReturn(// 2 log entries
    true).thenReturn(true).thenReturn(false);
    Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
    5).thenReturn(// role id
    7);
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ACTION)).thenReturn("ADD").thenReturn("DELETE");
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_MEMBER)).thenReturn("user.member1").thenReturn("user.member2");
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ADMIN)).thenReturn("user.admin1").thenReturn("user.admin2");
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_AUDIT_REF)).thenReturn("").thenReturn("audit-ref");
    Mockito.doReturn(new java.sql.Timestamp(1454358916)).when(mockResultSet).getTimestamp(ZMSConsts.DB_COLUMN_CREATED);
    List<RoleAuditLog> logs = jdbcConn.listRoleAuditLogs("my-domain", "role1");
    assertNotNull(logs);
    assertEquals(2, logs.size());
    assertEquals("ADD", logs.get(0).getAction());
    assertEquals("user.admin1", logs.get(0).getAdmin());
    assertEquals("user.member1", logs.get(0).getMember());
    assertNull(logs.get(0).getAuditRef());
    assertEquals("DELETE", logs.get(1).getAction());
    assertEquals("user.admin2", logs.get(1).getAdmin());
    assertEquals("user.member2", logs.get(1).getMember());
    assertEquals("audit-ref", logs.get(1).getAuditRef());
    jdbcConn.close();
}
Also used : RoleAuditLog(com.yahoo.athenz.zms.RoleAuditLog) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 2 with RoleAuditLog

use of com.yahoo.athenz.zms.RoleAuditLog in project athenz by yahoo.

the class JDBCConnection method listRoleAuditLogs.

@Override
public List<RoleAuditLog> listRoleAuditLogs(String domainName, String roleName) {
    final String caller = "listRoleAuditLogs";
    int domainId = getDomainId(domainName);
    if (domainId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_DOMAIN, domainName);
    }
    int roleId = getRoleId(domainId, roleName);
    if (roleId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_ROLE, ZMSUtils.roleResourceName(domainName, roleName));
    }
    List<RoleAuditLog> logs = new ArrayList<>();
    try (PreparedStatement ps = con.prepareStatement(SQL_LIST_ROLE_AUDIT_LOGS)) {
        ps.setInt(1, roleId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            while (rs.next()) {
                RoleAuditLog log = new RoleAuditLog();
                log.setAction(rs.getString(ZMSConsts.DB_COLUMN_ACTION));
                log.setMember(rs.getString(ZMSConsts.DB_COLUMN_MEMBER));
                log.setAdmin(rs.getString(ZMSConsts.DB_COLUMN_ADMIN));
                log.setAuditRef(saveValue(rs.getString(ZMSConsts.DB_COLUMN_AUDIT_REF)));
                log.setCreated(Timestamp.fromMillis(rs.getTimestamp(ZMSConsts.DB_COLUMN_CREATED).getTime()));
                logs.add(log);
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    return logs;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RoleAuditLog(com.yahoo.athenz.zms.RoleAuditLog)

Aggregations

RoleAuditLog (com.yahoo.athenz.zms.RoleAuditLog)2 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1