diff options
| author | realtradam <[email protected]> | 2024-07-18 21:45:42 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2024-07-18 21:45:42 -0400 |
| commit | 627100d9d5f03eee6ba2b4ec55bebd8367d5d641 (patch) | |
| tree | e69ecf7ed1cfbfbfe6cda7bcd4bf409bbf8169a9 | |
| parent | 2a43a36f448980b5f15f73dbcfab850441e69fdf (diff) | |
| download | spring-blog-627100d9d5f03eee6ba2b4ec55bebd8367d5d641.tar.gz spring-blog-627100d9d5f03eee6ba2b4ec55bebd8367d5d641.zip | |
add test for articlerepository
| -rw-r--r-- | pom.xml | 18 | ||||
| -rw-r--r-- | src/main/java/com/blog/web/dto/RegistrationDto.java | 13 | ||||
| -rw-r--r-- | src/main/java/com/blog/web/repository/ArticleRepository.java | 2 | ||||
| -rw-r--r-- | src/test/java/com/blog/web/repository/ArticleRepositoryTests.java | 89 |
4 files changed, 119 insertions, 3 deletions
@@ -78,7 +78,23 @@ <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> - </dependency> + </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <version>2.3.230</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework.security</groupId> + <artifactId>spring-security-test</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/src/main/java/com/blog/web/dto/RegistrationDto.java b/src/main/java/com/blog/web/dto/RegistrationDto.java index ca9fed5..fcf5f90 100644 --- a/src/main/java/com/blog/web/dto/RegistrationDto.java +++ b/src/main/java/com/blog/web/dto/RegistrationDto.java @@ -1,5 +1,6 @@ package com.blog.web.dto; +import com.blog.web.models.UserEntity; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotEmpty; @@ -12,6 +13,18 @@ public class RegistrationDto { @NotEmpty private String password; + public RegistrationDto() {}; + + public RegistrationDto( + String username, + String email, + String password + ) { + this.username = username; + this.email = email; + this.password = password; + } + public Long getId() { return id; } diff --git a/src/main/java/com/blog/web/repository/ArticleRepository.java b/src/main/java/com/blog/web/repository/ArticleRepository.java index db9edf6..594cb15 100644 --- a/src/main/java/com/blog/web/repository/ArticleRepository.java +++ b/src/main/java/com/blog/web/repository/ArticleRepository.java @@ -5,10 +5,8 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import java.util.List; -import java.util.Optional; public interface ArticleRepository extends JpaRepository<Article, Long> { - Optional<Article> findByTitle(String url); @Query("SELECT a from Article a WHERE a.title LIKE CONCAT('%', :search, '%')") List<Article> searchArticles(String search); } diff --git a/src/test/java/com/blog/web/repository/ArticleRepositoryTests.java b/src/test/java/com/blog/web/repository/ArticleRepositoryTests.java new file mode 100644 index 0000000..31764e3 --- /dev/null +++ b/src/test/java/com/blog/web/repository/ArticleRepositoryTests.java @@ -0,0 +1,89 @@ + +package com.blog.web.repository; + +import com.blog.web.dto.ArticleDto; +import com.blog.web.dto.RegistrationDto; +import com.blog.web.models.Article; +import com.blog.web.models.UserEntity; +import com.blog.web.services.ArticleService; +import com.blog.web.services.UserService; +import org.junit.Assert; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.jdbc.EmbeddedDatabaseConnection; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Lazy; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.context.junit4.SpringRunner; + +import java.time.LocalDateTime; +import java.util.List; + +@SpringBootTest +@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2) +public class ArticleRepositoryTests { + + @Autowired + private ArticleRepository articleRepository; + @Autowired + private UserService userService; + @Autowired + private ArticleService articleService; + + + @Test + @WithMockUser(username="test", password="blah") + public void ArticleRepository_Search_ReturnSearch() { + // Arrange + RegistrationDto userDto = new RegistrationDto( + "test", + "[email protected]", + "blah" + ); + userService.saveUser(userDto); + UserEntity user = userService.findByUsername("test"); + final ArticleDto articleDto1 = new ArticleDto( + 1, + "Title", + "https://duckduckgo.com", + "Content", + user, + LocalDateTime.now(), + LocalDateTime.now() + ); + articleService.saveArticle(articleDto1); + final ArticleDto articleDto2 = new ArticleDto( + 2, + "itleblah", + "https://duckduckgo.com", + "Content", + user, + LocalDateTime.now(), + LocalDateTime.now() + ); + articleService.saveArticle(articleDto2); + final ArticleDto articleDto3 = new ArticleDto( + 3, + "dontfindme", + "https://duckduckgo.com", + "Content", + user, + LocalDateTime.now(), + LocalDateTime.now() + ); + articleService.saveArticle(articleDto3); + + // Act + List<Article> list = articleRepository.searchArticles("itle"); // partial string should find 2 articles + + // Assert + Assertions.assertEquals(2, list.size()); + } +} |
