Skip to content

Commit

Permalink
Merge pull request #14 from AntonBabychP1T/test-impementation
Browse files Browse the repository at this point in the history
Add test to BookService, CategoryService, BookController and Category…
  • Loading branch information
AntonBabychP1T authored Dec 25, 2023
2 parents 1298bb9 + 8b96bf5 commit dc0e346
Show file tree
Hide file tree
Showing 24 changed files with 1,109 additions and 26 deletions.
40 changes: 34 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<java.version>17</java.version>
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
<jjwt.version>0.11.5</jjwt.version>
<testcontainers.version>1.19.3</testcontainers.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -31,7 +32,7 @@
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand All @@ -53,11 +54,6 @@
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
Expand Down Expand Up @@ -104,7 +100,39 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void delete(@PathVariable Long id) {

@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Create a new product", description = "Create a new product")
public BookDto createBook(@RequestBody @Valid CreateBookRequestDto requestDto) {
return bookService.save(requestDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class CategoryController {

@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Create a new category", description = "Create a new category")
@Parameter(name = "name", description = "name of the new category",
required = true, example = "New name")
Expand Down Expand Up @@ -66,12 +67,15 @@ public void deleteCategory(@PathVariable Long id) {
}

@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping
@PutMapping("/{id}")
@Operation(
summary = "Update category by id",
description = "Update specific category by id key"
)
public CategoryDto updateCategory(Long id, CategoryRequestDto requestDto) {
public CategoryDto updateCategory(
@PathVariable Long id,
@RequestBody CategoryRequestDto requestDto
) {
return categoryService.update(id, requestDto);
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/store/bookstoreapp/dto/book/BookDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import java.math.BigDecimal;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.Data;

@Getter
@Setter
@Data
public class BookDto {
private Long id;
private String title;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package store.bookstoreapp.dto.book;

import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;
import lombok.Data;

@Getter
@Setter
@Data
public class BookDtoWithoutCategoryIds {
private Long id;
private String title;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/store/bookstoreapp/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

@Getter
@Setter
@Data
@SQLDelete(sql = "UPDATE books SET is_deleted = true WHERE id=?")
@Where(clause = "is_deleted=false")
@Entity
Expand All @@ -40,6 +39,7 @@ public class Book {
@Column(nullable = false)
private boolean isDeleted = false;
@ManyToMany
@EqualsAndHashCode.Exclude
@JoinTable(
name = "books_categories",
joinColumns = @JoinColumn(name = "book_id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ databaseChangeLog:
unique: true
- column:
name: price
type: decimal
type: decimal(10, 2)
constraints:
nullable: false
- column:
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/store/bookstoreapp/config/CustomMySqlContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package store.bookstoreapp.config;

import org.testcontainers.containers.MySQLContainer;

public class CustomMySqlContainer extends MySQLContainer<CustomMySqlContainer> {
private static final String DB_IMAGE = "mysql:8";
private static CustomMySqlContainer mySqlContainer;

private CustomMySqlContainer() {
super(DB_IMAGE);
}

public static synchronized CustomMySqlContainer getInstance() {
if (mySqlContainer == null) {
mySqlContainer = new CustomMySqlContainer();
}
return mySqlContainer;
}

@Override
public void start() {
super.start();
System.setProperty("TEST_DB_URL", mySqlContainer.getJdbcUrl());
System.setProperty("TEST_DB_USERNAME", mySqlContainer.getUsername());
System.setProperty("TEST_DB_PASSWORD", mySqlContainer.getPassword());
}

@Override
public void stop() {

}
}
Loading

0 comments on commit dc0e346

Please sign in to comment.