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 38641bc3009b8dcd9d24e182275598ebbc7e8dac..49b3fae1d7c85b4a1ee71bf95605802b602ea614 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 d9fb15b0f488f337653c6a468491e4c7aa08929b..07fd17b7c8da6f6113521835e9261602e0ab09bc 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 7fa276fab18bb215ac033f0b551177dd27754371..122e8000d9c2cf1088f8f350108669f3bce9d62e 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();