diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationSnapshotServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationSnapshotServiceImpl.java index 8de8a3e740..fbe1a9d4dd 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationSnapshotServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationSnapshotServiceImpl.java @@ -1,5 +1,6 @@ package com.appsmith.server.services; +import com.appsmith.server.helpers.ResponseUtils; import com.appsmith.server.repositories.ApplicationSnapshotRepository; import com.appsmith.server.services.ce.ApplicationSnapshotServiceCEImpl; import com.appsmith.server.solutions.ApplicationPermission; @@ -12,7 +13,7 @@ import org.springframework.stereotype.Service; @Service public class ApplicationSnapshotServiceImpl extends ApplicationSnapshotServiceCEImpl implements ApplicationSnapshotService { - public ApplicationSnapshotServiceImpl(ApplicationSnapshotRepository applicationSnapshotRepository, ApplicationService applicationService, ImportExportApplicationService importExportApplicationService, ApplicationPermission applicationPermission, Gson gson) { - super(applicationSnapshotRepository, applicationService, importExportApplicationService, applicationPermission, gson); + public ApplicationSnapshotServiceImpl(ApplicationSnapshotRepository applicationSnapshotRepository, ApplicationService applicationService, ImportExportApplicationService importExportApplicationService, ApplicationPermission applicationPermission, Gson gson, ResponseUtils responseUtils) { + super(applicationSnapshotRepository, applicationService, importExportApplicationService, applicationPermission, gson, responseUtils); } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/ApplicationSnapshotServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/ApplicationSnapshotServiceCEImpl.java index 29ecee3db0..fcfeea81e7 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/ApplicationSnapshotServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/ApplicationSnapshotServiceCEImpl.java @@ -7,6 +7,7 @@ import com.appsmith.server.domains.ApplicationSnapshot; import com.appsmith.server.dtos.ApplicationJson; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; +import com.appsmith.server.helpers.ResponseUtils; import com.appsmith.server.repositories.ApplicationSnapshotRepository; import com.appsmith.server.services.ApplicationService; import com.appsmith.server.solutions.ApplicationPermission; @@ -29,6 +30,7 @@ public class ApplicationSnapshotServiceCEImpl implements ApplicationSnapshotServ private final ImportExportApplicationService importExportApplicationService; private final ApplicationPermission applicationPermission; private final Gson gson; + private final ResponseUtils responseUtils; private static final int MAX_SNAPSHOT_SIZE = 15*1024*1024; // 15 MB @@ -95,7 +97,8 @@ public class ApplicationSnapshotServiceCEImpl implements ApplicationSnapshotServ .flatMap(application -> applicationSnapshotRepository.deleteAllByApplicationId(application.getId()) .thenReturn(application) - ); + ) + .map(responseUtils::updateApplicationWithDefaultResources); } private Mono getApplicationJsonStringFromSnapShot(String applicationId) { diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/ApplicationSnapshotServiceUnitTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/ApplicationSnapshotServiceUnitTest.java index 043b41c1bb..aeaf36fd57 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/ApplicationSnapshotServiceUnitTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ce/ApplicationSnapshotServiceUnitTest.java @@ -3,6 +3,7 @@ package com.appsmith.server.services.ce; import com.appsmith.server.acl.AclPermission; import com.appsmith.server.constants.SerialiseApplicationObjective; import com.appsmith.server.domains.Application; +import com.appsmith.server.domains.ApplicationPage; import com.appsmith.server.domains.ApplicationSnapshot; import com.appsmith.server.domains.Layout; import com.appsmith.server.domains.NewPage; @@ -31,6 +32,7 @@ import java.util.Random; import static java.util.Arrays.copyOfRange; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; @@ -144,6 +146,58 @@ public class ApplicationSnapshotServiceUnitTest { .verifyComplete(); } + @Test + public void restoreSnapshot_WhenApplicationHasDefaultPageIds_IdReplacedWithDefaultPageId() { + String defaultAppId = "default-app-id", + branchedAppId = "branched-app-id", + workspaceId = "workspace-id", + branch = "development"; + + Application application = new Application(); + application.setName("Snapshot test"); + application.setWorkspaceId(workspaceId); + application.setId(branchedAppId); + + ApplicationPage applicationPage = new ApplicationPage(); + applicationPage.setId("original-page-id"); + applicationPage.setDefaultPageId("default-page-id"); + applicationPage.setSlug("original-page-slug"); + applicationPage.setIsDefault(true); + + application.setPages(List.of(applicationPage)); + + Mockito.when(applicationService.findByBranchNameAndDefaultApplicationId(branch, defaultAppId, AclPermission.MANAGE_APPLICATIONS)) + .thenReturn(Mono.just(application)); + + ApplicationJson applicationJson = new ApplicationJson(); + applicationJson.setExportedApplication(application); + + String jsonString = gson.toJson(applicationJson); + byte[] jsonStringBytes = jsonString.getBytes(StandardCharsets.UTF_8); + + List snapshots = List.of( + createSnapshot(branchedAppId, jsonStringBytes, 1) + ); + + Mockito.when(applicationSnapshotRepository.findByApplicationId(branchedAppId)) + .thenReturn(Flux.fromIterable(snapshots)); + + Mockito.when(importExportApplicationService.importApplicationInWorkspace(eq(application.getWorkspaceId()), any(), eq(branchedAppId), eq(branch))) + .thenReturn(Mono.just(application)); + + Mockito.when(applicationSnapshotRepository.deleteAllByApplicationId(branchedAppId)) + .thenReturn(Mono.just("application").then()); + + StepVerifier.create(applicationSnapshotService.restoreSnapshot(defaultAppId, branch)) + .assertNext(application1 -> { + assertThat(application1.getName()).isEqualTo(application.getName()); + application1.getPages().forEach(page -> { + assertThat(page.getId()).isEqualTo(page.getDefaultPageId()); + }); + }) + .verifyComplete(); + } + private ApplicationSnapshot createSnapshot(String applicationId, byte [] data, int chunkOrder) { ApplicationSnapshot applicationSnapshot = new ApplicationSnapshot(); applicationSnapshot.setApplicationId(applicationId);