public async Task <ApplicationRecordApiModel> GetApplicationAsync(string applicationId)
 {
     return(new ApplicationRecordApiModel(await _applicationDatabase.GetApplicationAsync(applicationId)));
 }
        private async Task ApproveAllApplications()
        {
            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await _applicationsDatabase.ApproveApplicationAsync(null, false, false);
            });

            await Assert.ThrowsAsync <ArgumentException>(async() =>
            {
                await _applicationsDatabase.ApproveApplicationAsync(Guid.Empty.ToString(), false, false);
            });

            Skip.If(!_fixture.RegistrationOk);
            int fullPasses = 0;

            foreach (var application in _applicationTestSet)
            {
                // read model to get state
                var applicationModel = await _applicationsDatabase.GetApplicationAsync(application.Model.ApplicationId.ToString());

                if (applicationModel.ApplicationState == ApplicationState.New)
                {
                    // approve app
                    applicationModel = await _applicationsDatabase.ApproveApplicationAsync(application.Model.ApplicationId.ToString(), true, false);

                    Assert.NotNull(applicationModel);
                    Assert.Equal(ApplicationState.Approved, applicationModel.ApplicationState);
                }

                // verify start condition
                if (applicationModel.ApplicationState != ApplicationState.Approved)
                {
                    continue;
                }

                // reject approved app should fail
                await Assert.ThrowsAsync <ResourceInvalidStateException>(async() =>
                {
                    await _applicationsDatabase.ApproveApplicationAsync(application.Model.ApplicationId.ToString(), false, false);
                });

                // force approved app to rejected state
                applicationModel = await _applicationsDatabase.ApproveApplicationAsync(application.Model.ApplicationId.ToString(), false, true);

                Assert.NotNull(applicationModel);
                Assert.NotEqual(applicationModel.ApplicationId, Guid.Empty);
                Assert.Equal(ApplicationState.Rejected, applicationModel.ApplicationState);
                ApplicationTestData.AssertEqualApplicationModelData(applicationModel, application.Model);

                // approve rejected app should fail
                await Assert.ThrowsAsync <ResourceInvalidStateException>(async() =>
                {
                    await _applicationsDatabase.ApproveApplicationAsync(application.Model.ApplicationId.ToString(), true, false);
                });

                // force approve of rejected app
                applicationModel = await _applicationsDatabase.ApproveApplicationAsync(application.Model.ApplicationId.ToString(), true, true);

                Assert.NotNull(applicationModel);
                Assert.NotEqual(applicationModel.ApplicationId, Guid.Empty);
                Assert.Equal(ApplicationState.Approved, applicationModel.ApplicationState);
                ApplicationTestData.AssertEqualApplicationModelData(applicationModel, application.Model);
                fullPasses++;
            }
            // not enough test passes to verify
            Skip.If(fullPasses < _applicationTestSet.Count / 2);
        }
        public async Task <string> StartSigningRequestAsync(
            string applicationId,
            string certificateGroupId,
            string certificateTypeId,
            byte[] certificateSigningRequest,
            string authorityId)
        {
            Application application = await _applicationsDatabase.GetApplicationAsync(applicationId);

            if (string.IsNullOrEmpty(certificateGroupId))
            {
                //TODO:
            }

            if (string.IsNullOrEmpty(certificateTypeId))
            {
                //TODO
            }

            CertificateRequest request = new CertificateRequest()
            {
                RequestId = Guid.NewGuid(), AuthorityId = authorityId
            };

            request.ID = _certRequestIdCounter++;
            request.CertificateRequestState = (int)CertificateRequestState.New;
            request.CertificateGroupId      = certificateGroupId;
            request.CertificateTypeId       = certificateTypeId;
            request.SubjectName             = null;
            request.DomainNames             = null;
            request.PrivateKeyFormat        = null;
            request.PrivateKeyPassword      = null;
            request.SigningRequest          = certificateSigningRequest;
            request.ApplicationId           = applicationId;
            request.RequestTime             = DateTime.UtcNow;

            bool retry;

            do
            {
                retry = false;
                try
                {
                    var result = await _certificateRequests.CreateAsync(request);
                }
                catch (DocumentClientException dce)
                {
                    if (dce.StatusCode == System.Net.HttpStatusCode.Conflict)
                    {
                        // retry with new guid and id
                        request.RequestId     = Guid.NewGuid();
                        _certRequestIdCounter = await GetMaxCertIDAsync();

                        request.ID = _certRequestIdCounter++;
                        retry      = true;
                    }
                }
            } while (retry);

            return(request.RequestId.ToString());
        }