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 ef7c8f39c940a9ba2192b0957a27beb4745ca661..a2ea4de838cd30a4e69ea22ededfe7d03ecd62b4 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/RootController.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/RootController.java @@ -14,8 +14,6 @@ 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; @@ -24,9 +22,7 @@ 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 { @@ -39,24 +35,20 @@ public class RootController { static final String REL_DOWNLOAD_TOKEN = "downloadToken"; static final String REL_CURRENT_USER = "currentUser"; - private static final String userManagerHealthPath = "/q/health"; // NOSONAR - @Autowired(required = false) public BuildProperties buildProperties; @Autowired private CurrentUserService currentUserService; - @Autowired - private RestTemplate restTemplate; - @Value("${kop.user-manager.url}") + @Value("${kop.user-manager.url:}") private String userManagerUrl; - @Value("${kop.user-manager.profile-template}") + @Value("${kop.user-manager.profile-template:}") private String userProfileTemplate; @GetMapping public EntityModel<RootResource> getRootResource() { - return ModelBuilder.fromEntity(new RootResource()) + var model = ModelBuilder.fromEntity(new RootResource()) .ifMatch(this::hasRole).addLinks( linkTo(RootController.class).withSelfRel(), linkTo(VorgangController.class).withRel(REL_VORGAENGE), @@ -66,9 +58,13 @@ 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::hasUserManager).addLinks( - Link.of(String.format(getUserProfilesUrl(), currentUserService.getUserId()), REL_CURRENT_USER)) .buildModel(); + + getUserProfilesUrl().ifPresent(urlTemplate -> { + model.add(Link.of(String.format(urlTemplate, currentUserService.getUserId()), REL_CURRENT_USER)); + }); + + return model; } boolean hasRole() { @@ -80,34 +76,12 @@ public class RootController { || currentUserService.hasRole(UserRole.VERWALTUNG_POSTSTELLE); } - 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; + Optional<String> getUserProfilesUrl() { + if (StringUtils.isNotEmpty(userManagerUrl) && StringUtils.isNotEmpty(userProfileTemplate)) { + return Optional.of(userManagerUrl + userProfileTemplate); } - return null; - } - - String getUserProfilesUrl() { - return userManagerUrl + userProfileTemplate; + return Optional.empty(); } private Link buildVorgangListByPageLink(String linkRel, Optional<UserId> assignedTo) { 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 6d8671312ad74a19f774a187bbe23f4bf439af09..9507c845040141269402a1171358c235b62dad91 100644 --- a/goofy-server/src/test/java/de/itvsh/goofy/RootControllerTest.java +++ b/goofy-server/src/test/java/de/itvsh/goofy/RootControllerTest.java @@ -8,6 +8,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.time.LocalDateTime; import java.time.ZoneOffset; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -22,8 +23,6 @@ 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; @@ -41,8 +40,6 @@ class RootControllerTest { private BuildProperties properties; @Mock private CurrentUserService currentUserService; - @Mock - private RestTemplate restTemplate; private MockMvc mockMvc; @@ -171,17 +168,10 @@ class RootControllerTest { @DisplayName("when usermanager url is configured") @Nested 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 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(); @@ -189,24 +179,6 @@ class RootControllerTest { .isEqualTo(Link.of("http://localhost:9092/api/userProfiles/" + GoofyUserTestFactory.ID, RootController.REL_CURRENT_USER) .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")