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();
}
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();
}
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();
}
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;
}
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();
}
Aggregations