@Test
  public void testBad2() throws Exception {
    // get start with the default config
    this.copyDefaultConfigToPlace();
    this.copyResource("/META-INF/nexus/default-oss-nexus.xml", getNexusConfiguration());
    Configuration config = this.loadNexusConfig(new File(this.getNexusConfiguration()));

    // make it bad

    // invalid policy
    CRepository invalidPolicyRepo = config.getRepositories().get(0);
    Xpp3Dom externalConfig = (Xpp3Dom) invalidPolicyRepo.getExternalConfiguration();
    ExternalConfigUtil.setNodeValue(externalConfig, "repositoryPolicy", "badPolicy");

    // duplicate the repository id
    for (CRepository repo : config.getRepositories()) {
      if (!repo.getId().equals("central")) {
        // duplicate
        repo.setId("central");
        break;
      }
    }

    // TODO: add more errors here

    // now validate it
    ValidationResponse response = underTest.validateModel(new ValidationRequest(config));

    assertThat(response.isValid(), is(false));
    assertThat(response.isModified(), is(false));

    assertThat(response.getValidationErrors(), hasSize(greaterThan(0)));
    assertThat(response.getValidationWarnings(), hasSize(0));
  }
  @Test
  public void repoEmptyName() {
    ApplicationValidationContext ctx = new ApplicationValidationContext();
    CRepository repo = new CRepository();
    repo.setId("id");
    repo.setLocalStatus(LocalStatus.IN_SERVICE.toString());

    ValidationResponse response = underTest.validateRepository(ctx, repo);

    assertThat(response.isValid(), is(true));
    assertThat(response.isModified(), is(true));

    assertThat(response.getValidationErrors(), hasSize(0));
    assertThat(response.getValidationWarnings(), hasSize(1));
    ValidationMessage actual = response.getValidationWarnings().get(0);
    assertThat(actual, hasKey("id"));
  }
  @Test
  public void testNexus1710Good() throws Exception {
    // this is after fix: groupId is appended by "-group" to resolve clash
    ValidationResponse response =
        underTest.validateModel(
            new ValidationRequest(
                getConfigurationFromStream(
                    getClass()
                        .getResourceAsStream(
                            "/org/sonatype/nexus/configuration/upgrade/nexus1710/nexus.xml.result"))));

    assertThat(response.isValid(), is(true));
    assertThat(response.isModified(), is(false));

    assertThat(response.getValidationErrors(), hasSize(0));
    assertThat(response.getValidationWarnings(), hasSize(0));
  }
  @Test
  public void repoClashShadowId() {
    ApplicationValidationContext ctx = new ApplicationValidationContext();
    ctx.addExistingRepositoryShadowIds();
    ctx.getExistingRepositoryShadowIds().add("id");
    CRepository repo = new CRepository();
    repo.setId("id");
    repo.setLocalStatus(LocalStatus.IN_SERVICE.toString());
    repo.setName("name");

    ValidationResponse response = underTest.validateRepository(ctx, repo);

    assertThat(response.isValid(), is(false));
    assertThat(response.isModified(), is(false));

    assertThat(response.getValidationErrors(), hasSize(1));
    assertThat(response.getValidationWarnings(), hasSize(0));
    assertThat(response.getValidationErrors().get(0), hasKey("id"));
  }
Esempio n. 5
0
  /**
   * A method to append a validation response to this validation response. The errors list and
   * warnings list are simply appended, and the isValid is logically AND-ed and isModified is
   * logically OR-ed.
   */
  public void append(ValidationResponse validationResponse) {
    for (ValidationMessage msg : validationResponse.getValidationErrors()) {
      if (getValidationError(msg.getKey()) != null) {
        msg.setKey(msg.getKey() + "(" + key++ + ")");
      }

      addValidationError(msg);
    }

    for (ValidationMessage msg : validationResponse.getValidationWarnings()) {
      if (getValidationWarning(msg.getKey()) != null) {
        msg.setKey(msg.getKey() + "(" + key++ + ")");
      }

      addValidationWarning(msg);
    }

    setValid(isValid() && validationResponse.isValid());

    setModified(isModified() || validationResponse.isModified());
  }
  @Test
  public void testBad1() throws Exception {
    // get start with the default config
    this.copyDefaultConfigToPlace();
    Configuration config = this.loadNexusConfig(new File(this.getNexusConfiguration()));

    // make it bad

    // remove the name from a repository
    CRepository missingNameRepo = (CRepository) config.getRepositories().get(0);
    missingNameRepo.setName(null);

    // TDOD add 2 more warnings

    // wrong shadow type
    CRepository badShadow = new DefaultCRepository();
    badShadow.setId("badShadow");
    badShadow.setName("Does not follow");
    badShadow.setProviderRole(ShadowRepository.class.getName());
    badShadow.setProviderHint("m2-m1-shadow");
    // Manipulate the dom
    Xpp3Dom externalConfig = new Xpp3Dom("externalConfiguration");
    badShadow.setExternalConfiguration(externalConfig);
    ExternalConfigUtil.setNodeValue(externalConfig, "masterRepositoryId", "non-existent");
    config.addRepository(badShadow);

    // now validate it
    ValidationResponse response = underTest.validateModel(new ValidationRequest(config));

    assertThat(response.getValidationWarnings(), hasSize(1));
    assertThat(response.getValidationErrors(), hasSize(0));

    // codehaus-snapshots has no name, it will be defaulted
    assertThat(response.isModified(), is(true));

    assertThat(response.isValid(), is(true));
  }
  @Test
  public void testNexus1710Bad() throws Exception {

    // this one is easy because you can compare:
    // /org/sonatype/nexus/configuration/upgrade/nexus1710/nexus.xml.result-bad
    // with
    // /org/sonatype/nexus/configuration/upgrade/nexus1710/nexus.xml.result
    // and you have the diff, and you already have to manually update the good one.

    // this was before fix: groupId/repoId name clash
    ValidationResponse response =
        underTest.validateModel(
            new ValidationRequest(
                getConfigurationFromStream(
                    getClass()
                        .getResourceAsStream(
                            "/org/sonatype/nexus/configuration/upgrade/nexus1710/nexus.xml.result-bad"))));

    assertThat(response.isValid(), is(false));
    assertThat(response.isModified(), is(false));

    assertThat(response.getValidationErrors(), hasSize(1));
    assertThat(response.getValidationWarnings(), hasSize(0));
  }