public void testGetIndex() throws Exception {
    req.setupGetRequestURI("/my/namespace/foo/");
    req.setupGetServletPath("/my/namespace/foo/");
    req.setupGetAttribute(null);
    req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    req.setupGetMethod("GET");

    ActionMapping mapping = mapper.getMapping(req, configManager);

    assertEquals("/my/namespace", mapping.getNamespace());
    assertEquals("foo/", mapping.getName());
    assertEquals("index", mapping.getMethod());
  }
  public void testPostCreate() throws Exception {
    req.setupGetRequestURI("/my/namespace/bar/1/foo/");
    req.setupGetServletPath("/my/namespace/bar/1/foo/");
    req.setupGetAttribute(null);
    req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    req.setupGetMethod("POST");

    ActionMapping mapping = mapper.getMapping(req, configManager);

    assertEquals("/my/namespace", mapping.getNamespace());
    assertEquals("foo/", mapping.getName());
    assertEquals("create", mapping.getMethod());
    assertEquals(1, mapping.getParams().size());
    assertEquals("1", mapping.getParams().get("bar"));
  }
  public void testGetEdit() throws Exception {
    mapper.setIdParameterName("id");
    mapper.setAllowDynamicMethodCalls("true");
    req.setupGetRequestURI("/my/namespace/foo/3!edit");
    req.setupGetServletPath("/my/namespace/foo/3!edit");
    req.setupGetAttribute(null);
    req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    req.setupGetMethod("GET");

    ActionMapping mapping = mapper.getMapping(req, configManager);

    assertEquals("/my/namespace", mapping.getNamespace());
    assertEquals("foo/3", mapping.getName());
    assertEquals("edit", mapping.getMethod());
    assertEquals("3", mapping.getParams().get("id"));
  }
  public void testPutUpdateWithIdParam() throws Exception {

    mapper.setIdParameterName("id");
    req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
    req.setupGetServletPath("/my/namespace/bar/1/foo/2");
    req.setupGetAttribute(null);
    req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    req.setupGetMethod("PUT");

    ActionMapping mapping = mapper.getMapping(req, configManager);

    assertEquals("/my/namespace", mapping.getNamespace());
    assertEquals("foo", mapping.getName());
    assertEquals("update", mapping.getMethod());
    assertEquals(2, mapping.getParams().size());
    assertEquals("1", mapping.getParams().get("bar"));
    assertEquals("2", mapping.getParams().get("id"));
  }
  /**
   * FIXME fails after new threadbased implementation
   *
   * @throws NoSuchFieldException
   * @throws IllegalAccessException
   */
  @Test
  public void testUpdateRequest() throws NoSuchFieldException, IllegalAccessException {
    MockFileBasedActiveBitPreservation mockabp = new MockFileBasedActiveBitPreservation();
    MockHttpServletRequest request = new MockHttpServletRequest();

    Locale l = new Locale("da");
    mockabp.calls.clear();

    // Setup to run find-missing-files
    request = new MockHttpServletRequest();
    request.setupAddParameter(
        dk.netarkivet.archive.webinterface.Constants.BITARCHIVE_NAME_PARAM,
        new String[] {Replica.getReplicaFromId("ONE").getName()});

    request.setupAddParameter(
        dk.netarkivet.archive.webinterface.Constants.UPDATE_TYPE_PARAM,
        new String[] {dk.netarkivet.archive.webinterface.Constants.FIND_MISSING_FILES_OPTION});

    BitpreserveFileState.processUpdateRequest(WebinterfaceTestCase.getDummyPageContext(l, request));

    // TODO something here to avoid the failure!!

    // assertTrue("One calls to Find Missing Files expected",
    // mockabp.calls.containsKey(FIND_MISSING_FILES));
    // assertFalse("No calls to Find Checksum expected",
    // mockabp.calls.containsKey(FIND_CHECKSUM));
    // mockabp.calls.clear();
    //
    // // Setup to run find-checksum
    // request = new MockHttpServletRequest();
    // request.setupAddParameter(dk.netarkivet.archive.webinterface.Constants.BITARCHIVE_NAME_PARAM,
    // new String[]{Replica.getReplicaFromId("ONE").getName()});
    // request.setupAddParameter(dk.netarkivet.archive.webinterface.Constants.UPDATE_TYPE_PARAM,
    // new String[]{dk.netarkivet.archive.webinterface.Constants.CHECKSUM_OPTION});
    //
    // BitpreserveFileState.processUpdateRequest(WebinterfaceTestCase.getDummyPageContext(l,
    // request));
    //
    // assertFalse("No calls to Find Missing Files expected",
    // mockabp.calls.containsKey(FIND_MISSING_FILES));
    // assertTrue("One calls to Find Checksum expected",
    // mockabp.calls.containsKey(FIND_CHECKSUM));
    // mockabp.calls.clear();
  }
  /** FIXME Fails in Hudson */
  public void failingTestExecute() {
    File arcFile = new File(TestInfo.BATCH_DIR, "MimeUrlSearch.jar");
    assertTrue(arcFile.isFile());

    Settings.set(
        "settings.common.batch.batchjobs.batchjob.class",
        "dk.netarkivet.common.utils.batch.UrlSearch");
    Settings.set("settings.common.batch.batchjobs.batchjob.jarfile", arcFile.getAbsolutePath());

    MockHttpServletRequest request =
        new MockHttpServletRequest() {
          @Override
          public Locale getLocale() {
            return new Locale("en");
          }

          @Override
          public int getRemotePort() {
            return 0; // To change body of implemented methods use File | Settings | File Templates.
          }

          @Override
          public String getLocalName() {
            return null; // To change body of implemented methods use File | Settings | File
                         // Templates.
          }

          @Override
          public String getLocalAddr() {
            return null; // To change body of implemented methods use File | Settings | File
                         // Templates.
          }

          @Override
          public int getLocalPort() {
            return 0; // To change body of implemented methods use File | Settings | File Templates.
          }
        };
    request.setupAddParameter(Constants.FILETYPE_PARAMETER, BatchFileType.Metadata.toString());
    request.setupAddParameter(Constants.JOB_ID_PARAMETER, "1234567890");
    request.setupAddParameter(
        Constants.BATCHJOB_PARAMETER, "dk.netarkivet.common.utils.batch.UrlSearch");
    request.setupAddParameter(Constants.REPLICA_PARAMETER, "BarOne");
    request.setupAddParameter("arg1", "DUMMY-ARG");
    request.setupAddParameter("arg2", "url");
    request.setupAddParameter("arg3", "mimetype");

    Locale l = new Locale("en");
    JspWriterMockup out = new JspWriterMockup();

    PageContext context = new WebinterfaceTestCase.TestPageContext(request, out, l);
    BatchGUI.execute(context);
  }
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    mapper = new Restful2ActionMapper();
    mapper.setExtensions("");
    req = new MockHttpServletRequest();
    req.setupGetParameterMap(new HashMap());
    req.setupGetContextPath("/my/namespace");

    config = new DefaultConfiguration();
    PackageConfig pkg = new PackageConfig.Builder("myns").namespace("/my/namespace").build();
    PackageConfig pkg2 = new PackageConfig.Builder("my").namespace("/my").build();
    config.addPackageConfig("mvns", pkg);
    config.addPackageConfig("my", pkg2);
    configManager =
        new ConfigurationManager() {
          public Configuration getConfiguration() {
            return config;
          }
        };
  }
  public void testDeleteRemoveWithFakeDelete() throws Exception {

    req.setupGetRequestURI("/my/namespace/bar/1/foo/2");
    req.setupGetServletPath("/my/namespace/bar/1/foo/2");
    req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE");
    req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE");
    req.setupGetAttribute(null);
    req.addExpectedGetAttributeName("javax.servlet.include.servlet_path");
    req.setupGetMethod("POST");

    ActionMapping mapping = mapper.getMapping(req, configManager);

    assertEquals("/my/namespace", mapping.getNamespace());
    assertEquals("foo/2", mapping.getName());
    assertEquals("remove", mapping.getMethod());
    assertEquals(1, mapping.getParams().size());
    assertEquals("1", mapping.getParams().get("bar"));
  }
  @Test
  public void testProcessMissingRequest() throws Exception {

    Settings.set(
        ArchiveSettings.DIR_ARCREPOSITORY_BITPRESERVATION, TestInfo.WORKING_DIR.getAbsolutePath());
    Settings.set(ArchiveSettings.DIRS_ARCREPOSITORY_ADMIN, TestInfo.WORKING_DIR.getAbsolutePath());

    // Ensure that a admin data exists before we start.
    AdminData.getUpdateableInstance();

    MockFileBasedActiveBitPreservation mockabp = new MockFileBasedActiveBitPreservation();
    MockHttpServletRequest request = new MockHttpServletRequest();
    String replicaID1 = "ONE";
    String replicaID2 = "TWO";
    String filename1 = "foo";
    String filename2 = "bar";
    Locale defaultLocale = new Locale("da");

    // First test a working set of params
    Map<String, String[]> args = new HashMap<String, String[]>();
    args.put(
        ADD_COMMAND,
        new String[] {
          Replica.getReplicaFromId(replicaID1).getName() + STRING_FILENAME_SEPARATOR + filename1
        });
    request.setupAddParameter(
        ADD_COMMAND,
        new String[] {
          Replica.getReplicaFromId(replicaID1).getName() + STRING_FILENAME_SEPARATOR + filename1
        });
    args.put(GET_INFO_COMMAND, new String[] {filename1});
    request.setupAddParameter(GET_INFO_COMMAND, new String[] {filename1});
    args.put(BITARCHIVE_NAME_PARAM, new String[] {Replica.getReplicaFromId(replicaID1).getName()});
    request.setupAddParameter(
        BITARCHIVE_NAME_PARAM, new String[] {Replica.getReplicaFromId(replicaID1).getName()});
    request.setupGetParameterMap(args);
    request.setupGetParameterNames(new Vector<String>(args.keySet()).elements());
    Map<String, PreservationState> status =
        BitpreserveFileState.processMissingRequest(
            WebinterfaceTestCase.getDummyPageContext(defaultLocale, request), new StringBuilder());
    assertEquals("Should have one call to reestablish", 1, mockabp.getCallCount(ADD_METHOD));
    assertEquals(
        "Should have one call to getFilePreservationStatus",
        1,
        mockabp.getCallCount(GET_INFO_METHOD));
    assertEquals("Should have one info element (with mock results)", null, status.get(filename1));

    // Check that we can call without any params
    mockabp.calls.clear();
    request = new MockHttpServletRequest();
    args.clear();
    args.put(BITARCHIVE_NAME_PARAM, new String[] {Replica.getReplicaFromId(replicaID1).getName()});
    request.setupAddParameter(
        BITARCHIVE_NAME_PARAM, new String[] {Replica.getReplicaFromId(replicaID1).getName()});
    request.setupGetParameterMap(args);
    status =
        BitpreserveFileState.processMissingRequest(
            WebinterfaceTestCase.getDummyPageContext(defaultLocale, request), new StringBuilder());
    assertEquals("Should have no call to restablish", 0, mockabp.getCallCount(ADD_METHOD));
    assertEquals(
        "Should have no call to getFilePreservationStatus",
        0,
        mockabp.getCallCount(GET_INFO_METHOD));
    assertEquals("Should have no status", 0, status.size());

    // Check that we can handle more than one call to each and that the
    // args are correct.
    mockabp.calls.clear();
    request = new MockHttpServletRequest();
    args.clear();
    args.put(BITARCHIVE_NAME_PARAM, new String[] {Replica.getReplicaFromId(replicaID2).getName()});
    request.setupAddParameter(
        BITARCHIVE_NAME_PARAM, new String[] {Replica.getReplicaFromId(replicaID2).getName()});
    request.setupAddParameter(
        ADD_COMMAND,
        new String[] {
          Replica.getReplicaFromId(replicaID2).getName() + STRING_FILENAME_SEPARATOR + filename1,
          Replica.getReplicaFromId(replicaID2).getName() + STRING_FILENAME_SEPARATOR + filename1
        });
    args.put(
        ADD_COMMAND,
        new String[] {
          Replica.getReplicaFromId(replicaID2).getName() + STRING_FILENAME_SEPARATOR + filename1,
          Replica.getReplicaFromId(replicaID2).getName() + STRING_FILENAME_SEPARATOR + filename1
        });
    request.setupAddParameter(GET_INFO_COMMAND, new String[] {filename1, filename2, filename1});
    args.put(GET_INFO_COMMAND, new String[] {filename1, filename2, filename1});
    request.setupGetParameterMap(args);
    status =
        BitpreserveFileState.processMissingRequest(
            WebinterfaceTestCase.getDummyPageContext(defaultLocale, request), new StringBuilder());
    assertEquals("Should have two calls to restablish", 2, mockabp.getCallCount(ADD_METHOD));
    assertEquals(
        "Should have three calls to getFilePreservationStatus",
        3,
        mockabp.getCallCount(GET_INFO_METHOD));
    assertEquals("Should have two info elements", 2, status.size());
    assertEquals("Should have info for filename1", null, status.get(filename1));
    assertEquals("Should have info for filename2", null, status.get(filename2));

    // Iterator<String> it = mockabp.calls.get(ADD_METHOD).iterator();
    // while (it.hasNext()) {
    // System.out.println(it.next());
    // }

    CollectionAsserts.assertIteratorEquals(
        "Should have the args given add",
        Arrays.asList(new String[] {filename1 + "," + replicaID2, filename1 + "," + replicaID2})
            .iterator(),
        mockabp.calls.get(ADD_METHOD).iterator());

    CollectionAsserts.assertIteratorEquals(
        "Should have the args given info",
        Arrays.asList(new String[] {filename1, filename2, filename1}).iterator(),
        mockabp.calls.get(GET_INFO_METHOD).iterator());
  }