Skip to content

Commit

Permalink
Merge pull request #10 from szymonpoltorak/tests-and-fixes
Browse files Browse the repository at this point in the history
Fixed mappings and added missing tests
  • Loading branch information
szymonpoltorak authored Dec 17, 2023
2 parents ab627a5 + 62902b2 commit e4420dc
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,56 @@ public class SprintControllerImpl implements SprintController {

@Override
@PostMapping(value = ADD_SPRINT)
public SprintResponse addNewSprint(@RequestBody SprintRequest sprintRequest, @JwtAuthed User user) {
public final SprintResponse addNewSprint(@RequestBody SprintRequest sprintRequest, @JwtAuthed User user) {
return sprintService.addNewSprint(sprintRequest, user);
}

@Override
@GetMapping(value = GET_SPRINT_BY_ID)
public SprintResponse getSprintById(@RequestParam long sprintId, @JwtAuthed User user) {
public final SprintResponse getSprintById(@RequestParam long sprintId, @JwtAuthed User user) {
return sprintService.getSprintById(sprintId, user);
}

@Override
@GetMapping(value = GET_SPRINTS_ON_PAGE)
public List<SprintResponse> getSprintsOnPage(@RequestParam int page, @RequestParam long projectId, @JwtAuthed User user) {
public final List<SprintResponse> getSprintsOnPage(@RequestParam int page, @RequestParam long projectId,
@JwtAuthed User user) {
return sprintService.getSprintsOnPage(page, projectId, user);
}

@Override
@PatchMapping(value = UPDATE_SPRINTS_NAME)
public SprintResponse updateSprintsName(@RequestParam String name, @RequestParam long sprintId, @JwtAuthed User user) {
public final SprintResponse updateSprintsName(@RequestParam String name, @RequestParam long sprintId,
@JwtAuthed User user) {
return sprintService.updateSprintsName(name, sprintId, user);
}

@Override
@PatchMapping(value = UPDATE_SPRINTS_DESCRIPTION)
public SprintResponse updateSprintsDescription(@RequestParam String description, @RequestParam long sprintId, @JwtAuthed User user) {
public final SprintResponse updateSprintsDescription(@RequestParam String description,
@RequestParam long sprintId,
@JwtAuthed User user) {
return sprintService.updateSprintsDescription(description, sprintId, user);
}

@Override
@PatchMapping(value = UPDATE_SPRINTS_START_DATE)
public SprintResponse updateSprintsStartDate(@RequestParam LocalDate startDate, @RequestParam long sprintId, @JwtAuthed User user) {
public final SprintResponse updateSprintsStartDate(@RequestParam LocalDate startDate,
@RequestParam long sprintId,
@JwtAuthed User user) {
return sprintService.updateSprintsStartDate(startDate, sprintId, user);
}

@Override
@PatchMapping(value = UPDATE_SPRINTS_END_DATE)
public SprintResponse updateSprintsEndDate(@RequestParam LocalDate endDate, @RequestParam long sprintId, @JwtAuthed User user) {
public final SprintResponse updateSprintsEndDate(@RequestParam LocalDate endDate, @RequestParam long sprintId,
@JwtAuthed User user) {
return sprintService.updateSprintsEndDate(endDate, sprintId, user);
}

@Override
@DeleteMapping(value = DELETE_SPRINT)
public SprintResponse deleteSprint(@RequestParam long sprintId, @JwtAuthed User user) {
public final SprintResponse deleteSprint(@RequestParam long sprintId, @JwtAuthed User user) {
return sprintService.deleteSprint(sprintId, user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
@RequiredArgsConstructor
public class SprintServiceImpl implements SprintService {
public static final int SPRINTS_PER_PAGE = 20;
private static final String UPDATED_SPRINT = "Updated sprint: {}";
private static final String FOUND_SPRINT_TO_UPDATE = "Found sprint to update: {}";
private final SprintRepository sprintRepository;
private final SprintMapper sprintMapper;

@Override
public final SprintResponse addNewSprint(SprintRequest sprintRequest, User user) {
//auth user
//TODO auth user

if (sprintRequest.endDate().isBefore(sprintRequest.startDate())) {
throw new SprintEndDateMustBeAfterStartDate(sprintRequest.startDate(), sprintRequest.endDate());
Expand All @@ -44,6 +46,7 @@ public final SprintResponse addNewSprint(SprintRequest sprintRequest, User user)
log.info("Instantiated sprint: {}", sprint);

Sprint newSprint = sprintRepository.save(sprint);

log.info("Saved sprint: {}", sprint);

return sprintMapper.toSprintResponse(newSprint);
Expand All @@ -53,10 +56,11 @@ public final SprintResponse addNewSprint(SprintRequest sprintRequest, User user)
public final SprintResponse getSprintById(long sprintId, User user) {
log.info("Getting sprint with id: {} for user: {}", sprintId, user);

//auth user
//TODO auth user

Sprint sprint = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));

log.info("Sprint found : {}", sprint);

return sprintMapper.toSprintResponse(sprint);
Expand All @@ -66,10 +70,11 @@ public final SprintResponse getSprintById(long sprintId, User user) {
public final List<SprintResponse> getSprintsOnPage(int page, long projectId, User user) {
log.info("Getting sprints in project {} on page: {} for user: {}", projectId, page, user);

//auth user
//TODO auth user

Pageable pageable = PageRequest.of(page, SPRINTS_PER_PAGE);
Page<Sprint> sprints = sprintRepository.findAllByProjectId(projectId, pageable);

log.info("Sprints found on page : {}", sprints.getTotalElements());

return sprints.map(sprintMapper::toSprintResponse).toList();
Expand All @@ -79,15 +84,18 @@ public final List<SprintResponse> getSprintsOnPage(int page, long projectId, Use
public final SprintResponse updateSprintsName(String name, long sprintId, User user) {
log.info("Updating sprint with id: {} name to: {}", sprintId, name);

//auth user
//TODO auth user

Sprint sprintToUpdate = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to update: {}", sprintToUpdate);

log.info(FOUND_SPRINT_TO_UPDATE, sprintToUpdate);

sprintToUpdate.setSprintName(name);

Sprint updatedSprint = sprintRepository.save(sprintToUpdate);
log.info("Updated sprint: {}", updatedSprint);

log.info(UPDATED_SPRINT, updatedSprint);

return sprintMapper.toSprintResponse(updatedSprint);
}
Expand All @@ -96,15 +104,18 @@ public final SprintResponse updateSprintsName(String name, long sprintId, User u
public final SprintResponse updateSprintsDescription(String description, long sprintId, User user) {
log.info("Updating sprint with id: {} description to: {}", sprintId, description);

//auth user
//TODO auth user

Sprint sprintToUpdate = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to update: {}", sprintToUpdate);

log.info(FOUND_SPRINT_TO_UPDATE, sprintToUpdate);

sprintToUpdate.setSprintDescription(description);

Sprint updatedSprint = sprintRepository.save(sprintToUpdate);
log.info("Updated sprint: {}", updatedSprint);

log.info(UPDATED_SPRINT, updatedSprint);

return sprintMapper.toSprintResponse(updatedSprint);
}
Expand All @@ -113,19 +124,21 @@ public final SprintResponse updateSprintsDescription(String description, long sp
public final SprintResponse updateSprintsStartDate(LocalDate startDate, long sprintId, User user) {
log.info("Updating sprint with id: {} startDate to: {}", sprintId, startDate);

//auth user
//TODO auth user

Sprint sprintToUpdate = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to update: {}", sprintToUpdate);

log.info(FOUND_SPRINT_TO_UPDATE, sprintToUpdate);

if (sprintToUpdate.isEndBefore(startDate)) {
throw new SprintEndDateMustBeAfterStartDate(startDate, sprintToUpdate.getSprintEndDate());
}

sprintToUpdate.setSprintStartDate(startDate);

Sprint updatedSprint = sprintRepository.save(sprintToUpdate);
log.info("Updated sprint: {}", updatedSprint);

log.info(UPDATED_SPRINT, updatedSprint);

return sprintMapper.toSprintResponse(updatedSprint);
}
Expand All @@ -134,19 +147,21 @@ public final SprintResponse updateSprintsStartDate(LocalDate startDate, long spr
public final SprintResponse updateSprintsEndDate(LocalDate endDate, long sprintId, User user) {
log.info("Updating sprint with id: {} endDate to: {}", sprintId, endDate);

//auth user
//TODO auth user

Sprint sprintToUpdate = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));
log.info("Found sprint to update: {}", sprintToUpdate);

log.info(FOUND_SPRINT_TO_UPDATE, sprintToUpdate);

if (sprintToUpdate.isStartAfter(endDate)) {
throw new SprintEndDateMustBeAfterStartDate(sprintToUpdate.getSprintStartDate(), endDate);
}

sprintToUpdate.setSprintEndDate(endDate);

Sprint updatedSprint = sprintRepository.save(sprintToUpdate);
log.info("Updated sprint: {}", updatedSprint);

log.info(UPDATED_SPRINT, updatedSprint);

return sprintMapper.toSprintResponse(sprintToUpdate);
}
Expand All @@ -155,13 +170,15 @@ public final SprintResponse updateSprintsEndDate(LocalDate endDate, long sprintI
public final SprintResponse deleteSprint(long sprintId, User user) {
log.info("Deleting sprint with id: {}", sprintId);

//auth user
//TODO auth user

Sprint sprintToDelete = sprintRepository.findById(sprintId)
.orElseThrow(() -> new SprintDoesNotExistException(sprintId));

log.info("Found sprint to delete: {}", sprintToDelete);

sprintRepository.deleteById(sprintId);

log.info("Deleted sprint with id: {}", sprintToDelete);

return sprintMapper.toSprintResponse(sprintToDelete);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package dev.corn.cornbackend.api.sprint.constants;

public final class SprintMappings {

private SprintMappings() {
}

public static final String SPRINT_API_ENDPOINT = "/api/v1/sprint";

public static final String GET_SPRINTS_ON_PAGE = "/getSprintsOnPage";
Expand All @@ -14,9 +10,15 @@ private SprintMappings() {
public static final String ADD_SPRINT = "/addSprint";

public static final String UPDATE_SPRINTS_NAME = "/updateSprintsName";
public static final String UPDATE_SPRINTS_DESCRIPTION = "/updateSprintsName";
public static final String UPDATE_SPRINTS_START_DATE = "/updateSprintsName";
public static final String UPDATE_SPRINTS_END_DATE = "/updateSprintsName";

public static final String UPDATE_SPRINTS_DESCRIPTION = "/updateSprintsDescription";

public static final String UPDATE_SPRINTS_START_DATE = "/updateSprintsStartDate";

public static final String UPDATE_SPRINTS_END_DATE = "/updateSprintsEndDate";

public static final String DELETE_SPRINT = "/deleteSprint";

private SprintMappings() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serial;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
Expand All @@ -33,10 +32,6 @@
@NoArgsConstructor
@AllArgsConstructor
public class User implements Jsonable, ServiceUser {
@Serial
private static final long serialVersionUID = 1236185595152412287L;
private static final String DEV_CORN_CORNBACKEND_ENTITIES_USER_USER = "dev.corn.cornbackend.entities.user.User";

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long userId;
Expand Down Expand Up @@ -76,8 +71,8 @@ public final String getFullName() {
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities;
public final Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.unmodifiableCollection(authorities);
}

@Override
Expand All @@ -104,14 +99,4 @@ public final int hashCode() {

return result;
}

@Serial
private void readObject(java.io.ObjectInputStream in) throws ClassNotFoundException, java.io.NotSerializableException {
throw new java.io.NotSerializableException(DEV_CORN_CORNBACKEND_ENTITIES_USER_USER);
}

@Serial
private void writeObject(java.io.ObjectOutputStream out) throws java.io.NotSerializableException {
throw new java.io.NotSerializableException(DEV_CORN_CORNBACKEND_ENTITIES_USER_USER);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.corn.cornbackend.api.sprint;


import dev.corn.cornbackend.api.sprint.data.SprintRequest;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;
import dev.corn.cornbackend.api.sprint.interfaces.SprintService;
import dev.corn.cornbackend.entities.sprint.interfaces.SprintMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import static org.mockito.Mockito.when;

@SpringBootTest(classes = SprintRepository.class)
public class SprintServiceTest {
class SprintServiceTest {

static final String SHOULD_THROW_SPRINT_DOES_NOT_EXIST_EXCEPTION = "Should throw SprintDoesNotExistException";
private static final String SHOULD_THROW_SPRINT_DOES_NOT_EXIST_EXCEPTION = "Should throw SprintDoesNotExistException";
static final String SPRINT_RESPONSE_SHOULD_BE_EQUAL_TO_EXPECTED = "SprintResponse should be equal to expected";

@InjectMocks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dev.corn.cornbackend.api.user;

import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;
import dev.corn.cornbackend.entities.user.interfaces.UserMapper;
import dev.corn.cornbackend.entities.user.interfaces.UserMapperImpl;
import dev.corn.cornbackend.entities.user.interfaces.UserRepository;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SpringBootTest(classes = {UserRepository.class, UserMapper.class})
class UserServiceTest {
@InjectMocks
private UserServiceImpl userService;

@Mock
private UserRepository userRepository;

@Mock
private UserMapper userMapper;

private static final UserMapper MAPPER = new UserMapperImpl();

@Test
final void test_registerUser_shouldRegisterUser() {
// given
String name = "name";
String surname = "surname";
String username = "username";
User user = User.builder()
.name(name)
.surname(surname)
.username(username)
.build();
UserResponse expected = MAPPER.toUserResponse(user);

// when
when(userRepository.save(user))
.thenReturn(user);
when(userMapper.toUserResponse(user))
.thenReturn(expected);

UserResponse actual = userService.registerUser(name, surname, username);

// then
assertEquals(expected, actual, "UserResponse should be equal to expected");
verify(userRepository).save(user);
}
}
Loading

0 comments on commit e4420dc

Please sign in to comment.