From 8fb8365c385cd37ec92d45c5ea3fea97f4a6268b Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Tue, 4 Oct 2022 14:31:36 +0200 Subject: [PATCH] =?UTF-8?q?OZG-2887=20Verwende=20usermanager=20health=20ch?= =?UTF-8?q?eck=20um=20zu=20pr=C3=BCfen=20ob=20der=20Usermanager=20verf?= =?UTF-8?q?=C3=BCgbar=20ist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/de/itvsh/goofy/RootController.java | 42 +++++++++++++++++-- .../src/main/resources/application-local.yml | 2 + .../de/itvsh/goofy/RootControllerTest.java | 31 +++++++++++++- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/goofy-server/src/main/java/de/itvsh/goofy/RootController.java b/goofy-server/src/main/java/de/itvsh/goofy/RootController.java index 38641bc300..49b3fae1d7 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/RootController.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/RootController.java @@ -3,16 +3,19 @@ package de.itvsh.goofy; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; import java.time.Instant; -import java.util.Objects; import java.util.Optional; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.info.BuildProperties; import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.Link; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; import de.itvsh.goofy.common.ModelBuilder; import de.itvsh.goofy.common.downloadtoken.DownloadTokenController; @@ -21,7 +24,9 @@ import de.itvsh.goofy.common.user.UserId; import de.itvsh.goofy.common.user.UserProfileController; import de.itvsh.goofy.common.user.UserRole; import de.itvsh.goofy.vorgang.VorgangController; +import lombok.extern.log4j.Log4j2; +@Log4j2 @RestController @RequestMapping("/api") public class RootController { @@ -34,10 +39,17 @@ public class RootController { static final String REL_DOWNLOAD_TOKEN = "downloadToken"; static final String REL_CURRENT_USER = "currentUser"; + private static final String userManagerHealthPath = "/q/health"; + @Autowired(required = false) public BuildProperties buildProperties; @Autowired private CurrentUserService currentUserService; + @Autowired + private RestTemplate restTemplate; + + @Value("${goofy.user-manager.url}") + private String userManagerUrl; @GetMapping public EntityModel<RootResource> getRootResource() { @@ -51,7 +63,7 @@ public class RootController { .ifMatch(this::hasVerwaltungRole).addLinks( buildVorgangListByPageLink(REL_MY_VORGAENGE, Optional.of(currentUserService.getUserId())), buildVorgangListByPageLink(REL_SEARCH_MY_VORGAENGE, Optional.of(currentUserService.getUserId()))) - .ifMatch(this::hasCurrentUser).addLinks( + .ifMatch(this::hasUserManager).addLinks( linkTo(methodOn(UserProfileController.class).getUser(currentUserService.getUserId())).withRel(REL_CURRENT_USER)) .buildModel(); } @@ -65,8 +77,30 @@ public class RootController { || currentUserService.hasRole(UserRole.VERWALTUNG_POSTSTELLE); } - boolean hasCurrentUser() { - return Objects.nonNull(currentUserService.getUser()); + boolean hasUserManager() { + String url = getUsermanagerHeathUrl(); + if (StringUtils.isNotEmpty(url)) { + return isServiceAvailable(url); + } + return false; + } + + private boolean isServiceAvailable(String url) { + try { + String status = restTemplate.getForObject(url, String.class); + return StringUtils.isNotEmpty(status) && status.contains("\"status\": \"UP\""); + } catch (RestClientException e) { + LOG.error("Error retrieving usermanager health status", e); + return false; + } + } + + String getUsermanagerHeathUrl() { + if (StringUtils.isNotEmpty(userManagerUrl)) { + return userManagerUrl + userManagerHealthPath; + } + + return null; } private Link buildVorgangListByPageLink(String linkRel, Optional<UserId> assignedTo) { diff --git a/goofy-server/src/main/resources/application-local.yml b/goofy-server/src/main/resources/application-local.yml index d9fb15b0f4..07fd17b7c8 100644 --- a/goofy-server/src/main/resources/application-local.yml +++ b/goofy-server/src/main/resources/application-local.yml @@ -5,6 +5,8 @@ logging: goofy: production: false + user-manager: + url: http://localhost:9092 keycloak: auth-server-url: http://localhost:8088/auth diff --git a/goofy-server/src/test/java/de/itvsh/goofy/RootControllerTest.java b/goofy-server/src/test/java/de/itvsh/goofy/RootControllerTest.java index 7fa276fab1..122e8000d9 100644 --- a/goofy-server/src/test/java/de/itvsh/goofy/RootControllerTest.java +++ b/goofy-server/src/test/java/de/itvsh/goofy/RootControllerTest.java @@ -22,6 +22,8 @@ import org.springframework.hateoas.Link; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; import de.itvsh.goofy.common.user.CurrentUserService; import de.itvsh.goofy.common.user.GoofyUserTestFactory; @@ -39,6 +41,8 @@ class RootControllerTest { private BuildProperties properties; @Mock private CurrentUserService currentUserService; + @Mock + private RestTemplate restTemplate; private MockMvc mockMvc; @@ -166,8 +170,9 @@ class RootControllerTest { class CurrentUser { @Test void shouldHaveCurrentUserLink() { - when(currentUserService.getUser()).thenReturn(GoofyUserTestFactory.create()); + when(controller.getUsermanagerHeathUrl()).thenReturn("http://localhost:9092/q/health"); when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID); + when(restTemplate.getForObject(anyString(), eq(String.class))).thenReturn("{\"status\": \"UP\"}"); var model = controller.getRootResource(); @@ -176,7 +181,29 @@ class RootControllerTest { } @Test - void shouldNotHaveCurrentUserLink() { + void shouldNotHaveCurrentUserLinkWhenDown() { + when(controller.getUsermanagerHeathUrl()).thenReturn("http://localhost:9092/q/health"); + when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID); + when(restTemplate.getForObject(anyString(), eq(String.class))).thenReturn("{\"status\": \"DOWN\"}"); + + var model = controller.getRootResource(); + + assertThat(model.getLink(RootController.REL_CURRENT_USER)).isNotPresent(); + } + + @Test + void shouldNotHaveCurrentUserLinkWhenError() { + when(controller.getUsermanagerHeathUrl()).thenReturn("http://localhost:9092/q/health"); + when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID); + when(restTemplate.getForObject(anyString(), eq(String.class))).thenThrow(new RestClientException("Test error")); + + var model = controller.getRootResource(); + + assertThat(model.getLink(RootController.REL_CURRENT_USER)).isNotPresent(); + } + + @Test + void shouldNotHaveCurrentUserLinkWhenNotConfigured() { var model = controller.getRootResource(); assertThat(model.getLink(RootController.REL_CURRENT_USER)).isNotPresent(); -- GitLab