public void testFrom103_1() throws Exception {
    TimeZone defaultTZ = TimeZone.getDefault();

    // use UTC for this test
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    copyFromClasspathToFile(
        "/org/sonatype/nexus/configuration/upgrade/103-1/nexus-103.xml", getNexusConfiguration());

    // trick: copying by nexus.xml the tasks.xml too
    copyFromClasspathToFile(
        "/org/sonatype/nexus/configuration/upgrade/103-1/tasks.xml",
        new File(new File(getNexusConfiguration()).getParentFile(), "tasks.xml"));

    Configuration configuration =
        configurationUpgrader.loadOldConfiguration(new File(getNexusConfiguration()));

    // set back to the default timezone
    TimeZone.setDefault(defaultTZ);

    assertEquals(Configuration.MODEL_VERSION, configuration.getVersion());

    // 6 repos 3 groups
    assertEquals(6 + 3, configuration.getRepositories().size());

    assertEquals(2, configuration.getRepositoryGrouping().getPathMappings().size());

    resultIsFine("/org/sonatype/nexus/configuration/upgrade/103-1/nexus-103.xml", configuration);
    securityResultIsFine(
        "/org/sonatype/nexus/configuration/upgrade/103-1/security-configuration-103.xml");
  }
  private void setupEmailConfig() throws IOException, XmlPullParserException {
    FileInputStream fis = null;
    Configuration config = null;
    try {
      fis = new FileInputStream(this.getNexusConfiguration());

      NexusConfigurationXpp3Reader reader = new NexusConfigurationXpp3Reader();
      config = reader.read(fis);

      config.getSmtpConfiguration().setPort(this.emailServerPort);
      config.getSmtpConfiguration().setHostname("localhost");
      // config.getSmtpConfiguration().setDebugMode( true );

    } finally {
      IOUtil.close(fis);
    }

    // now write it back out
    FileWriter writer = null;

    try {
      writer = new FileWriter(this.getNexusConfiguration());
      new NexusConfigurationXpp3Writer().write(writer, config);
    } finally {
      IOUtil.close(writer);
    }
  }
  @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));
  }
  public void testSaveGlobalProxyConfiguration() throws Exception {
    Configuration config = nexusConfiguration.getConfigurationModel();

    assertEquals(null, config.getGlobalHttpProxySettings());
    assertTrue(!globalHttpProxySettings.isEnabled());

    globalHttpProxySettings.setHostname("testhost.proxy.com");
    globalHttpProxySettings.setPort(1234);

    nexusConfiguration.saveConfiguration();

    // force reload
    nexusConfiguration.loadConfiguration(true);

    config = nexusConfiguration.getConfigurationModel();

    String proxyHostName =
        nexusConfiguration.getGlobalRemoteStorageContext().getRemoteProxySettings().getHostname();

    int proxyPort =
        nexusConfiguration.getGlobalRemoteStorageContext().getRemoteProxySettings().getPort();

    assertEquals(
        nexusConfiguration.getConfigurationModel().getGlobalHttpProxySettings().getProxyHostname(),
        proxyHostName);

    assertEquals(
        nexusConfiguration.getConfigurationModel().getGlobalHttpProxySettings().getProxyPort(),
        proxyPort);
  }
  public void testLoadConfiguration() throws Exception {
    // get it
    Configuration config = nexusConfiguration.getConfigurationModel();

    // check it for default value
    assertEquals("smtp-host", config.getSmtpConfiguration().getHostname());

    // modify it
    nexusEmailer.setSMTPHostname("NEW-HOST");

    // save it
    nexusConfiguration.saveConfiguration();

    // replace it again with default "from behind"
    InputStreamFacade isf =
        new InputStreamFacade() {

          public InputStream getInputStream() throws IOException {
            return getClass().getResourceAsStream("/META-INF/nexus/nexus.xml");
          }
        };
    FileUtils.copyStreamToFile(isf, new File(getNexusConfiguration()));

    // force reload
    nexusConfiguration.loadConfiguration(true);

    // get the config
    config = nexusConfiguration.getConfigurationModel();

    // it again contains default value, coz we overwritten it before
    assertEquals("smtp-host", config.getSmtpConfiguration().getHostname());
    assertEquals("smtp-host", nexusEmailer.getSMTPHostname());
  }
  public void testFrom143() throws Exception {
    copyFromClasspathToFile(
        "/org/sonatype/nexus/configuration/upgrade/nexus-143.xml", getNexusConfiguration());

    Configuration configuration =
        configurationUpgrader.loadOldConfiguration(new File(getNexusConfiguration()));

    assertEquals(Configuration.MODEL_VERSION, configuration.getVersion());

    resultIsFine("/org/sonatype/nexus/configuration/upgrade/nexus-143.xml", configuration);
  }
  public void testNEXUS1710() throws Exception {
    copyFromClasspathToFile(
        "/org/sonatype/nexus/configuration/upgrade/nexus1710/nexus.xml", getNexusConfiguration());

    Configuration configuration =
        configurationUpgrader.loadOldConfiguration(new File(getNexusConfiguration()));

    assertEquals(Configuration.MODEL_VERSION, configuration.getVersion());

    resultIsFine("/org/sonatype/nexus/configuration/upgrade/nexus1710/nexus.xml", configuration);
    securityResultIsFine(
        "/org/sonatype/nexus/configuration/upgrade/nexus1710/security-configuration-1710.xml");
  }
  public void testFromDECInt() throws Exception {
    copyFromClasspathToFile(
        "/org/sonatype/nexus/configuration/upgrade/nexus-001-3.xml", getNexusConfiguration());

    Configuration configuration =
        configurationUpgrader.loadOldConfiguration(new File(getNexusConfiguration()));

    assertEquals(Configuration.MODEL_VERSION, configuration.getVersion());

    assertEquals(7 + 2, configuration.getRepositories().size());

    assertEquals(2, configuration.getRepositoryGrouping().getPathMappings().size());

    resultIsFine("/org/sonatype/nexus/configuration/upgrade/nexus-001-3.xml", configuration);
    securityResultIsFine(
        "/org/sonatype/nexus/configuration/upgrade/security-configuration-001-3.xml");
  }
  public void testNEXUS2212SaveInvalidConfig() throws Exception {
    Configuration nexusConfig = nexusConfiguration.getConfigurationModel();

    CRepository centralCRepo = null;

    for (CRepository cRepo : nexusConfig.getRepositories()) {
      if (cRepo.getId().equals("central")) {
        centralCRepo = cRepo;
        break;
      }
    }

    assertNotNull(centralCRepo);

    centralCRepo.setLocalStatus(LocalStatus.OUT_OF_SERVICE.name());
    nexusConfiguration.saveConfiguration();
  }
  @SuppressWarnings("unchecked")
  public void verifyTargetsConfig(List<RepositoryTargetResource> targetResources)
      throws IOException {
    // check the nexus.xml
    Configuration config = getTest().getNexusConfigUtil().getNexusConfig();

    List<CRepositoryTarget> repoTargets = config.getRepositoryTargets();

    // TODO: we can't check the size unless we reset the config after each run...
    // check to see if the size matches
    // Assert.assertTrue( "Configuration had a different number: (" + repoTargets.size()
    // + ") of targets then expected: (" + targetResources.size() + ")",
    // repoTargets.size() == targetResources.size() );

    // look for the target by id

    for (Iterator<RepositoryTargetResource> iter = targetResources.iterator(); iter.hasNext(); ) {
      RepositoryTargetResource targetResource = iter.next();
      boolean found = false;

      for (Iterator<CRepositoryTarget> iterInner = repoTargets.iterator(); iterInner.hasNext(); ) {
        CRepositoryTarget repositoryTarget = iterInner.next();

        if (targetResource.getId().equals(repositoryTarget.getId())) {
          found = true;
          Assert.assertEquals(targetResource.getId(), repositoryTarget.getId());
          Assert.assertEquals(targetResource.getContentClass(), repositoryTarget.getContentClass());
          Assert.assertEquals(targetResource.getName(), repositoryTarget.getName());
          // order doesn't matter
          Assert.assertEquals(
              new HashSet<String>(targetResource.getPatterns()),
              new HashSet<String>(repositoryTarget.getPatterns()));

          break;
        }
      }

      if (!found) {

        Assert.fail(
            "Target with ID: " + targetResource.getId() + " could not be found in configuration.");
      }
    }
  }
  public void testFrom103_2() throws Exception {
    // same as above, but we have no tasks.xml
    copyFromClasspathToFile(
        "/org/sonatype/nexus/configuration/upgrade/103-2/nexus-103.xml", getNexusConfiguration());

    Configuration configuration =
        configurationUpgrader.loadOldConfiguration(new File(getNexusConfiguration()));

    assertEquals(Configuration.MODEL_VERSION, configuration.getVersion());

    // 6 repos, 1 shadow, 2 groups
    assertEquals(6 + 1 + 2, configuration.getRepositories().size());

    assertEquals(2, configuration.getRepositoryGrouping().getPathMappings().size());

    resultIsFine("/org/sonatype/nexus/configuration/upgrade/103-2/nexus-103.xml", configuration);
    securityResultIsFine(
        "/org/sonatype/nexus/configuration/upgrade/103-2/security-configuration-103.xml");
  }
  public void verifyCompleteTargetsConfig(List<RepositoryTargetListResource> targets)
      throws IOException {
    // check the nexus.xml
    Configuration config = getTest().getNexusConfigUtil().getNexusConfig();

    List<CRepositoryTarget> repoTargets = config.getRepositoryTargets();
    // check to see if the size matches
    Assert.assertTrue(
        repoTargets.size() == targets.size(),
        "Configuration had a different number: ("
            + repoTargets.size()
            + ") of targets then expected: ("
            + targets.size()
            + ")");

    // look for the target by id

    for (Iterator<RepositoryTargetListResource> iter = targets.iterator(); iter.hasNext(); ) {
      RepositoryTargetListResource targetResource = iter.next();
      boolean found = false;

      for (Iterator<CRepositoryTarget> iterInner = repoTargets.iterator(); iterInner.hasNext(); ) {
        CRepositoryTarget repositoryTarget = iterInner.next();

        if (targetResource.getId().equals(repositoryTarget.getId())) {
          found = true;
          Assert.assertEquals(targetResource.getId(), repositoryTarget.getId());
          Assert.assertEquals(targetResource.getContentClass(), repositoryTarget.getContentClass());
          Assert.assertEquals(targetResource.getName(), repositoryTarget.getName());

          break;
        }
      }

      if (!found) {

        Assert.fail(
            "Target with ID: " + targetResource.getId() + " could not be found in configuration.");
      }
    }
  }
  @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));
  }