@Test
  public void testProjectOverThreshold() {
    // add enough violations to be over threshold
    int threshold = settings.getInt(ProfileProgressionPlugin.QUALITY_PROFILE_CHANGE_THRESHOLD_KEY);
    List<ActiveRule> rules = testProjectModel.getRulesProfile().getActiveRules();
    int numberOfRules = rules.size();
    List<Violation> violations = decoratorContext.getViolations();
    int initialNumberOfViolations = violations.size();

    int numberOfExtraViolations =
        ((threshold + 1) * (numberOfRules / 100)) - initialNumberOfViolations;

    for (int i = 0; i < numberOfExtraViolations; i++) {
      violations.add(Violation.create(rules.get(initialNumberOfViolations + i), testProject));
    }

    // test profile isn't progressed
    String profileBefore = testProjectModel.getRulesProfile().getName();
    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    testOutcome(
        "Projects profile has been changed despite the violation threshold not met.",
        profileBefore,
        profileAfter,
        false);
  }
  @Test
  public void testProjectWithZeroViolationsAndRules() {
    // remove all violations
    violations.clear();

    // empty rules from profile
    for (Iterator<ActiveRule> iterator =
            testProjectModel.getRulesProfile().getActiveRules().iterator();
        iterator.hasNext(); ) {
      testProjectModel.getRulesProfile().removeActiveRule(iterator.next());
    }

    // test quality profile changes
    String profileBefore = testProjectModel.getRulesProfile().getName();
    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    // check profile has change
    assertThat(
        "Projects profile has not been changed despite meeting the violation threshold.",
        profileBefore,
        not(equalTo(profileAfter)));
    testOutcome(
        "Projects new profile is not the next profile in pression list.",
        rulesProfiles[0].getName(),
        profileAfter,
        true);
  }
  @Test
  public void testEmptyTargetLanguageQualityProfile() {
    // set threshold to more than 100
    settings.removeProperty(ProfileProgressionPlugin.GLOBAL_TARGET_LANGUAGE_QUALITY_PROFILE_KEY);

    // test it doesn't throw any error
    String profileBefore = testProjectModel.getRulesProfile().getName();
    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    testOutcome(
        "Projects profile has been changed despite the target language quality profile list being empty.",
        profileBefore,
        profileAfter,
        false);
  }
  @Test
  public void testThresholdTooBig() {
    // set threshold to more than 100
    settings.setProperty(ProfileProgressionPlugin.QUALITY_PROFILE_CHANGE_THRESHOLD_KEY, "101");

    // test it doesn't throw any error
    String profileBefore = testProjectModel.getRulesProfile().getName();
    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    testOutcome(
        "Projects profile has been changed despite theshold being too big.",
        profileBefore,
        profileAfter,
        false);
  }
  @Test
  public void testPluginDisabledByProject() {
    // disable plug-in at project level
    settings.setProperty(
        ProfileProgressionPlugin.PROJECT_QUALITY_PROFILE_CHANGE_ENABLED_KEY, "false");

    String profileBefore = testProjectModel.getRulesProfile().getName();
    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    testOutcome(
        "Projects profile has been changed despite plug-in being disabled by project.",
        profileBefore,
        profileAfter,
        false);
  }
  @Test
  public void shouldSaveNewProject() {
    setupData("shared");

    ResourcePersister persister =
        new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class));
    persister.saveProject(singleProject, null);

    checkTables(
        "shouldSaveNewProject", new String[] {"build_date", "created_at"}, "projects", "snapshots");

    // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table
    ResourceModel model =
        getSession().getSingleResult(ResourceModel.class, "key", singleProject.getKey());
    assertThat(model.getCreatedAt()).isNotNull();
  }
  @Test
  public void testProjectWithLastManagedProfile() {
    // set project's profile to first managed profile
    testProjectModel.setRulesProfile(rulesProfiles[rulesProfiles.length - 1]);

    // test quality profile changes
    String profileBefore = testProjectModel.getRulesProfile().getName();
    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    // check profile isn't changed
    testOutcome(
        "Project's profile has been changed despite being last managed profile.",
        profileBefore,
        profileAfter,
        false);
  }
  @Test
  public void testProjectUnderThreshold() {
    // test quality profile changes
    String profileBefore = testProjectModel.getRulesProfile().getName();
    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    // check profile has change
    assertThat(
        "Projects profile has not been changed despite meeting the violation threshold.",
        profileBefore,
        not(equalTo(profileAfter)));
    testOutcome(
        "Projects new profile is not the next profile in pression list.",
        rulesProfiles[0].getName(),
        profileAfter,
        true);
  }
  @Test
  public void testProjectWithParentLastManagedProfile() {
    // make current test profile child of managed one
    testProjectModel
        .getRulesProfile()
        .setParentName(rulesProfiles[rulesProfiles.length - 1].getName());

    // test quality profile changes
    String parentProfileBefore = testProjectModel.getRulesProfile().getParentName();
    runAnalysis();
    String parentProfileAfter = testProjectModel.getRulesProfile().getParentName();

    testOutcome(
        "Projects profile's parent has been changed despite being last managed profile.",
        parentProfileBefore,
        parentProfileAfter,
        false);
  }
