public async Task TestProjectGetHistoryAllocatedBillings()
        {
            var quota = new Domain.ValueObjects.Resources(5, 10, 15);

            lqproject.Quota = quota;

            db.BillingCycleEntries.Add(new BillingCycleEntry(lqproject.BillingCycleSubject));
            await db.SaveChangesAsync();

            var(service, billingTask, _) = CreateService();

            // wait 2 cycles
            await WaitForTaskForExecuteCycles(billingTask, billingConfiguration.CheckCycleMs, 2 * 2);

            var resp = await service.GetHistoryAllocatedBillings(new GetHistoryAllocatedBillingsRequest
            {
                Id          = lqproject.Id.ToString(),
                SubjectType = Protos.Common.SubjectType.Project,
            }, TestContext);

            Assert.Equal(2, resp.Billings.Count);

            var billing = resp.Billings.First();

            Assert.Equal(quota.ToGrpc(), billing.Quota);
            Assert.Equal(PricePlan.Instance.Calculate(quota), (decimal)billing.Amount);
        }
        public async Task TestGetCurrentUsedBillings()
        {
            var resources = new Domain.ValueObjects.Resources(1, 20, 30);

            cjd67project.Resources = resources;
            db.UseCycleEntries.Add(new UseCycleEntry(cjd67project.UseCycleSubject));
            db.UseCycleEntries.Add(new UseCycleEntry(lqproject.UseCycleSubject));
            db.UseCycleEntries.Add(new UseCycleEntry(nju.UseCycleSubject));
            await db.SaveChangesAsync();

            var(service, _, _) = CreateService();


            async Task Validate(UseCycleSubject subject)
            {
                var resp = await service.GetCurrentUsedBilling(new GetCurrentUsedBillingRequest
                {
                    SubjectType = subject.SubjectType.ToGrpc(),
                    Id          = subject.Id.ToString(),
                }, TestContext);

                Assert.Equal(PricePlan.Instance.Calculate(resources), (decimal)resp.Billing.Amount);
                Assert.Equal(subject.Id.ToString(), resp.Billing.SubjectId);
                Assert.Equal(resources.ToGrpc(), resp.Billing.Resources);
            }

            await Validate(cjd67project.UseCycleSubject);
            await Validate(lqproject.UseCycleSubject);
            await Validate(nju.UseCycleSubject);
        }
        public async Task TestUsersGetHistoryAllocatedBillings()
        {
            var quota = new Domain.ValueObjects.Resources(5, 10, 15);

            cjd67project.Quota = quota;

            db.BillingCycleEntries.Add(new BillingCycleEntry(cjd67project.BillingCycleSubject));
            await db.SaveChangesAsync();

            var(service, billingTask, _) = CreateService();

            // wait 1 cycle
            await WaitForTaskForExecuteCycles(billingTask, billingConfiguration.CheckCycleMs, 2);

            var resp = await service.GetHistoryAllocatedBillings(new GetHistoryAllocatedBillingsRequest
            {
                Id          = cjd67project.Id.ToString(),
                SubjectType = Protos.Common.SubjectType.UserProjectAssignment,
            }, TestContext);

            // a allocated billings is issued to user project
            // but the price must be 0
            var billing = Assert.Single(resp.Billings);

            Assert.Equal(0, billing.Amount);
        }
        public async Task TestGetHistoryUsedBillings()
        {
            var usedResources = new Domain.ValueObjects.Resources(5, 10, 15);

            cjd67project.Resources = usedResources;

            db.UseCycleEntries.Add(new UseCycleEntry(cjd67project.UseCycleSubject));
            db.UseCycleEntries.Add(new UseCycleEntry(lqproject.UseCycleSubject));
            await db.SaveChangesAsync();

            var(service, _, useTask) = CreateService();

            await WaitForTaskForExecuteCycles(useTask, useConfiguration.CheckCycleMs, 2);

            var resp = await service.GetHistoryUsedBillings(new GetHistoryUsedBillingsRequest
            {
                Id          = cjd67project.Id.ToString(),
                SubjectType = Protos.Common.SubjectType.UserProjectAssignment
            }, TestContext);

            Assert.Single(resp.Billings);

            var billing = resp.Billings.First();

            Assert.Equal(usedResources.ToGrpc(), billing.Resources);
            Assert.Equal(PricePlan.Instance.Calculate(usedResources), (decimal)billing.Amount);
        }
        public async Task TestGetCurrentAllocatedBilling()
        {
            var quota = new Domain.ValueObjects.Resources(1, 20, 30);

            nju.Quota = quota;
            db.BillingCycleEntries.Add(new BillingCycleEntry(nju.BillingCycleSubject));
            await db.SaveChangesAsync();

            var(service, _, _) = CreateService();

            var resp = await service.GetCurrentAllocatedBilling(new Protos.Billing.GetCurrentAllocatedBillingRequest
            {
                SubjectType = Protos.Common.SubjectType.Domain,
                Id          = nju.Id.ToString(),
            }, TestContext);

            Assert.Equal(PricePlan.Instance.Calculate(quota), (decimal)resp.Billing.Amount);
            Assert.Equal(nju.Id.ToString(), resp.Billing.SubjectId);
            Assert.Equal(quota.ToGrpc(), resp.Billing.Quota);
            Assert.Equal(njuadmin.Id.ToString(), resp.Billing.PayerId);
        }
        public async Task TestChangeProjectUserResourcesOnSocialProject()
        {
            var socialDomain                 = db.Domains.Find(Constants.SocialDomainId);
            var lqsocialproject              = new Project(Guid.NewGuid(), lq, socialDomain, Domain.ValueObjects.Resources.QuotaForSocialProject);
            var lqlqsocialproject            = new UserProjectAssignment(Guid.NewGuid(), lq, lqsocialproject, Domain.ValueObjects.Resources.QuotaForSocialProject);
            var lqlqsocialprojectTokenClaims = new TokenClaims(false, true, lq.Id, Constants.SocialDomainId, lqsocialproject.Id, lqlqsocialproject.Id, UserRole.Admin);

            // set this token as a social project token.
            var initial = new Domain.ValueObjects.Resources(3, 4, 5);

            lqlqsocialproject.Resources = initial.Clone();
            db.UseCycleEntries.Add(new UseCycleEntry(lqlqsocialproject.UseCycleSubject));
            db.UseCycleEntries.Add(new UseCycleEntry(lqsocialproject.UseCycleSubject));
            db.BillingCycleEntries.Add(new BillingCycleEntry(lqlqsocialproject.BillingCycleSubject));
            db.BillingCycleEntries.Add(new BillingCycleEntry(lqsocialproject.BillingCycleSubject));
            await db.SaveChangesAsync();

            var service = CreateService(lqlqsocialprojectTokenClaims);
            // change delta
            var delta = new Domain.ValueObjects.Resources(-3, -4, -5);

            await service.ChangeProjectUserResources(new Protos.Interop.ChangeProjectUserResourcesRequest
            {
                ResourcesDelta = delta.ToGrpc()
            }, TestContext);

            // 1 allocated billing with 0 amount to be allocated to user.
            var billing = Assert.Single(lqlqsocialproject.BillingCycleRecords);

            Assert.Equal(0, billing.Amount);


            // No resources is now being used
            Assert.Equal(Domain.ValueObjects.Resources.Zero, lqsocialproject.Resources);

            // lq should pay the resources price
            // the previous 0 allocated billing should also be included
            AssertIEnumerableIgnoreOrder(new[] { PricePlan.Instance.Calculate(initial), 0 }, lq.PayedUserTransactions.Select(x => x.Amount));
        }
        public async Task TestGetProjectsCurrentUsedBillings()
        {
            var resources = new Domain.ValueObjects.Resources(1, 20, 30);

            cjd67project.Resources = resources;
            db.UseCycleEntries.Add(new UseCycleEntry(lqproject.UseCycleSubject));
            await db.SaveChangesAsync();

            var(service, _, useTask) = CreateService(njuadminnjuTokenClaims);

            await WaitForTaskForExecuteCycles(useTask, useConfiguration.CheckCycleMs, 2);

            var resp = await service.GetCurrentUsedBillings(new GetCurrentUsedBillingsRequest
            {
                SubjectType = Protos.Common.SubjectType.Project
            }, TestContext);

            var billing = Assert.Single(resp.Billings);

            Assert.Equal(PricePlan.Instance.Calculate(resources), (decimal)billing.Amount);
            Assert.Equal(resources.ToGrpc(), billing.Resources);
        }
        public async Task TestGetDomainsCurrentUsedBillings()
        {
            var resources = new Domain.ValueObjects.Resources(1, 20, 30);

            cjd67project.Resources = resources;
            db.UseCycleEntries.Add(new UseCycleEntry(nju.UseCycleSubject));
            await db.SaveChangesAsync();

            var(service, _, useTask) = CreateService(systemTokenClaims);

            await WaitForTaskForExecuteCycles(useTask, useConfiguration.CheckCycleMs, 2);

            var resp = await service.GetCurrentUsedBillings(new GetCurrentUsedBillingsRequest
            {
                SubjectType = Protos.Common.SubjectType.Domain
            }, TestContext);


            AssertIEnumerableIgnoreOrder(new[] { Constants.SocialDomainId, nju.Id }, resp.Billings.Select(x => Guid.Parse(x.SubjectId)));

            // the content should be the same with GetCurrentUsedBilling
        }
        public async Task TestGetDomainsCurrentAllocatedBillings()
        {
            var quota = new Domain.ValueObjects.Resources(1, 20, 30);

            nju.Quota = quota.Clone();
            pku.Quota = quota.Clone();
            db.BillingCycleEntries.Add(new BillingCycleEntry(nju.BillingCycleSubject));
            db.BillingCycleEntries.Add(new BillingCycleEntry(pku.BillingCycleSubject));
            await db.SaveChangesAsync();

            var(service, billingTask, _) = CreateService(systemTokenClaims);

            await WaitForTaskForExecuteCycles(billingTask, billingConfiguration.CheckCycleMs, 2);

            var resp = await service.GetCurrentAllocatedBillings(new GetCurrentAllocatedBillingsRequest
            {
                SubjectType = Protos.Common.SubjectType.Domain
            }, TestContext);

            Assert.Equal(2, resp.Billings.Count);

            // the content should be the same with GetCurrentUsedBilling
        }
示例#10
0
        public async Task TestGetUsersCurrentUsedBillings()
        {
            var resources = new Domain.ValueObjects.Resources(1, 20, 30);

            cjd67project.Resources = resources;
            db.UseCycleEntries.Add(new UseCycleEntry(cjd67project.UseCycleSubject));
            await db.SaveChangesAsync();

            var(service, billingTask, _) = CreateService(lqlqTokenClaims);

            await WaitForTaskForExecuteCycles(billingTask, billingConfiguration.CheckCycleMs, 2);

            var resp = await service.GetCurrentUsedBillings(new GetCurrentUsedBillingsRequest
            {
                SubjectType = Protos.Common.SubjectType.UserProjectAssignment
            }, TestContext);

            var billing = Assert.Single(resp.Billings);

            // User does not pay, but will know how much they should have paid.
            Assert.Equal(PricePlan.Instance.Calculate(resources), (decimal)billing.Amount);
            Assert.Equal(resources.ToGrpc(), billing.Resources);
        }
示例#11
0
        public async Task TestGetProjectsCurrentAllocatedBillings()
        {
            var quota = new Domain.ValueObjects.Resources(1, 20, 30);

            lqproject.Quota = quota.Clone();
            db.BillingCycleEntries.Add(new BillingCycleEntry(lqproject.BillingCycleSubject));
            await db.SaveChangesAsync();

            var(service, billingTask, _) = CreateService(njuadminnjuTokenClaims);

            await WaitForTaskForExecuteCycles(billingTask, billingConfiguration.CheckCycleMs, 2);

            var resp = await service.GetCurrentAllocatedBillings(new GetCurrentAllocatedBillingsRequest
            {
                SubjectType = Protos.Common.SubjectType.Project
            }, TestContext);

            var billing = Assert.Single(resp.Billings);

            Assert.Equal(PricePlan.Instance.Calculate(quota), (decimal)billing.Amount);
            Assert.Equal(lq.Id.ToString(), billing.PayerId);
            Assert.Equal(quota.ToGrpc(), billing.Quota);
        }
