Search in sources :

Example 26 with ObjectStoreConnection

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");
}
Also used : ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Example 27 with ObjectStoreConnection

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;
}
Also used : ObjectStore(com.yahoo.athenz.zms.store.ObjectStore) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) AthenzDomain(com.yahoo.athenz.zms.store.AthenzDomain) Test(org.testng.annotations.Test)

Example 28 with ObjectStoreConnection

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;
}
Also used : ObjectStore(com.yahoo.athenz.zms.store.ObjectStore) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Example 29 with ObjectStoreConnection

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);
}
Also used : ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Example 30 with ObjectStoreConnection

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());
}
Also used : ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Aggregations

ObjectStoreConnection (com.yahoo.athenz.zms.store.ObjectStoreConnection)173 Test (org.testng.annotations.Test)96 ObjectStore (com.yahoo.athenz.zms.store.ObjectStore)38 AthenzDomain (com.yahoo.athenz.zms.store.AthenzDomain)34 Authority (com.yahoo.athenz.auth.Authority)23 Timestamp (com.yahoo.rdl.Timestamp)17 ArrayList (java.util.ArrayList)16 MemberDueDays (com.yahoo.athenz.zms.config.MemberDueDays)11 Principal (com.yahoo.athenz.auth.Principal)7 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)7 EmbeddedMysql (com.wix.mysql.EmbeddedMysql)5 FilePrivateKeyStore (com.yahoo.athenz.auth.impl.FilePrivateKeyStore)5 Crypto (com.yahoo.athenz.auth.util.Crypto)5 AuditReferenceValidator (com.yahoo.athenz.common.server.audit.AuditReferenceValidator)5 NotificationManager (com.yahoo.athenz.common.server.notification.NotificationManager)5 ResourceUtils (com.yahoo.athenz.common.server.util.ResourceUtils)5 DataCache (com.yahoo.athenz.zms.DBService.DataCache)5 MockAuditReferenceValidatorImpl (com.yahoo.athenz.zms.audit.MockAuditReferenceValidatorImpl)5 JDBCConnection (com.yahoo.athenz.zms.store.impl.jdbc.JDBCConnection)5 ZMSUtils (com.yahoo.athenz.zms.utils.ZMSUtils)5