Skip to content

Commit

Permalink
Merge pull request #25 from szymonpoltorak/comments-test-doc
Browse files Browse the repository at this point in the history
Comments test and docs
  • Loading branch information
szymonpoltorak authored May 23, 2023
2 parents 2078efa + 083ecb1 commit 2fd2b9c
Show file tree
Hide file tree
Showing 38 changed files with 1,056 additions and 86 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Makefile scripts may need administrator priviliges due to *docker* command usage

* `make`, `make all`, `make build` - uses *docker-compose* to build the whole project.
Frontend of the app will be available at `localhost:4200`,
database at `localhost:5000` and backend at `localhost:8080`
database at `localhost:5432` and backend at `localhost:8080`

* `make clean` - command used to remove with force images created ,
during *docker-compose* build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import razepl.dev.socialappbackend.auth.apicalls.*;
import razepl.dev.socialappbackend.auth.interfaces.AuthInterface;
import razepl.dev.socialappbackend.auth.interfaces.AuthServiceInterface;
import razepl.dev.socialappbackend.exceptions.validators.NullChecker;
import razepl.dev.socialappbackend.exceptions.validators.ArgumentValidator;

import static razepl.dev.socialappbackend.auth.constants.AuthMappings.*;

Expand All @@ -32,7 +32,7 @@ public class AuthController implements AuthInterface {
@Override
@PostMapping(value = REGISTER_MAPPING)
public final ResponseEntity<AuthResponse> registerUser(@RequestBody RegisterRequest registerRequest) {
NullChecker.throwAppropriateException(registerRequest);
ArgumentValidator.throwIfNull(registerRequest);

log.info("Registering user with data: \n{}", registerRequest);

Expand All @@ -42,7 +42,7 @@ public final ResponseEntity<AuthResponse> registerUser(@RequestBody RegisterRequ
@Override
@PostMapping(value = LOGIN_MAPPING)
public final ResponseEntity<AuthResponse> loginUser(@RequestBody LoginRequest loginRequest) {
NullChecker.throwAppropriateException(loginRequest);
ArgumentValidator.throwIfNull(loginRequest);

log.info("Logging user with data: \n{}", loginRequest);

Expand All @@ -52,7 +52,7 @@ public final ResponseEntity<AuthResponse> loginUser(@RequestBody LoginRequest lo
@Override
@PostMapping(value = REFRESH_MAPPING)
public final ResponseEntity<AuthResponse> refreshUserToken(HttpServletRequest request, HttpServletResponse response) {
NullChecker.throwAppropriateException(request, response);
ArgumentValidator.throwIfNull(request, response);

log.info("Refreshing users token.");

Expand All @@ -62,7 +62,7 @@ public final ResponseEntity<AuthResponse> refreshUserToken(HttpServletRequest re
@Override
@PostMapping(value = AUTHENTICATE_MAPPING)
public final ResponseEntity<TokenResponse> authenticateUser(@RequestBody TokenRequest request) {
NullChecker.throwAppropriateException(request);
ArgumentValidator.throwIfNull(request);

log.info("Authenticating user with data:\n{}", request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import razepl.dev.socialappbackend.entities.jwt.interfaces.TokenManager;
import razepl.dev.socialappbackend.config.interfaces.JwtServiceInterface;
import razepl.dev.socialappbackend.exceptions.*;
import razepl.dev.socialappbackend.exceptions.validators.NullChecker;
import razepl.dev.socialappbackend.exceptions.validators.ArgumentValidator;
import razepl.dev.socialappbackend.entities.user.Role;
import razepl.dev.socialappbackend.entities.user.User;
import razepl.dev.socialappbackend.entities.user.interfaces.UserRepository;
Expand All @@ -45,7 +45,7 @@ public class AuthService implements AuthServiceInterface {

@Override
public final AuthResponse register(RegisterUserRequest userRequest) {
NullChecker.throwAppropriateException(userRequest);
ArgumentValidator.throwIfNull(userRequest);

String password = userRequest.getPassword();

Expand Down Expand Up @@ -78,7 +78,7 @@ public final AuthResponse register(RegisterUserRequest userRequest) {

@Override
public final AuthResponse login(LoginUserRequest loginRequest) {
NullChecker.throwAppropriateException(loginRequest);
ArgumentValidator.throwIfNull(loginRequest);

String username = loginRequest.getUsername();

Expand All @@ -96,7 +96,7 @@ public final AuthResponse login(LoginUserRequest loginRequest) {

@Override
public final AuthResponse refreshToken(HttpServletRequest request, HttpServletResponse response) {
NullChecker.throwAppropriateException(request, response);
ArgumentValidator.throwIfNull(request, response);

String refreshToken = jwtService.getJwtRefreshToken(request);

Expand Down Expand Up @@ -130,7 +130,7 @@ public final AuthResponse refreshToken(HttpServletRequest request, HttpServletRe

@Override
public final TokenResponse validateUsersTokens(TokenRequest request) {
NullChecker.throwAppropriateException(request);
ArgumentValidator.throwIfNull(request);

User user = userRepository.findUserByToken(request.authToken()).orElseThrow(TokensUserNotFoundException::new);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import razepl.dev.socialappbackend.config.constants.Headers;
import razepl.dev.socialappbackend.config.constants.Properties;
import razepl.dev.socialappbackend.config.interfaces.JwtServiceInterface;
import razepl.dev.socialappbackend.exceptions.validators.NullChecker;
import razepl.dev.socialappbackend.exceptions.validators.ArgumentValidator;

import java.security.Key;
import java.util.Collections;
Expand All @@ -37,14 +37,14 @@ public class JwtService implements JwtServiceInterface {

@Override
public final String getUsernameFromToken(String jwtToken) {
NullChecker.throwAppropriateException(jwtToken);
ArgumentValidator.throwIfNull(jwtToken);

return getClaimFromToken(jwtToken, Claims::getSubject);
}

@Override
public final <T> T getClaimFromToken(String jwtToken, Function<Claims, T> claimsHandler) {
NullChecker.throwAppropriateException(jwtToken, claimsHandler);
ArgumentValidator.throwIfNull(jwtToken, claimsHandler);

Claims claims = getAllClaims(jwtToken);

Expand All @@ -53,28 +53,28 @@ public final <T> T getClaimFromToken(String jwtToken, Function<Claims, T> claims

@Override
public final String generateRefreshToken(UserDetails userDetails) {
NullChecker.throwAppropriateException(userDetails);
ArgumentValidator.throwIfNull(userDetails);

return buildToken(Collections.emptyMap(), userDetails, refreshTime);
}

@Override
public final String generateToken(UserDetails userDetails) {
NullChecker.throwAppropriateException(userDetails);
ArgumentValidator.throwIfNull(userDetails);

return generateToken(Collections.emptyMap(), userDetails, expirationTime);
}

@Override
public final String generateToken(Map<String, Object> additionalClaims, UserDetails userDetails, long expiration) {
NullChecker.throwAppropriateException(additionalClaims, userDetails);
ArgumentValidator.throwIfNull(additionalClaims, userDetails);

return buildToken(additionalClaims, userDetails, expiration);
}

@Override
public final boolean isTokenValid(String jwtToken, UserDetails userDetails) {
NullChecker.throwAppropriateException(jwtToken, userDetails);
ArgumentValidator.throwIfNull(jwtToken, userDetails);

String username = getUsernameFromToken(jwtToken);

Expand All @@ -83,7 +83,7 @@ public final boolean isTokenValid(String jwtToken, UserDetails userDetails) {

@Override
public final String getJwtToken(HttpServletRequest request) {
NullChecker.throwAppropriateException(request);
ArgumentValidator.throwIfNull(request);

String authHeader = request.getHeader(Headers.AUTH_HEADER);

Expand All @@ -95,7 +95,7 @@ public final String getJwtToken(HttpServletRequest request) {

@Override
public final String getJwtRefreshToken(HttpServletRequest request) {
NullChecker.throwAppropriateException(request);
ArgumentValidator.throwIfNull(request);

String authHeader = request.getHeader(Headers.AUTH_HEADER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
import org.springframework.data.jpa.repository.Query;
import razepl.dev.socialappbackend.entities.post.Post;

/**
* Repository interface for accessing and managing Comment entities.
* Extends the JpaRepository interface to inherit common CRUD and pagination operations.
*/
public interface CommentRepository extends JpaRepository<Comment, Long> {
/**
* Retrieves a page of comments for a specific post, sorted by comment date.
*
* @param postId - The ID of the post to retrieve comments for.
* @param pageable - The pageable object specifying the page number, size, and sort criteria.
* @return A page of comments for the specified post.
*/
@Query("""
select c
from Comment as c
Expand All @@ -15,5 +26,11 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
""")
Page<Comment> findCommentsByPostId(long postId, Pageable pageable);

/**
* Counts the number of comments associated with a specific post.
*
* @param post - The post to count comments for.
* @return The number of comments for the specified post.
*/
long countCommentsByPost(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,26 @@

import java.util.Optional;

/**
* Repository interface for accessing and managing CommentLike entities.
* Extends the JpaRepository interface to inherit common CRUD operations.
*/
public interface CommentLikeRepository extends JpaRepository<CommentLike, Long> {
/**
* Counts the number of likes for a specific comment.
*
* @param comment - The comment to count likes for.
* @return The number of likes for the specified comment.
*/
long countByComment(Comment comment);

/**
* Retrieves a like for a specific comment and user.
*
* @param user - The user who liked the comment.
* @param comment - The comment to find the like for.
* @return An Optional containing the like for the specified comment and user, or empty if not found.
*/
@Query("""
select l
from CommentLike as l
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import razepl.dev.socialappbackend.entities.jwt.interfaces.TokenManager;
import razepl.dev.socialappbackend.entities.jwt.interfaces.TokenRepository;
import razepl.dev.socialappbackend.config.interfaces.JwtServiceInterface;
import razepl.dev.socialappbackend.exceptions.validators.NullChecker;
import razepl.dev.socialappbackend.exceptions.validators.ArgumentValidator;
import razepl.dev.socialappbackend.entities.user.User;

import java.util.List;
Expand All @@ -33,7 +33,7 @@ public final AuthResponse buildTokensIntoResponse(String authToken, String refre

@Override
public final AuthResponse buildTokensIntoResponse(User user, boolean shouldIRevoke) {
NullChecker.throwAppropriateException(user);
ArgumentValidator.throwIfNull(user);

String authToken = jwtService.generateToken(user);
String refreshToken = jwtService.generateRefreshToken(user);
Expand All @@ -48,7 +48,7 @@ public final AuthResponse buildTokensIntoResponse(User user, boolean shouldIRevo

@Override
public final void revokeUserTokens(User user) {
NullChecker.throwAppropriateException(user);
ArgumentValidator.throwIfNull(user);

List<JwtToken> userTokens = tokenRepository.findAllValidTokensByUserId(user.getUserId());

Expand All @@ -64,7 +64,7 @@ public final void revokeUserTokens(User user) {
}

private AuthResponse buildResponse(String authToken, String refreshToken) {
NullChecker.throwAppropriateException(authToken, refreshToken);
ArgumentValidator.throwIfNull(authToken, refreshToken);

return AuthResponse.builder()
.authToken(authToken)
Expand All @@ -73,7 +73,7 @@ private AuthResponse buildResponse(String authToken, String refreshToken) {
}

private JwtToken buildToken(String jwtToken, User user) {
NullChecker.throwAppropriateException(jwtToken, user);
ArgumentValidator.throwIfNull(jwtToken, user);

return JwtToken.builder()
.token(jwtToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package razepl.dev.socialappbackend.exceptions;

public class NegativeIdException extends IllegalArgumentException {
public NegativeIdException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package razepl.dev.socialappbackend.exceptions.validators;

import razepl.dev.socialappbackend.exceptions.NegativeIdException;
import razepl.dev.socialappbackend.exceptions.NullArgumentException;

import java.util.Arrays;
import java.util.Objects;

public class ArgumentValidator {
private ArgumentValidator() {
}

public static void throwIfNegativeId(long... ids) {
if (!ArgumentValidator.areIdPositive(ids)) {
throw new NegativeIdException("There are negative id's!");
}
}

public static void throwIfNegativeId(int... ids) {
if (!ArgumentValidator.areIdPositive(ids)) {
throw new NegativeIdException("There are negative id's!");
}
}

public static void throwIfNull(Object... objects) {
if (!ArgumentValidator.areArgumentsNullSafe(objects)) {
throw new NullArgumentException("Encountered null arguments in method!");
}
}

private static boolean areIdPositive(long... ids) {
return Arrays.stream(ids).noneMatch(id -> id < 0L);
}

private static boolean areIdPositive(int... ids) {
return Arrays.stream(ids).noneMatch(id -> id < 0);
}

private static boolean areArgumentsNullSafe(Object... objects) {
return Arrays.stream(objects).noneMatch(Objects::isNull);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import razepl.dev.socialappbackend.entities.user.User;
import razepl.dev.socialappbackend.exceptions.validators.NullChecker;
import razepl.dev.socialappbackend.home.data.*;
import razepl.dev.socialappbackend.home.interfaces.HomeInterface;
import razepl.dev.socialappbackend.home.interfaces.HomeServiceInterface;
Expand All @@ -30,8 +29,6 @@ public class HomeController implements HomeInterface {
@Override
@GetMapping(value = USERDATA_MAPPING)
public final ResponseEntity<UserData> getUserData(@AuthenticationPrincipal User user) {
NullChecker.throwAppropriateException(user);

log.info("Getting userdata for user : {}", user);

return ResponseEntity.ok(homeService.buildUserDataFromDb(user));
Expand All @@ -40,8 +37,6 @@ public final ResponseEntity<UserData> getUserData(@AuthenticationPrincipal User
@Override
@GetMapping(value = FRIENDS_LIST_MAPPING)
public final ResponseEntity<List<FriendData>> getFriendsList(@AuthenticationPrincipal User user) {
NullChecker.throwAppropriateException(user);

log.info("Finding list of users for : {}", user);

return ResponseEntity.ok(homeService.buildUsersFriendList(user));
Expand All @@ -60,8 +55,6 @@ public final ResponseEntity<List<PostData>> getPostsList(@RequestParam int numOf
@Override
@PostMapping(value = CREATE_POST_MAPPING)
public final ResponseEntity<PostData> createPost(@RequestParam String postContent, User user) {
NullChecker.throwAppropriateException(postContent);

log.info("Creating post with data : {}", postContent);
log.info("User who wants to create post : {}", user);

Expand Down
Loading

0 comments on commit 2fd2b9c

Please sign in to comment.