@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());
  }
  @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()));
  }
Example #3
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 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 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);
  }