use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.
the class JDBCConnectionTest method testGetServiceIdentityAllFields.
@Test
public void testGetServiceIdentityAllFields() throws Exception {
Mockito.when(mockResultSet.next()).thenReturn(true);
Mockito.doReturn("test description").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_DESCRIPTION);
Mockito.doReturn(new java.sql.Timestamp(1454358916)).when(mockResultSet).getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED);
Mockito.doReturn("/usr/bin64/athenz").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_EXECTUABLE);
Mockito.doReturn("users").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_SVC_GROUP);
Mockito.doReturn("root").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_SVC_USER);
Mockito.doReturn("http://server.athenzcompany.com").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_PROVIDER_ENDPOINT);
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
ServiceIdentity service = jdbcConn.getServiceIdentity("my-domain", "service1");
assertNotNull(service);
assertEquals("my-domain.service1", service.getName());
assertEquals("/usr/bin64/athenz", service.getExecutable());
assertEquals("users", service.getGroup());
assertEquals("root", service.getUser());
assertEquals("http://server.athenzcompany.com", service.getProviderEndpoint().toString());
Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
Mockito.verify(mockPrepStmt, times(1)).setString(2, "service1");
jdbcConn.close();
}
use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.
the class JDBCConnectionTest method testInsertServiceIdentity.
@Test
public void testInsertServiceIdentity() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
ServiceIdentity service = new ServiceIdentity().setName("my-domain.service1");
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
Mockito.when(mockResultSet.next()).thenReturn(true);
// return domain id
Mockito.when(mockResultSet.getInt(1)).thenReturn(5);
boolean requestSuccess = jdbcConn.insertServiceIdentity("my-domain", service);
assertTrue(requestSuccess);
// get domain id
Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
// update service
Mockito.verify(mockPrepStmt, times(1)).setString(1, "service1");
Mockito.verify(mockPrepStmt, times(1)).setString(2, "");
Mockito.verify(mockPrepStmt, times(1)).setString(3, "");
Mockito.verify(mockPrepStmt, times(1)).setString(4, "");
Mockito.verify(mockPrepStmt, times(1)).setString(5, "");
jdbcConn.close();
}
use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.
the class JDBCConnectionTest method testUpdateServiceIdentityException.
@Test
public void testUpdateServiceIdentityException() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
ServiceIdentity service = new ServiceIdentity().setName("my-domain.service1");
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
Mockito.when(mockResultSet.next()).thenReturn(true);
// return domain id
Mockito.when(mockResultSet.getInt(1)).thenReturn(5).thenReturn(// service id
4);
Mockito.when(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
try {
jdbcConn.updateServiceIdentity("my-domain", service);
fail();
} catch (Exception ex) {
assertTrue(true);
}
jdbcConn.close();
}
use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.
the class JDBCConnectionTest method testInsertServiceIdentityInvalidName.
@Test
public void testInsertServiceIdentityInvalidName() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
ServiceIdentity service = new ServiceIdentity().setName("service1");
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
Mockito.when(mockResultSet.next()).thenReturn(true);
// return domain id
Mockito.when(mockResultSet.getInt(1)).thenReturn(5).thenReturn(// service id
4);
try {
jdbcConn.insertServiceIdentity("my-domain", service);
fail();
} catch (ResourceException ex) {
assertEquals(400, ex.getCode());
}
jdbcConn.close();
}
use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.
the class JDBCConnection method getAthenzDomainServices.
void getAthenzDomainServices(String domainName, int domainId, AthenzDomain athenzDomain, String caller) {
Map<String, ServiceIdentity> serviceMap = new HashMap<>();
try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES)) {
ps.setInt(1, domainId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
String serviceName = rs.getString(ZMSConsts.DB_COLUMN_NAME);
ServiceIdentity service = new ServiceIdentity().setName(ZMSUtils.serviceResourceName(domainName, serviceName)).setProviderEndpoint(saveValue(rs.getString(ZMSConsts.DB_COLUMN_PROVIDER_ENDPOINT))).setExecutable(saveValue(rs.getString(ZMSConsts.DB_COLUMN_EXECTUABLE))).setUser(saveValue(rs.getString(ZMSConsts.DB_COLUMN_SVC_USER))).setGroup(saveValue(rs.getString(ZMSConsts.DB_COLUMN_SVC_GROUP))).setModified(Timestamp.fromMillis(rs.getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED).getTime()));
List<PublicKeyEntry> publicKeys = new ArrayList<>();
service.setPublicKeys(publicKeys);
serviceMap.put(serviceName, service);
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES_HOSTS)) {
ps.setInt(1, domainId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
String serviceName = rs.getString(1);
ServiceIdentity service = serviceMap.get(serviceName);
if (service == null) {
continue;
}
List<String> hosts = service.getHosts();
if (hosts == null) {
hosts = new ArrayList<>();
service.setHosts(hosts);
}
hosts.add(rs.getString(2));
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES_PUBLIC_KEYS)) {
ps.setInt(1, domainId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
String serviceName = rs.getString(1);
ServiceIdentity service = serviceMap.get(serviceName);
if (service == null) {
continue;
}
PublicKeyEntry publicKey = new PublicKeyEntry().setId(rs.getString(ZMSConsts.DB_COLUMN_KEY_ID)).setKey(rs.getString(ZMSConsts.DB_COLUMN_KEY_VALUE));
service.getPublicKeys().add(publicKey);
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
athenzDomain.getServices().addAll(serviceMap.values());
}
Aggregations