Search in sources :

Example 1 with Quota

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

the class FileConnection method getQuota.

@Override
public Quota getQuota(String domainName) {
    File f = new File(quotaDir, domainName);
    if (!f.exists()) {
        return null;
    }
    Quota quota = null;
    try {
        Path path = Paths.get(f.toURI());
        quota = JSON.fromBytes(Files.readAllBytes(path), Quota.class);
    } catch (IOException e) {
    }
    return quota;
}
Also used : Path(java.nio.file.Path) Quota(com.yahoo.athenz.zms.Quota) IOException(java.io.IOException) File(java.io.File)

Example 2 with Quota

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

the class JDBCConnectionTest method testUpdateQuotaException.

@Test
public void testUpdateQuotaException() 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(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.doReturn(5).when(mockResultSet).getInt(1);
    try {
        jdbcConn.updateQuota("athenz", quota);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : Quota(com.yahoo.athenz.zms.Quota) SQLException(java.sql.SQLException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Test(org.testng.annotations.Test)

Example 3 with Quota

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

the class JDBCConnectionTest method testInsertQuotaException.

@Test
public void testInsertQuotaException() 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(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.doReturn(5).when(mockResultSet).getInt(1);
    try {
        jdbcConn.insertQuota("athenz", quota);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : Quota(com.yahoo.athenz.zms.Quota) SQLException(java.sql.SQLException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Test(org.testng.annotations.Test)

Example 4 with Quota

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

the class JDBCConnectionTest method testInsertQuota.

@Test
public void testInsertQuota() 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.insertQuota("athenz", quota);
    assertTrue(requestSuccess);
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "athenz");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
    Mockito.verify(mockPrepStmt, times(1)).setInt(2, 14);
    Mockito.verify(mockPrepStmt, times(1)).setInt(3, 15);
    Mockito.verify(mockPrepStmt, times(1)).setInt(4, 12);
    Mockito.verify(mockPrepStmt, times(1)).setInt(5, 10);
    Mockito.verify(mockPrepStmt, times(1)).setInt(6, 16);
    Mockito.verify(mockPrepStmt, times(1)).setInt(7, 17);
    Mockito.verify(mockPrepStmt, times(1)).setInt(8, 13);
    Mockito.verify(mockPrepStmt, times(1)).setInt(9, 11);
    Mockito.verify(mockPrepStmt, times(1)).setInt(10, 18);
    jdbcConn.close();
}
Also used : Quota(com.yahoo.athenz.zms.Quota) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 5 with Quota

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

the class JDBCConnectionTest method testInsertQuotaInvalidDomain.

@Test
public void testInsertQuotaInvalidDomain() 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.insertQuota("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)

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