/**
   * Verify that a remote answering with '403 Forbidden' will not be auto-blocked with and without a
   * local artifact cached.
   */
  @Test
  public void testNoAutoblockOn403() throws Exception {
    stopProxy();

    server = Server.withPort(proxyPort).start();

    server.serve("/").withBehaviours(content("ok"));
    server.serve("/remote/release-proxy-repo-1/nexus4593/*").withBehaviours(get(getTestFile("/")));

    downloadArtifact(GavUtil.newGav("nexus4593", "artifact", "1.0.0"), "target");

    assertThat(ProxyMode.valueOf(getStatus().getProxyMode()), is(ProxyMode.ALLOW));

    server.serve("/*").withBehaviours(error(403));

    // download will not fail because we have a local copy cached, but the remote will be hit b/c
    // maxAge is set to 0
    downloadArtifact(GavUtil.newGav("nexus4593", "artifact", "1.0.0"), "target");

    assertThat(ProxyMode.valueOf(getStatus().getProxyMode()), is(ProxyMode.ALLOW));

    try {
      downloadArtifact(GavUtil.newGav("g", "a", "403"), "target");
      assertThat("should fail b/c of 403", false);
    } catch (IOException e) {
      // expected, remote will answer with 403
    }

    assertThat(ProxyMode.valueOf(getStatus().getProxyMode()), is(ProxyMode.ALLOW));
  }
  /**
   * Verify that a remote answering with '401' will still be auto-blocked.
   *
   * <p>This test will change repo status to auto blocked.
   */
  @Test(dependsOnMethods = "testNoAutoblockOn403")
  public void testAutoblockOn401() throws Exception {
    startErrorServer(401);

    try {
      downloadArtifact(GavUtil.newGav("g", "a", "401"), "target");
    } catch (IOException e) {
      // expected, remote will answer with 401
    }

    RepositoryStatusResource status = getStatus();

    assertThat(ProxyMode.valueOf(status.getProxyMode()), is(ProxyMode.BLOCKED_AUTO));
  }
  @Test
  public void hostedTestRelRepoAndDeployFile() throws IOException, Exception {
    // create a release repository
    String repoId = "tmp-releases";
    RepositoryResource repo = new RepositoryResource();
    repo.setProvider("ivy");
    repo.setFormat("maven2");
    repo.setRepoPolicy("release");
    repo.setChecksumPolicy("ignore");
    repo.setBrowseable(false);

    repo.setId(repoId);
    repo.setName(repoId);
    repo.setRepoType("hosted");
    repo.setWritePolicy(RepositoryWritePolicy.ALLOW_WRITE.name());
    repo.setDownloadRemoteIndexes(true);
    repo.setBrowseable(true);
    repo.setRepoPolicy(RepositoryPolicy.RELEASE.name());
    repo.setChecksumPolicy(ChecksumPolicy.IGNORE.name());

    repo.setIndexable(true); // being sure!!!
    repoUtil.createRepository(repo);

    // firstly assert that repository is there
    repo = (RepositoryResource) repoUtil.getRepository(repoId);
    Assert.assertTrue(repo.isIndexable());

    TaskScheduleUtil.waitForAllTasksToStop();

    // then deploy a release artifact
    Gav gav = GavUtil.newGav("ivyRemover", "auth-mod", "1.0");
    getDeployUtils().deployUsingGavWithRest(repoId, gav, getTestFile("simple-artifact.jar"));

    TaskScheduleUtil.waitForAllTasksToStop();
    getEventInspectorsUtil().waitForCalmPeriod();

    // secondly assert that the artifact is in the release repository
    List<NexusArtifact> result = getSearchMessageUtil().searchForGav(gav, repoId);
    Assert.assertEquals(
        result.size(), 1, "Results: \n" + XStreamFactory.getXmlXStream().toXML(result));

    // thirdly assert that there is at least alltogether one release artifact in the release
    // repository
    result = getSearchMessageUtil().searchFor(Collections.singletonMap("q", "ivyRemover"), repoId);
    Assert.assertEquals(
        result.size(), 1, "Results: \n" + XStreamFactory.getXmlXStream().toXML(result));
  }
 protected String getRelitiveArtifactPath(
     String groupId, String artifactId, String version, String extension, String classifier)
     throws FileNotFoundException {
   return GavUtil.getRelitiveArtifactPath(groupId, artifactId, version, extension, classifier);
 }
 protected String getRelitiveArtifactPath(Gav gav) throws FileNotFoundException {
   return GavUtil.getRelitiveArtifactPath(gav);
 }
  @Test
  public void testSnapRepoDeployFilesRunTaskDeleteIFRelease() throws IOException, Exception {

    // create a snapshot repository
    createSnapshotRepo(snapRepoId);

    // add some artifacts
    copyToSnapRepo(snapRepoId);

    TaskScheduleUtil.waitForAllTasksToStop();

    // update indexes for search
    RepositoryMessageUtil.updateIndexes(snapRepoId);

    TaskScheduleUtil.waitForAllTasksToStop();

    // set Remover task parameters 2 latest version snapshots should be left over
    ScheduledServicePropertyResource keepSnapshotsProp = new ScheduledServicePropertyResource();
    keepSnapshotsProp.setKey(IVYSnapshotRemovalTaskDescriptor.MIN_TO_KEEP_FIELD_ID);
    keepSnapshotsProp.setValue(String.valueOf(2));

    ScheduledServicePropertyResource ageProp = new ScheduledServicePropertyResource();
    ageProp.setKey(IVYSnapshotRemovalTaskDescriptor.KEEP_DAYS_FIELD_ID);
    ageProp.setValue(String.valueOf(0));

    // and if a release version exists it should be removed too
    ScheduledServicePropertyResource relProp = new ScheduledServicePropertyResource();
    relProp.setKey(IVYSnapshotRemovalTaskDescriptor.REMOVE_WHEN_RELEASED_FIELD_ID);
    relProp.setValue(Boolean.toString(true));

    ScheduledServicePropertyResource repos = new ScheduledServicePropertyResource();
    repos.setKey("IvyRepositoryId");
    repos.setValue(snapRepoId);
    TaskScheduleUtil.runTask(
        "IvySnapshotRemoval",
        IVYSnapshotRemovalTaskDescriptor.ID,
        repos,
        keepSnapshotsProp,
        ageProp,
        relProp);

    TaskScheduleUtil.waitForAllTasksToStop();
    getEventInspectorsUtil().waitForCalmPeriod();

    // firstly assert that there is the artifact "auth-api" version "0.8-102" left over with
    // ".source.jar" and ".jar"
    Gav gav = GavUtil.newGav("ivyRemover", "auth-api", "0.8-102");

    List<NexusArtifact> result = getSearchMessageUtil().searchForGav(gav, snapRepoId);

    Assert.assertEquals(
        result.size(), 2, "Results: \n" + XStreamFactory.getXmlXStream().toXML(result));

    // secondly assert that there is the artifact "auth-api" version "0.8-103" left over with
    // ".source.jar" and ".jar"
    gav = GavUtil.newGav("ivyRemover", "auth-api", "0.8-103");

    result = getSearchMessageUtil().searchForGav(gav, snapRepoId);
    Assert.assertEquals(
        result.size(), 2, "Results: \n" + XStreamFactory.getXmlXStream().toXML(result));

    // thirdly assert that there isn't the artifact "auth-mod" version "0.8-102" left over
    gav = GavUtil.newGav("ivyRemover", "auth-mod", "1.0-102");

    result = getSearchMessageUtil().searchForGav(gav, snapRepoId);
    Assert.assertEquals(
        result.size(), 0, "Results: \n" + XStreamFactory.getXmlXStream().toXML(result));

    // fourthly assert that there isn't the artifact "auth-api" version "0.8-103" left over
    gav = GavUtil.newGav("ivyRemover", "auth-mod", "1.0-103");

    result = getSearchMessageUtil().searchForGav(gav, snapRepoId);
    Assert.assertEquals(
        result.size(), 0, "Results: \n" + XStreamFactory.getXmlXStream().toXML(result));

    // fifthly assert that there are at least 2 of 8 snapshots with 2 jar files in the whole test
    // group, 6 removed
    result =
        getSearchMessageUtil().searchFor(Collections.singletonMap("q", "ivyRemover"), snapRepoId);
    Assert.assertEquals(
        result.size(), 4, "Results: \n" + XStreamFactory.getXmlXStream().toXML(result));
  }