Skip to content

Commit

Permalink
Merge pull request #89 from nastiausenko/link-controller-integration-…
Browse files Browse the repository at this point in the history
…test

Link controller integration test
  • Loading branch information
egorsivenko authored Apr 24, 2024
2 parents 7747499 + 6ca1e2c commit bb05e92
Show file tree
Hide file tree
Showing 58 changed files with 138 additions and 56 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Environment Variables

The application uses the following environment variables for configuration:

- `POSTGRES_HOST`: Specifies the host address of the PostgreSQL database server.
- `POSTGRES_PORT`: Specifies the port number of the PostgreSQL database server.
- `POSTGRES_DB`: Specifies the name of the PostgreSQL database.
- `POSTGRES_USER`: Specifies the username for connecting to the PostgreSQL database.
- `POSTGRES_PASSWORD`: Specifies the password for connecting to the PostgreSQL database.
- `REDIS_HOST`: Specifies the host address of the Redis server.
- `REDIS_PORT`: Specifies the port number of the Redis server.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.linkurlshorter.urlshortener.exception;

import com.linkurlshorter.urlshortener.auth.exception.EmailAlreadyTakenException;
import com.linkurlshorter.urlshortener.link.*;
import com.linkurlshorter.urlshortener.user.NoSuchEmailFoundException;
import com.linkurlshorter.urlshortener.user.NoUserFoundByEmailException;
import com.linkurlshorter.urlshortener.user.NoUserFoundByIdException;
import com.linkurlshorter.urlshortener.link.exception.*;
import com.linkurlshorter.urlshortener.user.exception.NoSuchEmailFoundException;
import com.linkurlshorter.urlshortener.user.exception.NoUserFoundByEmailException;
import com.linkurlshorter.urlshortener.user.exception.NoUserFoundByIdException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package com.linkurlshorter.urlshortener.link;

import com.linkurlshorter.urlshortener.link.dto.LinkInfoDto;
import com.linkurlshorter.urlshortener.link.dto.LinkInfoDtoMapper;
import com.linkurlshorter.urlshortener.link.dto.LinkStatisticsDto;
import com.linkurlshorter.urlshortener.link.exception.DeletedLinkException;
import com.linkurlshorter.urlshortener.link.exception.ForbiddenException;
import com.linkurlshorter.urlshortener.link.exception.InternalServerLinkException;
import com.linkurlshorter.urlshortener.link.exception.LinkStatusException;
import com.linkurlshorter.urlshortener.link.request.CreateLinkRequest;
import com.linkurlshorter.urlshortener.link.request.EditLinkContentRequest;
import com.linkurlshorter.urlshortener.link.response.CreateLinkResponse;
import com.linkurlshorter.urlshortener.link.response.LinkInfoResponse;
import com.linkurlshorter.urlshortener.link.response.LinkModifyingResponse;
import com.linkurlshorter.urlshortener.link.response.LinkStatisticsResponse;
import com.linkurlshorter.urlshortener.user.User;
import com.linkurlshorter.urlshortener.user.UserService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkurlshorter.urlshortener.link;

