@Test
  public void repoEmptyId() {
    ApplicationValidationContext ctx = new ApplicationValidationContext();

    CRepository repo = new CRepository();
    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"));
  }
コード例 #2
0
  public void updateCatalogEntry(String catalogId, String entryId, CCatalogEntry entry)
      throws InvalidConfigurationException {
    ValidationResponse vr = validator.validateCatalogEntry(entry);
    if (vr.getValidationErrors().size() > 0) {
      throw new InvalidConfigurationException(vr);
    }

    lock.lock();

    try {

      CCatalog catalog = getCatalog(catalogId);
      CCatalogEntry current = getCatalogEntry(catalogId, entryId);

      if (current != null) {
        catalog.getEntries().remove(current);
        catalog.getEntries().add(entry);

        save();
      }

    } finally {
      lock.unlock();
    }
  }
  @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));
  }
コード例 #4
0
  public void createCatalog(CCatalog catalog) throws InvalidConfigurationException {
    lock.lock();

    try {
      ValidationResponse vr = validator.validateCatalog(catalog);
      if (vr.getValidationErrors().size() > 0) {
        throw new InvalidConfigurationException(vr);
      }

      getConfiguration().getCatalogs().add(catalog);

      save();
    } finally {
      lock.unlock();
    }
  }
  @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));
  }
コード例 #6
0
  public void addCatalogEntry(String catalogId, CCatalogEntry catalogEntry)
      throws InvalidConfigurationException {
    CCatalog catalog = readCatalog(catalogId);

    catalogEntry.setId(idGenerator.generateId());

    ValidationResponse vr = validator.validateCatalogEntry(catalogEntry);
    if (vr.getValidationErrors().size() > 0) {
      throw new InvalidConfigurationException(vr);
    }

    lock.lock();

    try {

      catalog.addEntry(catalogEntry);
      save();
    } finally {
      lock.unlock();
    }
  }
コード例 #7
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());
  }
コード例 #8
0
  public void createOrUpdateCatalog(CCatalog catalog) throws InvalidConfigurationException {
    ValidationResponse vr = validator.validateCatalog(catalog);
    if (vr.getValidationErrors().size() > 0) {
      throw new InvalidConfigurationException(vr);
    }

    lock.lock();

    try {

      CCatalog current = getCatalog(catalog.getId());

      if (current != null) {
        getConfiguration().getCatalogs().remove(current);
      }

      getConfiguration().getCatalogs().add(catalog);

      save();
    } finally {
      lock.unlock();
    }
  }
  @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));
  }