Commit 933ad851 authored by julien-reboul's avatar julien-reboul

refactor import modal to include a second step showing the conflicts

during import
parent 0fd5fca8
......@@ -18,3 +18,4 @@ frontend/node/
frontend/build/
tests/node/
*.swp
......@@ -35,7 +35,7 @@ import org.bonitasoft.web.designer.controller.export.steps.HtmlExportStep;
import org.bonitasoft.web.designer.controller.export.steps.PagePropertiesExportStep;
import org.bonitasoft.web.designer.controller.export.steps.WidgetByIdExportStep;
import org.bonitasoft.web.designer.controller.export.steps.WidgetsExportStep;
import org.bonitasoft.web.designer.controller.importer.ArtefactImporter;
import org.bonitasoft.web.designer.controller.importer.ArtifactImporter;
import org.bonitasoft.web.designer.controller.importer.dependencies.AssetImporter;
import org.bonitasoft.web.designer.controller.importer.dependencies.WidgetImporter;
import org.bonitasoft.web.designer.controller.utils.Unzipper;
......@@ -176,13 +176,13 @@ public class DesignerConfig {
}
@Bean
public ArtefactImporter<Page> pageImporter(Unzipper unzip, PageRepository pageRepository, WidgetImporter widgetImporter, AssetImporter<Page> pageAssetImporter) {
return new ArtefactImporter<>(unzip, pageRepository, pageFileBasedLoader(), widgetImporter, pageAssetImporter);
public ArtifactImporter<Page> pageImporter(Unzipper unzip, PageRepository pageRepository, WidgetImporter widgetImporter, AssetImporter<Page> pageAssetImporter) {
return new ArtifactImporter<>(unzip, pageRepository, pageFileBasedLoader(), widgetImporter, pageAssetImporter);
}
@Bean
public ArtefactImporter<Widget> widgetImporter(Unzipper unzip, WidgetLoader widgetLoader, WidgetRepository widgetRepository, AssetImporter<Widget> widgetAssetImporter) {
return new ArtefactImporter<>(unzip, widgetRepository, widgetLoader, widgetAssetImporter);
public ArtifactImporter<Widget> widgetImporter(Unzipper unzip, WidgetLoader widgetLoader, WidgetRepository widgetRepository, AssetImporter<Widget> widgetAssetImporter) {
return new ArtifactImporter<>(unzip, widgetRepository, widgetLoader, widgetAssetImporter);
}
@Bean
......
......@@ -17,15 +17,15 @@ package org.bonitasoft.web.designer.controller;
import javax.inject.Inject;
import javax.inject.Named;
import org.bonitasoft.web.designer.controller.importer.ArtefactImporter;
import org.bonitasoft.web.designer.controller.importer.ArtifactImporter;
import org.bonitasoft.web.designer.controller.importer.MultipartFileImporter;
import org.bonitasoft.web.designer.controller.importer.report.ImportReport;
import org.bonitasoft.web.designer.model.page.Page;
import org.bonitasoft.web.designer.model.widget.Widget;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -37,14 +37,14 @@ import org.springframework.web.multipart.MultipartFile;
public class ImportController {
private MultipartFileImporter multipartFileImporter;
private ArtefactImporter<Page> pageImporter;
private ArtefactImporter<Widget> widgetImporter;
private ArtifactImporter<Page> pageImporter;
private ArtifactImporter<Widget> widgetImporter;
@Inject
public ImportController(
MultipartFileImporter multipartFileImporter,
@Named("pageImporter") ArtefactImporter<Page> pageImporter,
@Named("widgetImporter") ArtefactImporter<Widget> widgetImporter) {
@Named("pageImporter") ArtifactImporter<Page> pageImporter,
@Named("widgetImporter") ArtifactImporter<Widget> widgetImporter) {
this.multipartFileImporter = multipartFileImporter;
this.pageImporter = pageImporter;
this.widgetImporter = widgetImporter;
......@@ -62,10 +62,35 @@ public class ImportController {
return multipartFileImporter.importFile(file, pageImporter);
}
@RequestMapping(value = "/import/page/{uuid}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public ImportReport importPage(@PathVariable("uuid") String uuid) {
return pageImporter.forceExecution(uuid);
}
@RequestMapping(value = "/import/page/cancel/{uuid}", method = RequestMethod.GET)
public void cancelPageImport(@PathVariable("uuid") String uuid) {
pageImporter.cancelImport(uuid);
}
@RequestMapping(value = "/import/widget", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public ImportReport importWidget(@RequestParam("file") MultipartFile file) {
return multipartFileImporter.importFile(file, widgetImporter);
}
@RequestMapping(value = "/import/widget/{uuid}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public ImportReport importWidget(@PathVariable("uuid") String uuid) {
return widgetImporter.forceExecution(uuid);
}
@RequestMapping(value = "/import/widget/cancel/{uuid}", method = RequestMethod.GET)
public void cancelWidgetImport(@PathVariable("uuid") String uuid) {
widgetImporter.cancelImport(uuid);
}
}
......@@ -67,7 +67,7 @@ public class Exporter<T extends Identifiable> {
// add json model
zipper.addToZip(objectMapper.toJson(identifiable), String.format("%s/%s.json", RESOURCES, repository.getComponentName()));
// execute export steps
// forceExecution export steps
for (ExportStep exporter : exportSteps) {
exporter.execute(zipper, identifiable);
}
......
......@@ -25,8 +25,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.ZipException;
import org.apache.commons.lang3.StringUtils;
import org.bonitasoft.web.designer.controller.exception.ImportException;
import org.bonitasoft.web.designer.controller.exception.ImportException.Type;
import org.bonitasoft.web.designer.controller.exception.ServerImportException;
......@@ -40,31 +43,72 @@ import org.bonitasoft.web.designer.repository.exception.RepositoryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ArtefactImporter<T extends Identifiable> {
public class ArtifactImporter<T extends Identifiable> {
protected static final Logger logger = LoggerFactory.getLogger(ArtefactImporter.class);
protected static final Logger logger = LoggerFactory.getLogger(ArtifactImporter.class);
private Unzipper unzip;
private Repository<T> repository;
private Loader<T> loader;
private DependencyImporter[] dependencyImporters;
private Map<String, String> extractedDirPathMap = new ConcurrentHashMap<>();
public ArtefactImporter(Unzipper unzip, Repository<T> repository, Loader<T> loader, DependencyImporter... dependencyImporters) {
public ArtifactImporter(Unzipper unzip, Repository<T> repository, Loader<T> loader, DependencyImporter... dependencyImporters) {
this.loader = loader;
this.repository = repository;
this.unzip = unzip;
this.dependencyImporters = dependencyImporters;
}
public void cancelImport(String uuid) {
String dir = extractedDirPathMap.remove(uuid);
if (StringUtils.isNotBlank(dir)) {
deleteQuietly(unzip.getTemporaryZipPath().resolve(dir).toFile());
}
}
public ImportReport execute(InputStream is) {
Path extractDir = unzip(is);
return importFromPath(extractDir);
}
Path resources = extractDir.resolve("resources");
if (notExists(resources)) {
logger.error("Incorrect zip structure, a resources folder is needed");
throw new ImportException(Type.UNEXPECTED_ZIP_STRUCTURE, "Incorrect zip structure");
public ImportReport forceExecution(String uuid) {
if (extractedDirPathMap.get(uuid) != null) {
Path extractDir = unzip.getTemporaryZipPath().resolve(extractedDirPathMap.get(uuid));
return forceImportFromPath(extractDir);
}
throw new UnsupportedOperationException("Cannot forceExecution import with null UUID");
}
public ImportReport forceImportFromPath(Path extractDir) {
Path resources = getPath(extractDir);
try {
return tryToImportAndGenerateReport(resources, null);
} finally {
deleteQuietly(extractDir.toFile());
}
}
public ImportReport importFromPath(Path extractDir) {
Path resources = getPath(extractDir);
ImportReport importReport = null;
try {
importReport = tryToImportAndGenerateReport(resources, extractDir);
return importReport;
} finally {
if (importReport == null || StringUtils.isBlank(importReport.getUUID())) {
deleteQuietly(extractDir.toFile());
} else {
extractDir.toFile().deleteOnExit();
}
}
}
/*
* if uploadedFileDirectory is null, the import is forced
*/
private ImportReport tryToImportAndGenerateReport(Path resources, Path uploadedFileDirectory) {
try {
// first load everything
T element = loader.load(resources, repository.getComponentName() + ".json");
......@@ -72,20 +116,32 @@ public class ArtefactImporter<T extends Identifiable> {
ImportReport report = buildReport(element, dependencies);
// then save them
saveArtefactDependencies(resources, dependencies);
repository.save(element);
if (uploadedFileDirectory == null
|| (report.doesNotOverrideElements())) {
// then save them
saveArtefactDependencies(resources, dependencies);
repository.save(element);
} else {
String uuid = UUID.randomUUID().toString();
extractedDirPathMap.put(uuid, uploadedFileDirectory.getFileName().toFile().getName());
report.setUUID(uuid);
};
return report;
} catch (IOException e) {
logger.error("Error while getting artefacts, verify the zip content", e);
throw new ServerImportException("Error while getting artefacts", e);
} catch (RepositoryException e) {
logger.error("Error while saving artefacts, verify the zip content", e);
throw new ServerImportException("Error while saving artefacts", e);
} finally {
deleteQuietly(extractDir.toFile());
} catch (IOException | RepositoryException e) {
String errorMessage = "Error while " + ((e instanceof IOException) ? "unzipping" : "saving") + " artefacts";
logger.error(errorMessage + ", check uploaded content", e);
throw new ServerImportException(errorMessage, e);
}
}
private Path getPath(Path extractDir) {
Path resources = extractDir.resolve("resources");
if (notExists(resources)) {
logger.error("Incorrect zip structure, a resources folder is needed");
throw new ImportException(Type.UNEXPECTED_ZIP_STRUCTURE, "Incorrect zip structure");
}
return resources;
}
private ImportReport buildReport(T element, Map<DependencyImporter, List<?>> dependencies) {
......
......@@ -14,22 +14,21 @@
*/
package org.bonitasoft.web.designer.controller.importer;
import javax.inject.Named;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Named;
import org.bonitasoft.web.designer.controller.exception.ImportException;
import org.bonitasoft.web.designer.controller.exception.ImportException.Type;
import org.bonitasoft.web.designer.controller.importer.report.ImportReport;
import org.bonitasoft.web.designer.controller.utils.MimeType;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
@Named
public class MultipartFileImporter {
public ImportReport importFile(MultipartFile file, ArtefactImporter importer) {
public ImportReport importFile(MultipartFile file, ArtifactImporter importer) {
try {
return doImport(file, importer);
} catch (IOException | IllegalArgumentException e) {
......@@ -37,7 +36,7 @@ public class MultipartFileImporter {
}
}
private ImportReport doImport(MultipartFile file, ArtefactImporter importer) throws IOException {
private ImportReport doImport(MultipartFile file, ArtifactImporter importer) throws IOException {
if (file == null || file.isEmpty()) {
throw new IllegalArgumentException("Part named [file] is needed to successfully import a component");
}
......
......@@ -14,17 +14,20 @@
*/
package org.bonitasoft.web.designer.controller.importer.report;
import java.util.List;
import java.util.Map;
import org.bonitasoft.web.designer.controller.importer.dependencies.DependencyImporter;
import org.bonitasoft.web.designer.model.Identifiable;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class ImportReport {
private Identifiable element;
private Boolean overridden = false;
private Dependencies dependencies;
private String uuid;
public ImportReport(Identifiable element, Dependencies dependencies) {
this.element = element;
......@@ -35,7 +38,6 @@ public class ImportReport {
return new ImportReport(element, Dependencies.from(dependencies));
}
public void setElement(Identifiable element) {
this.element = element;
}
......@@ -59,4 +61,17 @@ public class ImportReport {
public void setDependencies(Dependencies dependencies) {
this.dependencies = dependencies;
}
public String getUUID() {
return uuid;
}
public void setUUID(String uuid) {
this.uuid = uuid;
}
@JsonIgnore
public boolean doesNotOverrideElements() {
return !this.isOverridden() && (this.getDependencies().getOverridden() == null || this.getDependencies().getOverridden().isEmpty());
}
}
......@@ -20,6 +20,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import java.util.zip.ZipException;
import javax.inject.Named;
import org.apache.commons.io.FileUtils;
......@@ -29,8 +30,18 @@ import org.zeroturnaround.zip.ZipUtil;
@Named
public class Unzipper {
private Path temporaryZipPath;
public Unzipper() throws IOException {
temporaryZipPath = Files.createTempDirectory("uid-tmp");
}
public Path getTemporaryZipPath() {
return temporaryZipPath;
}
public Path unzipInTempDir(InputStream is, String tempDirPrefix) throws IOException {
Path tempDirectory = Files.createTempDirectory(tempDirPrefix);
Path tempDirectory = Files.createTempDirectory(temporaryZipPath, tempDirPrefix);
Path zipFile = writeInDir(is, tempDirectory);
try {
ZipUtil.unpack(zipFile.toFile(), tempDirectory.toFile());
......
......@@ -26,6 +26,9 @@ public class ImportReportBuilder {
private Identifiable element;
List<Identifiable> added = new ArrayList<>();
List<Identifiable> overridden = new ArrayList<>();
private String uuid;
private boolean override;
public ImportReportBuilder(Identifiable element) {
this.element = element;
......@@ -49,6 +52,16 @@ public class ImportReportBuilder {
return this;
}
public ImportReportBuilder withUUID(String uuid) {
this.uuid = uuid;
return this;
}
public ImportReportBuilder withOverride(boolean override) {
this.override = override;
return this;
}
public ImportReport build() {
Dependencies dependencies = new Dependencies();
for (Identifiable identifiable : added) {
......@@ -57,6 +70,9 @@ public class ImportReportBuilder {
for (Identifiable identifiable : overridden) {
dependencies.addOverriddenDependency(identifiable.getClass().getSimpleName().toLowerCase(), identifiable);
}
return new ImportReport(element, dependencies);
ImportReport importReport = new ImportReport(element, dependencies);
importReport.setUUID(uuid);
importReport.setOverridden(override);
return importReport;
}
}
......@@ -20,13 +20,12 @@ import static org.bonitasoft.web.designer.builder.WidgetBuilder.aWidget;
import static org.bonitasoft.web.designer.utils.UIDesignerMockMvcBuilder.mockServer;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.bonitasoft.web.designer.controller.exception.ImportException;
import org.bonitasoft.web.designer.controller.exception.ImportException.Type;
import org.bonitasoft.web.designer.controller.importer.ArtefactImporter;
import org.bonitasoft.web.designer.controller.importer.ArtifactImporter;
import org.bonitasoft.web.designer.controller.importer.MultipartFileImporter;
import org.bonitasoft.web.designer.controller.importer.report.ImportReport;
import org.bonitasoft.web.designer.model.page.Page;
......@@ -46,9 +45,9 @@ public class ImportControllerTest {
private MockMvc mockMvc;
@Mock
private ArtefactImporter<Page> pageImporter;
private ArtifactImporter<Page> pageImporter;
@Mock
private ArtefactImporter<Widget> widgetImporter;
private ArtifactImporter<Widget> widgetImporter;
@Mock
private MultipartFileImporter multipartFileImporter;
......@@ -63,7 +62,7 @@ public class ImportControllerTest {
//We construct a mockfile (the first arg is the name of the property expected in the controller
MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
ImportReport expectedReport =
anImportReportFor(aPage().withId("aPage").withName("thePage"))
anImportReportFor(aPage().withId("aPage").withName("thePage")).withUUID("UUIDZipFile")
.withAdded(aWidget().id("addedWidget").name("newWidget"))
.withOverridden(aWidget().id("overriddenWidget").name("oldWidget")).build();
when(multipartFileImporter.importFile(file, pageImporter)).thenReturn(expectedReport);
......@@ -71,6 +70,8 @@ public class ImportControllerTest {
mockMvc.perform(fileUpload("/import/page").file(file))
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
.andExpect(status().isCreated())
.andExpect(jsonPath("uuid").value("UUIDZipFile"))
.andExpect(jsonPath("extractedDirName").doesNotExist())
.andExpect(jsonPath("element.id").value("aPage"))
.andExpect(jsonPath("element.name").value("thePage"))
.andExpect(jsonPath("dependencies.added.widget[0].id").value("addedWidget"))
......@@ -79,6 +80,24 @@ public class ImportControllerTest {
.andExpect(jsonPath("dependencies.overridden.widget[0].name").value("oldWidget"));
}
@Test
public void should_try_to_import_a_page_with_its_dependencies_and_cancel_it() throws Exception {
//We construct a mockfile (the first arg is the name of the property expected in the controller
MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
String uuid = "UUIDZipFile";
ImportReport expectedReport =
anImportReportFor(aPage().withId("aPage").withName("thePage")).withUUID(uuid)
.withAdded(aWidget().id("addedWidget").name("newWidget"))
.withOverridden(aWidget().id("overriddenWidget").name("oldWidget")).build();
when(multipartFileImporter.importFile(file, pageImporter)).thenReturn(expectedReport);
mockMvc.perform(fileUpload("/import/page").file(file))
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
.andExpect(status().isCreated())
.andExpect(jsonPath("uuid").value(uuid));
mockMvc.perform(get("/import/page/cancel/" + uuid)).andExpect(status().isOk());
}
@Test
public void should_respond_an_error_with_ok_code_when_import_exception_occurs_while_importing_a_page() throws Exception {
MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
......@@ -95,14 +114,31 @@ public class ImportControllerTest {
public void should_import_a_widget() throws Exception {
//We construct a mockfile (the first arg is the name of the property expected in the controller
MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
ImportReport expectedReport = anImportReportFor(aWidget().id("aWidget").name("myWidgetName")).build() ;
ImportReport expectedReport = anImportReportFor(aWidget().id("aWidget").name("myWidgetName")).build();
when(multipartFileImporter.importFile(file, widgetImporter)).thenReturn(expectedReport);
mockMvc.perform(fileUpload("/import/widget").file(file))
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
.andExpect(status().isCreated())
.andExpect(jsonPath("element.id").value("aWidget"))
.andExpect(jsonPath("element.name").value("myWidgetName"));
}
@Test
public void should_import_a_widget_and_cancel() throws Exception {
//We construct a mockfile (the first arg is the name of the property expected in the controller
MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
String uuid = "UUID";
ImportReport expectedReport = anImportReportFor(aWidget().id("aWidget").name("myWidgetName")).withUUID(uuid).withOverride(true).build();
when(multipartFileImporter.importFile(file, widgetImporter)).thenReturn(expectedReport);
mockMvc.perform(fileUpload("/import/widget").file(file))
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
.andExpect(status().isCreated())
.andExpect(jsonPath("uuid").value(uuid))
.andExpect(jsonPath("element.id").value("aWidget"))
.andExpect(jsonPath("element.name").value("myWidgetName"));
mockMvc.perform(get("/import/page/cancel/" + uuid)).andExpect(status().isOk());
}
@Test
......
......@@ -14,7 +14,6 @@
*/
package org.bonitasoft.web.designer.controller.importer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
......@@ -23,15 +22,12 @@ import java.io.InputStream;
import org.bonitasoft.web.designer.controller.exception.ImportException;
import org.bonitasoft.web.designer.model.page.Page;
import org.bonitasoft.web.designer.controller.ErrorMessage;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
@RunWith(MockitoJUnitRunner.class)
......@@ -41,7 +37,7 @@ public class MultipartFileImportTest {
public ExpectedException exception = ExpectedException.none();
@Mock
private ArtefactImporter<Page> importer;
private ArtifactImporter<Page> importer;
private MultipartFileImporter multipartFileImporter = new MultipartFileImporter();
......
......@@ -32,6 +32,7 @@ import java.nio.file.Path;
import java.util.List;
import java.util.zip.ZipException;
import org.assertj.core.util.Lists;
import org.bonitasoft.web.designer.controller.exception.ImportException;
import org.bonitasoft.web.designer.controller.exception.ServerImportException;
import org.bonitasoft.web.designer.controller.importer.dependencies.AssetImporter;
......@@ -49,7 +50,6 @@ import org.bonitasoft.web.designer.repository.WidgetLoader;
import org.bonitasoft.web.designer.repository.WidgetRepository;
import org.bonitasoft.web.designer.repository.exception.RepositoryException;
import org.bonitasoft.web.designer.utils.rule.TemporaryFolder;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
......@@ -59,7 +59,7 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PageArtefactImporterTest {
public class PageArtifactImporterTest {
private static final String WIDGETS_FOLDER = "widgets";
......@@ -67,6 +67,7 @@ public class PageArtefactImporterTest {
public ExpectedException exception = ExpectedException.none();
@Rule
public TemporaryFolder tempDir = new TemporaryFolder();
@Mock
private Unzipper unzip;
@Mock
......@@ -79,19 +80,21 @@ public class PageArtefactImporterTest {
private WidgetRepository widgetRepository;
@Mock
private AssetImporter<Widget> widgetAssetImporter;
private ArtifactImporter<Page> importer;
private ArtefactImporter<Page> importer;
private Path pageImportPath;
private Path unzippedPath;
private WidgetImportMock wMocks;
private PageImportMock pMocks;
@Before
public void setUp() throws IOException {
pageImportPath = Files.createTempDirectory(tempDir.toPath(), "pageImport");
DependencyImporter widgetImporter = new WidgetImporter(widgetLoader, widgetRepository, widgetAssetImporter);
importer = new ArtefactImporter<>(unzip, pageRepository, pageLoader, widgetImporter);
when(unzip.unzipInTempDir(any(InputStream.class), anyString())).thenReturn(tempDir.toPath());
unzippedPath = tempDir.newFolderPath("resources");
importer = new ArtifactImporter<>(unzip, pageRepository, pageLoader, widgetImporter);
when(unzip.unzipInTempDir(any(InputStream.class), anyString())).thenReturn(pageImportPath);
unzippedPath = pageImportPath.resolve("resources");
Files.createDirectory(unzippedPath);
when(pageRepository.getComponentName()).thenReturn("page");
wMocks = new WidgetImportMock(unzippedPath, widgetLoader, widgetRepository);
......@@ -103,7 +106,7 @@ public class PageArtefactImporterTest {
List<Widget> widgets = wMocks.mockWidgetsAsAddedDependencies();
Page page = pMocks.mockPageToBeImported();
importer.execute(aStream());
importer.importFromPath(pageImportPath);
verify(widgetRepository).saveAll(widgets);
verify(pageRepository).save(page);
......@@ -115,7 +118,7 @@ public class PageArtefactImporterTest {
List<Widget> overridenWidgets = wMocks.mockWidgetsAsOverridenDependencies();
Page page = pMocks.mockPageToBeImported();
ImportReport report = importer.execute(aStream());
ImportReport report = importer.importFromPath(pageImportPath);
assertThat(report.getDependencies().getAdded().get("widget")).isEqualTo(addedWidgets);
assertThat(report.getDependencies().getOverridden().get("widget")).isEqualTo(overridenWidgets);
......@@ -127,7 +130,7 @@ public class PageArtefactImporterTest {
Page page = pMocks.mockPageToBeImported();
when(pageRepository.exists(page.getId())).thenReturn(true);
ImportReport report = importer.execute(aStream());
ImportReport report = importer.importFromPath(pageImportPath);
assertThat(report.getElement()).isEqualTo(page);
assertThat(report.isOverridden()).isTrue();
......@@ -138,7 +141,7 @@ public class PageArtefactImporterTest {
Page page = pMocks.mockPageToBeImported();
when(pageRepository.exists(page.getId())).thenReturn(false);
ImportReport report = importer.execute(aStream());
ImportReport report = importer.importFromPath(pageImportPath);
assertThat(report.getElement()).isEqualTo(page);
assertThat(report.isOverridden()).isFalse();
......@@ -148,20 +151,20 @@ public class PageArtefactImporterTest {
public void should_delete_created_folder_after_import() throws Exception {
when(pageLoader.load(any(Path.class), eq("page.json"))).thenReturn(aPage().withId("page-id").build());
importer.execute(aStream());
importer.importFromPath(pageImportPath);
assertThat(Files.exists(tempDir.toPath())).isFalse();
assertThat(Files.exists(pageImportPath)).isFalse();
}
@Test
public void should_delete_created_folder_even_if_an_exception_occurs_when_importing() throws Exception {
when(pageLoader.load(any(Path.class),eq("page.json"))).thenThrow(ImportException.class);
when(pageLoader.load(any(Path.class), eq("page.json"))).thenThrow(ImportException.class);
try {
importer.execute(aStream());
importer.importFromPath(pageImportPath);
failBecauseExceptionWasNotThrown(ImportException.class);
} catch (ImportException e) {