diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/boot/domain/Employee.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/boot/domain/Employee.java new file mode 100644 index 0000000000..c96aca9921 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/boot/domain/Employee.java @@ -0,0 +1,50 @@ +package com.baeldung.boot.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author harshavs + * @since 2019-08-01 + */ +@Entity +@Table(name = "employees") +public class Employee { + + @Id + @GeneratedValue + private int id; + private String name; + private String title; + + public Employee() { + + } + + public Employee(String name, String title) { + this.name = name; + this.title = title; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getId() { + return id; + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/boot/repository/EmployeeRepository.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/boot/repository/EmployeeRepository.java new file mode 100644 index 0000000000..e5b0be6c97 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/boot/repository/EmployeeRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.boot.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.boot.domain.Employee; + +/** + * @author harshavs + * @since 2019-08-01 + */ +@Repository +public interface EmployeeRepository extends JpaRepository { + +} diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootInitialLoadTest.java b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootInitialLoadTest.java new file mode 100644 index 0000000000..105d18166c --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootInitialLoadTest.java @@ -0,0 +1,36 @@ +package com.baeldung.springbootinitialload.tests; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlConfig; +import org.springframework.test.context.jdbc.SqlConfig.TransactionMode; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.boot.Application; +import com.baeldung.boot.repository.EmployeeRepository; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +@Sql({ "/employees_schema.sql", "/import_employees.sql" }) +public class SpringBootInitialLoadTest { + + @Autowired + private EmployeeRepository employeeRepository; + + @Test + public void testLoadDataForTestClass() { + assertEquals(employeeRepository.findAll().size(), 3); + } + + @Test + @Sql(scripts = {"/import_senior_employees.sql" } + , config = @SqlConfig(encoding = "utf-8", transactionMode = TransactionMode.ISOLATED)) + public void testLoadDataForTestCase() { + assertEquals(employeeRepository.findAll().size(), 5); + } +} diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootSqlGroupAnnotationTest.java b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootSqlGroupAnnotationTest.java new file mode 100644 index 0000000000..0f9724489f --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springbootinitialload/tests/SpringBootSqlGroupAnnotationTest.java @@ -0,0 +1,35 @@ +package com.baeldung.springbootinitialload.tests; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.Sql.ExecutionPhase; +import org.springframework.test.context.jdbc.SqlConfig; +import org.springframework.test.context.jdbc.SqlConfig.TransactionMode; +import org.springframework.test.context.jdbc.SqlGroup; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.boot.Application; +import com.baeldung.boot.repository.EmployeeRepository; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +@SqlGroup({ + @Sql(scripts = "/employees_schema_with_comments.sql", config = @SqlConfig(encoding = "utf-8", commentPrefix = "\\", transactionMode = TransactionMode.ISOLATED)), + @Sql("/import_employees.sql"), + @Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts = { "/drop_employees.sql" }) }) +public class SpringBootSqlGroupAnnotationTest { + + @Autowired + private EmployeeRepository employeeRepository; + + @Test + public void testLoadDataForTestCase() { + assertEquals(employeeRepository.findAll().size(), 3); + } +} diff --git a/persistence-modules/spring-boot-persistence/src/test/resources/drop_employees.sql b/persistence-modules/spring-boot-persistence/src/test/resources/drop_employees.sql new file mode 100644 index 0000000000..eead688647 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/resources/drop_employees.sql @@ -0,0 +1 @@ +drop table if exists EMPLOYEES; \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence/src/test/resources/employees_schema.sql b/persistence-modules/spring-boot-persistence/src/test/resources/employees_schema.sql new file mode 100644 index 0000000000..7b9e64190e --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/resources/employees_schema.sql @@ -0,0 +1,8 @@ +drop table EMPLOYEES if exists; + +create table EMPLOYEES( + ID int not null AUTO_INCREMENT, + NAME varchar(100) not null, + TITLE varchar(100), + PRIMARY KEY ( ID ) +); diff --git a/persistence-modules/spring-boot-persistence/src/test/resources/employees_schema_with_comments.sql b/persistence-modules/spring-boot-persistence/src/test/resources/employees_schema_with_comments.sql new file mode 100644 index 0000000000..25805eba84 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/resources/employees_schema_with_comments.sql @@ -0,0 +1,10 @@ +--Drop the table +drop table EMPLOYEES if exists; + +--Create the table +create table EMPLOYEES( + ID int not null AUTO_INCREMENT, + NAME varchar(100) not null, + TITLE varchar(100), + PRIMARY KEY ( ID ) +); diff --git a/persistence-modules/spring-boot-persistence/src/test/resources/import_employees.sql b/persistence-modules/spring-boot-persistence/src/test/resources/import_employees.sql new file mode 100644 index 0000000000..40442d2804 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/resources/import_employees.sql @@ -0,0 +1,3 @@ +insert into EMPLOYEES values(1, 'Harsha', 'Developer'); +insert into EMPLOYEES values(2, 'John', 'Tester'); +insert into EMPLOYEES values(3, 'Ram', 'Manager'); \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence/src/test/resources/import_senior_employees.sql b/persistence-modules/spring-boot-persistence/src/test/resources/import_senior_employees.sql new file mode 100644 index 0000000000..3d0819175b --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/test/resources/import_senior_employees.sql @@ -0,0 +1,2 @@ +insert into EMPLOYEES values(4, 'Eric', 'Senior Developer'); +insert into EMPLOYEES values(5, 'Vidhyaah', 'Senior Manager'); \ No newline at end of file