From 9e22d43bab859baab9fbaa7f81845a85c0fc56fb Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Tue, 16 Nov 2021 20:44:48 +0000 Subject: [PATCH] [JAVA-6455] Update H2 code with latest Spring Boot changes --- .../springboot/SpringBootH2Application.java | 2 + .../springboot/daos/CountryRepository.java | 7 +++ .../h2db/springboot/daos/UserRepository.java | 10 ---- .../h2db/springboot/models/Country.java | 21 ++++++++ .../baeldung/h2db/springboot/models/User.java | 54 ------------------- .../main/resources/application-h2.properties | 1 + .../src/main/resources/data-h2.sql | 5 ++ .../src/main/resources/data-trans.sql | 14 ----- .../baeldung/SpringBootH2IntegrationTest.java | 36 +++---------- 9 files changed, 43 insertions(+), 107 deletions(-) create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/CountryRepository.java delete mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/UserRepository.java create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Country.java delete mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/User.java create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/resources/application-h2.properties create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/resources/data-h2.sql diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/SpringBootH2Application.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/SpringBootH2Application.java index 378093cfa9..d6d7a20f55 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/SpringBootH2Application.java +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/SpringBootH2Application.java @@ -2,8 +2,10 @@ package com.baeldung.h2db.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.PropertySource; @SpringBootApplication +@PropertySource("classpath:application-h2.properties") public class SpringBootH2Application { public static void main(String... args) { diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/CountryRepository.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/CountryRepository.java new file mode 100644 index 0000000000..d576be9098 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/CountryRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.h2db.springboot.daos; + +import com.baeldung.h2db.springboot.models.Country; +import org.springframework.data.repository.CrudRepository; + +public interface CountryRepository extends CrudRepository { +} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/UserRepository.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/UserRepository.java deleted file mode 100644 index 35e496e910..0000000000 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/UserRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.h2db.springboot.daos; - - - - -import com.baeldung.h2db.springboot.models.User; -import org.springframework.data.repository.CrudRepository; - -public interface UserRepository extends CrudRepository { -} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Country.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Country.java new file mode 100644 index 0000000000..f0f02c41d3 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Country.java @@ -0,0 +1,21 @@ +package com.baeldung.h2db.springboot.models; + +import lombok.Data; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +@Table(name = "countries") +@Entity +@Data +public class Country { + + @Id + @GeneratedValue + private int id; + + private String name; + +} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/User.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/User.java deleted file mode 100644 index fa3c01c035..0000000000 --- a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/User.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung.h2db.springboot.models; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; - -@Table(name = "users") -@Entity -public class User { - - @Id - @GeneratedValue - private int id; - - private String firstName; - - private String lastName; - - public User() { } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - @Override - public String toString() { - return "User{" + - "id=" + id + - ", firstName='" + firstName + '\'' + - ", lastName='" + lastName + '\'' + - '}'; - } -} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-h2.properties b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-h2.properties new file mode 100644 index 0000000000..6fb436f520 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application-h2.properties @@ -0,0 +1 @@ +spring.sql.init.data-locations=data-h2.sql diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-h2.sql b/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-h2.sql new file mode 100644 index 0000000000..3d04b578d2 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-h2.sql @@ -0,0 +1,5 @@ +INSERT INTO countries (id, name) VALUES (1, 'USA'); +INSERT INTO countries (id, name) VALUES (2, 'France'); +INSERT INTO countries (id, name) VALUES (3, 'Brazil'); +INSERT INTO countries (id, name) VALUES (4, 'Italy'); +INSERT INTO countries (id, name) VALUES (5, 'Canada'); \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-trans.sql b/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-trans.sql index b8835e70cb..e4fda99437 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-trans.sql +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/data-trans.sql @@ -1,17 +1,3 @@ -DROP TABLE IF EXISTS billionaires; - -CREATE TABLE billionaires ( - id INT AUTO_INCREMENT PRIMARY KEY, - first_name VARCHAR(250) NOT NULL, - last_name VARCHAR(250) NOT NULL, - career VARCHAR(250) DEFAULT NULL -); - -INSERT INTO billionaires (first_name, last_name, career) VALUES -('Aliko', 'Dangote', 'Billionaire Industrialist'), -('Bill', 'Gates', 'Billionaire Tech Entrepreneur'), -('Folrunsho', 'Alakija', 'Billionaire Oil Magnate'); - insert into USER values (101, 'user1', 'comment1'); insert into USER values (102, 'user2', 'comment2'); insert into USER values (103, 'user3', 'comment3'); diff --git a/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/SpringBootH2IntegrationTest.java b/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/SpringBootH2IntegrationTest.java index aecc63c599..abdbd76c19 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/SpringBootH2IntegrationTest.java +++ b/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/SpringBootH2IntegrationTest.java @@ -1,50 +1,28 @@ package com.baeldung; import com.baeldung.h2db.springboot.SpringBootH2Application; -import com.baeldung.h2db.springboot.daos.UserRepository; -import com.baeldung.h2db.springboot.models.User; +import com.baeldung.h2db.springboot.daos.CountryRepository; +import com.baeldung.h2db.springboot.models.Country; 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.junit4.SpringRunner; -import java.util.List; - -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootH2Application.class) public class SpringBootH2IntegrationTest { @Autowired - private UserRepository userRepository; + private CountryRepository countryRepository; @Test - public void contextLoads() { } + public void givenInitData_whenApplicationStarts_thenDataIsLoaded() { + Iterable users = countryRepository.findAll(); - @Test - public void givenUserProfile_whenAddUser_thenCreateNewUser() { - User user = new User(); - user.setFirstName("John"); - user.setLastName("Doe"); - userRepository.save(user); - List users = (List) userRepository.findAll(); - assertFalse(users.isEmpty()); - - String firstName = "Aliko"; - String lastName = "Dangote"; - User user1 = userRepository.findById(users.get(0).getId()).get(); - user1.setLastName(lastName); - user1.setFirstName(firstName); - userRepository.save(user1); - - user = userRepository.findById(user.getId()).get(); - assertEquals(user.getFirstName(), firstName); - assertEquals(user.getLastName(), lastName); - - userRepository.deleteById(user.getId()); - assertTrue( ((List) userRepository.findAll()).isEmpty()); + assertThat(users).hasSize(5); } }