Skip to content
Snippets Groups Projects
Commit 8fb8365c authored by OZGCloud's avatar OZGCloud
Browse files

OZG-2887 Verwende usermanager health check um zu prüfen ob der

Usermanager verfügbar ist
parent 10c14d58
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......
......@@ -5,6 +5,8 @@ logging:
goofy:
production: false
user-manager:
url: http://localhost:9092
keycloak:
auth-server-url: http://localhost:8088/auth
......
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment