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

OZG-400 add link 'search-user-profiles'

parent 4ea1fd90
Branches
Tags
No related merge requests found
......@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import de.itvsh.goofy.common.ModelBuilder;
import de.itvsh.goofy.common.user.CurrentUserService;
import de.itvsh.goofy.common.user.UserProfileController;
import de.itvsh.goofy.common.user.UserRole;
import de.itvsh.goofy.vorgang.VorgangController;
......@@ -22,6 +23,7 @@ public class RootController {
static final String REL_VORGAENGE = "vorgaenge";
static final String REL_SEARCH = "search";
static final String REL_SEARCH_USER = "search-user-profiles";
@Autowired(required = false)
public BuildProperties buildProperties;
......@@ -36,13 +38,19 @@ public class RootController {
.addLink(linkTo(methodOn(VorgangController.class).getVorgangListByPage(0)).withRel(REL_VORGAENGE))//
.ifMatch(this::isEinheitlicherAnsprechpartner)
.addLink(() -> linkTo(methodOn(VorgangController.class).searchVorgangList(0, null)).withRel(REL_SEARCH))
.ifMatch(this::hasRoleVerwaltungUser)
.addLink(() -> linkTo(methodOn(UserProfileController.class).findUsers(null)).withRel(REL_SEARCH_USER))
.buildModel();
}
private boolean isEinheitlicherAnsprechpartner(RootResource root) {
private boolean isEinheitlicherAnsprechpartner() {
return userService.hasRole(UserRole.EINHEITLICHER_ANSPRECHPARTNER);
}
private boolean hasRoleVerwaltungUser() {
return userService.hasRole(UserRole.VERWALTUNG_USER);
}
class RootResource {
public String getVersion() {
......
......@@ -6,6 +6,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
......@@ -62,6 +63,10 @@ public class ModelBuilder<T> {
return new ConditionalLinkAdder(predicate.test(Objects.isNull(entity) ? model.getContent() : entity));
}
public ConditionalLinkAdder ifMatch(BooleanSupplier guard) {
return new ConditionalLinkAdder(guard.getAsBoolean());
}
public ModelBuilder<T> map(UnaryOperator<EntityModel<T>> mapper) {
this.mapper.add(mapper);
return this;
......
......@@ -26,9 +26,7 @@ public enum CommandOrder {
CREATE_WIEDERVORLAGE(false, Type.WIEDERVORLAGE),
EDIT_WIEDERVORLAGE(false, Type.WIEDERVORLAGE),
WIEDERVORLAGE_ERLEDIGEN(false, Type.WIEDERVORLAGE),
WIEDERVORLAGE_WIEDEREROEFFNEN(false, Type.WIEDERVORLAGE),
UNRECOGNIZED(false, null);
WIEDERVORLAGE_WIEDEREROEFFNEN(false, Type.WIEDERVORLAGE);
enum Type {
VORGANG, WIEDERVORLAGE, KOMMENTAR, FORWARDING
......
......@@ -6,6 +6,7 @@ import org.springframework.hateoas.EntityModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
......@@ -14,6 +15,8 @@ public class UserProfileController {
static final String BASE_PATH = "/api/userProfiles"; // NOSONAR
static final String PARAM_SEARCH_BY = "searchBy";
@Autowired
private UserProfileService service;
@Autowired
......@@ -24,8 +27,8 @@ public class UserProfileController {
return modelAssembler.toModel(service.getUser(userId));
}
@GetMapping
public CollectionModel<EntityModel<UserProfile>> findUsers() {
return modelAssembler.toCollectionModel(service.findUsers());
@GetMapping(params = PARAM_SEARCH_BY)
public CollectionModel<EntityModel<UserProfile>> findUsers(@RequestParam String searchBy) {
return modelAssembler.toCollectionModel(service.findUsers(searchBy));
}
}
package de.itvsh.goofy.common.user;
import org.mapstruct.Mapper;
@Mapper
interface UserProfileMapper {
UserProfile from(GoofyUser goofyUser);
}
......@@ -4,6 +4,7 @@ import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.base.Objects;
......@@ -13,17 +14,24 @@ import de.itvsh.goofy.common.errorhandling.ResourceNotFoundException;
@Service
class UserProfileService {
@Autowired
private UserRemoteService userRemoteService;
@Autowired
private UserProfileMapper userProfileMapper;
// FIXME remove dummy implementation
private static final List<UserProfile> DUMMY_USERS = List.of(
UserProfile.builder().id(UserId.from(UUID.randomUUID())).firstName("Theo").lastName("Test").build(),
UserProfile.builder().id(UserId.from(UUID.randomUUID())).firstName("Viktoria").lastName("Valid").build());
Stream<UserProfile> findUsers() {
public Stream<UserProfile> findUsers(String searchBy) {
return DUMMY_USERS.stream();
}
public UserProfile getUser(UserId id) {
return findUsers().filter(profile -> Objects.equal(profile.getId(), id)).findAny()
// FIXME DUMMY implementation
return DUMMY_USERS.stream().filter(profile -> Objects.equal(profile.getId(), id)).findAny()
.orElseThrow(() -> new ResourceNotFoundException(UserProfile.class, id));
}
......
......@@ -23,6 +23,7 @@ import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import de.itvsh.goofy.common.user.CurrentUserService;
import de.itvsh.goofy.common.user.UserRole;
class RootControllerTest {
......@@ -82,6 +83,26 @@ class RootControllerTest {
}
}
@Nested
class TestLinkForProfileSearch {
@BeforeEach
void init() {
when(userService.hasRole(anyString())).thenReturn(false);
}
@DisplayName("should exists on user with role VERWALTUNG_USER")
@Test
void shouldExist() {
when(userService.hasRole(UserRole.VERWALTUNG_USER)).thenReturn(true);
var model = controller.getRootResource();
assertThat(model.getLink(RootController.REL_SEARCH_USER)).isPresent().get().extracting(Link::getHref)
.isEqualTo("/api/userProfiles?searchBy={searchBy}");
}
}
@Test
void shouldHaveJavaVersion() throws Exception {
callEndpoint().andExpect(jsonPath("$.javaVersion").exists());
......
......@@ -40,12 +40,14 @@ class UserProfileControllerTest {
@Nested
class findUsersTest {
private static final String SEARCH_BY = "theo";
private Stream<UserProfile> profiles = Stream.of(UserProfileTestFactory.create());
@SuppressWarnings("unchecked")
@BeforeEach
void init() {
when(service.findUsers()).thenReturn(profiles);
when(service.findUsers(anyString())).thenReturn(profiles);
when(modelAssembler.toCollectionModel(Mockito.<Stream<UserProfile>>any()))
.then(i -> CollectionModel.of(
......@@ -61,7 +63,7 @@ class UserProfileControllerTest {
void shouldCallService() throws Exception {
doRequest();
verify(service).findUsers();
verify(service).findUsers(anyString());
}
@Test
......@@ -78,7 +80,8 @@ class UserProfileControllerTest {
}
private ResultActions doRequest() throws Exception {
return mockMvc.perform(get(UserProfileController.BASE_PATH)).andExpect(status().is2xxSuccessful());
return mockMvc.perform(get(UserProfileController.BASE_PATH).param(UserProfileController.PARAM_SEARCH_BY, SEARCH_BY))
.andExpect(status().is2xxSuccessful());
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment