Search in sources :

Example 6 with Quota

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

the class JDBCConnectionTest method testUpdateQuota.

@Test
public void testUpdateQuota() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Quota quota = new Quota().setName("athenz").setAssertion(10).setEntity(11).setPolicy(12).setPublicKey(13).setRole(14).setRoleMember(15).setService(16).setServiceHost(17).setSubdomain(18);
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.doReturn(5).when(mockResultSet).getInt(1);
    boolean requestSuccess = jdbcConn.updateQuota("athenz", quota);
    assertTrue(requestSuccess);
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "athenz");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 14);
    Mockito.verify(mockPrepStmt, times(1)).setInt(2, 15);
    Mockito.verify(mockPrepStmt, times(1)).setInt(3, 12);
    Mockito.verify(mockPrepStmt, times(1)).setInt(4, 10);
    Mockito.verify(mockPrepStmt, times(1)).setInt(5, 16);
    Mockito.verify(mockPrepStmt, times(1)).setInt(6, 17);
    Mockito.verify(mockPrepStmt, times(1)).setInt(7, 13);
    Mockito.verify(mockPrepStmt, times(1)).setInt(8, 11);
    Mockito.verify(mockPrepStmt, times(1)).setInt(9, 18);
    // domain id
    Mockito.verify(mockPrepStmt, times(1)).setInt(10, 5);
    jdbcConn.close();
}
Also used : Quota(com.yahoo.athenz.zms.Quota) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 7 with Quota

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

the class JDBCConnection method getQuota.

@Override
public Quota getQuota(String domainName) {
    final String caller = "getQuota";
    int domainId = getDomainId(domainName);
    if (domainId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_DOMAIN, domainName);
    }
    Quota quota = null;
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_QUOTA)) {
        ps.setInt(1, domainId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            if (rs.next()) {
                quota = new Quota().setName(domainName);
                quota.setAssertion(rs.getInt(ZMSConsts.DB_COLUMN_ASSERTION));
                quota.setRole(rs.getInt(ZMSConsts.DB_COLUMN_ROLE));
                quota.setRoleMember(rs.getInt(ZMSConsts.DB_COLUMN_ROLE_MEMBER));
                quota.setPolicy(rs.getInt(ZMSConsts.DB_COLUMN_POLICY));
                quota.setService(rs.getInt(ZMSConsts.DB_COLUMN_SERVICE));
                quota.setServiceHost(rs.getInt(ZMSConsts.DB_COLUMN_SERVICE_HOST));
                quota.setPublicKey(rs.getInt(ZMSConsts.DB_COLUMN_PUBLIC_KEY));
                quota.setEntity(rs.getInt(ZMSConsts.DB_COLUMN_ENTITY));
                quota.setSubdomain(rs.getInt(ZMSConsts.DB_COLUMN_SUBDOMAIN));
                quota.setModified(Timestamp.fromMillis(rs.getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED).getTime()));
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    return quota;
}
Also used : Quota(com.yahoo.athenz.zms.Quota) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 8 with Quota

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

the class JDBCConnectionTest method testGetQuotaNull.

@Test
public void testGetQuotaNull() throws Exception {
    Mockito.when(mockResultSet.next()).thenReturn(true).thenReturn(false);
    // domain id
    Mockito.doReturn(7).when(mockResultSet).getInt(1);
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Quota quota = jdbcConn.getQuota("athenz");
    assertNull(quota);
    jdbcConn.close();
}
Also used : Quota(com.yahoo.athenz.zms.Quota) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 9 with Quota

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

the class JDBCConnectionTest method testUpdateQuotaInvalidDomain.

@Test
public void testUpdateQuotaInvalidDomain() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Quota quota = new Quota().setName("athenz").setAssertion(10).setEntity(11).setPolicy(12).setPublicKey(13).setRole(14).setRoleMember(15).setService(16).setServiceHost(17).setSubdomain(18);
    Mockito.when(mockResultSet.next()).thenReturn(false);
    try {
        jdbcConn.updateQuota("athenz", quota);
        fail();
    } catch (ResourceException ex) {
        assertEquals(404, ex.getCode());
    }
    jdbcConn.close();
}
Also used : Quota(com.yahoo.athenz.zms.Quota) ResourceException(com.yahoo.athenz.zms.ResourceException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 10 with Quota

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

the class JDBCConnectionTest method testGetQuota.

@Test
public void testGetQuota() throws Exception {
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // domain id
    Mockito.doReturn(7).when(mockResultSet).getInt(1);
    Mockito.doReturn(10).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_ASSERTION);
    Mockito.doReturn(11).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_ROLE);
    Mockito.doReturn(12).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_ROLE_MEMBER);
    Mockito.doReturn(13).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_POLICY);
    Mockito.doReturn(14).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_SERVICE);
    Mockito.doReturn(15).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_SERVICE_HOST);
    Mockito.doReturn(16).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_PUBLIC_KEY);
    Mockito.doReturn(17).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_ENTITY);
    Mockito.doReturn(18).when(mockResultSet).getInt(ZMSConsts.DB_COLUMN_SUBDOMAIN);
    Mockito.doReturn(new java.sql.Timestamp(1454358916)).when(mockResultSet).getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED);
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Quota quota = jdbcConn.getQuota("athenz");
    assertNotNull(quota);
    assertEquals(quota.getAssertion(), 10);
    assertEquals(quota.getRole(), 11);
    assertEquals(quota.getRoleMember(), 12);
    assertEquals(quota.getPolicy(), 13);
    assertEquals(quota.getService(), 14);
    assertEquals(quota.getServiceHost(), 15);
    assertEquals(quota.getPublicKey(), 16);
    assertEquals(quota.getEntity(), 17);
    assertEquals(quota.getSubdomain(), 18);
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "athenz");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 7);
    jdbcConn.close();
}
Also used : Quota(com.yahoo.athenz.zms.Quota) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Aggregations

Quota (com.yahoo.athenz.zms.Quota)10 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)8 Test (org.testng.annotations.Test)8 ResourceException (com.yahoo.athenz.zms.ResourceException)4 SQLException (java.sql.SQLException)3 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1