* [BAEL-1981] Article entity and repository + tests

* [BAEL-1981] Removing unnecessary fields

* [BAEL-1981] spring-data-jpa module creation
This commit is contained in:
François Dupire
2018-07-23 06:49:25 +02:00
committed by maibin
parent ecb14dd834
commit 0be7aa087f
8 changed files with 57 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-jpa</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,14 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,23 @@
package com.baeldung.domain;
import javax.persistence.*;
import java.util.Date;
@Entity
public class Article {
@Id
@GeneratedValue
private Integer id;
@Temporal(TemporalType.DATE)
private Date publicationDate;
@Temporal(TemporalType.TIME)
private Date publicationTime;
@Temporal(TemporalType.TIMESTAMP)
private Date creationDateTime;
public Integer getId() {
return id;
}
}
@@ -0,0 +1,22 @@
package com.baeldung.repository;
import com.baeldung.domain.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Date;
import java.util.List;
public interface ArticleRepository extends JpaRepository<Article, Integer> {
List<Article> findAllByPublicationDate(Date publicationDate);
List<Article> findAllByPublicationTimeBetween(Date publicationTimeStart,
Date publicationTimeEnd);
@Query("select a from Article a where a.creationDateTime <= :creationDateTime")
List<Article> findAllWithCreationDateTimeBefore(
@Param("creationDateTime") Date creationDateTime);
}
@@ -0,0 +1,67 @@
package com.baeldung.repository;
import com.baeldung.domain.Article;
import com.baeldung.repository.ArticleRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@DataJpaTest
public class ArticleRepositoryIntegrationTest {
@Autowired
private ArticleRepository repository;
@Test
public void givenImportedArticlesWhenFindAllByPublicationDateThenArticles1And2Returned()
throws Exception {
List<Article> result = repository.findAllByPublicationDate(
new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-01")
);
assertEquals(2, result.size());
assertTrue(result.stream()
.map(Article::getId)
.allMatch(id -> Arrays.asList(1, 2).contains(id))
);
}
@Test
public void givenImportedArticlesWhenFindAllByPublicationTimeBetweenThenArticles2And3Returned()
throws Exception {
List<Article> result = repository.findAllByPublicationTimeBetween(
new SimpleDateFormat("HH:mm").parse("15:15"),
new SimpleDateFormat("HH:mm").parse("16:30")
);
assertEquals(2, result.size());
assertTrue(result.stream()
.map(Article::getId)
.allMatch(id -> Arrays.asList(2, 3).contains(id))
);
}
@Test
public void givenImportedArticlesWhenFindAllWithCreationDateTimeBeforeThenArticles2And3Returned() throws Exception {
List<Article> result = repository.findAllWithCreationDateTimeBefore(
new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-12-15 10:00")
);
assertEquals(2, result.size());
assertTrue(result.stream()
.map(Article::getId)
.allMatch(id -> Arrays.asList(2, 3).contains(id))
);
}
}
@@ -0,0 +1,15 @@
# spring.datasource.x
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
# hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.datasource.data=import_articles.sql
@@ -0,0 +1,3 @@
insert into Article(id, publication_date, publication_time, creation_date_time) values(1, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:00', 'HH24:MI'), TO_DATE('31/12/2017 07:30', 'DD/MM/YYYY HH24:MI'));
insert into Article(id, publication_date, publication_time, creation_date_time) values(2, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:30', 'HH24:MI'), TO_DATE('15/12/2017 08:00', 'DD/MM/YYYY HH24:MI'));
insert into Article(id, publication_date, publication_time, creation_date_time) values(3, TO_DATE('15/12/2017', 'DD/MM/YYYY'), TO_DATE('16:00', 'HH24:MI'), TO_DATE('01/12/2017 13:45', 'DD/MM/YYYY HH24:MI'));