Search in sources :

Example 1 with Entity

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

the class JDBCConnectionTest method testGetEntityNotFound.

@Test
public void testGetEntityNotFound() throws Exception {
    Mockito.when(mockResultSet.next()).thenReturn(// for domain id
    true).thenReturn(false);
    Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
    5);
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Entity entity = jdbcConn.getEntity("my-domain", "entity1");
    assertNull(entity);
    jdbcConn.close();
}
Also used : Entity(com.yahoo.athenz.zms.Entity) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 2 with Entity

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

the class JDBCConnectionTest method testInsertEntityException.

@Test
public void testInsertEntityException() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Entity entity = new Entity().setName("entity1").setValue(JSON.fromString("{\"value\":1}", Struct.class));
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.doReturn(5).when(mockResultSet).getInt(1);
    Mockito.when(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
    try {
        jdbcConn.insertEntity("my-domain", entity);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : Entity(com.yahoo.athenz.zms.Entity) SQLException(java.sql.SQLException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Struct(com.yahoo.rdl.Struct) Test(org.testng.annotations.Test)

Example 3 with Entity

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

the class JDBCConnectionTest method testUpdateEntity.

@Test
public void testUpdateEntity() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Entity entity = new Entity().setName("entity1").setValue(JSON.fromString("{\"value\":1}", Struct.class));
    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.updateEntity("my-domain", entity);
    assertTrue(requestSuccess);
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "{\"value\":1}");
    Mockito.verify(mockPrepStmt, times(1)).setInt(2, 5);
    Mockito.verify(mockPrepStmt, times(1)).setString(3, "entity1");
    jdbcConn.close();
}
Also used : Entity(com.yahoo.athenz.zms.Entity) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Struct(com.yahoo.rdl.Struct) Test(org.testng.annotations.Test)

Example 4 with Entity

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

the class JDBCConnection method getEntity.

@Override
public Entity getEntity(String domainName, String entityName) {
    final String caller = "getEntity";
    int domainId = getDomainId(domainName);
    if (domainId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_DOMAIN, domainName);
    }
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_ENTITY)) {
        ps.setInt(1, domainId);
        ps.setString(2, entityName);
        try (ResultSet rs = executeQuery(ps, caller)) {
            if (rs.next()) {
                Entity entity = new Entity().setName(entityName).setValue(JSON.fromString(rs.getString(ZMSConsts.DB_COLUMN_VALUE), Struct.class));
                return entity;
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    return null;
}
Also used : Entity(com.yahoo.athenz.zms.Entity) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Struct(com.yahoo.rdl.Struct)

Example 5 with Entity

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

the class JDBCConnectionTest method testUpdateEntityException.

@Test
public void testUpdateEntityException() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Entity entity = new Entity().setName("entity1").setValue(JSON.fromString("{\"value\":1}", Struct.class));
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.doReturn(5).when(mockResultSet).getInt(1);
    Mockito.when(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
    try {
        jdbcConn.updateEntity("my-domain", entity);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : Entity(com.yahoo.athenz.zms.Entity) SQLException(java.sql.SQLException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Struct(com.yahoo.rdl.Struct) Test(org.testng.annotations.Test)

Aggregations

Entity (com.yahoo.athenz.zms.Entity)9 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)8 Test (org.testng.annotations.Test)8 Struct (com.yahoo.rdl.Struct)7 SQLException (java.sql.SQLException)5 ResourceException (com.yahoo.athenz.zms.ResourceException)4 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1