Skip to content

Commit

Permalink
Added tests for a Book functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
TarasYashchuk committed Aug 7, 2024
1 parent fb12b07 commit e414a55
Show file tree
Hide file tree
Showing 15 changed files with 1,291 additions and 18 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>

<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>2.3.1</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>6.2.2</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/mate/academy/controller/CategoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -24,6 +25,7 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand Down Expand Up @@ -110,6 +112,7 @@ public CategoryDto updateCategory(@PathVariable Long id,
})
@PreAuthorize("hasRole('ADMIN')")
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteCategory(@PathVariable Long id) {
categoryService.deleteById(id);
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/mate/academy/dto/book/BookSearchParametersDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package mate.academy.dto.book;

public record BookSearchParametersDto(
String title,
String author,
String isbn,
String price) {
import lombok.Data;

@Data
public class BookSearchParametersDto {
private String title;
private String author;
private String isbn;
private String price;
}
4 changes: 2 additions & 2 deletions src/main/java/mate/academy/dto/book/CreateBookRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
@Data
public class CreateBookRequestDto {
@NotBlank(message = "Title cannot be blank")
@Size(max = 20, message = "Title length must not exceed {max} characters")
@Size(max = 50, message = "Title length must not exceed {max} characters")
private String title;

@NotBlank(message = "Author cannot be blank")
@Size(max = 30, message = "Author length must not exceed {max} characters")
@Size(max = 50, message = "Author length must not exceed {max} characters")
private String author;

@Isbn
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mate/academy/dto/book/UpdateBookRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
@Data
public class UpdateBookRequestDto {
@NotBlank(message = "Title cannot be blank")
@Size(max = 20, message = "Title length must not exceed {max} characters")
@Size(max = 50, message = "Title length must not exceed {max} characters")
private String title;

@NotBlank(message = "Author cannot be blank")
@Size(max = 30, message = "Author length must not exceed {max} characters")
@Size(max = 50, message = "Author length must not exceed {max} characters")
private String author;

@Isbn
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/mate/academy/dto/category/CategoryDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package mate.academy.dto.category;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;

public record CategoryDto(@NotBlank(message = "name cannot be blank") String name,
@NotBlank(message = "description cannot be blank")
String description) {
@Data
public class CategoryDto {
@NotBlank(message = "name cannot be blank")
private String name;

@NotBlank(message = "description cannot be blank")
private String description;
}

Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public ResponseEntity<Object> handleRegistrationException(RegistrationException
body.put(ERRORS, List.of(ex.getMessage()));
return new ResponseEntity<>(body, HttpStatus.CONFLICT);
}

@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<Object> handleEntityNotFoundException(EntityNotFoundException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put(TIME_STAMP, LocalDateTime.now());
body.put(STATUS, HttpStatus.NOT_FOUND.value());
body.put(ERRORS, List.of(ex.getMessage()));
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public Specification<Book> build(BookSearchParametersDto searchParameters) {
Specification<Book> specification = Specification.where(null);

specification = addSpecification(specification,
BookSpecificationKeys.TITLE, searchParameters.title());
BookSpecificationKeys.TITLE, searchParameters.getTitle());
specification = addSpecification(specification,
BookSpecificationKeys.PRICE, searchParameters.price());
BookSpecificationKeys.PRICE, searchParameters.getPrice());
specification = addSpecification(specification,
BookSpecificationKeys.ISBN, searchParameters.isbn());
BookSpecificationKeys.ISBN, searchParameters.getIsbn());
specification = addSpecification(specification,
BookSpecificationKeys.AUTHOR, searchParameters.author());
BookSpecificationKeys.AUTHOR, searchParameters.getAuthor());

return specification;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import mate.academy.model.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public CategoryDto save(CategoryDto categoryDto) {
public CategoryDto update(Long id, CategoryDto categoryDto) {
Category category = categoryRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Category not found"));
category.setName(categoryDto.name());
category.setDescription(categoryDto.description());
category.setName(categoryDto.getName());
category.setDescription(categoryDto.getDescription());
return categoryMapper.toDto(categoryRepository.save(category));
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/mate/academy/constans/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mate.academy.constans;

public class Constants {
public static final String USER = "user";
public static final String ADMIN = "admin";
public static final String ADMIN_ROLE = "ADMIN";
public static final String BASE_CATEGORY_URL = "/categories";
public static final String CATEGORY_BY_ID_URL = BASE_CATEGORY_URL + "/{id}";
public static final String CATEGORY_BOOKS_URL = CATEGORY_BY_ID_URL + "/books";
public static final String BASE_BOOK_URL = "/books";
public static final String BOOK_BY_ID_URL = BASE_BOOK_URL + "/{id}";
public static final String BOOK_SEARCH_URL = BASE_BOOK_URL + "/search";
}
Loading

0 comments on commit e414a55

Please sign in to comment.