示例#12
0
        public async Task TestChangeProjectUserResources()
        {
            var service = CreateService(cjdlqTokenClaims);
            var initial = new Domain.ValueObjects.Resources(3, 4, 5);

            cjd67project.Resources = initial;
            db.UseCycleEntries.Add(new UseCycleEntry(cjd67project.UseCycleSubject));
            db.UseCycleEntries.Add(new UseCycleEntry(lqproject.UseCycleSubject));
            db.UseCycleEntries.Add(new UseCycleEntry(nju.UseCycleSubject));
            await db.SaveChangesAsync();

            // change delta
            var delta = new Domain.ValueObjects.Resources(5, 6, 7);

            await service.ChangeProjectUserResources(new Protos.Interop.ChangeProjectUserResourcesRequest
            {
                ResourcesDelta = delta.ToGrpc()
            }, TestContext);

            // there should be a UseCycleRecord of the initial resources on cjd67project, lqproject and nju domain

            void AssertInitial(UseCycleSubject subject)
            {
                var record = Assert.Single(subject.UseCycleRecords);

                Assert.Equal(initial, record.Resources);
                Assert.Equal(PricePlan.Instance.Calculate(initial), record.Amount);
            }

            AssertInitial(cjd67project.UseCycleSubject);
            AssertInitial(lqproject.UseCycleSubject);
            AssertInitial(nju.UseCycleSubject);
            Assert.Equal(cjd67project.Resources, initial + delta);
            Assert.Equal(lqproject.Resources, initial + delta);
            Assert.Equal(nju.Resources, initial + delta);
        }
示例#13
0
        public async Task TestGetSystemQuotaStatus()
        {
            var service = CreateService();

            var systemResourecs = new Domain.ValueObjects.Resources(1000, 1000000, 1000);

            nju.Quota = new Domain.ValueObjects.Resources(20, 30, 40);
            pku.Quota = new Domain.ValueObjects.Resources(30, 40, 50);

            cjd67project.Resources = new Domain.ValueObjects.Resources(1, 2, 3);
            fcfcproject.Resources  = new Domain.ValueObjects.Resources(2, 3, 4);

            await db.SaveChangesAsync();

            var resp = await service.GetQuotaStatus(new Protos.Interop.GetQuotaStatusRequest
            {
                Subject = new Protos.Interop.Subject {
                    Type = SubjectType.System, Id = ""
                }
            }, TestContext);

            Assert.Equal(systemResourecs, resp.Total.FromGrpc());
            Assert.Equal(nju.Quota + pku.Quota, resp.Used.FromGrpc());
        }