public void GetQualityProfilesAsync_WhenExceptionThrown_PropagateIt()
        {
            var request = new QualityProfileRequest {
                ProjectKey = "project"
            };

            Method_WhenExceptionThrown_PropagateIt((c, t) => c.GetQualityProfilesAsync(request, t));
        }
        public void GetQualityProfilesAsync_WhenCancellationRequested_ThrowsException()
        {
            var request = new QualityProfileRequest {
                ProjectKey = "project"
            };

            Method_WhenCancellationRequested_ThrowsException((c, t) => c.GetQualityProfilesAsync(request, t));
        }
        public async Task GetQualityProfilesAsync_WhenRequestIsNotSuccess_ReturnsDefaultAndErrorCode()
        {
            var request = new QualityProfileRequest {
                ProjectKey = "project"
            };

            await Method_WhenRequestIsNotSuccess_ReturnsDefaultAndErrorCode(
                (c, t) => c.GetQualityProfilesAsync(request, t), HttpStatusCode.InternalServerError);
        }
        public async Task GetQualityProfilesAsync_WhenRequestIsSuccesful_ReturnsIsSuccessAndNotNullData()
        {
            var request = new QualityProfileRequest {
                ProjectKey = "project"
            };

            await Method_WhenRequestIsSuccesful_ReturnsIsSuccessAndNotNullData(
                (c, t) => c.GetQualityProfilesAsync(request, t),
                @"{""profiles"":[{""key"":""AU-TpxcA-iU5OvuD2FL3"",""name"":""Sonar way"",""language"":""cs"",""languageName"":""C#"",""isInherited"":false,""activeRuleCount"":37,""activeDeprecatedRuleCount"":0,""isDefault"":true,""ruleUpdatedAt"":""2016-12-22T19:10:03+0100"",""lastUsed"":""2016-12-01T19:10:03+0100""}]}",
                result => result.Length.Should().Be(1));
        }
        public Task <Result <QualityProfileResponse[]> > GetQualityProfilesAsync(QualityProfileRequest request,
                                                                                 CancellationToken token)
        {
            if (request.ProjectKey == null)
            {
                request.Defaults = true;
            }

            // Since 5.2
            return(InvokeSonarQubeApi("api/qualityprofiles/search", request, token,
                                      stringResponse => JObject.Parse(stringResponse)["profiles"].ToObject <QualityProfileResponse[]>()));
        }
        public async Task GetQualityProfilesAsync_CallsTheExpectedUri()
        {
            var request = new QualityProfileRequest {
                ProjectKey = "project"
            };

            await Method_CallsTheExpectedUri(new Uri("api/qualityprofiles/search?projectKey=project", UriKind.RelativeOrAbsolute),
                                             @"{""profiles"":[]}", (c, t) => c.GetQualityProfilesAsync(request, t));

            request = new QualityProfileRequest {
                ProjectKey = null
            };
            await Method_CallsTheExpectedUri(new Uri("api/qualityprofiles/search?defaults=true", UriKind.RelativeOrAbsolute),
                                             @"{""profiles"":[]}", (c, t) => c.GetQualityProfilesAsync(request, t));
        }
        public async Task <SonarQubeQualityProfile> GetQualityProfileAsync(string projectKey, string organizationKey,
                                                                           SonarQubeLanguage language, CancellationToken token)
        {
            EnsureIsConnected();

            var qualityProfileRequest = new QualityProfileRequest {
                ProjectKey = projectKey, OrganizationKey = organizationKey
            };
            var qualityProfilesResult = await this.sonarqubeClient.GetQualityProfilesAsync(qualityProfileRequest, token);

            // Special handling for the case when a project was not analyzed yet, in which case a 404 is returned
            if (qualityProfilesResult.StatusCode == HttpStatusCode.NotFound)
            {
                qualityProfileRequest = new QualityProfileRequest {
                    ProjectKey = null, OrganizationKey = organizationKey
                };
                qualityProfilesResult = await this.sonarqubeClient.GetQualityProfilesAsync(qualityProfileRequest, token);
            }

            qualityProfilesResult.EnsureSuccess();

            var profilesWithGivenLanguage = qualityProfilesResult.Value.Where(x => x.Language == language.Key).ToList();

            var qualityProfile = profilesWithGivenLanguage.Count > 1
                ? profilesWithGivenLanguage.Single(x => x.IsDefault)
                : profilesWithGivenLanguage.Single();

            var qualityProfileChangeLogRequest = new QualityProfileChangeLogRequest
            {
                PageSize          = 1,
                QualityProfileKey = qualityProfile.Key
            };
            var changeLog = await this.sonarqubeClient.GetQualityProfileChangeLogAsync(qualityProfileChangeLogRequest, token);

            changeLog.EnsureSuccess();

            return(SonarQubeQualityProfile.FromResponse(qualityProfile, changeLog.Value.Events.Single().Date));
        }