Skip to content
Snippets Groups Projects
Commit 4f040d43 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-2887 Status check entfernt

parent c70dee13
No related branches found
No related tags found
No related merge requests found
...@@ -14,8 +14,6 @@ import org.springframework.hateoas.Link; ...@@ -14,8 +14,6 @@ import org.springframework.hateoas.Link;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; 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.ModelBuilder;
import de.itvsh.goofy.common.downloadtoken.DownloadTokenController; import de.itvsh.goofy.common.downloadtoken.DownloadTokenController;
...@@ -24,9 +22,7 @@ import de.itvsh.goofy.common.user.UserId; ...@@ -24,9 +22,7 @@ import de.itvsh.goofy.common.user.UserId;
import de.itvsh.goofy.common.user.UserProfileController; import de.itvsh.goofy.common.user.UserProfileController;
import de.itvsh.goofy.common.user.UserRole; import de.itvsh.goofy.common.user.UserRole;
import de.itvsh.goofy.vorgang.VorgangController; import de.itvsh.goofy.vorgang.VorgangController;
import lombok.extern.log4j.Log4j2;
@Log4j2
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api")
public class RootController { public class RootController {
...@@ -39,24 +35,20 @@ public class RootController { ...@@ -39,24 +35,20 @@ public class RootController {
static final String REL_DOWNLOAD_TOKEN = "downloadToken"; static final String REL_DOWNLOAD_TOKEN = "downloadToken";
static final String REL_CURRENT_USER = "currentUser"; static final String REL_CURRENT_USER = "currentUser";
private static final String userManagerHealthPath = "/q/health"; // NOSONAR
@Autowired(required = false) @Autowired(required = false)
public BuildProperties buildProperties; public BuildProperties buildProperties;
@Autowired @Autowired
private CurrentUserService currentUserService; private CurrentUserService currentUserService;
@Autowired
private RestTemplate restTemplate;
@Value("${kop.user-manager.url}") @Value("${kop.user-manager.url:}")
private String userManagerUrl; private String userManagerUrl;
@Value("${kop.user-manager.profile-template}") @Value("${kop.user-manager.profile-template:}")
private String userProfileTemplate; private String userProfileTemplate;
@GetMapping @GetMapping
public EntityModel<RootResource> getRootResource() { public EntityModel<RootResource> getRootResource() {
return ModelBuilder.fromEntity(new RootResource()) var model = ModelBuilder.fromEntity(new RootResource())
.ifMatch(this::hasRole).addLinks( .ifMatch(this::hasRole).addLinks(
linkTo(RootController.class).withSelfRel(), linkTo(RootController.class).withSelfRel(),
linkTo(VorgangController.class).withRel(REL_VORGAENGE), linkTo(VorgangController.class).withRel(REL_VORGAENGE),
...@@ -66,9 +58,13 @@ public class RootController { ...@@ -66,9 +58,13 @@ public class RootController {
.ifMatch(this::hasVerwaltungRole).addLinks( .ifMatch(this::hasVerwaltungRole).addLinks(
buildVorgangListByPageLink(REL_MY_VORGAENGE, Optional.of(currentUserService.getUserId())), buildVorgangListByPageLink(REL_MY_VORGAENGE, Optional.of(currentUserService.getUserId())),
buildVorgangListByPageLink(REL_SEARCH_MY_VORGAENGE, Optional.of(currentUserService.getUserId()))) buildVorgangListByPageLink(REL_SEARCH_MY_VORGAENGE, Optional.of(currentUserService.getUserId())))
.ifMatch(this::hasUserManager).addLinks(
Link.of(String.format(getUserProfilesUrl(), currentUserService.getUserId()), REL_CURRENT_USER))
.buildModel(); .buildModel();
getUserProfilesUrl().ifPresent(urlTemplate -> {
model.add(Link.of(String.format(urlTemplate, currentUserService.getUserId()), REL_CURRENT_USER));
});
return model;
} }
boolean hasRole() { boolean hasRole() {
...@@ -80,34 +76,12 @@ public class RootController { ...@@ -80,34 +76,12 @@ public class RootController {
|| currentUserService.hasRole(UserRole.VERWALTUNG_POSTSTELLE); || currentUserService.hasRole(UserRole.VERWALTUNG_POSTSTELLE);
} }
boolean hasUserManager() { Optional<String> getUserProfilesUrl() {
String url = getUsermanagerHeathUrl(); if (StringUtils.isNotEmpty(userManagerUrl) && StringUtils.isNotEmpty(userProfileTemplate)) {
if (StringUtils.isNotEmpty(url)) { return Optional.of(userManagerUrl + userProfileTemplate);
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;
} }
String getUserProfilesUrl() { return Optional.empty();
return userManagerUrl + userProfileTemplate;
} }
private Link buildVorgangListByPageLink(String linkRel, Optional<UserId> assignedTo) { private Link buildVorgangListByPageLink(String linkRel, Optional<UserId> assignedTo) {
......
...@@ -8,6 +8,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -8,6 +8,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
...@@ -22,8 +23,6 @@ import org.springframework.hateoas.Link; ...@@ -22,8 +23,6 @@ import org.springframework.hateoas.Link;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 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.CurrentUserService;
import de.itvsh.goofy.common.user.GoofyUserTestFactory; import de.itvsh.goofy.common.user.GoofyUserTestFactory;
...@@ -41,8 +40,6 @@ class RootControllerTest { ...@@ -41,8 +40,6 @@ class RootControllerTest {
private BuildProperties properties; private BuildProperties properties;
@Mock @Mock
private CurrentUserService currentUserService; private CurrentUserService currentUserService;
@Mock
private RestTemplate restTemplate;
private MockMvc mockMvc; private MockMvc mockMvc;
...@@ -171,17 +168,10 @@ class RootControllerTest { ...@@ -171,17 +168,10 @@ class RootControllerTest {
@DisplayName("when usermanager url is configured") @DisplayName("when usermanager url is configured")
@Nested @Nested
class UsermanagerUrlConfigured { class UsermanagerUrlConfigured {
@BeforeEach
void init() {
when(controller.getUsermanagerHeathUrl()).thenReturn("http://localhost:9092/q/health");
when(controller.getUserProfilesUrl()).thenReturn("http://localhost:9092/api/userProfiles/%s");
when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID);
}
@Test @Test
void shouldHaveCurrentUserLink() { void shouldHaveCurrentUserLink() {
when(restTemplate.getForObject(anyString(), eq(String.class))).thenReturn("{\"status\": \"UP\"}"); when(controller.getUserProfilesUrl()).thenReturn(Optional.of("http://localhost:9092/api/userProfiles/%s"));
when(currentUserService.getUserId()).thenReturn(GoofyUserTestFactory.ID);
var model = controller.getRootResource(); var model = controller.getRootResource();
...@@ -189,24 +179,6 @@ class RootControllerTest { ...@@ -189,24 +179,6 @@ class RootControllerTest {
.isEqualTo(Link.of("http://localhost:9092/api/userProfiles/" + GoofyUserTestFactory.ID, RootController.REL_CURRENT_USER) .isEqualTo(Link.of("http://localhost:9092/api/userProfiles/" + GoofyUserTestFactory.ID, RootController.REL_CURRENT_USER)
.getHref()); .getHref());
} }
@Test
void shouldNotHaveCurrentUserLinkWhenDown() {
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(restTemplate.getForObject(anyString(), eq(String.class))).thenThrow(new RestClientException("Test error"));
var model = controller.getRootResource();
assertThat(model.getLink(RootController.REL_CURRENT_USER)).isNotPresent();
}
} }
@DisplayName("when usermanager url is not configured") @DisplayName("when usermanager url is not configured")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment