use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testRemovePrincipalFromAllRolesDeleteUserException.
@Test
public void testRemovePrincipalFromAllRolesDeleteUserException() {
DomainRoleMember roles = new DomainRoleMember();
roles.setMemberRoles(new ArrayList<>());
MemberRole role1 = new MemberRole();
role1.setDomainName("dom1");
role1.setRoleName("role1");
roles.getMemberRoles().add(role1);
MemberRole role2 = new MemberRole();
role2.setDomainName("dom1");
role2.setRoleName("role2");
roles.getMemberRoles().add(role2);
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
Mockito.when(conn.getPrincipalRoles("user.joe", null)).thenReturn(roles);
Mockito.when(conn.deleteRoleMember("dom1", "role1", "user.joe", adminUser, "unittest")).thenReturn(true);
Mockito.when(conn.deleteRoleMember("dom1", "role2", "user.joe", adminUser, "unittest")).thenThrow(new ResourceException(501));
// we should handle the exception without any errors
zms.dbService.removePrincipalFromAllRoles(mockDomRsrcCtx, conn, "user.joe", adminUser, "unittest");
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testExecutePutAssertionCondition.
@Test
public void testExecutePutAssertionCondition() {
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
String domain = "assertion-condition-dom";
String policy = "assertion-condition-pol";
ObjectStore savedStore = zms.dbService.store;
zms.dbService.store = mockObjStore;
Domain dom = new Domain().setName(domain);
Mockito.when(mockObjStore.getConnection(false, true)).thenReturn(conn);
Mockito.when(conn.getDomain(anyString())).thenReturn(dom);
Mockito.when(conn.getNextConditionId(anyLong(), anyString())).thenReturn(1);
Map<String, AssertionConditionData> m1 = new HashMap<>();
AssertionConditionData cd11 = new AssertionConditionData().setOperator(AssertionConditionOperator.EQUALS).setValue("host1");
m1.put("instances", cd11);
AssertionConditionData cd12 = new AssertionConditionData().setOperator(AssertionConditionOperator.EQUALS).setValue("ENFORCE");
m1.put("enforcementState", cd12);
AssertionCondition c1 = new AssertionCondition().setConditionsMap(m1);
Mockito.when(conn.insertAssertionCondition(1, c1)).thenReturn(// no condition id in DB. insert works
true).thenReturn(// no condition id in DB. insert fails
false).thenReturn(// condition id in DB. insert works
true).thenReturn(// condition id in DB. insert fails
false);
// no condition id in the request. insertion is successful
try {
zms.dbService.executePutAssertionCondition(mockDomRsrcCtx, domain, policy, 1L, c1, auditRef, "PutAssertionCondition");
} catch (ResourceException ignored) {
fail();
}
// no condition id in the request. insertion failed
c1.setId(null);
try {
zms.dbService.executePutAssertionCondition(mockDomRsrcCtx, domain, policy, 1L, c1, auditRef, "PutAssertionCondition");
fail();
} catch (ResourceException re) {
assertEquals(re.getCode(), ResourceException.BAD_REQUEST);
}
// condition id found in request
Mockito.when(conn.deleteAssertionCondition(1, 1)).thenReturn(// delete works
true).thenReturn(// delete fails
false).thenReturn(true).thenThrow(new ResourceException(ResourceException.CONFLICT));
c1.setId(1);
try {
zms.dbService.executePutAssertionCondition(mockDomRsrcCtx, domain, policy, 1L, c1, auditRef, "PutAssertionCondition");
} catch (ResourceException ignored) {
fail();
}
c1.setId(1);
try {
zms.dbService.executePutAssertionCondition(mockDomRsrcCtx, domain, policy, 1L, c1, auditRef, "PutAssertionCondition");
fail();
} catch (ResourceException re) {
assertEquals(re.getCode(), ResourceException.NOT_FOUND);
}
try {
zms.dbService.executePutAssertionCondition(mockDomRsrcCtx, domain, policy, 1L, c1, auditRef, "PutAssertionCondition");
fail();
} catch (ResourceException re) {
assertEquals(re.getCode(), ResourceException.BAD_REQUEST);
}
// retry test
int savedRetryCount = zms.dbService.defaultRetryCount;
zms.dbService.defaultRetryCount = 2;
try {
zms.dbService.executePutAssertionCondition(mockDomRsrcCtx, domain, policy, 1L, c1, auditRef, "PutAssertionCondition");
fail();
} catch (ResourceException re) {
assertEquals(re.getCode(), ResourceException.CONFLICT);
}
zms.dbService.defaultRetryCount = savedRetryCount;
zms.dbService.store = savedStore;
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testGetGroupExpiryMembersFailure.
@Test
public void testGetGroupExpiryMembersFailure() {
ObjectStore saveStore = zms.dbService.store;
zms.dbService.store = mockObjStore;
ObjectStoreConnection mockConn = Mockito.mock(ObjectStoreConnection.class);
Mockito.when(mockObjStore.getConnection(true, true)).thenReturn(mockConn);
Mockito.when(mockConn.updateGroupMemberExpirationNotificationTimestamp(anyString(), anyLong(), anyInt())).thenReturn(false);
assertNull(zms.dbService.getGroupExpiryMembers(1));
zms.dbService.store = saveStore;
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testUpdateGroupMembersDueDateFailures.
@Test
public void testUpdateGroupMembersDueDateFailures() {
final String domainName = "group-meta-duedate";
Group originalGroup = createGroupObject(domainName, "group1", "user.john", "user.jane");
originalGroup.setMemberExpiryDays(10);
Group updateGroup = createGroupObject(domainName, "group1", "user.john", "user.jane");
updateGroup.setMemberExpiryDays(5);
ObjectStoreConnection mockConn = Mockito.mock(ObjectStoreConnection.class);
Mockito.when(mockConn.insertGroupMember(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyString())).thenReturn(false).thenThrow(new IllegalArgumentException());
// we're going to make sure to throw an exception here
// since this should never be called
Mockito.when(mockConn.updateDomainModTimestamp(domainName)).thenThrow(new IllegalArgumentException());
zms.dbService.updateGroupMembersDueDates(mockDomRsrcCtx, mockConn, domainName, "group1", originalGroup, updateGroup, auditRef);
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testProcessRoleUpdate.
@Test
public void testProcessRoleUpdate() {
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
Role originalRole = new Role().setName("originalRole").setAuditEnabled(false);
Role role = new Role().setName("newRole").setAuditEnabled(true);
StringBuilder auditDetails = new StringBuilder("testAudit");
zms.dbService.processRole(conn, originalRole, "auditedDomain", "newRole", role, adminUser, auditRef, false, auditDetails);
// original role does not have auditEnabled
assertFalse(role.getAuditEnabled());
originalRole.setAuditEnabled(true);
Role role2 = new Role().setName("newRole2").setAuditEnabled(false);
zms.dbService.processRole(conn, originalRole, "auditedDomain", "newRole2", role2, adminUser, auditRef, false, auditDetails);
// original role has auditEnabled
assertTrue(role2.getAuditEnabled());
Role role3 = new Role().setName("newRole3").setAuditEnabled(false).setSelfServe(true);
zms.dbService.processRole(conn, originalRole, "auditedDomain", "newRole3", role3, adminUser, auditRef, false, auditDetails);
assertTrue(role3.getSelfServe());
Role role4 = new Role().setName("newRole4").setAuditEnabled(false).setSelfServe(false);
zms.dbService.processRole(conn, originalRole, "auditedDomain", "newRole4", role4, adminUser, auditRef, false, auditDetails);
assertFalse(role4.getSelfServe());
}
Aggregations