FolderLoader implements BundleEntityLoader { @Override public void load(Bundle bundle, Element element) { final Element folderElement = getSingleChildElement(getSingleChildElement(element, RESOURCE), FOLDER); String name = getSingleChildElementTextContent(folderElement, NAME); final String id = folderElement.getAttribute(ATTRIBUTE_ID); final String parentFolderID = folderElement.getAttribute(ATTRIBUTE_FOLDER_ID); final Folder parentFolder; if (ROOT_FOLDER_ID.equals(id)) { parentFolder = null; } else { parentFolder = ServiceAndPolicyLoaderUtil.getFolder(bundle, parentFolderID); } name = CharacterBlacklistUtil.encodeName(name); Folder folder = new Folder(); folder.setId(id); folder.setName(name); folder.setParentFolder(parentFolder); folder.setPath(getPath(folder).toString()); bundle.getFolders().put(folder.getPath(), folder); }  @Override void load(Bundle bundle, Element element); @Override String getEntityType();  }
@Test void loadWithoutParent() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); assertThrows(BundleLoadException.class, () -> loader.load(bundle, createFolderXml(doc, TEST_FOLDER_1, TEST_FOLDER_1, Folder.ROOT_FOLDER_ID))); }
@Test void loadWithMoreThanOneParent() { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); Folder f1 = new Folder(); f1.setId(TEST_FOLDER_1); bundle.getFolders().put(TEST_FOLDER_1, f1); bundle.getFolders().put(TEST_FOLDER_1 + "_1", f1); assertThrows(BundleLoadException.class, () -> loader.load(bundle, createFolderXml(doc, TEST_FOLDER_2, TEST_FOLDER_2, TEST_FOLDER_1))); }
@Test void loadWithInvalidCharName() throws UnsupportedEncodingException { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); final String encodedRoot = URLEncoder.encode(Folder.ROOT_FOLDER_NAME, "UTF-8"); final String encodedTestFolder = URLEncoder.encode(TEST_FOLDER_1 + "/1", "UTF-8"); loader.load(bundle, createFolderXml(doc, Folder.ROOT_FOLDER_NAME, Folder.ROOT_FOLDER_ID, null)); loader.load(bundle, createFolderXml(doc, TEST_FOLDER_1 + "/1", TEST_FOLDER_1, Folder.ROOT_FOLDER_ID)); assertNotNull(bundle.getFolders().get(encodedRoot)); assertEquals(encodedRoot, bundle.getFolders().get(encodedRoot).getName()); assertNotNull(bundle.getFolders().get(encodedTestFolder)); assertEquals(encodedTestFolder, bundle.getFolders().get(encodedTestFolder).getName()); }
@Test void load() throws UnsupportedEncodingException { Document doc = DocumentTools.INSTANCE.getDocumentBuilder().newDocument(); Bundle bundle = new Bundle(); loader.load(bundle, createFolderXml(doc, Folder.ROOT_FOLDER_NAME, Folder.ROOT_FOLDER_ID, null)); loader.load(bundle, createFolderXml(doc, TEST_FOLDER_1, TEST_FOLDER_1, Folder.ROOT_FOLDER_ID)); loader.load(bundle, createFolderXml(doc, TEST_FOLDER_2, TEST_FOLDER_2, TEST_FOLDER_1)); assertFalse(bundle.getFolders().isEmpty()); assertEquals(3, bundle.getFolders().size()); final String encodedRoot = URLEncoder.encode(Folder.ROOT_FOLDER_NAME, "UTF-8"); Folder root = bundle.getFolders().get(encodedRoot); assertNotNull(root); Assertions.assertEquals(Folder.ROOT_FOLDER_ID, root.getId()); Assertions.assertEquals(encodedRoot, root.getName()); assertNull(root.getParentFolder()); Assertions.assertEquals(encodedRoot, root.getPath()); Folder folder1 = bundle.getFolders().get(TEST_FOLDER_1); assertNotNull(folder1); assertEquals(TEST_FOLDER_1, folder1.getId()); assertEquals(TEST_FOLDER_1, folder1.getName()); assertEquals(root, folder1.getParentFolder()); assertEquals(TEST_FOLDER_1, folder1.getPath()); String f2Path = Paths.get(TEST_FOLDER_1, TEST_FOLDER_2).toString(); Folder folder2 = bundle.getFolders().get(f2Path); assertNotNull(folder2); assertEquals(TEST_FOLDER_2, folder2.getId()); assertEquals(TEST_FOLDER_2, folder2.getName()); assertEquals(folder1, folder2.getParentFolder()); assertEquals(f2Path, folder2.getPath()); }