From 1b7fdf5e5b2f1b0ba42d82d8ab5df43a6b3fa75b Mon Sep 17 00:00:00 2001 From: FC-LinuxMachine Date: Thu, 15 Aug 2024 01:17:56 +0700 Subject: [PATCH 1/3] #254 fixes bug & add test for it --- .../controller/WorkspaceSettingsController.java | 9 ++++++++- .../web/WorkspaceSettingsControllerIT.java | 12 ++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java b/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java index 1802aadb..038d20b4 100644 --- a/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java +++ b/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java @@ -10,6 +10,7 @@ import io.hexlet.typoreporter.service.WorkspaceSettingsService; import io.hexlet.typoreporter.service.dto.typo.TypoInfo; import io.hexlet.typoreporter.service.dto.workspace.AllowedUrlDTO; +import io.hexlet.typoreporter.service.dto.workspace.WorkspaceInfo; import jakarta.servlet.http.HttpServletRequest; import jakarta.transaction.Transactional; import jakarta.validation.Valid; @@ -167,7 +168,9 @@ public String deleteAllowedUrl(Model model, @GetMapping("/integration") @PreAuthorize(IS_USER_RELATED_TO_WKS) public String getWorkspaceIntegrationPage(Model model, @PathVariable Long wksId, HttpServletRequest req) { - if (!workspaceService.existsWorkspaceById(wksId)) { + var wksOptional = workspaceService.getWorkspaceInfoById(wksId); + + if (wksOptional.isEmpty()) { //TODO send to error page log.error("Workspace with id {} not found", wksId); return "redirect:/workspaces"; @@ -175,6 +178,10 @@ public String getWorkspaceIntegrationPage(Model model, @PathVariable Long wksId, addTokenAndUrlToModel(model, wksId, req); + WorkspaceInfo wksInfo = wksOptional.get(); + model.addAttribute("wksInfo", wksInfo); + model.addAttribute("wksName", wksInfo.name()); + getStatisticDataToModel(model, wksId); getLastTypoDataToModel(model, wksId); diff --git a/src/test/java/io/hexlet/typoreporter/web/WorkspaceSettingsControllerIT.java b/src/test/java/io/hexlet/typoreporter/web/WorkspaceSettingsControllerIT.java index 61de04be..5a84838c 100644 --- a/src/test/java/io/hexlet/typoreporter/web/WorkspaceSettingsControllerIT.java +++ b/src/test/java/io/hexlet/typoreporter/web/WorkspaceSettingsControllerIT.java @@ -83,18 +83,25 @@ static void datasourceProperties(DynamicPropertyRegistry registry) { @ParameterizedTest @MethodSource("io.hexlet.typoreporter.test.factory.EntitiesFactory#getWorkspacesAndUsersRelated") void getWorkspaceIntegrationPageIsSuccessful(final Long wksId, final String email) throws Exception { - final var apiAccessToken = workspaceSettingsRepository.getWorkspaceSettingsByWorkspaceId(wksId) + final var workspaces = workspaceSettingsRepository.getWorkspaceSettingsByWorkspaceId(wksId); + final var apiAccessToken = workspaces .map(s -> s.getId() + ":" + s.getApiAccessToken()) .map(String::getBytes) .map(Base64.getEncoder()::encodeToString) .orElse(null); + final var wksName = workspaces + .map(m -> m.getWorkspace().getName()) + .orElse(null); + MockHttpServletResponse response = mockMvc.perform(get("/workspace/{wksId}/integration", wksId.toString()) .with(user(email))) - .andExpect(model().attributeExists("wksBasicToken")) + .andExpect(model().attributeExists("wksBasicToken", "wksName")) .andReturn().getResponse(); assertThat(response.getContentAsString()).contains(apiAccessToken); + assertThat(response.getContentAsString()).contains(wksName); + } @ParameterizedTest @@ -213,4 +220,5 @@ void delAllowedUrlFromWorkspace() throws Exception { WORKSPACE_101_ID); assertThat(deletedAllowedUrlOptional).isEmpty(); } + } From bb057c9674c4a76051d415a708431c735e2ab42c Mon Sep 17 00:00:00 2001 From: FC-LinuxMachine Date: Sun, 25 Aug 2024 19:20:02 +0700 Subject: [PATCH 2/3] #254 updated getWorkSpaceIntegrationPage for use Optional --- .../controller/WorkspaceSettingsController.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java b/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java index 038d20b4..6c4ec971 100644 --- a/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java +++ b/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java @@ -168,9 +168,9 @@ public String deleteAllowedUrl(Model model, @GetMapping("/integration") @PreAuthorize(IS_USER_RELATED_TO_WKS) public String getWorkspaceIntegrationPage(Model model, @PathVariable Long wksId, HttpServletRequest req) { - var wksOptional = workspaceService.getWorkspaceInfoById(wksId); + var wksOptional = workspaceService.getWorkspaceInfoById(wksId).orElse(null); - if (wksOptional.isEmpty()) { + if (wksOptional == null) { //TODO send to error page log.error("Workspace with id {} not found", wksId); return "redirect:/workspaces"; @@ -178,9 +178,8 @@ public String getWorkspaceIntegrationPage(Model model, @PathVariable Long wksId, addTokenAndUrlToModel(model, wksId, req); - WorkspaceInfo wksInfo = wksOptional.get(); - model.addAttribute("wksInfo", wksInfo); - model.addAttribute("wksName", wksInfo.name()); + model.addAttribute("wksInfo", wksOptional); + model.addAttribute("wksName", wksOptional.name()); getStatisticDataToModel(model, wksId); getLastTypoDataToModel(model, wksId); From 36300e2402b8edef66dc969548afd3950e3038f5 Mon Sep 17 00:00:00 2001 From: FC-LinuxMachine Date: Sun, 25 Aug 2024 20:21:18 +0700 Subject: [PATCH 3/3] #254 fix checkstyle --- .../typoreporter/controller/WorkspaceSettingsController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java b/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java index 6c4ec971..b7b95b0a 100644 --- a/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java +++ b/src/main/java/io/hexlet/typoreporter/controller/WorkspaceSettingsController.java @@ -10,7 +10,6 @@ import io.hexlet.typoreporter.service.WorkspaceSettingsService; import io.hexlet.typoreporter.service.dto.typo.TypoInfo; import io.hexlet.typoreporter.service.dto.workspace.AllowedUrlDTO; -import io.hexlet.typoreporter.service.dto.workspace.WorkspaceInfo; import jakarta.servlet.http.HttpServletRequest; import jakarta.transaction.Transactional; import jakarta.validation.Valid;