@Test
  public void testUpdateAsset() throws NoSuchFileException {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("simple content").type("bpmn2").name("process").location("/");

    String id = repository.createAsset(builder.getAsset());

    Collection<Asset> foundAsset = repository.listAssets("/", new FilterByExtension("bpmn2"));

    assertNotNull(foundAsset);
    assertEquals(1, foundAsset.size());

    builder.content("updated content").uniqueId(id);

    id = repository.updateAsset(builder.getAsset(), "", "");

    foundAsset = repository.listAssetsRecursively("/", new FilterByFileName("process.bpmn2"));

    assertNotNull(foundAsset);
    assertEquals(1, foundAsset.size());

    String content = ((Asset<String>) repository.loadAsset(id)).getAssetContent();
    assertNotNull(content);
    assertEquals("updated content", content);
  }
  @Test
  public void testLoadDictionary() throws Exception {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    profile.setRepository(repository);
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder
        .content("test dictionary content")
        .type("json")
        .name("processdictionary")
        .location("/global");
    repository.createAsset(builder.getAsset());
    // setup parameters
    Map<String, String> params = new HashMap<String, String>();

    params.put("action", "load");
    params.put("profile", "jbpm");

    DictionaryServlet dictionaryServlet = new DictionaryServlet();
    dictionaryServlet.setProfile(profile);

    dictionaryServlet.init(new TestServletConfig(new TestServletContext(repository)));
    TestHttpServletResponse response = new TestHttpServletResponse();
    dictionaryServlet.doPost(new TestHttpServletRequest(params), response);

    String dictionaryContent = new String(response.getContent());
    assertNotNull(dictionaryContent);
    assertEquals("test dictionary content", dictionaryContent);
  }
  @Test
  public void testDirectoryExists() {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    boolean rootFolderExists = repository.directoryExists("/test");
    assertFalse(rootFolderExists);

    Directory directoryId = repository.createDirectory("/test");
    assertNotNull(directoryId);
    assertEquals("test", directoryId.getName());
    assertEquals("/", directoryId.getLocation());
    assertNotNull(directoryId.getUniqueId());

    rootFolderExists = repository.directoryExists("/test");
    assertTrue(rootFolderExists);

    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Byte);
    builder.content("simple content".getBytes()).type("png").name("test").location("/test");

    String id = repository.createAsset(builder.getAsset());

    assertNotNull(id);

    boolean assetPathShouldNotExists = repository.directoryExists("/test/test.png");
    assertFalse(assetPathShouldNotExists);
  }
  private JSONObject storeTaskFormInRepository(
      String formType, String taskName, String packageName, String formValue, Repository repository)
      throws Exception {

    repository.deleteAssetFromPath(
        packageName + "/" + taskName + TASKFORM_NAME_EXTENSION + "." + formType);

    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Byte);
    builder
        .location(packageName)
        .name(taskName + TASKFORM_NAME_EXTENSION)
        .type(formType)
        .content(formValue.getBytes("UTF-8"));

    repository.createAsset(builder.getAsset());

    Asset newFormAsset =
        repository.loadAssetFromPath(
            packageName + "/" + taskName + TASKFORM_NAME_EXTENSION + "." + formType);

    String uniqueId = newFormAsset.getUniqueId();
    if (Base64Backport.isBase64(uniqueId)) {
      byte[] decoded = Base64.decodeBase64(uniqueId);
      try {
        uniqueId = new String(decoded, "UTF-8");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
    }

    JSONObject retObj = new JSONObject();
    retObj.put("formid", uniqueId);

    return retObj;
  }
  @Test
  public void testGetAssetSourceByPath() throws Exception {

    Repository repository =
        new VFSRepository(fileSystem, producer.getIoService(), producer.getActiveFileSystems());
    ((VFSRepository) repository).init();
    profile.setRepository(repository);
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder
        .content("custom editors content")
        .type("bpmn2")
        .name("testprocess")
        .location("/defaultPackage");
    String id = repository.createAsset(builder.getAsset());
    // setup parameters
    Map<String, String> params = new HashMap<String, String>();

    params.put("profile", "jbpm");
    params.put("action", "getassetsource");
    params.put("assetlocation", "/defaultPackage/testprocess.bpmn2");
    params.put("loadoption", "optionbypath");
    boolean assetExists = repository.assetExists(id);
    assertTrue(assetExists);

    AssetServiceServlet assetServiceServlet = new AssetServiceServlet();
    assetServiceServlet.setProfile(profile);

    assetServiceServlet.init(new TestServletConfig(new TestServletContext(repository)));
    TestHttpServletResponse response = new TestHttpServletResponse();
    assetServiceServlet.doPost(new TestHttpServletRequest(params), response);

    String jsonResponse = new String(response.getContent());
    assertNotNull(jsonResponse);
    assertEquals(jsonResponse, "custom editors content\n");
  }
  @Ignore // git based vfs does not yet support move
  @Test
  public void testMoveDirectory() throws NoSuchFileException {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    Directory sourceDir = repository.createDirectory("/source");

    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("simple content").type("bpmn2").name("process").location("/source");

    String id = repository.createAsset(builder.getAsset());

    Collection<Asset> foundAsset = repository.listAssets("/source", new FilterByExtension("bpmn2"));

    assertNotNull(foundAsset);
    assertEquals(1, foundAsset.size());

    boolean assetExistsBeforeDelete = repository.assetExists(id);
    assertTrue(assetExistsBeforeDelete);

    boolean copied = repository.moveDirectory("/source", "/target", null);
    assertTrue(copied);

    foundAsset = repository.listAssets("/target/source", new FilterByExtension("bpmn2"));

    assertNotNull(foundAsset);
    assertEquals(1, foundAsset.size());

    boolean assetExists = repository.assetExists("/target/source/process.bpmn2");
    assertTrue(assetExists);

    boolean movedDirectoryExists = repository.directoryExists("/source");
    assertFalse(movedDirectoryExists);
  }
  @Test
  public void testListAssetsRecursively() {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("simple content").type("bpmn2").name("process").location("/1/2/3/4/5/6");

    String id = repository.createAsset(builder.getAsset());

    Collection<Asset> foundAsset =
        repository.listAssetsRecursively("/", new FilterByExtension("bpmn2"));

    assertNotNull(foundAsset);
    assertEquals(4, foundAsset.size());
  }
  @Override
  public Path createProcess(final Path context, final String fileName) {
    final Path path = paths.convert(paths.convert(context).resolve(fileName), false);

    String location = paths.convert(path).getParent().toString();
    String name = path.getFileName();
    String processId = buildProcessId(location, name);

    String processContent = PROCESS_STUB.replaceAll("\\$\\{processid\\}", processId);

    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(name);
    builder.location(location).content(processContent).uniqueId(path.toURI());
    Asset<String> processAsset = builder.getAsset();

    repository.createAsset(processAsset);

    return path;
  }
  private String createAssetIfNotExisting(
      Repository repository, String location, String name, String type, byte[] content) {
    try {
      boolean assetExists = repository.assetExists(location + "/" + name + "." + type);
      if (!assetExists) {
        // create theme asset
        AssetBuilder assetBuilder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Byte);
        assetBuilder.content(content).location(location).name(name).type(type).version("1.0");

        Asset<byte[]> customEditorsAsset = assetBuilder.getAsset();

        return repository.createAsset(customEditorsAsset);
      }

    } catch (Exception e) {
      logger.error(e.getMessage());
    }

    return null;
  }
  @Test
  public void testAssetExists() throws NoSuchFileException {

    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    Collection<Asset> assets = repository.listAssets("/");
    assertNotNull(assets);
    for (Asset aset : assets) {
      System.out.println(aset.getAssetLocation() + " " + aset.getFullName());
    }
    assertEquals(0, assets.size());

    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("simple content").type("txt").name("test").location("/");

    String id = repository.createAsset(builder.getAsset());

    assertNotNull(id);

    boolean assetExists = repository.assetExists(id);
    assertTrue(assetExists);
  }
  @Test
  public void testSaveFormAsset() throws Exception {
    Repository repository =
        new VFSRepository(fileSystem, producer.getIoService(), producer.getActiveFileSystems());
    ((VFSRepository) repository).init();
    profile.setRepository(repository);
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("bpmn2 content").type("bpmn2").name("testprocess").location("/defaultPackage");
    String uniqueId = repository.createAsset(builder.getAsset());
    // setup parameters
    Map<String, String> params = new HashMap<String, String>();
    params.put("uuid", uniqueId);
    params.put("action", "save");
    params.put("profile", "jbpm");
    params.put("taskname", "evaluate");
    params.put("tfvalue", "this is simple task content");

    TaskFormsEditorServlet taskFormsEditorServlet = new TaskFormsEditorServlet();
    taskFormsEditorServlet.setProfile(profile);

    taskFormsEditorServlet.init(new TestServletConfig(new TestServletContext(repository)));

    taskFormsEditorServlet.doPost(
        new TestHttpServletRequest(params), new TestHttpServletResponse());

    Collection<Asset> forms =
        repository.listAssets("/defaultPackage", new FilterByExtension("flt"));
    assertNotNull(forms);
    assertEquals(1, forms.size());
    Iterator<Asset> assets = forms.iterator();

    Asset asset1 = assets.next();
    assertEquals("evaluate-taskform", asset1.getName());
    assertEquals("/defaultPackage", asset1.getAssetLocation());

    Asset<String> form1 = repository.loadAsset(asset1.getUniqueId());
    assertNotNull(form1.getAssetContent());
    assertEquals("this is simple task content\n", form1.getAssetContent());
  }
  @Test
  public void testGetAssetInfoById() throws Exception {

    Repository repository =
        new VFSRepository(fileSystem, producer.getIoService(), producer.getActiveFileSystems());
    ((VFSRepository) repository).init();
    profile.setRepository(repository);
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder
        .content("custom editors content")
        .type("bpmn2")
        .name("testprocess")
        .location("/defaultPackage");
    String id = repository.createAsset(builder.getAsset());
    // setup parameters
    Map<String, String> params = new HashMap<String, String>();

    params.put("profile", "jbpm");
    params.put("action", "getassetinfo");
    params.put("assetid", id);
    params.put("loadoption", "optionbyid");
    boolean assetExists = repository.assetExists(id);
    assertTrue(assetExists);

    AssetServiceServlet assetServiceServlet = new AssetServiceServlet();
    assetServiceServlet.setProfile(profile);

    assetServiceServlet.init(new TestServletConfig(new TestServletContext(repository)));
    TestHttpServletResponse response = new TestHttpServletResponse();
    assetServiceServlet.doPost(new TestHttpServletRequest(params), response);

    String jsonResponse = new String(response.getContent());
    assertNotNull(jsonResponse);
    assertTrue(
        jsonResponse.indexOf(
                "\"location\":\"/defaultPackage\",\"description\":\"\",\"name\":\"testprocess\",\"owner\":\"\",\"type\":\"bpmn2\",\"fullname\":\"testprocess.bpmn2\"")
            != -1);
  }
  @Test
  public void testDeleteAssetFromPath() throws NoSuchFileException {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("simple content").type("bpmn2").name("process").location("/");

    String id = repository.createAsset(builder.getAsset());

    Collection<Asset> foundAsset = repository.listAssets("/", new FilterByExtension("bpmn2"));

    assertNotNull(foundAsset);
    assertEquals(1, foundAsset.size());

    boolean assetExistsBeforeDelete = repository.assetExists(id);
    assertTrue(assetExistsBeforeDelete);

    boolean deleted = repository.deleteAssetFromPath("/process.bpmn2");
    assertTrue(deleted);

    boolean assetExists = repository.assetExists(id);
    assertFalse(assetExists);
  }
  @Test
  public void testStoreSingleTextAsset() throws NoSuchFileException {

    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    Collection<Asset> assets = repository.listAssets("/");
    assertNotNull(assets);
    assertEquals(0, assets.size());

    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("simple content").type("txt").name("test").location("/");

    String id = repository.createAsset(builder.getAsset());

    assertNotNull(id);

    Asset<String> asset = repository.loadAsset(id);

    assertEquals("txt", asset.getAssetType());
    assertEquals("test", asset.getName());
    assertEquals("test.txt", asset.getFullName());
    assertEquals("/", asset.getAssetLocation());
    assertEquals("simple content", asset.getAssetContent());
  }
  @Test
  public void testStoreSingleBinaryAssetSpaceInName() throws NoSuchFileException {

    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    Collection<Asset> assets = repository.listAssets("/");
    assertNotNull(assets);
    assertEquals(0, assets.size());

    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Byte);
    builder.content("simple content".getBytes()).type("png").name("test asset").location("/");

    String id = repository.createAsset(builder.getAsset());

    assertNotNull(id);

    Asset<byte[]> asset = repository.loadAsset(id);

    assertEquals("png", asset.getAssetType());
    assertEquals("test asset", asset.getName());
    assertEquals("test asset.png", asset.getFullName());
    assertEquals("/", asset.getAssetLocation());
    assertFalse(asset.getAssetContent().length == 0);
  }
  @Test
  public void testLoadFormAsset() throws Exception {
    Repository repository =
        new VFSRepository(fileSystem, producer.getIoService(), producer.getActiveFileSystems());
    ((VFSRepository) repository).init();
    profile.setRepository(repository);
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("bpmn2 content").type("bpmn2").name("testprocess").location("/defaultPackage");
    String uniqueId = repository.createAsset(builder.getAsset());

    AssetBuilder builderForm = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builderForm
        .content("this is simple task content")
        .type("flt")
        .name("evaluate-taskform")
        .location("/defaultPackage");
    String uniqueIdForm = repository.createAsset(builderForm.getAsset());

    // setup parameters
    Map<String, String> params = new HashMap<String, String>();
    params.put("uuid", uniqueId);
    params.put("action", "load");
    params.put("profile", "jbpm");
    params.put("taskname", "evaluate");
    params.put("tfvalue", "this is simple task content");

    TaskFormsEditorServlet taskFormsEditorServlet = new TaskFormsEditorServlet();
    taskFormsEditorServlet.setProfile(profile);

    taskFormsEditorServlet.init(new TestServletConfig(new TestServletContext(repository)));
    TestHttpServletResponse response = new TestHttpServletResponse();
    taskFormsEditorServlet.doPost(new TestHttpServletRequest(params), response);

    String formData = new String(response.getContent());
    System.out.println(formData);
    assertEquals("this is simple task content\n", formData);
  }
  private String getTaskFormFromRepository(
      String formType, String taskName, String packageName, Repository repository) {
    try {
      Asset<String> formAsset =
          repository.loadAssetFromPath(
              packageName + "/" + taskName + TASKFORM_NAME_EXTENSION + "." + formType);

      if (formType.equals(FORMMODELER_FILE_EXTENSION)) {
        String uniqueId = formAsset.getUniqueId();
        if (Base64Backport.isBase64(uniqueId)) {
          byte[] decoded = Base64.decodeBase64(uniqueId);
          try {
            uniqueId = new String(decoded, "UTF-8");
          } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
          }
        }
        return formAsset.getName() + "." + formAsset.getAssetType() + "|" + uniqueId;
      } else {
        return formAsset.getAssetContent();
      }
    } catch (NoSuchFileException anfe) {
      try {
        String formValue = "";
        if (formType.equals(FORMMODELER_FILE_EXTENSION)) {
          formValue =
              formModelerService.buildEmptyFormXML(
                  taskName + TASKFORM_NAME_EXTENSION + "." + formType);
        }

        AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Byte);
        builder
            .location(packageName)
            .name(taskName + TASKFORM_NAME_EXTENSION)
            .type(formType)
            .content(formValue.getBytes("UTF-8"));
        repository.createAsset(builder.getAsset());

        Asset<String> newFormAsset =
            repository.loadAssetFromPath(
                packageName + "/" + taskName + TASKFORM_NAME_EXTENSION + "." + formType);

        String uniqueId = newFormAsset.getUniqueId();
        if (Base64Backport.isBase64(uniqueId)) {
          byte[] decoded = Base64.decodeBase64(uniqueId);
          try {
            uniqueId = new String(decoded, "UTF-8");
          } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
          }
        }

        if (formType.equals(FORMMODELER_FILE_EXTENSION)) {
          return newFormAsset.getName() + "." + newFormAsset.getAssetType() + "|" + uniqueId;
        } else {
          return formValue;
        }
      } catch (Exception e) {
        e.printStackTrace();
        _logger.error(e.getMessage());
      }
    }
    return "false";
  }
  @Test
  public void testProprocess() {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    profile.setRepository(repository);
    // prepare folders that will be used
    repository.createDirectory("/myprocesses");
    repository.createDirectory("/global");

    // prepare process asset that will be used to preprocess
    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Text);
    builder.content("bpmn2 content").type("bpmn2").name("process").location("/myprocesses");
    String uniqueId = repository.createAsset(builder.getAsset());

    // create instance of preprocessing unit
    JbpmPreprocessingUnit preprocessingUnitVFS =
        new JbpmPreprocessingUnit(new TestServletContext(), "/", null);

    // setup parameters
    Map<String, String> params = new HashMap<String, String>();
    params.put("uuid", uniqueId);

    // run preprocess
    preprocessingUnitVFS.preprocess(
        new TestHttpServletRequest(params), null, new TestIDiagramProfile(repository), null);

    // validate results
    Collection<Asset> globalAssets = repository.listAssets("/global");
    assertNotNull(globalAssets);
    assertEquals(30, globalAssets.size());
    repository.assetExists("/global/backboneformsinclude.fw");
    repository.assetExists("/global/backbonejsinclude.fw");
    repository.assetExists("/global/cancelbutton.fw");
    repository.assetExists("/global/checkbox.fw");
    repository.assetExists("/global/customeditors.json");
    repository.assetExists("/global/div.fw");
    repository.assetExists("/global/dropdownmenu.fw");
    repository.assetExists("/global/fieldset.fw");
    repository.assetExists("/global/form.fw");
    repository.assetExists("/global/handlebarsinclude.fw");
    repository.assetExists("/global/htmlbasepage.fw");
    repository.assetExists("/global/image.fw");
    repository.assetExists("/global/jqueryinclude.fw");
    repository.assetExists("/global/jquerymobileinclude.fw");
    repository.assetExists("/global/link.fw");
    repository.assetExists("/global/mobilebasepage.fw");
    repository.assetExists("/global/orderedlist.fw");
    repository.assetExists("/global/passwordfield.fw");
    repository.assetExists("/global/radiobutton.fw");
    repository.assetExists("/global/script.fw");
    repository.assetExists("/global/submitbutton.fw");
    repository.assetExists("/global/table.fw");
    repository.assetExists("/global/textarea.fw");
    repository.assetExists("/global/textfield.fw");
    repository.assetExists("/global/themes.json");
    repository.assetExists("/global/unorderedlist.fw");
    repository.assetExists("/global/defaultemailicon.gif");
    repository.assetExists("/global/defaultlogicon.gif");
    repository.assetExists("/global/defaultservicenodeicon.png");
    repository.assetExists("/global/.gitignore");

    Collection<Asset> defaultStuff = repository.listAssets("/myprocesses");
    assertNotNull(defaultStuff);
    assertEquals(3, defaultStuff.size());
    repository.assetExists("/myprocesses/WorkDefinitions.wid");
    // this is the process asset that was created for the test but let's check it anyway
    repository.assetExists("/myprocesses/process.bpmn2");
    repository.assetExists("/myprocesses/.gitignore");
  }