KBArchiveFactory { public KBArchive createKBArchive(long groupId, ZipReader zipReader) throws PortalException { List<String> entries = zipReader.getEntries(); if (entries == null) { throw new KBArticleImportException( "The uploaded file is not a ZIP archive or it is corrupted"); } KBGroupServiceConfiguration kbGroupServiceConfiguration = _configurationProvider.getConfiguration( KBGroupServiceConfiguration.class, new GroupServiceSettingsLocator( groupId, KBConstants.SERVICE_NAME)); Collections.sort(entries); KBArchiveState kbArchiveState = new KBArchiveState(zipReader); for (String entry : entries) { String[] articleExtensions = kbGroupServiceConfiguration.markdownImporterArticleExtensions(); if (!ArrayUtil.exists(articleExtensions, entry::endsWith)) { continue; } Path entryPath = Paths.get(entry); Path entryParentPath = entryPath.getParent(); kbArchiveState.setCurrentFolder(entryParentPath); String markdownImporterArticleIntro = kbGroupServiceConfiguration.markdownImporterArticleIntro(); String entryFileName = String.valueOf(entryPath.getFileName()); if (entryFileName.endsWith(markdownImporterArticleIntro)) { kbArchiveState.setCurrentFolderIntroFile(entryPath); } else { kbArchiveState.addCurrentFolderFile(entryPath); } } return new KBArchiveImpl(kbArchiveState.getFolders()); }  KBArchive createKBArchive(long groupId, ZipReader zipReader); @Reference(unbind = "-") void setConfigurationProvider(
		ConfigurationProvider configurationProvider);  }
@Test public void testEmptyZipReaderReturnsNoFolders() throws Exception { Mockito.when( _zipReader.getEntries() ).thenReturn( Collections.<String>emptyList() ); KBArchive kbArchive = _kbArchiveFactory.createKBArchive( 1234L, _zipReader); Collection<KBArchive.Folder> folders = kbArchive.getFolders(); Assert.assertTrue(folders.toString(), folders.isEmpty()); }
@Test public void testZipReaderWithALoneIntroFileReturnsFolder() throws Exception { Mockito.when( _zipReader.getEntries() ).thenReturn( Arrays.asList("/intro.md") ); KBArchive kbArchive = _kbArchiveFactory.createKBArchive( 1234L, _zipReader); Collection<KBArchive.Folder> folders = kbArchive.getFolders(); Iterator<KBArchive.Folder> iterator = folders.iterator(); KBArchive.Folder folder = iterator.next(); Assert.assertEquals(StringPool.SLASH, folder.getName()); KBArchive.File introFile = folder.getIntroFile(); Assert.assertEquals("/intro.md", introFile.getName()); }
@Test public void testZipReaderWithIntroFileAndArticleFiles() throws Exception { Mockito.when( _zipReader.getEntries() ).thenReturn( Arrays.asList("/intro.md", "/x/a.md", "/y/z/b.md", "/y/z/c.md") ); KBArchive kbArchive = _kbArchiveFactory.createKBArchive( 1234L, _zipReader); Collection<KBArchive.Folder> folders = kbArchive.getFolders(); Assert.assertEquals(folders.toString(), 3, folders.size()); Iterator<KBArchive.Folder> folderIterator = folders.iterator(); KBArchive.Folder folder0 = folderIterator.next(); Assert.assertEquals(StringPool.SLASH, folder0.getName()); KBArchive.File introFile0 = folder0.getIntroFile(); Assert.assertEquals("/intro.md", introFile0.getName()); Assert.assertNull(folder0.getParentFolderIntroFile()); Collection<KBArchive.File> files0 = folder0.getFiles(); Assert.assertTrue(files0.isEmpty()); KBArchive.Folder folder1 = folderIterator.next(); Assert.assertEquals("/x", folder1.getName()); Assert.assertNull(folder1.getIntroFile()); Assert.assertNotNull(folder1.getParentFolderIntroFile()); Collection<KBArchive.File> files1 = folder1.getFiles(); Assert.assertEquals(files1.toString(), 1, files1.size()); KBArchive.Folder folder2 = folderIterator.next(); Assert.assertEquals("/y/z", folder2.getName()); Assert.assertNull(folder2.getIntroFile()); Assert.assertNull(folder2.getParentFolderIntroFile()); Collection<KBArchive.File> files2 = folder2.getFiles(); Assert.assertEquals(files2.toString(), 2, files2.size()); }
@Test(expected = KBArticleImportException.class) public void testZipReaderWithNoEntriesFails() throws Exception { Mockito.when( _zipReader.getEntries() ).thenReturn( null ); _kbArchiveFactory.createKBArchive(1234L, _zipReader); }
@Test public void testZipReaderWithNoIntroFileReturnsFolder() throws Exception { Mockito.when( _zipReader.getEntries() ).thenReturn( Arrays.asList("/a.md", "/b.md") ); KBArchive kbArchive = _kbArchiveFactory.createKBArchive( 1234L, _zipReader); Collection<KBArchive.Folder> folders = kbArchive.getFolders(); Assert.assertEquals(folders.toString(), 1, folders.size()); Iterator<KBArchive.Folder> foldersIterator = folders.iterator(); KBArchive.Folder folder = foldersIterator.next(); Assert.assertNull(folder.getIntroFile()); Collection<KBArchive.File> files = folder.getFiles(); Assert.assertEquals(files.toString(), 2, files.size()); Iterator<KBArchive.File> filesIterator = files.iterator(); KBArchive.File file0 = filesIterator.next(); Assert.assertEquals("/a.md", file0.getName()); KBArchive.File file1 = filesIterator.next(); Assert.assertEquals("/b.md", file1.getName()); }
@Test public void testZipReaderWithNoValidFilesReturnsNoFolders() throws Exception { Mockito.when( _zipReader.getEntries() ).thenReturn( Arrays.asList("/a.txt", "/b.txt") ); KBArchive kbArchive = _kbArchiveFactory.createKBArchive( 1234L, _zipReader); Collection<KBArchive.Folder> folders = kbArchive.getFolders(); Assert.assertTrue(folders.toString(), folders.isEmpty()); }