Example #10
0
 /** Maps a resource to a resource model and returns the resource */
 public static ResourceModel build(Resource resource) {
   ResourceModel model = new ResourceModel();
   model.setEnabled(Boolean.TRUE);
   model.setDescription(resource.getDescription());
   model.setKey(resource.getKey());
   if (resource.getLanguage() != null) {
     model.setLanguageKey(resource.getLanguage().getKey());
   }
   if (StringUtils.isNotBlank(resource.getName())) {
     model.setName(resource.getName());
   } else {
     model.setName(resource.getKey());
   }
   model.setLongName(resource.getLongName());
   model.setQualifier(resource.getQualifier());
   model.setScope(resource.getScope());
   return model;
 }
Example #11
0
 @Override
 public Object clone() {
   ResourceModel clone =
       new ResourceModel(getScope(), getKey(), getQualifier(), getRootId(), getName());
   clone.setDescription(getDescription());
   clone.setEnabled(getEnabled());
   clone.setProjectLinks(getProjectLinks());
   clone.setRulesProfile(getRulesProfile());
   clone.setLanguageKey(getLanguageKey());
   clone.setCopyResourceId(getCopyResourceId());
   clone.setLongName(getLongName());
   return clone;
 }
  @Test
  public void testProjectWithParentManagedProfile() {
    // make current test profile child of managed one
    testProjectModel.getRulesProfile().setParentName(rulesProfiles[0].getName());

    // test quality profile changes
    String parentProfileBefore = testProjectModel.getRulesProfile().getParentName();
    runAnalysis();
    String parentProfileAfter = testProjectModel.getRulesProfile().getParentName();

    assertThat(
        "Project profile's parent has not been changed despite meeting the violation threshold.",
        parentProfileBefore,
        not(equalTo(parentProfileAfter)));
    testOutcome(
        "Project profile's parent is not the next profile in pression list.",
        rulesProfiles[1].getName(),
        parentProfileAfter,
        true);
  }
  @Test
  public void testProjectWithManagedProfile() {
    // set project's profile to first managed profile
    testProjectModel.setRulesProfile(rulesProfiles[0]);

    // test quality profile changes
    String profileBefore = testProjectModel.getRulesProfile().getName();
    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    assertThat(
        "Projects profile has not been changed despite meeting the violation threshold.",
        profileBefore,
        not(equalTo(profileAfter)));
    testOutcome(
        "Projects new profile is not the next profile in pression list.",
        rulesProfiles[1].getName(),
        profileAfter,
        true);
  }
  @Before
  public void initialise() {
    // create mock db objects
    databaseSession = new MockDatabaseSession();
    databaseSession.setEntities(entities);

    // create quality profile hierarchy
    int numberOfProfiles = 4;
    int numberOfRules = 100;
    rulesProfiles = createRuleProfileHierarchy("qp", numberOfProfiles, numberOfRules);

    // create test project DTO
    Integer projectId = new Integer(123);
    testProject = new Project("test.project.key");
    testProject.setId(projectId);
    testProject.setName("testProject");
    testProject.setLanguage(Java.INSTANCE);

    // create test project entity
    testProjectModel = new ResourceModel();
    testProjectModel.setId(projectId);
    testProjectModel.setKey(testProject.getKey());
    testProjectModel.setEnabled(Boolean.TRUE);
    testProjectModel.setRootId(projectId);
    testProjectModel.setLanguageKey(Java.KEY);
    testProjectModel.setRulesProfile(createRulesProfile("testProjectProfile", numberOfRules));

    // add project to db objects
    MockDatabaseSession.EntityKey projectKey =
        databaseSession.new EntityKey(ResourceModel.class, projectId);
    entities.put(projectKey, testProjectModel);
    databaseSession.addMockQueryResults(ResourceModel.class, Arrays.asList(testProjectModel));

    // define test threshold
    int violationThreshold = 10;

    // create violations
    List<ActiveRule> rules = rulesProfiles[0].getActiveRules();
    int numberOfViolationsToBUnderThreshold = ((numberOfRules / 100) * violationThreshold) - 1;
    for (int i = 0; i < numberOfViolationsToBUnderThreshold; i++) {
      violations.add(Violation.create(rules.get(i), testProject));
    }

    // create context
    decoratorContext = new MockDecoratorContext(testProject, testProject, violations);

    // define default settings
    settings = new Settings();
    settings.setProperty(
        ProfileProgressionPlugin.GLOBAL_QUALITY_PROFILE_CHANGE_ENABLED_KEY, "true");
    settings.setProperty(
        ProfileProgressionPlugin.PROJECT_QUALITY_PROFILE_CHANGE_ENABLED_KEY, "true");
    settings.setProperty(
        ProfileProgressionPlugin.QUALITY_PROFILE_CHANGE_THRESHOLD_KEY,
        String.valueOf(violationThreshold));
    settings.setProperty(
        ProfileProgressionPlugin.GLOBAL_TARGET_LANGUAGE_QUALITY_PROFILE_KEY,
        rulesProfiles[numberOfProfiles - 1].getName());
  }
  protected void runAnalysis() {
    // create decorator
    ProfileProgressionDecorator decorator =
        new ProfileProgressionDecorator(
            databaseSession, testProjectModel.getRulesProfile(), settings, notificationManager);

    // provide mock profiles dao to access mock entity manager - could
    // better implement mock entity manager to avoid this
    decorator.setProfilesDao(new MockProfilesDao(databaseSession));

    if (decorator.shouldExecuteOnProject(testProject)) {
      decorator.decorate(testProject, decoratorContext);
    }
  }
  @Test
  public void testProjectTargetLanguageQualityProfile() {
    RulesProfile[] projectRulesHierarchy = createRuleProfileHierarchy("pqp", 12, 50);

    // set project setting for target language quality profile
    settings.setProperty(
        ProfileProgressionPlugin.PROJECT_TARGET_LANGUAGE_QUALITY_PROFILE_KEY,
        projectRulesHierarchy[projectRulesHierarchy.length - 1].getName());

    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    testOutcome(
        "Projects profile has not been changed to next profile in project specific hierarchy.",
        profileAfter,
        projectRulesHierarchy[0].getName(),
        true);
  }
  @Test
  public void testProjectWithGrandParentLastManagedProfile() {
    // create profile to be parent of project's profile
    RulesProfile testProjectParentProfile = createRulesProfile("testProjectParentProfile", 10);
    testProjectModel.getRulesProfile().setParentName(testProjectParentProfile.getName());

    // make parent profile's parent managed profile
    testProjectParentProfile.setParentName(rulesProfiles[rulesProfiles.length - 1].getName());

    // test quality profile changes
    String grandParentProfileBefore = testProjectParentProfile.getParentName();
    runAnalysis();
    String grandParentProfileAfter = testProjectParentProfile.getParentName();

    testOutcome(
        "Projects profile's grand-parent has been changed despite being last managed profile.",
        grandParentProfileBefore,
        grandParentProfileAfter,
        false);
  }
  @Test
  public void testMultiLanguageProjectTargetLanguageQualityProfile() {
    RulesProfile[] javaRulesHierarchy = createRuleProfileHierarchy("javaqp", 12, 50);
    RulesProfile[] jsRulesHierarchy = createRuleProfileHierarchy("jsqp", 12, 50);
    RulesProfile[] webRulesHierarchy = createRuleProfileHierarchy("webqp", 12, 50);

    String targetLanguageQualityProfiles =
        "java"
            + ProfileProgressionPlugin.TARGET_LANGUAGE_QUALITY_PROFILE_ASSIGN
            + javaRulesHierarchy[javaRulesHierarchy.length - 1].getName()
            + ProfileProgressionPlugin.TARGET_LANGUAGE_QUALITY_PROFILE_DELIM;

    targetLanguageQualityProfiles =
        targetLanguageQualityProfiles
            + "js"
            + ProfileProgressionPlugin.TARGET_LANGUAGE_QUALITY_PROFILE_ASSIGN
            + jsRulesHierarchy[jsRulesHierarchy.length - 1].getName()
            + ProfileProgressionPlugin.TARGET_LANGUAGE_QUALITY_PROFILE_DELIM;

    targetLanguageQualityProfiles =
        targetLanguageQualityProfiles
            + "web"
            + ProfileProgressionPlugin.TARGET_LANGUAGE_QUALITY_PROFILE_ASSIGN
            + webRulesHierarchy[webRulesHierarchy.length - 1].getName();

    // set project setting for target language quality profile
    settings.setProperty(
        ProfileProgressionPlugin.PROJECT_TARGET_LANGUAGE_QUALITY_PROFILE_KEY,
        targetLanguageQualityProfiles);

    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    testOutcome(
        "Projects profile has not been changed to next profile in project language specific hierarchy.",
        profileAfter,
        javaRulesHierarchy[0].getName(),
        true);
  }
  @Test
  public void testSingleLanguageGlobalTargetLanguageQualityProfile() {
    RulesProfile[] javaRulesHierarchy = createRuleProfileHierarchy("javaqp", 12, 50);

    String targetLanguageQualityProfiles =
        "java"
            + ProfileProgressionPlugin.TARGET_LANGUAGE_QUALITY_PROFILE_ASSIGN
            + javaRulesHierarchy[javaRulesHierarchy.length - 1].getName()
            + ProfileProgressionPlugin.TARGET_LANGUAGE_QUALITY_PROFILE_DELIM;

    // set global setting for target language quality profile
    settings.setProperty(
        ProfileProgressionPlugin.GLOBAL_TARGET_LANGUAGE_QUALITY_PROFILE_KEY,
        targetLanguageQualityProfiles);

    runAnalysis();
    String profileAfter = testProjectModel.getRulesProfile().getName();

    testOutcome(
        "Projects profile has not been changed to next profile in global language specific hierarchy.",
        profileAfter,
        javaRulesHierarchy[0].getName(),
        true);
  }
  @Test
  public void testProjectWithGrandParentManagedProfile() {
    // create profile to be parent of project's profile
    RulesProfile testProjectParentProfile = createRulesProfile("testProjectParentProfile", 10);
    testProjectModel.getRulesProfile().setParentName(testProjectParentProfile.getName());

    // make parent profile's parent managed profile
    testProjectParentProfile.setParentName(rulesProfiles[2].getName());

    // test quality profile changes
    String grandParentProfileBefore = testProjectParentProfile.getParentName();
    runAnalysis();
    String grandParentProfileAfter = testProjectParentProfile.getParentName();

    assertThat(
        "Project profile's grand-parent has not been changed despite meeting the violation threshold.",
        grandParentProfileBefore,
        not(equalTo(grandParentProfileAfter)));
    testOutcome(
        "Project profile's grand-parent is not the next profile in pression list.",
        rulesProfiles[3].getName(),
        grandParentProfileAfter,
        true);
  }
  @Test
  public void testProjectWithParentManagedProfileUsedByAnotherProject() {
    // make current test profile child of managed one
    testProjectModel.getRulesProfile().setParentName(rulesProfiles[0].getName());

    // create another test project entity
    Integer project2Id = new Integer(5678);
    ResourceModel testProjectModel2 = new ResourceModel();
    testProjectModel2.setId(project2Id);
    testProjectModel2.setKey("test.project2.key");
    testProjectModel2.setEnabled(Boolean.TRUE);
    testProjectModel2.setRootId(project2Id);
    testProjectModel2.setLanguageKey(Java.KEY);
    testProjectModel2.setRulesProfile(testProjectModel.getRulesProfile());

    // add projects to db objects
    MockDatabaseSession.EntityKey projectKey =
        databaseSession.new EntityKey(ResourceModel.class, project2Id);
    entities.put(projectKey, testProjectModel2);
    databaseSession.addMockQueryResults(
        ResourceModel.class, Arrays.asList(testProjectModel, testProjectModel2));

    // test quality profile changes
    String parentProfileBefore = testProjectModel.getRulesProfile().getParentName();
    runAnalysis();
    String parentProfileAfter = testProjectModel.getRulesProfile().getParentName();

    testOutcome(
        "Project profile's parent has been changed despite being used by another project.",
        parentProfileBefore,
        parentProfileAfter,
        true);

    ArgumentCaptor<Notification> argument = ArgumentCaptor.forClass(Notification.class);
    verify(notificationManager).scheduleForSending(argument.capture());
    Notification notification = argument.getValue();

    // check notification has project info
    assertThat(
        "Notification doesn't contain project name",
        notification.getFieldValue(ProfileProgressionPlugin.NOTIFICATION_PROJECT_NAME_KEY),
        equalTo(testProjectModel.getName()));
    assertThat(
        "Notification doesn't contain project id",
        notification.getFieldValue(ProfileProgressionPlugin.NOTIFICATION_PROJECT_ID_KEY),
        equalTo(String.valueOf(testProjectModel.getId())));
    assertThat(
        "Notification doesn't contain project key",
        notification.getFieldValue(ProfileProgressionPlugin.NOTIFICATION_PROJECT_KEY_KEY),
        equalTo(testProjectModel.getKey()));

    // check that message mentioned other project
    assertThat(
        "Notification message doesn't contain key of project blocking progression",
        notification.getFieldValue(ProfileProgressionPlugin.NOTIFICATION_MESSAGE_KEY),
        containsString(testProjectModel2.getKey()));
  }