Search in sources :

Example 21 with JDBCConnection

use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.

the class JDBCConnectionTest method testListResourceAccessNotRegisteredRolePrincipals.

@Test
public void testListResourceAccessNotRegisteredRolePrincipals() throws SQLException {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    // no role principals
    Mockito.when(mockResultSet.next()).thenReturn(false);
    try {
        jdbcConn.listResourceAccess("user.user1", "update", "user");
        fail();
    } catch (ResourceException ex) {
        assertEquals(ResourceException.NOT_FOUND, ex.getCode());
    }
    jdbcConn.close();
}
Also used : ResourceException(com.yahoo.athenz.zms.ResourceException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 22 with JDBCConnection

use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.

the class JDBCConnectionTest method testRollbackException.

@Test
public void testRollbackException() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, false);
    assertFalse(jdbcConn.transactionCompleted);
    Mockito.verify(mockConn, times(1)).setAutoCommit(false);
    Mockito.doThrow(new SQLException("failed operation", "state", 1001)).when(mockConn).rollback();
    jdbcConn.rollbackChanges();
    assertTrue(jdbcConn.transactionCompleted);
    Mockito.verify(mockConn, times(1)).rollback();
    Mockito.verify(mockConn, times(1)).setAutoCommit(true);
    jdbcConn.close();
}
Also used : SQLException(java.sql.SQLException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 23 with JDBCConnection

use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.

the class JDBCConnectionTest method testGetPublicKeyEntry.

@Test
public void testGetPublicKeyEntry() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
    5).thenReturn(// service id
    7);
    Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
    true).thenReturn(// this one is for service id
    true).thenReturn(// for key
    true);
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_KEY_VALUE)).thenReturn("Value1");
    PublicKeyEntry publicKey = jdbcConn.getPublicKeyEntry("my-domain", "service1", "zone1", false);
    assertNotNull(publicKey);
    assertEquals("Value1", publicKey.getKey());
    assertEquals("zone1", publicKey.getId());
    jdbcConn.close();
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 24 with JDBCConnection

use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.

the class JDBCConnectionTest method testDeletePrincipalSubDomainFailure.

@Test
public void testDeletePrincipalSubDomainFailure() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    // domain delete is success, but sub-domain is failure
    // thus the result must be successful
    Mockito.when(mockPrepStmt.executeUpdate()).thenReturn(1).thenReturn(0);
    Mockito.when(mockResultSet.next()).thenReturn(true);
    boolean requestSuccess = jdbcConn.deletePrincipal("user.jake", true);
    assertTrue(requestSuccess);
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.jake");
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.jake.%");
    jdbcConn.close();
}
Also used : JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 25 with JDBCConnection

use of com.yahoo.athenz.zms.store.jdbc.JDBCConnection in project athenz by yahoo.

the class JDBCConnectionTest method testInsertRoleMemberUpdate.

@Test
public void testInsertRoleMemberUpdate() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
    5).thenReturn(// role id
    7).thenReturn(// principal id
    9);
    Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
    true).thenReturn(// this one is for role id
    true).thenReturn(// validate principle domain
    true).thenReturn(// principal id
    true).thenReturn(// member exists
    true);
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    RoleMember roleMember = new RoleMember().setMemberName("user.user1");
    Timestamp expiration = Timestamp.fromCurrentTime();
    roleMember.setExpiration(expiration);
    java.sql.Timestamp javaExpiration = new java.sql.Timestamp(expiration.toDate().getTime());
    boolean requestSuccess = jdbcConn.insertRoleMember("my-domain", "role1", roleMember, "user.admin", "audit-ref");
    // this is combined for all operations above
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "role1");
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.user1");
    // we need additional operation for the audit log
    Mockito.verify(mockPrepStmt, times(2)).setInt(1, 7);
    Mockito.verify(mockPrepStmt, times(1)).setInt(2, 9);
    // update operation
    Mockito.verify(mockPrepStmt, times(1)).setTimestamp(1, javaExpiration);
    Mockito.verify(mockPrepStmt, times(1)).setInt(2, 7);
    Mockito.verify(mockPrepStmt, times(1)).setInt(3, 9);
    // the rest of the audit log details
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "user.admin");
    Mockito.verify(mockPrepStmt, times(1)).setString(3, "user.user1");
    Mockito.verify(mockPrepStmt, times(1)).setString(4, "UPDATE");
    Mockito.verify(mockPrepStmt, times(1)).setString(5, "audit-ref");
    assertTrue(requestSuccess);
    jdbcConn.close();
}
Also used : Timestamp(com.yahoo.rdl.Timestamp) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) RoleMember(com.yahoo.athenz.zms.RoleMember) Test(org.testng.annotations.Test)

Aggregations

JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)307 Test (org.testng.annotations.Test)307 ResourceException (com.yahoo.athenz.zms.ResourceException)131 SQLException (java.sql.SQLException)125 Assertion (com.yahoo.athenz.zms.Assertion)16 PrincipalRole (com.yahoo.athenz.zms.PrincipalRole)15 Role (com.yahoo.athenz.zms.Role)14 PublicKeyEntry (com.yahoo.athenz.zms.PublicKeyEntry)11 ServiceIdentity (com.yahoo.athenz.zms.ServiceIdentity)11 AthenzDomain (com.yahoo.athenz.zms.store.AthenzDomain)11 Domain (com.yahoo.athenz.zms.Domain)10 Entity (com.yahoo.athenz.zms.Entity)8 Quota (com.yahoo.athenz.zms.Quota)8 Policy (com.yahoo.athenz.zms.Policy)7 ResourceAccessList (com.yahoo.athenz.zms.ResourceAccessList)7 ArrayList (java.util.ArrayList)7 RoleMember (com.yahoo.athenz.zms.RoleMember)6 Struct (com.yahoo.rdl.Struct)6 Timestamp (com.yahoo.rdl.Timestamp)6 DomainModifiedList (com.yahoo.athenz.zms.DomainModifiedList)5