diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ActionServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ActionServiceTest.java index 72d5fc2f36..076be5e39b 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ActionServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/ActionServiceTest.java @@ -53,7 +53,6 @@ import java.util.UUID; import static com.appsmith.external.constants.ActionConstants.DEFAULT_ACTION_EXECUTION_TIMEOUT_MS; import static com.appsmith.server.acl.AclPermission.MANAGE_ACTIONS; import static com.appsmith.server.acl.AclPermission.READ_ACTIONS; -import static com.appsmith.server.acl.AclPermission.READ_PAGES; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @@ -563,40 +562,6 @@ public class ActionServiceTest { .verifyComplete(); } - @Test - @WithUserDetails(value = "api_user") - public void updateActionUpdatesLayout() { - Mockito.when(pluginExecutorHelper.getPluginExecutor(Mockito.any())).thenReturn(Mono.just(new MockPluginExecutor())); - - Action action = new Action(); - action.setName("query1"); - action.setPageId(testPage.getId()); - ActionConfiguration actionConfiguration = new ActionConfiguration(); - actionConfiguration.setHttpMethod(HttpMethod.GET); - action.setActionConfiguration(actionConfiguration); - action.setDatasource(datasource); - - Mono resultMono = actionService - .create(action) - .flatMap(savedAction -> { - Action updates = new Action(); - updates.setExecuteOnLoad(true); - updates.setPolicies(null); - updates.setUserPermissions(null); - return layoutActionService.updateAction(savedAction.getId(), updates); - }) - .flatMap(savedAction -> pageService.findById(testPage.getId(), READ_PAGES)); - - StepVerifier - .create(resultMono) - .assertNext(page -> { - assertThat(page.getLayouts()).hasSize(2); - assertThat(page.getLayouts().get(1).getLayoutOnLoadActions()).hasSize(1); - assertThat(page.getLayouts().get(1).getLayoutOnLoadActions().get(0).iterator().next().getName()).isEqualTo("query1"); - }) - .verifyComplete(); - } - private void executeAndAssertAction(ExecuteActionDTO executeActionDTO, ActionConfiguration actionConfiguration, ActionExecutionResult mockResult) { Mono actionExecutionResultMono = executeAction(executeActionDTO, actionConfiguration, mockResult); diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/LayoutActionServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/LayoutActionServiceTest.java new file mode 100644 index 0000000000..4064d1ebb8 --- /dev/null +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/LayoutActionServiceTest.java @@ -0,0 +1,156 @@ +package com.appsmith.server.services; + +import com.appsmith.external.models.ActionConfiguration; +import com.appsmith.server.acl.AclPermission; +import com.appsmith.server.domains.Action; +import com.appsmith.server.domains.Application; +import com.appsmith.server.domains.Datasource; +import com.appsmith.server.domains.Layout; +import com.appsmith.server.domains.Organization; +import com.appsmith.server.domains.Page; +import com.appsmith.server.domains.Plugin; +import com.appsmith.server.domains.User; +import com.appsmith.server.helpers.MockPluginExecutor; +import com.appsmith.server.helpers.PluginExecutorHelper; +import com.appsmith.server.repositories.OrganizationRepository; +import com.appsmith.server.repositories.PluginRepository; +import lombok.extern.slf4j.Slf4j; +import net.minidev.json.JSONObject; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpMethod; +import org.springframework.security.test.context.support.WithUserDetails; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import java.util.Map; +import java.util.UUID; + +import static com.appsmith.server.acl.AclPermission.READ_PAGES; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +@DirtiesContext +public class LayoutActionServiceTest { + @Autowired + ActionService actionService; + + @Autowired + ApplicationPageService applicationPageService; + + @Autowired + PageService pageService; + + @Autowired + UserService userService; + + @Autowired + OrganizationService organizationService; + + @Autowired + OrganizationRepository organizationRepository; + + @Autowired + PluginRepository pluginRepository; + + @MockBean + PluginExecutorHelper pluginExecutorHelper; + + @Autowired + LayoutActionService layoutActionService; + + @Autowired + LayoutService layoutService; + + Application testApp = null; + + Page testPage = null; + + Datasource datasource; + + @Before + @WithUserDetails(value = "api_user") + public void setup() { + + User apiUser = userService.findByEmail("api_user").block(); + String orgId = apiUser.getOrganizationIds().iterator().next(); + Organization organization = organizationService.findById(orgId).block(); + + if (testApp == null && testPage == null) { + //Create application and page which will be used by the tests to create actions for. + Application application = new Application(); + application.setName(UUID.randomUUID().toString()); + testApp = applicationPageService.createApplication(application, organization.getId()).block(); + + final String pageId = testApp.getPages().get(0).getId(); + Layout layout = new Layout(); + JSONObject dsl = new JSONObject(Map.of("text", "{{ query1.data }}")); + layout.setDsl(dsl); + layout.setPublishedDsl(dsl); + layoutService.createLayout(pageId, layout).block(); + + testPage = pageService.getById(pageId).block(); + } + + Organization testOrg = organizationRepository.findByName("Another Test Organization", AclPermission.READ_ORGANIZATIONS).block(); + orgId = testOrg.getId(); + datasource = new Datasource(); + datasource.setName("Default Database"); + datasource.setOrganizationId(orgId); + Plugin installed_plugin = pluginRepository.findByPackageName("installed-plugin").block(); + datasource.setPluginId(installed_plugin.getId()); + } + + @After + @WithUserDetails(value = "api_user") + public void cleanup() { + applicationPageService.deleteApplication(testApp.getId()).block(); + testApp = null; + testPage = null; + + } + + @Test + @WithUserDetails(value = "api_user") + public void updateActionUpdatesLayout() { + Mockito.when(pluginExecutorHelper.getPluginExecutor(Mockito.any())).thenReturn(Mono.just(new MockPluginExecutor())); + + Action action = new Action(); + action.setName("query1"); + action.setPageId(testPage.getId()); + ActionConfiguration actionConfiguration = new ActionConfiguration(); + actionConfiguration.setHttpMethod(HttpMethod.GET); + action.setActionConfiguration(actionConfiguration); + action.setDatasource(datasource); + + Mono resultMono = actionService + .create(action) + .flatMap(savedAction -> { + Action updates = new Action(); + updates.setExecuteOnLoad(true); + updates.setPolicies(null); + updates.setUserPermissions(null); + return layoutActionService.updateAction(savedAction.getId(), updates); + }) + .flatMap(savedAction -> pageService.findById(testPage.getId(), READ_PAGES)); + + StepVerifier + .create(resultMono) + .assertNext(page -> { + assertThat(page.getLayouts()).hasSize(2); + assertThat(page.getLayouts().get(1).getLayoutOnLoadActions()).hasSize(1); + assertThat(page.getLayouts().get(1).getLayoutOnLoadActions().get(0).iterator().next().getName()).isEqualTo("query1"); + }) + .verifyComplete(); + } +}