Search in sources :

Example 6 with Sponsorship

use of cz.metacentrum.perun.core.api.Sponsorship in project perun by CESNET.

the class ExpirationNotifSchedulerTest method testSponsorshipDoesntExpireInFuture.

@Test
public void testSponsorshipDoesntExpireInFuture() throws Exception {
    System.out.println(CLASS_NAME + "testSponsorshipDoesntExpireInFuture");
    LocalDate today = LocalDate.of(2020, 2, 2);
    when(spyScheduler.getCurrentLocalDate()).thenReturn(today);
    Member member = setUpMember();
    User sponsor = perun.getUsersManagerBl().getUserByMember(session, setUpMember());
    AuthzResolverBlImpl.setRole(session, sponsor, vo, Role.SPONSOR);
    LocalDate nextYear = today.plusYears(1);
    perun.getMembersManagerBl().setSponsorshipForMember(session, member, sponsor, nextYear);
    ReflectionTestUtils.invokeMethod(spyScheduler, "expireSponsorships");
    Sponsorship sponsorship = perun.getMembersManagerBl().getSponsorship(session, member, sponsor);
    assertThat(sponsorship.isActive());
}
Also used : Sponsorship(cz.metacentrum.perun.core.api.Sponsorship) EnrichedSponsorship(cz.metacentrum.perun.core.api.EnrichedSponsorship) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) LocalDate(java.time.LocalDate) Member(cz.metacentrum.perun.core.api.Member) Test(org.junit.Test)

Example 7 with Sponsorship

use of cz.metacentrum.perun.core.api.Sponsorship in project perun by CESNET.

the class MembersManagerEntryIntegrationTest method updateSponsorshipValidity.

@Test
public void updateSponsorshipValidity() throws Exception {
    System.out.println(CLASS_NAME + "updateSponsorshipValidity");
    Member member = setUpMember(createdVo);
    User sponsor = perun.getUsersManagerBl().getUserByMember(sess, setUpSponsor(createdVo));
    AuthzResolverBlImpl.setRole(sess, sponsor, createdVo, Role.SPONSOR);
    membersManagerEntry.setSponsorshipForMember(sess, member, sponsor, null);
    LocalDate validity = LocalDate.now().plusMonths(1);
    membersManagerEntry.updateSponsorshipValidity(sess, member, sponsor, validity);
    Sponsorship sponsorship = perun.getMembersManagerBl().getSponsorship(sess, member, sponsor);
    assertThat(sponsorship.getValidityTo()).isEqualTo(validity);
}
Also used : Sponsorship(cz.metacentrum.perun.core.api.Sponsorship) User(cz.metacentrum.perun.core.api.User) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) LocalDate(java.time.LocalDate) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 8 with Sponsorship

use of cz.metacentrum.perun.core.api.Sponsorship in project perun by CESNET.

the class MembersManagerBlImpl method moveMembersSponsorships.

/**
 * Moves sponsorships in VO from source user to target user - moves sponsorships
 * where the source user is sponsor or where the source member is sponsored.
 *
 * @param sess
 * @param vo VO to move sponsorships in
 * @param sourceUser user to move sponsorships from
 * @param targetUser user to move sponsorships to
 * @param sourceMember member of the source user in the VO
 * @param targetMember member of the target user in the VO
 */
private void moveMembersSponsorships(PerunSession sess, Vo vo, User sourceUser, User targetUser, Member sourceMember, Member targetMember) {
    // move sponsorships where sourceUser is sponsor
    List<Member> sponsoredMembers = this.getSponsoredMembers(sess, vo, sourceUser);
    for (Member sponsoredMember : sponsoredMembers) {
        try {
            Sponsorship sponsorship = this.getSponsorship(sess, sponsoredMember, sourceUser);
            // if target user isn't in role SPONSOR for the VO, assign the role to him
            if (!getPerunBl().getVosManagerBl().isUserInRoleForVo(sess, targetUser, Role.SPONSOR, vo, true)) {
                AuthzResolverBlImpl.setRole(sess, targetUser, vo, Role.SPONSOR);
            }
            // first add sponsorship to targetUser
            this.sponsorMember(sess, sponsoredMember, targetUser, sponsorship.getValidityTo());
            // then remove sponsorship from sourceUser
            this.removeSponsor(sess, sponsoredMember, sourceUser);
        } catch (SponsorshipDoesNotExistException | MemberNotSponsoredException e) {
            throw new ConsistencyErrorException(e);
        } catch (AlreadySponsorException e) {
            log.warn("When moving sponsorships from sponsor {} to sponsor {}, the sponsor already sponsored " + "member {}. Exception: {}", sourceUser, targetUser, sponsoredMember, e);
        } catch (UserNotInRoleException | AlreadyAdminException | RoleCannotBeManagedException e) {
            throw new InternalErrorException(e);
        }
    }
    // move sponsorships where sourceMember is sponsored
    if (sourceMember.isSponsored()) {
        List<User> sponsors = getPerunBl().getUsersManagerBl().getSponsors(sess, sourceMember);
        try {
            for (int i = 0; i < sponsors.size(); i++) {
                User sponsor = sponsors.get(i);
                Sponsorship sponsorship = this.getSponsorship(sess, sourceMember, sponsor);
                // because the member needs to be marked as sponsored first
                if (i == 0) {
                    this.setSponsorshipForMember(sess, targetMember, sponsor, sponsorship.getValidityTo());
                } else {
                    this.sponsorMember(sess, targetMember, sponsor, sponsorship.getValidityTo());
                }
            }
        } catch (SponsorshipDoesNotExistException e) {
            throw new ConsistencyErrorException(e);
        } catch (AlreadySponsoredMemberException | MemberNotSponsoredException | AlreadySponsorException | UserNotInRoleException e) {
            throw new InternalErrorException(e);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) AlreadySponsoredMemberException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) MemberNotSponsoredException(cz.metacentrum.perun.core.api.exceptions.MemberNotSponsoredException) AlreadyAdminException(cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException) UserNotInRoleException(cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException) Sponsorship(cz.metacentrum.perun.core.api.Sponsorship) AlreadySponsorException(cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException) RoleCannotBeManagedException(cz.metacentrum.perun.core.api.exceptions.RoleCannotBeManagedException) SponsorshipDoesNotExistException(cz.metacentrum.perun.core.api.exceptions.SponsorshipDoesNotExistException) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member)

