Search in sources :

Example 26 with Language

use of org.sonar.api.resources.Language in project sonarqube by SonarSource.

the class QualityProfileEventsStepTest method added_event_if_qp_is_added.

@Test
public void added_event_if_qp_is_added() {
    QualityProfile qp = qp(QP_NAME_1, LANGUAGE_KEY_1, new Date());
    qProfileStatusRepository.register(qp.getQpKey(), ADDED);
    Language language = mockLanguageInRepository(LANGUAGE_KEY_1);
    mockMeasures(treeRootHolder.getRoot(), null, arrayOf(qp));
    underTest.execute(new TestComputationStepContext());
    verify(eventRepository).add(eq(treeRootHolder.getRoot()), eventArgumentCaptor.capture());
    verifyNoMoreInteractions(eventRepository);
    verifyEvent(eventArgumentCaptor.getValue(), "Use '" + qp.getQpName() + "' (" + language.getName() + ")", null);
}
Also used : AbstractLanguage(org.sonar.api.resources.AbstractLanguage) Language(org.sonar.api.resources.Language) QualityProfile(org.sonar.server.qualityprofile.QualityProfile) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Date(java.util.Date) Test(org.junit.Test)

Example 27 with Language

use of org.sonar.api.resources.Language in project sonarqube by SonarSource.

the class CompareAction method writeRule.

private void writeRule(JsonWriter json, RuleDefinitionDto rule, @Nullable RuleRepositoryDto repository) {
    String repositoryKey = rule.getRepositoryKey();
    json.prop(ATTRIBUTE_KEY, rule.getKey().toString()).prop(ATTRIBUTE_NAME, rule.getName()).prop(ATTRIBUTE_PLUGIN_KEY, repositoryKey);
    if (repository != null) {
        String languageKey = repository.getLanguage();
        Language language = languages.get(languageKey);
        json.prop(ATTRIBUTE_PLUGIN_NAME, repository.getName());
        json.prop(ATTRIBUTE_LANGUAGE_KEY, languageKey);
        json.prop(ATTRIBUTE_LANGUAGE_NAME, language == null ? null : language.getName());
    }
}
Also used : Language(org.sonar.api.resources.Language)

Example 28 with Language

use of org.sonar.api.resources.Language in project sonarqube by SonarSource.

the class ActiveRuleCompleter method writeProfile.

private void writeProfile(Map<String, Rules.QProfile> profilesResponse, QProfileDto profile) {
    Rules.QProfile.Builder profileResponse = Rules.QProfile.newBuilder();
    ofNullable(profile.getName()).ifPresent(profileResponse::setName);
    if (profile.getLanguage() != null) {
        profileResponse.setLang(profile.getLanguage());
        Language language = languages.get(profile.getLanguage());
        String langName = language == null ? profile.getLanguage() : language.getName();
        profileResponse.setLangName(langName);
    }
    ofNullable(profile.getParentKee()).ifPresent(profileResponse::setParent);
    profilesResponse.put(profile.getKee(), profileResponse.build());
}
Also used : Language(org.sonar.api.resources.Language)

Example 29 with Language

use of org.sonar.api.resources.Language in project sonarqube by SonarSource.

the class LanguageValidationTest method fail_if_conflicting_languages.

@Test
public void fail_if_conflicting_languages() {
    Language lang1 = mock(Language.class);
    Language lang2 = mock(Language.class);
    when(lang1.getKey()).thenReturn("key");
    when(lang2.getKey()).thenReturn("key");
    ServerPluginRepository repo = mock(ServerPluginRepository.class);
    when(repo.getPluginKey(lang1)).thenReturn("plugin1");
    when(repo.getPluginKey(lang2)).thenReturn("plugin2");
    LanguageValidation languageValidation = new LanguageValidation(repo, lang1, lang2);
    assertThatThrownBy(languageValidation::start).isInstanceOf(IllegalStateException.class).hasMessage("There are two languages declared with the same key 'key' declared by the plugins 'plugin1' and 'plugin2'. " + "Please uninstall one of the conflicting plugins.");
}
Also used : Language(org.sonar.api.resources.Language) ServerPluginRepository(org.sonar.server.plugins.ServerPluginRepository) Test(org.junit.Test)

Example 30 with Language

use of org.sonar.api.resources.Language in project sonarqube by SonarSource.

the class SearchActionTest method json_example.

@Test
public void json_example() {
    // languages
    Language cs = newLanguage("cs", "C#");
    Language java = newLanguage("java", "Java");
    Language python = newLanguage("py", "Python");
    // profiles
    QProfileDto sonarWayCs = db.qualityProfiles().insert(p -> p.setName("Sonar way").setKee("AU-TpxcA-iU5OvuD2FL3").setIsBuiltIn(true).setLanguage(cs.getKey()));
    QProfileDto myCompanyProfile = db.qualityProfiles().insert(p -> p.setName("My Company Profile").setKee("iU5OvuD2FLz").setLanguage(java.getKey()));
    QProfileDto myBuProfile = db.qualityProfiles().insert(p -> p.setName("My BU Profile").setKee("AU-TpxcA-iU5OvuD2FL1").setParentKee(myCompanyProfile.getKee()).setLanguage(java.getKey()));
    QProfileDto sonarWayPython = db.qualityProfiles().insert(p -> p.setName("Sonar way").setKee("AU-TpxcB-iU5OvuD2FL7").setIsBuiltIn(true).setLanguage(python.getKey()));
    db.qualityProfiles().setAsDefault(sonarWayCs, myCompanyProfile, sonarWayPython);
    // rules
    List<RuleDefinitionDto> javaRules = range(0, 10).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(java.getKey())).getDefinition()).collect(MoreCollectors.toList());
    List<RuleDefinitionDto> deprecatedJavaRules = range(0, 5).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(java.getKey()).setStatus(DEPRECATED)).getDefinition()).collect(MoreCollectors.toList());
    range(0, 7).forEach(i -> db.qualityProfiles().activateRule(myCompanyProfile, javaRules.get(i)));
    range(0, 2).forEach(i -> db.qualityProfiles().activateRule(myCompanyProfile, deprecatedJavaRules.get(i)));
    range(0, 10).forEach(i -> db.qualityProfiles().activateRule(myBuProfile, javaRules.get(i)));
    range(0, 5).forEach(i -> db.qualityProfiles().activateRule(myBuProfile, deprecatedJavaRules.get(i)));
    range(0, 2).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(python.getKey())).getDefinition()).forEach(rule -> db.qualityProfiles().activateRule(sonarWayPython, rule));
    range(0, 3).mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(cs.getKey())).getDefinition()).forEach(rule -> db.qualityProfiles().activateRule(sonarWayCs, rule));
    // project
    range(0, 7).mapToObj(i -> db.components().insertPrivateProjectDto()).forEach(project -> db.qualityProfiles().associateWithProject(project, myBuProfile));
    // User
    UserDto user = db.users().insertUser();
    db.qualityProfiles().addUserPermission(myCompanyProfile, user);
    db.qualityProfiles().addUserPermission(myBuProfile, user);
    userSession.logIn(user);
    underTest = new SearchAction(userSession, new Languages(cs, java, python), dbClient, new ComponentFinder(dbClient, null));
    ws = new WsActionTester(underTest);
    String result = ws.newRequest().execute().getInput();
    assertJson(result).ignoreFields("ruleUpdatedAt", "lastUsed", "userUpdatedAt").isSimilarTo(ws.getDef().responseExampleAsString());
}
Also used : ComponentFinder(org.sonar.server.component.ComponentFinder) SearchWsResponse(org.sonarqube.ws.Qualityprofiles.SearchWsResponse) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) IntStream.range(java.util.stream.IntStream.range) GlobalPermission(org.sonar.db.permission.GlobalPermission) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Languages(org.sonar.api.resources.Languages) QualityProfileDbTester(org.sonar.db.qualityprofile.QualityProfileDbTester) PARAM_PROJECT(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT) WebService(org.sonar.api.server.ws.WebService) DateUtils(org.sonar.api.utils.DateUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) PARAM_LANGUAGE(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE) PARAM_DEFAULTS(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS) MediaTypes(org.sonarqube.ws.MediaTypes) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Language(org.sonar.api.resources.Language) PARAM_QUALITY_PROFILE(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE) PARAM_PROJECT_KEY(org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_KEY) GroupDto(org.sonar.db.user.GroupDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) QualityProfile(org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) QualityProfileTesting.newQualityProfileDto(org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto) LanguageTesting.newLanguage(org.sonar.server.language.LanguageTesting.newLanguage) TestRequest(org.sonar.server.ws.TestRequest) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) WsActionTester(org.sonar.server.ws.WsActionTester) DEPRECATED(org.sonar.api.rule.RuleStatus.DEPRECATED) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbClient(org.sonar.db.DbClient) List(java.util.List) Rule(org.junit.Rule) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ProjectDto(org.sonar.db.project.ProjectDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Change(org.sonar.api.server.ws.Change) ComponentFinder(org.sonar.server.component.ComponentFinder) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Language(org.sonar.api.resources.Language) LanguageTesting.newLanguage(org.sonar.server.language.LanguageTesting.newLanguage) UserDto(org.sonar.db.user.UserDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Languages(org.sonar.api.resources.Languages) WsActionTester(org.sonar.server.ws.WsActionTester) Test(org.junit.Test)

Aggregations

Language (org.sonar.api.resources.Language)33 Test (org.junit.Test)12 AbstractLanguage (org.sonar.api.resources.AbstractLanguage)8 QualityProfile (org.sonar.server.qualityprofile.QualityProfile)6 Languages (org.sonar.api.resources.Languages)4 JsonWriter (org.sonar.api.utils.text.JsonWriter)4 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)3 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)3 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)3 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)3 LanguageTesting.newLanguage (org.sonar.server.language.LanguageTesting.newLanguage)3 Date (java.util.Date)2 IntStream.range (java.util.stream.IntStream.range)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)2 Rule (org.junit.Rule)2 DEPRECATED (org.sonar.api.rule.RuleStatus.DEPRECATED)2 WebService (org.sonar.api.server.ws.WebService)2 DateUtils (org.sonar.api.utils.DateUtils)2 DateUtils.parseDateTime (org.sonar.api.utils.DateUtils.parseDateTime)2