@Ignore
  public void testGetFileText() throws Exception {
    loginAsRepositoryAdmin();
    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});
    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName});
    mp.defineInstance(IUnifiedRepository.class, repo);

    login("admin", mainTenant_1, new String[] {authenticatedAuthorityName});
    String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath();

    createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + "file.txt", "abcdefg");

    WebResource webResource = resource();

    ClientResponse r1 =
        webResource
            .path("repos/:public:file.txt/content")
            .accept(TEXT_PLAIN)
            .get(ClientResponse.class);
    assertResponse(r1, Status.OK, MediaType.TEXT_PLAIN);
    assertEquals("abcdefg", r1.getEntity(String.class));

    // check again but with no Accept header
    ClientResponse r2 =
        webResource.path("repos/:public:file.txt/content").get(ClientResponse.class);
    assertResponse(r2, Status.OK, MediaType.TEXT_PLAIN);
    assertEquals("abcdefg", r2.getEntity(String.class));

    // check again but with */*
    ClientResponse r3 =
        webResource
            .path("repos/:public:file.txt/content")
            .accept(TEXT_PLAIN)
            .accept(MediaType.WILDCARD)
            .get(ClientResponse.class);
    assertResponse(r3, Status.OK, MediaType.TEXT_PLAIN);
    assertEquals("abcdefg", r3.getEntity(String.class));

    cleanupUserAndRoles(mainTenant_1);
    cleanupUserAndRoles(systemTenant);
  }
  @Test
  public void testWriteBinaryFile() throws InterruptedException {
    final String str = "some binary text";
    final String fileName = "file.bn";

    // set object in PentahoSystem
    mp.defineInstance(IUnifiedRepository.class, repo);

    loginAsRepositoryAdmin();
    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});

    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1,
        "admin",
        "password",
        "",
        new String[] {adminAuthorityName, authenticatedAuthorityName});
    try {
      login(sysAdminUserName, systemTenant, new String[] {adminAuthorityName});
      login("admin", mainTenant_1, new String[] {adminAuthorityName, authenticatedAuthorityName});

      WebResource webResource = resource();
      final byte[] blob = str.getBytes();
      String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath();
      createTestFileBinary(publicFolderPath.replaceAll("/", ":") + ":" + fileName, blob);

      // the file might not actually be ready.. wait a second
      Thread.sleep(20000);

      ClientResponse response =
          webResource
              .path("repo/files/:public:file.bn")
              .accept(APPLICATION_OCTET_STREAM)
              .get(ClientResponse.class);
      assertResponse(response, Status.OK, APPLICATION_OCTET_STREAM);

      byte[] data = response.getEntity(byte[].class);
      assertEquals("contents of file incorrect/missing", str, new String(data));
    } catch (Exception ex) {
      TestCase.fail();
    } finally {
      cleanupUserAndRoles(mainTenant_1);
      cleanupUserAndRoles(systemTenant);
    }
  }
 protected void createTestFolder(String pathId) {
   WebResource webResource = resource();
   // webResource.path("repo/dirs/" + pathId).put();
   ClientResponse response =
       webResource.path("repo/dirs/" + pathId).type(TEXT_PLAIN).put(ClientResponse.class);
   assertResponse(response, Status.OK);
 }
 @Ignore
 public void b1_FileDNE() {
   WebResource webResource = resource();
   ClientResponse response =
       webResource.path("repos/xjunit/public/doesnotexist.txt").get(ClientResponse.class);
   assertResponse(response, ClientResponse.Status.NOT_FOUND);
 }
 @Ignore
 public void b1_PluginDNE() {
   WebResource webResource = resource();
   ClientResponse response =
       webResource.path("repos/non-existent-plugin/private/private.txt").get(ClientResponse.class);
   assertResponse(response, ClientResponse.Status.NOT_FOUND);
 }
 @Ignore
 public void b1_PrivateFile() {
   WebResource webResource = resource();
   ClientResponse response =
       webResource.path("repos/xjunit/private/private.txt").get(ClientResponse.class);
   assertResponse(response, ClientResponse.Status.FORBIDDEN);
 }
  @Ignore
  public void a2_HappyPath() {
    loginAsRepositoryAdmin();

    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});
    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName});
    mp.defineInstance(IUnifiedRepository.class, repo);

    login("admin", mainTenant_1, new String[] {authenticatedAuthorityName});
    String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath();

    createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + "test.xjunit", "abcdefg");
    createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + "js/test.js", "js content");
    createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + "test.css", "css content");

    WebResource webResource = resource();

    // load the css
    ClientResponse response =
        webResource.path("repos/:public:test.xjunit/test.css").get(ClientResponse.class);
    assertResponse(response, ClientResponse.Status.OK, "text/css");
    assertEquals(
        "contents of file incorrect/missing", "css content", response.getEntity(String.class));

    // load the js
    response = webResource.path("repos/:public:test.xjunit/js/test.js").get(ClientResponse.class);
    assertResponse(response, ClientResponse.Status.OK, "text/javascript");
    assertEquals(
        "contents of file incorrect/missing", "js content", response.getEntity(String.class));
  }
 protected void createTestFileBinary(String pathId, byte[] data) {
   WebResource webResource = resource();
   ClientResponse response =
       webResource
           .path("repo/files/" + pathId)
           .type(APPLICATION_OCTET_STREAM)
           .put(ClientResponse.class, new String(data));
   assertResponse(response, Status.OK);
 }
 @Ignore
 public void c1_HappyPath() {
   WebResource webResource = resource();
   ClientResponse response =
       webResource.path("repos/test-plugin/public/file.txt").get(ClientResponse.class);
   assertResponse(response, ClientResponse.Status.OK, TEXT_PLAIN);
   assertEquals(
       "contents of file incorrect/missing", "test text", response.getEntity(String.class));
 }
  @Test
  public void testWriteTextFile() throws Exception {
    final String text = "sometext";

    mp.defineInstance(IUnifiedRepository.class, repo);
    final String fileName = "file.txt";

    loginAsRepositoryAdmin();
    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});

    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName});
    try {
      login(
          sysAdminUserName,
          systemTenant,
          new String[] {adminAuthorityName, authenticatedAuthorityName});

      login("admin", mainTenant_1, new String[] {authenticatedAuthorityName});

      WebResource webResource = resource();
      String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath();
      createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + fileName, text);

      ClientResponse response =
          webResource
              .path("repo/files/:public:" + fileName)
              .accept(TEXT_PLAIN)
              .get(ClientResponse.class);
      assertResponse(response, Status.OK, TEXT_PLAIN);
      assertEquals("contents of file incorrect/missing", text, response.getEntity(String.class));

    } catch (Throwable th) {
      TestCase.fail();
    } finally {
      cleanupUserAndRoles(mainTenant_1);
      cleanupUserAndRoles(systemTenant);
    }
  }
  @Ignore
  public void b3_HappyPath_GET_withMimeType() throws PlatformInitializationException {
    stubGetFile(repo, "/public/test.xjunit");

    WebResource webResource = resource();

    // get the output of the .xjunit file (should invoke the content generator)
    ClientResponse response =
        webResource.path("repos/xjunit/report").accept("application/pdf").get(ClientResponse.class);

    assertResponse(response, ClientResponse.Status.OK, "application/pdf; charset=UTF-8");
  }
  @Test
  public void testCopyFiles() throws Exception {
    mp.defineInstance(IUnifiedRepository.class, repo);
    loginAsRepositoryAdmin();
    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});
    login(
        sysAdminUserName,
        systemTenant,
        new String[] {adminAuthorityName, authenticatedAuthorityName});

    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName});
    login("admin", mainTenant_1, new String[] {authenticatedAuthorityName});

    try {
      final String destFolderPath = "public:folder3:folder4";
      final String fileName = "file.txt";

      String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath();
      createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + fileName, "abcdefg");
      WebResource webResource = resource();
      RepositoryFile file =
          repo.getFile(ClientRepositoryPaths.getPublicFolderPath() + "/" + fileName);
      ClientResponse r =
          webResource
              .path("repo/files/" + destFolderPath + "/children")
              .accept(TEXT_PLAIN)
              .put(ClientResponse.class, file.getId());
      assertResponse(r, Status.OK);
    } catch (Throwable ex) {
      TestCase.fail();
    } finally {
      cleanupUserAndRoles(mainTenant_1);
      cleanupUserAndRoles(systemTenant);
    }
  }
  @Ignore
  public void a3_HappyPath_POST() {
    loginAsRepositoryAdmin();

    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});
    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName});
    mp.defineInstance(IUnifiedRepository.class, repo);

    login("admin", mainTenant_1, new String[] {authenticatedAuthorityName});
    String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath();

    createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + "test.xjunit", "abcdefg");

    WebResource webResource = resource();

    // get the output of the .junit file (should invoke the content generator)
    MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
    formData.add("testParam", "testParamValue");

    ClientResponse response =
        webResource
            .path("repos/:public:test.xjunit/viewer")
            .type(APPLICATION_FORM_URLENCODED)
            .post(ClientResponse.class, formData);
    assertResponse(response, Status.OK);
    assertEquals(
        "Content generator failed to provide correct output",
        "hello viewer content generator",
        response.getEntity(String.class));
  }
  // We should be testing the underlying class functionality in unit tests
  @Test
  public void testGetDirChildren() {
    loginAsRepositoryAdmin();
    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});
    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName});
    try {
      login("admin", mainTenant_1, new String[] {authenticatedAuthorityName});

      // set object in PentahoSystem
      mp.defineInstance(IUnifiedRepository.class, repo);
      final String fileName = "file.txt";
      createTestFile("/public".replaceAll("/", ":") + ":" + fileName, "abcdefg");
      WebResource webResource = resource();
      ClientResponse response =
          webResource
              .path("repo/files/public/children")
              .accept(APPLICATION_XML)
              .get(ClientResponse.class);

      assertResponse(response, Status.OK, APPLICATION_XML);

      String xml = response.getEntity(String.class);
      assertTrue(xml.startsWith("<?"));
    } catch (Throwable ex) {
      TestCase.fail();
    } finally {
      cleanupUserAndRoles(mainTenant_1);
      cleanupUserAndRoles(systemTenant);
    }
  }
  @Ignore
  public void c3_HappyPath_POST_withCommand() {
    WebResource webResource = resource();

    MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
    formData.add("testParam", "testParamValue");

    ClientResponse response =
        webResource
            .path("repos/test-plugin/testservice/dosomething")
            .type(APPLICATION_FORM_URLENCODED)
            .post(ClientResponse.class, formData);
    assertResponse(response, Status.OK);
    String expectedResponse =
        "hello this is service content generator servicing command dosomething";
    assertEquals(
        "Content generator failed to provide correct output",
        expectedResponse,
        response.getEntity(String.class));
  }
  // This is testing the Rest end points, we should instead be testing the underlying functionality
  // in unit tests
  @Test
  public void testBrowserDownload() {
    final String text = "abcdefg";

    // mock converters map
    StreamConverter streamConverter = new StreamConverter(repo);
    Map<String, Converter> converterMap = new HashMap<String, Converter>();
    converterMap.put("txt", streamConverter);

    // stub DefaultExportProcessor
    DefaultExportHandler defaultExportHandler = new DefaultExportHandler();
    defaultExportHandler.setConverters(converterMap);
    defaultExportHandler.setRepository(repo);

    loginAsRepositoryAdmin();
    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});
    login(
        sysAdminUserName,
        systemTenant,
        new String[] {adminAuthorityName, authenticatedAuthorityName});

    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName});
    try {
      login("admin", mainTenant_1, new String[] {authenticatedAuthorityName, adminAuthorityName});

      mp.defineInstance(IUnifiedRepository.class, repo);
      mp.defineInstance(DefaultExportHandler.class, defaultExportHandler);

      final String fileName = "file.txt";
      createTestFile("/public".replaceAll("/", ":") + ":" + fileName, text);

      // test download of file
      WebResource webResource = resource();
      webResource.path("repo/files/public:file.txt/download").get(ClientResponse.class);

      // test download of dir as a zip file
      ClientResponse r2 =
          webResource.path("repo/files/public:file.txt/download").get(ClientResponse.class);
      assertResponse(r2, Status.OK);
      JerseyTestUtil.assertResponseIsZip(r2);
    } catch (Throwable ex) {
      TestCase.fail();
    } finally {
      cleanupUserAndRoles(mainTenant_1);
      cleanupUserAndRoles(systemTenant);
    }
  }
  @Test
  public void testFileAcls() throws InterruptedException {
    loginAsRepositoryAdmin();
    ITenant systemTenant =
        tenantManager.createTenant(
            null,
            ServerRepositoryPaths.getPentahoRootFolderName(),
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName});
    ITenant mainTenant_1 =
        tenantManager.createTenant(
            systemTenant,
            MAIN_TENANT_1,
            adminAuthorityName,
            authenticatedAuthorityName,
            "Anonymous");
    userRoleDao.createUser(
        mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName});
    try {
      login("admin", mainTenant_1, new String[] {authenticatedAuthorityName});
      mp.defineInstance(IUnifiedRepository.class, repo);

      String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath();
      createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + "aclFile.txt", "abcdefg");

      WebResource webResource = resource();
      RepositoryFileAclDto fileAcls =
          webResource
              .path("repo/files/public:aclFile.txt/acl")
              .accept(APPLICATION_XML)
              .get(RepositoryFileAclDto.class);
      List<RepositoryFileAclAceDto> aces = fileAcls.getAces();
      assertEquals(2, aces.size());
      RepositoryFileAclAceDto ace = aces.get(0);
      assertEquals(authenticatedAuthorityName, ace.getRecipient());
      List<Integer> permissions = ace.getPermissions();
      assertEquals(1, permissions.size());
      Assert.assertTrue(permissions.contains(new Integer(0)));

      String authenticated = authenticatedAuthorityName;

      aces = new ArrayList<RepositoryFileAclAceDto>();
      ace = new RepositoryFileAclAceDto();
      ace.setRecipient(authenticated);
      ace.setRecipientType(1);
      permissions = new ArrayList<Integer>();
      permissions.add(2);
      ace.setPermissions(permissions);
      aces.add(ace);
      fileAcls.setAces(aces);

      ClientResponse putResponse2 =
          webResource
              .path("repo/files/public:aclFile.txt/acl")
              .type(APPLICATION_XML)
              .put(ClientResponse.class, fileAcls);
      assertResponse(putResponse2, Status.OK);
    } catch (Throwable ex) {
      TestCase.fail();
    } finally {
      cleanupUserAndRoles(mainTenant_1);
      cleanupUserAndRoles(systemTenant);
    }
  }
 protected void createTestFile(String pathId, String text) {
   WebResource webResource = resource();
   ClientResponse response =
       webResource.path("repo/files/" + pathId).type(TEXT_PLAIN).put(ClientResponse.class, text);
   assertResponse(response, Status.OK);
 }