Example 9 with Sponsorship

use of cz.metacentrum.perun.core.api.Sponsorship in project perun by CESNET.

the class MembersManagerEntryIntegrationTest method getSponsorshipsExpiringInRangeDoesntReturnExpiringAfterRange.

@Test
public void getSponsorshipsExpiringInRangeDoesntReturnExpiringAfterRange() throws Exception {
    System.out.println(CLASS_NAME + "getSponsorshipsExpiringInRangeDoesntReturnExpiringAfterRange");
    Member member = setUpMember(createdVo);
    User sponsor1 = perun.getUsersManagerBl().getUserByMember(sess, setUpSponsor(createdVo));
    User sponsor2 = perun.getUsersManagerBl().getUserByMember(sess, setUpSponsor2(createdVo));
    AuthzResolverBlImpl.setRole(sess, sponsor1, createdVo, Role.SPONSOR);
    AuthzResolverBlImpl.setRole(sess, sponsor2, createdVo, Role.SPONSOR);
    LocalDate today = LocalDate.of(2020, 2, 2);
    LocalDate nextDay = today.plusDays(1);
    LocalDate nextMonth = today.plusMonths(1);
    membersManagerEntry.setSponsorshipForMember(sess, member, sponsor1, nextDay);
    membersManagerEntry.sponsorMember(sess, member, sponsor2, nextMonth);
    LocalDate nextWeek = today.plusDays(7);
    List<Sponsorship> sponsorships = perun.getMembersManagerBl().getSponsorshipsExpiringInRange(sess, today, nextWeek);
    assertThat(sponsorships).hasSize(1);
    assertThat(sponsorships.get(0).getValidityTo()).isEqualTo(nextDay);
}
Also used : Sponsorship(cz.metacentrum.perun.core.api.Sponsorship) User(cz.metacentrum.perun.core.api.User) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) LocalDate(java.time.LocalDate) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 10 with Sponsorship

use of cz.metacentrum.perun.core.api.Sponsorship in project perun by CESNET.

the class MembersManagerEntryIntegrationTest method getSoonExpiringSponsorshipsReturnsMultipleSponsorships.

@Test
public void getSoonExpiringSponsorshipsReturnsMultipleSponsorships() throws Exception {
    System.out.println(CLASS_NAME + "getSoonExpiringSponsorshipsReturnsMultipleSponsorships");
    Member member = setUpMember(createdVo);
    User sponsor1 = perun.getUsersManagerBl().getUserByMember(sess, setUpSponsor(createdVo));
    User sponsor2 = perun.getUsersManagerBl().getUserByMember(sess, setUpSponsor2(createdVo));
    AuthzResolverBlImpl.setRole(sess, sponsor1, createdVo, Role.SPONSOR);
    AuthzResolverBlImpl.setRole(sess, sponsor2, createdVo, Role.SPONSOR);
    LocalDate today = LocalDate.of(2020, 2, 2);
    LocalDate nextDay = today.plusDays(1);
    LocalDate nextMonth = today.plusMonths(1);
    membersManagerEntry.setSponsorshipForMember(sess, member, sponsor1, nextDay);
    membersManagerEntry.sponsorMember(sess, member, sponsor2, nextMonth);
    LocalDate nextYear = today.plusYears(1);
    List<Sponsorship> sponsorships = perun.getMembersManagerBl().getSponsorshipsExpiringInRange(sess, today, nextYear);
    assertThat(sponsorships).hasSize(2);
}
Also used : Sponsorship(cz.metacentrum.perun.core.api.Sponsorship) User(cz.metacentrum.perun.core.api.User) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) LocalDate(java.time.LocalDate) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Aggregations

Sponsorship (cz.metacentrum.perun.core.api.Sponsorship)14 Member (cz.metacentrum.perun.core.api.Member)13 User (cz.metacentrum.perun.core.api.User)13 LocalDate (java.time.LocalDate)12 Test (org.junit.Test)12 RichMember (cz.metacentrum.perun.core.api.RichMember)10 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)9 RichUser (cz.metacentrum.perun.core.api.RichUser)5 SponsorshipDoesNotExistException (cz.metacentrum.perun.core.api.exceptions.SponsorshipDoesNotExistException)4 EnrichedSponsorship (cz.metacentrum.perun.core.api.EnrichedSponsorship)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 Sponsor (cz.metacentrum.perun.core.api.Sponsor)2 AlreadySponsorException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsorException)2 AlreadySponsoredMemberException (cz.metacentrum.perun.core.api.exceptions.AlreadySponsoredMemberException)2 UserNotInRoleException (cz.metacentrum.perun.core.api.exceptions.UserNotInRoleException)2 Attribute (cz.metacentrum.perun.core.api.Attribute)1 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)1 AttributesManager (cz.metacentrum.perun.core.api.AttributesManager)1 BanOnResource (cz.metacentrum.perun.core.api.BanOnResource)1 BanOnVo (cz.metacentrum.perun.core.api.BanOnVo)1