ArtifactImporter { public ImportReport doImport(Import anImport) { return tryToImportAndGenerateReport(anImport, false); } ArtifactImporter(Repository<T> repository, ArtifactService<T> artifactService, Loader<T> loader, DependencyImporter... dependencyImporters); ImportReport forceImport(Import anImport); ImportReport doImport(Import anImport);  }
@Test public void should_import_artifact_located_on_disk() throws Exception { List<Widget> widgets = wMocks.mockWidgetsAsAddedDependencies(); Page page = pMocks.mockPageToBeImported(); pageImporter.doImport(anImport(pageImportPath)); verify(widgetRepository).saveAll(widgets); verify(pageRepository).updateLastUpdateAndSave(page); }
@Test public void should_return_an_import_report_containing_imported_element_and_imported_dependencies() throws Exception { List<Widget> addedWidgets = wMocks.mockWidgetsAsAddedDependencies(); List<Widget> overridenWidgets = wMocks.mockWidgetsAsOverridenDependencies(); Page page = pMocks.mockPageToBeImported(); ImportReport report = pageImporter.doImport(anImport(pageImportPath)); assertThat(report.getDependencies().getAdded().get("widget")).isEqualTo(addedWidgets); assertThat(report.getDependencies().getOverwritten().get("widget")).isEqualTo(overridenWidgets); assertThat(report.getElement()).isEqualTo(page); }
@Test public void should_return_an_import_report_saying_that_page_is_going_to_be_overwritten_when_element_already_exists_in_repository() throws Exception { Page page = pMocks.mockPageToBeImported(); Page existingPageInRepo = aPage().withUUID(page.getUUID()).withName("alreadyHere").build(); when(pageRepository.getByUUID(page.getUUID())).thenReturn(existingPageInRepo); when(pageRepository.get(existingPageInRepo.getId())).thenReturn(existingPageInRepo); ImportReport report = pageImporter.doImport(anImport(pageImportPath)); assertThat(report.getElement()).isEqualTo(page); assertThat(report.getOverwrittenElement()).isEqualTo(existingPageInRepo); assertThat(report.isOverwritten()).isTrue(); }
@Test public void should_return_an_import_report_saying_that_widget_is_going_to_be_overwritten_when_element_already_exists_in_repository() throws Exception { Widget widget = aWidget().id("aWidget").custom().build(); Widget existingWidgetInRepo = aWidget().id("aWidget").favorite().custom().build(); when(widgetLoader.load(widgetUnzippedPath.resolve("widget.json"))).thenReturn(widget); when(widgetRepository.exists(widget.getId())).thenReturn(true); when(widgetRepository.get(widget.getId())).thenReturn(existingWidgetInRepo); ImportReport report = widgetImporter.doImport(anImport(widgetImportPath)); assertThat(report.getElement()).isEqualTo(existingWidgetInRepo); assertThat(report.isOverwritten()).isTrue(); }
@Test public void should_return_an_import_report_saying_that_element_has_not_been_overwritten_when_element_does_not_exists_in_repository() throws Exception { Page page = pMocks.mockPageToBeImported(); when(pageRepository.getByUUID(page.getUUID())).thenReturn(null); ImportReport report = pageImporter.doImport(anImport(pageImportPath)); assertThat(report.getElement()).isEqualTo(page); assertThat(report.isOverwritten()).isFalse(); assertThat(report.getOverwrittenElement()).isNull(); }
@Test public void should_return_an_import_report_saying_that_element_has_been_imported_when_there_are_no_conflict() throws Exception { List<Widget> widgets = wMocks.mockWidgetsAsAddedDependencies(); Page page = pMocks.mockPageToBeImported(); ImportReport report = pageImporter.doImport(anImport(pageImportPath)); assertThat(report.getStatus()).isEqualTo(ImportReport.Status.IMPORTED); verify(widgetRepository).saveAll(widgets); verify(pageRepository).updateLastUpdateAndSave(page); }
@Test public void should_return_an_import_report_saying_that_element_has_not_been_imported_when_there_are_conflict() throws Exception { List<Widget> overriddenWidgets = wMocks.mockWidgetsAsOverridenDependencies(); Page page = pMocks.mockPageToBeImported(); ImportReport report = pageImporter.doImport(anImport(pageImportPath)); assertThat(report.getStatus()).isEqualTo(ImportReport.Status.CONFLICT); verify(widgetRepository, never()).saveAll(overriddenWidgets); verify(pageRepository, never()).updateLastUpdateAndSave(page); }
@Test public void should_throw_import_exception_when_there_is_no_resource_folder_in_import_path() throws Exception { Path newFolder = tempDir.newFolderPath("emptyFolder"); exception.expect(ImportException.class); exception.expect(hasType(UNEXPECTED_ZIP_STRUCTURE)); pageImporter.doImport(anImport(newFolder)); }
@Test(expected = ServerImportException.class) public void should_throw_server_import_exception_when_error_occurs_while_saving_files_in_repository() throws Exception { Page page = aPage().withId("aPage").build(); when(pageLoader.load(pageUnzippedPath.resolve("page.json"))).thenReturn(page); doThrow(RepositoryException.class).when(pageRepository).updateLastUpdateAndSave(page); pageImporter.doImport(anImport(pageImportPath)); }
@Test(expected = ServerImportException.class) public void should_throw_import_exception_when_an_error_occurs_while_getting_widgets() throws Exception { Files.createDirectory(pageUnzippedPath.resolve(WIDGETS_FOLDER)); when(widgetLoader.loadAllCustom(pageUnzippedPath.resolve(WIDGETS_FOLDER))).thenThrow(new IOException()); pageImporter.doImport(anImport(pageImportPath)); }