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

OZG-400 Rest-API for UserProfiles

parent 19a0d783
No related branches found
No related tags found
No related merge requests found
package de.itvsh.goofy.common.user;
import java.util.UUID;
import de.itvsh.goofy.common.datatypes.StringBasedValue;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
......@@ -15,6 +17,10 @@ public class UserId extends StringBasedValue {
super(userId);
}
public static UserId from(UUID userId) {
return UserId.from(userId.toString());
}
public static UserId from(String userId) {
return new UserId(userId);
}
......
package de.itvsh.goofy.common.user;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class UserProfile {
@JsonIgnore
private UserId id;
private String firstName;
private String lastName;
}
package de.itvsh.goofy.common.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.CollectionModel;
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.RestController;
@RestController
@RequestMapping(UserProfileController.BASE_PATH)
public class UserProfileController {
static final String BASE_PATH = "/api/userProfiles"; // NOSONAR
@Autowired
private UserProfileService service;
@Autowired
private UserProfileModelAssembler modelAssembler;
@GetMapping("{userId}")
public EntityModel<UserProfile> getUser(@PathVariable UserId userId) {
return modelAssembler.toModel(service.getUser(userId));
}
@GetMapping
public CollectionModel<EntityModel<UserProfile>> findUsers() {
return modelAssembler.toCollectionModel(service.findUsers());
}
}
package de.itvsh.goofy.common.user;
import java.util.stream.Stream;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.stereotype.Component;
@Component
class UserProfileModelAssembler implements RepresentationModelAssembler<UserProfile, EntityModel<UserProfile>> {
@Override
public EntityModel<UserProfile> toModel(UserProfile entity) {
// TODO Auto-generated method stub
return null;
}
public CollectionModel<EntityModel<UserProfile>> toCollectionModel(Stream<UserProfile> userProfile) {
// FIXME
return null;
}
}
package de.itvsh.goofy.common.user;
import java.util.stream.Stream;
import org.springframework.stereotype.Service;
@Service
class UserProfileService {
Stream<UserProfile> findUsers() {
// FIXME
return Stream.empty();
}
public UserProfile getUser(UserId id) {
// TODO Auto-generated method stub
return null;
}
}
package de.itvsh.goofy.common.user;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
class UserProfileControllerTest {
@InjectMocks
private UserProfileController controller;
@Mock
private UserProfileService service;
@Mock
private UserProfileModelAssembler modelAssembler;
private MockMvc mockMvc;
@BeforeEach
void initTest() {
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Nested
class findUsersTest {
private Stream<UserProfile> profiles = Stream.of(UserProfileTestFactory.create());
@SuppressWarnings("unchecked")
@BeforeEach
void init() {
when(service.findUsers()).thenReturn(profiles);
when(modelAssembler.toCollectionModel(Mockito.<Stream<UserProfile>>any()))
.then(i -> CollectionModel.of(
((Stream<UserProfile>) i.getArgument(0)).map(profile -> EntityModel.of(profile)).collect(Collectors.toList())));
}
@Test
void shouldReturnOk() throws Exception {
doRequest().andExpect(status().isOk());
}
@Test
void shouldCallService() throws Exception {
doRequest();
verify(service).findUsers();
}
@Test
void shouldCallModellAssembler() throws Exception {
doRequest();
verify(modelAssembler).toCollectionModel(profiles);
}
@Test
void shouldReturnProfiles() throws Exception {
doRequest()
.andExpect(jsonPath("content.length()").value(1));
}
private ResultActions doRequest() throws Exception {
return mockMvc.perform(get(UserProfileController.BASE_PATH)).andExpect(status().is2xxSuccessful());
}
}
@Nested
class getUserTest {
static final String SINGLE_RESOURCE_URL = UserProfileController.BASE_PATH + "/{id}";
private UserProfile userProfile = UserProfileTestFactory.create();
@BeforeEach
void init() {
when(service.getUser(any())).thenReturn(userProfile);
when(modelAssembler.toModel(any())).then(i -> EntityModel.of(i.getArgument(0)));
}
@Test
void shouldReturnOk() throws Exception {
doRequest().andExpect(status().isOk());
}
@Test
void shouldCallService() throws Exception {
doRequest();
verify(service).getUser(UserProfileTestFactory.ID);
}
@Test
void shouldCallModellAssembler() throws Exception {
doRequest();
verify(modelAssembler).toModel(userProfile);
}
@Test
void shouldReturnProfile() throws Exception {
doRequest()
.andExpect(jsonPath("firstName").value(UserProfileTestFactory.FIRST_NAME));
}
private ResultActions doRequest() throws Exception {
return mockMvc.perform(get(SINGLE_RESOURCE_URL, UserProfileTestFactory.ID)).andExpect(status().is2xxSuccessful());
}
}
}
package de.itvsh.goofy.common.user;
import java.util.UUID;
import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum;
public class UserProfileTestFactory {
private static final Lorem LOREM = LoremIpsum.getInstance();
public static final UserId ID = UserId.from(UUID.randomUUID());
public static final String FIRST_NAME = LOREM.getFirstName();
public static final String LAST_NAME = LOREM.getLastName();
public static UserProfile create() {
return createBuilder().build();
}
public static UserProfile.UserProfileBuilder createBuilder() {
return UserProfile.builder()
.id(ID)
.firstName(FIRST_NAME)
.lastName(LAST_NAME);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment