예제 #1
0
  @Test
  public void fail_to_recreate_built_in_profile_when_rule_not_found() throws Exception {
    String name = "Default";
    String language = "java";

    RulesProfile profile = RulesProfile.create(name, language);
    Rule rule = Rule.create("pmd", "rule");
    profile.activateRule(rule, null);

    ProfileDefinition profileDefinition = mock(ProfileDefinition.class);
    when(profileDefinition.createProfile(any(ValidationMessages.class))).thenReturn(profile);
    definitions.add(profileDefinition);

    when(ruleDao.getNullableByKey(session, RuleKey.of("pmd", "rule"))).thenReturn(null);

    when(qProfileOperations.newProfile(
            eq(name), eq(language), eq(true), any(UserSession.class), eq(session)))
        .thenReturn(new QProfile().setId(1));

    try {
      backup.recreateBuiltInProfilesByLanguage(language);
      fail();
    } catch (Exception e) {
      assertThat(e).isInstanceOf(NotFoundException.class);
    }
    verifyZeroInteractions(qProfileActiveRuleOperations);
  }
예제 #2
0
  @Test
  public void no_changes() {
    ruleDao.insert(
        dbSession,
        RuleTesting.newDto(RULE_KEY)
            // the following fields are not supposed to be updated
            .setNoteData("my *note*")
            .setNoteUserLogin("me")
            .setTags(ImmutableSet.of("tag1"))
            .setSubCharacteristicId(33)
            .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name())
            .setRemediationCoefficient("1d")
            .setRemediationOffset("5min"));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY);
    assertThat(update.isEmpty()).isTrue();
    updater.update(update, userSessionRule);

    dbSession.clearCache();
    RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
    assertThat(rule.getNoteData()).isEqualTo("my *note*");
    assertThat(rule.getNoteUserLogin()).isEqualTo("me");
    assertThat(rule.getTags()).containsOnly("tag1");
    assertThat(rule.getSubCharacteristicId()).isEqualTo(33);
    assertThat(rule.getRemediationFunction())
        .isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name());
    assertThat(rule.getRemediationCoefficient()).isEqualTo("1d");
    assertThat(rule.getRemediationOffset()).isEqualTo("5min");
  }
예제 #3
0
  @Test
  public void
      recreate_built_in_profiles_from_language_with_multiple_profiles_with_same_name_and_same_language()
          throws Exception {
    RulesProfile profile1 = RulesProfile.create("Default", "java");
    profile1.activateRule(Rule.create("pmd", "rule").setSeverity(RulePriority.BLOCKER), null);
    ProfileDefinition profileDefinition1 = mock(ProfileDefinition.class);
    when(profileDefinition1.createProfile(any(ValidationMessages.class))).thenReturn(profile1);
    definitions.add(profileDefinition1);

    RulesProfile profile2 = RulesProfile.create("Default", "java");
    profile2.activateRule(Rule.create("checkstyle", "rule").setSeverity(RulePriority.MAJOR), null);
    ProfileDefinition profileDefinition2 = mock(ProfileDefinition.class);
    when(profileDefinition2.createProfile(any(ValidationMessages.class))).thenReturn(profile2);
    definitions.add(profileDefinition2);

    when(ruleDao.getNullableByKey(session, RuleKey.of("pmd", "rule")))
        .thenReturn(new RuleDto().setId(10).setSeverity("INFO"));
    when(ruleDao.getNullableByKey(session, RuleKey.of("checkstyle", "rule")))
        .thenReturn(new RuleDto().setId(11).setSeverity("INFO"));

    when(qProfileOperations.newProfile(
            eq("Default"), eq("java"), eq(true), any(UserSession.class), eq(session)))
        .thenReturn(new QProfile().setId(1));

    backup.recreateBuiltInProfilesByLanguage("java");

    verify(qProfileActiveRuleOperations)
        .createActiveRule(
            eq(QualityProfileKey.of("Default", "java")),
            eq(RuleKey.of("pmd", "rule")),
            eq("BLOCKER"),
            eq(session));
    verify(qProfileActiveRuleOperations)
        .createActiveRule(
            eq(QualityProfileKey.of("Default", "java")),
            eq(RuleKey.of("checkstyle", "rule")),
            eq("MAJOR"),
            eq(session));
    verifyNoMoreInteractions(qProfileActiveRuleOperations);

    verify(dryRunCache).reportGlobalModification(session);
    verify(session).commit();
  }
예제 #4
0
  @Test
  public void remove_markdown_note() {
    ruleDao.insert(
        dbSession, RuleTesting.newDto(RULE_KEY).setNoteData("my *note*").setNoteUserLogin("me"));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setMarkdownNote(null);
    updater.update(update, userSessionRule);

    dbSession.clearCache();
    RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
    assertThat(rule.getNoteData()).isNull();
    assertThat(rule.getNoteUserLogin()).isNull();
    assertThat(rule.getNoteCreatedAt()).isNull();
    assertThat(rule.getNoteUpdatedAt()).isNull();
  }
예제 #5
0
  @Test
  public void remove_tags() {
    ruleDao.insert(
        dbSession,
        RuleTesting.newDto(RULE_KEY)
            .setTags(Sets.newHashSet("security"))
            .setSystemTags(Sets.newHashSet("java8", "javadoc")));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(null);
    updater.update(update, userSessionRule);

    dbSession.clearCache();
    RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
    assertThat(rule.getTags()).isEmpty();
    assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc");

    // verify that tags are indexed in index
    Set<String> tags = tester.get(RuleService.class).listTags();
    assertThat(tags).containsOnly("java8", "javadoc");
  }
예제 #6
0
  @Test
  public void unset_debt() {
    insertDebtCharacteristics(dbSession);
    ruleDao.insert(
        dbSession,
        RuleTesting.newDto(RULE_KEY)
            .setDefaultSubCharacteristicId(hardReliabilityId)
            .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR.name())
            .setDefaultRemediationCoefficient("1d")
            .setDefaultRemediationOffset("5min")
            .setSubCharacteristicId(softReliabilityId)
            .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name())
            .setRemediationCoefficient(null)
            .setRemediationOffset("1min"));
    dbSession.commit();

    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setDebtSubCharacteristic(null);
    updater.update(update, userSessionRule);

    // verify db
    dbSession.clearCache();
    RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY);
    assertThat(rule.getSubCharacteristicId()).isEqualTo(-1);
    assertThat(rule.getRemediationFunction()).isNull();
    assertThat(rule.getRemediationCoefficient()).isNull();
    assertThat(rule.getRemediationOffset()).isNull();

    assertThat(rule.getDefaultSubCharacteristicId()).isNotNull();
    assertThat(rule.getDefaultRemediationFunction()).isNotNull();
    assertThat(rule.getDefaultRemediationCoefficient()).isNotNull();
    assertThat(rule.getDefaultRemediationOffset()).isNotNull();

    // verify index
    Rule indexedRule = tester.get(RuleIndex.class).getByKey(RULE_KEY);
    assertThat(indexedRule.debtCharacteristicKey()).isEqualTo("NONE");
    assertThat(indexedRule.debtSubCharacteristicKey()).isEqualTo("NONE");
    assertThat(indexedRule.debtRemediationFunction()).isNull();
    assertThat(indexedRule.debtOverloaded()).isTrue();
  }
예제 #7
0
  @Test
  public void recreate_built_in_profiles_from_language() throws Exception {
    String name = "Default";
    String language = "java";

    RulesProfile profile = RulesProfile.create(name, language);
    Rule rule = Rule.create("pmd", "rule");
    rule.createParameter("max");
    ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
    activeRule.setParameter("max", "10");

    ProfileDefinition profileDefinition = mock(ProfileDefinition.class);
    when(profileDefinition.createProfile(any(ValidationMessages.class))).thenReturn(profile);
    definitions.add(profileDefinition);

    when(ruleDao.getNullableByKey(session, RuleKey.of("pmd", "rule")))
        .thenReturn(new RuleDto().setId(10).setSeverity("INFO"));

    when(qProfileOperations.newProfile(
            eq(name), eq(language), eq(true), any(UserSession.class), eq(session)))
        .thenReturn(new QProfile().setId(1));

    backup.recreateBuiltInProfilesByLanguage(language);

    verify(qProfileActiveRuleOperations)
        .createActiveRule(
            eq(QualityProfileKey.of(name, language)),
            eq(RuleKey.of("pmd", "rule")),
            eq("BLOCKER"),
            eq(session));
    verify(qProfileActiveRuleOperations)
        .updateActiveRuleParam(any(ActiveRuleDto.class), eq("max"), eq("10"), eq(session));
    verifyNoMoreInteractions(qProfileActiveRuleOperations);

    verify(dryRunCache).reportGlobalModification(session);
    verify(session).commit();
  }