import com.linkurlshorter.urlshortener.link.dto.LinkStatisticsDto;
import com.linkurlshorter.urlshortener.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -35,7 +36,7 @@ public interface LinkRepository extends JpaRepository<Link, UUID> {

List<Link> findAllByUser(User user);

@Query("SELECT new com.linkurlshorter.urlshortener.link.LinkStatisticsDto(l.id, l.shortLink, l.statistics)" +
@Query("SELECT new com.linkurlshorter.urlshortener.link.dto.LinkStatisticsDto(l.id, l.shortLink, l.statistics)" +
" FROM Link l WHERE l.user.id = :userId AND l.status <> 'DELETED'")
List<LinkStatisticsDto> getLinkUsageStatsForUser(@Param(value = "userId") UUID userId);
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.linkurlshorter.urlshortener.link;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkurlshorter.urlshortener.link.dto.LinkStatisticsDto;
import com.linkurlshorter.urlshortener.link.exception.DeletedLinkException;
import com.linkurlshorter.urlshortener.link.exception.InactiveLinkException;
import com.linkurlshorter.urlshortener.link.exception.NoLinkFoundByShortLinkException;
import com.linkurlshorter.urlshortener.link.exception.NullLinkPropertyException;
import com.linkurlshorter.urlshortener.link.validation.EndTimeLinkValidator;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -44,10 +50,15 @@ public String getLongLinkFromShortLink(String shortLink) {
Link link = jedis.exists(shortLink)
? mapper.readValue(jedis.get(shortLink), Link.class)
: findByShortLink(shortLink);

if (link.getStatus() == LinkStatus.INACTIVE) {
throw new InactiveLinkException(shortLink);
}
if (link.getExpirationTime().isBefore(LocalDateTime.now())) {
link.setStatus(LinkStatus.INACTIVE);
jedis.set(link.getShortLink(), mapper.writeValueAsString(link));
save(link);
throw new InactiveLinkException(shortLink);
}
updateLinkStatsAndSave(link, jedis);
return link.getLongLink();
}
Expand All @@ -61,7 +72,7 @@ public String getLongLinkFromShortLink(String shortLink) {
* @param link the link to be updated
*/
@SneakyThrows
private void updateLinkStatsAndSave(Link link, Jedis jedis) {
private void updateLinkStatsAndSave(@EndTimeLinkValidator Link link, Jedis jedis) {
link.setStatistics(link.getStatistics() + 1);
link.setExpirationTime(LocalDateTime.now().plusMonths(1));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkurlshorter.urlshortener.link;

import com.linkurlshorter.urlshortener.link.exception.InternalServerLinkException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.dto;

import com.linkurlshorter.urlshortener.link.LinkStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.dto;

import com.linkurlshorter.urlshortener.link.Link;
import com.linkurlshorter.urlshortener.link.dto.LinkInfoDto;
import org.springframework.stereotype.Component;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.exception;

/**
* Exception thrown when attempting to operate with a link marked as deleted
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.exception;

public class ForbiddenException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.exception;

public class InactiveLinkException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.exception;

/**
* Indicating unexpected exceptions which do not depend on user
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.exception;

/**
* Exception thrown when the status of a link is invalid for a particular operation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.exception;

/**
* Exception thrown when no link is found by the provided ID.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.exception;

/**
* Exception thrown when no link is found by the provided shortLink.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.exception;

/**
* This exception class represents a situation where a required property or link entity is null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.request;

import com.linkurlshorter.urlshortener.link.validation.UrlLongFormatValidator;
import com.linkurlshorter.urlshortener.link.validation.UrlNewShortValidator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.request;

import com.linkurlshorter.urlshortener.link.validation.UrlNewShortValidator;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;

/**
* Represents a request to edit the content of a link.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.response;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.response;

import com.linkurlshorter.urlshortener.link.dto.LinkInfoDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.response;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.response;

import com.linkurlshorter.urlshortener.link.dto.LinkStatisticsDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;


import jakarta.validation.Constraint;
Expand All @@ -15,7 +15,7 @@
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Target({ElementType.FIELD, ElementType.METHOD,ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Constraint(validatedBy = UrlShortValidatorImpl.class)
public @interface EndTimeLinkValidator {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import com.linkurlshorter.urlshortener.link.validation.EndTimeLinkValidator;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import com.linkurlshorter.urlshortener.link.validation.UrlLongFormatValidator;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import okhttp3.OkHttpClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import com.linkurlshorter.urlshortener.link.LinkService;
import com.linkurlshorter.urlshortener.link.validation.UrlNewShortValidator;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

/**
* The UrlShortValidatorImpl class implements the {@link ConstraintValidator} interface for annotating an UrlShortValidator.
* Used to validate short links to ensure that they are unique and their length does not exceed the set maximum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import com.linkurlshorter.urlshortener.link.LinkService;
import com.linkurlshorter.urlshortener.link.validation.UrlShortFormatValidator;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.linkurlshorter.urlshortener.link;
package com.linkurlshorter.urlshortener.link.validation;

import com.linkurlshorter.urlshortener.link.Link;
import com.linkurlshorter.urlshortener.link.LinkService;
import com.linkurlshorter.urlshortener.link.LinkStatus;
import com.linkurlshorter.urlshortener.link.validation.UrlShortValidator;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.linkurlshorter.urlshortener.redirect;

import com.linkurlshorter.urlshortener.link.LinkService;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class LinkRedirectController {
*/
@SneakyThrows
@GetMapping("/{shortLink}")
@ApiResponse(responseCode = "302", description = "Redirect to original link")
public RedirectView redirectToOriginalLink(@PathVariable @NotBlank String shortLink) {
String longLink = linkService.getLongLinkFromShortLink(shortLink);
return buildRedirectView(longLink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Email;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.linkurlshorter.urlshortener.auth.exception.EmailAlreadyTakenException;
import com.linkurlshorter.urlshortener.jwt.JwtUtil;
import com.linkurlshorter.urlshortener.security.CustomUserDetailsService;
import com.linkurlshorter.urlshortener.user.exception.NoSuchEmailFoundException;
import com.linkurlshorter.urlshortener.user.request.ChangeUserEmailRequest;
import com.linkurlshorter.urlshortener.user.request.ChangeUserPasswordRequest;
import com.linkurlshorter.urlshortener.user.responce.UserModifyingResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.linkurlshorter.urlshortener.user;

import com.linkurlshorter.urlshortener.user.exception.NoUserFoundByEmailException;
import com.linkurlshorter.urlshortener.user.exception.NoUserFoundByIdException;
import com.linkurlshorter.urlshortener.user.exception.NullEmailException;
import com.linkurlshorter.urlshortener.user.exception.NullUserPropertyException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.user;
package com.linkurlshorter.urlshortener.user.exception;

/**
* This exception is thrown when no matching email is found in the database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.user;
package com.linkurlshorter.urlshortener.user.exception;

/**
* Exception indicating that no user was found based on the provided email.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.user;
package com.linkurlshorter.urlshortener.user.exception;

/**
* Exception indicating that no user was found based on the provided ID.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package com.linkurlshorter.urlshortener.user;
package com.linkurlshorter.urlshortener.user.exception;

/**
* This exception is thrown when a null email is provided, indicating that the request cannot be processed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkurlshorter.urlshortener.user;
package com.linkurlshorter.urlshortener.user.exception;

/**
* Exception thrown when a user entity or a required user property is found to be null,
Expand Down
Loading

0 comments on commit bb05e92

Please sign in to comment.