use of com.yahoo.athenz.zms.Membership in project athenz by yahoo.
the class JDBCConnectionTest method testGetRoleMemberNo.
@Test
public void testGetRoleMemberNo() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
Mockito.when(mockResultSet.next()).thenReturn(// domain id
true).thenReturn(// rold id
true).thenReturn(// not a member
false);
Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
5).thenReturn(// role id
7);
Membership membership = jdbcConn.getRoleMember("my-domain", "role1", "user.user1");
Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
Mockito.verify(mockPrepStmt, times(1)).setString(2, "role1");
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 7);
Mockito.verify(mockPrepStmt, times(1)).setString(2, "user.user1");
assertEquals(membership.getMemberName(), "user.user1");
assertEquals(membership.getRoleName(), "my-domain:role.role1");
assertFalse(membership.getIsMember());
jdbcConn.close();
}
use of com.yahoo.athenz.zms.Membership in project athenz by yahoo.
the class JDBCConnectionTest method testGetRoleMemberYes.
@Test
public void testGetRoleMemberYes() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
// yes a member
Mockito.when(mockResultSet.next()).thenReturn(true);
Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
5).thenReturn(// role id
7);
Membership membership = jdbcConn.getRoleMember("my-domain", "role1", "user.user1");
Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
Mockito.verify(mockPrepStmt, times(1)).setString(2, "role1");
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 7);
Mockito.verify(mockPrepStmt, times(1)).setString(2, "user.user1");
assertEquals(membership.getMemberName(), "user.user1");
assertEquals(membership.getRoleName(), "my-domain:role.role1");
assertTrue(membership.getIsMember());
jdbcConn.close();
}
use of com.yahoo.athenz.zms.Membership in project athenz by yahoo.
the class FileConnection method getRoleMember.
@Override
public Membership getRoleMember(String domainName, String roleName, String principal) {
DomainStruct domainStruct = getDomainStruct(domainName);
if (domainStruct == null) {
throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain not found", "getRoleMember");
}
Role role = getRoleObject(domainStruct, roleName);
if (role == null) {
throw ZMSUtils.error(ResourceException.NOT_FOUND, "role not found", "getRoleMember");
}
Membership membership = new Membership().setMemberName(principal).setRoleName(ZMSUtils.roleResourceName(domainName, roleName)).setIsMember(false);
if (role.getRoleMembers() != null) {
Set<RoleMember> members = new HashSet<>(role.getRoleMembers());
for (RoleMember member : members) {
if (member.getMemberName().equalsIgnoreCase(principal)) {
membership.setIsMember(true);
membership.setExpiration(member.getExpiration());
break;
}
}
}
return membership;
}
use of com.yahoo.athenz.zms.Membership in project athenz by yahoo.
the class JDBCConnection method getRoleMember.
@Override
public Membership getRoleMember(String domainName, String roleName, String member) {
final String caller = "getRoleMember";
int domainId = getDomainId(domainName);
if (domainId == 0) {
throw notFoundError(caller, ZMSConsts.OBJECT_DOMAIN, domainName);
}
int roleId = getRoleId(domainId, roleName);
if (roleId == 0) {
throw notFoundError(caller, ZMSConsts.OBJECT_ROLE, ZMSUtils.roleResourceName(domainName, roleName));
}
Membership membership = new Membership().setMemberName(member).setRoleName(ZMSUtils.roleResourceName(domainName, roleName)).setIsMember(false);
try (PreparedStatement ps = con.prepareStatement(SQL_GET_ROLE_MEMBER)) {
ps.setInt(1, roleId);
ps.setString(2, member);
try (ResultSet rs = executeQuery(ps, caller)) {
if (rs.next()) {
membership.setIsMember(true);
java.sql.Timestamp expiration = rs.getTimestamp(2);
if (expiration != null) {
membership.setExpiration(Timestamp.fromMillis(expiration.getTime()));
}
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
return membership;
}
Aggregations