Skip to content

Commit 869bac4

Browse files
authored
Merge pull request hhimanshu#2 from hhimanshu/add-data
Added Liquibase Changesets, Entities and Tests
2 parents e702d74 + eb1f365 commit 869bac4

14 files changed

+354
-2
lines changed

pom.xml

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
<groupId>org.springframework.boot</groupId>
3434
<artifactId>spring-boot-starter-web</artifactId>
3535
</dependency>
36-
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-jpa</artifactId>
39+
</dependency>
3740
<dependency>
3841
<groupId>com.h2database</groupId>
3942
<artifactId>h2</artifactId>
@@ -44,7 +47,7 @@
4447
<artifactId>spring-boot-starter-test</artifactId>
4548
<scope>test</scope>
4649
</dependency>
47-
</dependencies>
50+
</dependencies>
4851

4952
<build>
5053
<plugins>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hhimanshu.business;
2+
3+
import com.hhimanshu.persistence.entities.Author;
4+
import com.hhimanshu.persistence.repositories.AuthorRepository;
5+
import java.util.stream.Stream;
6+
import java.util.stream.StreamSupport;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class AuthorsManager {
12+
13+
private AuthorRepository authorRepository;
14+
15+
@Autowired
16+
public AuthorsManager(AuthorRepository authorRepository) {
17+
this.authorRepository = authorRepository;
18+
}
19+
20+
public Stream<Author> getAllAuthors() {
21+
return StreamSupport.stream(authorRepository.findAll().spliterator(), true);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hhimanshu.business;
2+
3+
import com.hhimanshu.persistence.entities.Book;
4+
import com.hhimanshu.persistence.repositories.BookRepository;
5+
import java.util.stream.Stream;
6+
import java.util.stream.StreamSupport;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class BooksManager {
12+
13+
private BookRepository bookRepository;
14+
15+
@Autowired
16+
public BooksManager(BookRepository bookRepository) {
17+
this.bookRepository = bookRepository;
18+
}
19+
20+
public Stream<Book> getAllBooks() {
21+
return StreamSupport.stream(bookRepository.findAll().spliterator(), true);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.hhimanshu.persistence.entities;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.Table;
9+
10+
@Entity
11+
@Table(name = "authors")
12+
public class Author {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
private Integer id;
17+
18+
@Column(nullable = false, unique = true)
19+
private String name;
20+
21+
protected Author() {
22+
// for JPA
23+
}
24+
25+
public Author(Integer id, String name) {
26+
this.id = id;
27+
this.name = name;
28+
}
29+
30+
public Integer getId() {
31+
return id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "Author{" +
41+
"id=" + id +
42+
", name='" + name + '\'' +
43+
'}';
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.hhimanshu.persistence.entities;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.ManyToOne;
10+
import javax.persistence.Table;
11+
12+
@Entity
13+
@Table(name = "books")
14+
public class Book {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.AUTO)
18+
private int id;
19+
20+
@Column(nullable = false, unique = true)
21+
private String name;
22+
23+
@ManyToOne
24+
@JoinColumn(name = "author")
25+
private Author author;
26+
27+
protected Book() {
28+
// for JPA
29+
}
30+
31+
public Book(int id, String name, Author author) {
32+
this.id = id;
33+
this.name = name;
34+
this.author = author;
35+
}
36+
37+
public int getId() {
38+
return id;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public Author getAuthor() {
46+
return author;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "Book{" +
52+
"id=" + id +
53+
", name='" + name + '\'' +
54+
", author=" + author +
55+
'}';
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hhimanshu.persistence.repositories;
2+
3+
import com.hhimanshu.persistence.entities.Author;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface AuthorRepository extends CrudRepository<Author, Integer> {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hhimanshu.persistence.repositories;
2+
3+
import com.hhimanshu.persistence.entities.Book;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface BookRepository extends CrudRepository<Book, Integer> {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.jpa.hibernate.ddl-auto=none
2+
spring.h2.console.enabled=true
3+
liquibase.change-log=classpath:db/liquibase-changelog.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<databaseChangeLog
4+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
7+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
8+
9+
<changeSet id="01" author="hhimanshu">
10+
<createTable tableName="books"
11+
remarks="A table to contain all books">
12+
<column name="id" type="int">
13+
<constraints nullable="false" unique="true" primaryKey="true"/>
14+
</column>
15+
<column name="name" type="varchar(255)">
16+
<constraints nullable="false" unique="true"/>
17+
</column>
18+
<column name="author" type="int">
19+
<constraints nullable="false"/>
20+
</column>
21+
</createTable>
22+
23+
<createTable tableName="authors"
24+
remarks="A table to contain all the authors">
25+
<column name="id" type="int">
26+
<constraints nullable="false" primaryKey="true"/>
27+
</column>
28+
<column name="name" type="varchar(100)">
29+
<constraints nullable="false"/>
30+
</column>
31+
</createTable>
32+
33+
<addForeignKeyConstraint baseTableName="books" baseColumnNames="author"
34+
constraintName="author_fk"
35+
referencedTableName="authors" referencedColumnNames="id"/>
36+
</changeSet>
37+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<databaseChangeLog
4+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
7+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
8+
9+
<changeSet id="02" author="hhimanshu">
10+
<comment>Inserting Authors</comment>
11+
<insert tableName="authors">
12+
<column name="id" valueNumeric="1"/>
13+
<column name="name" value="author_01"/>
14+
</insert>
15+
<insert tableName="authors">
16+
<column name="id" valueNumeric="2"/>
17+
<column name="name" value="author_02"/>
18+
</insert>
19+
<insert tableName="authors">
20+
<column name="id" valueNumeric="3"/>
21+
<column name="name" value="author_03"/>
22+
</insert>
23+
<insert tableName="authors">
24+
<column name="id" valueNumeric="4"/>
25+
<column name="name" value="author_04"/>
26+
</insert>
27+
<insert tableName="authors">
28+
<column name="id" valueNumeric="5"/>
29+
<column name="name" value="author_05"/>
30+
</insert>
31+
</changeSet>
32+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<databaseChangeLog
4+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
7+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
8+
9+
<changeSet id="03" author="hhimanshu">
10+
<comment>Inserting Books</comment>
11+
<insert tableName="books">
12+
<column name="id" valueNumeric="1"/>
13+
<column name="name" value="book_01_a_01"/>
14+
<column name="author" valueNumeric="01"/>
15+
</insert>
16+
<insert tableName="books">
17+
<column name="id" valueNumeric="2"/>
18+
<column name="name" value="book_02_a_02"/>
19+
<column name="author" valueNumeric="02"/>
20+
</insert>
21+
<insert tableName="books">
22+
<column name="id" valueNumeric="3"/>
23+
<column name="name" value="book_01_a_03"/>
24+
<column name="author" valueNumeric="03"/>
25+
</insert>
26+
<insert tableName="books">
27+
<column name="id" valueNumeric="4"/>
28+
<column name="name" value="book_01_a_04"/>
29+
<column name="author" valueNumeric="04"/>
30+
</insert>
31+
<insert tableName="books">
32+
<column name="id" valueNumeric="5"/>
33+
<column name="name" value="book_01_a_05"/>
34+
<column name="author" valueNumeric="05"/>
35+
</insert>
36+
</changeSet>
37+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<databaseChangeLog
4+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
7+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
8+
9+
<include file="changelog/01-create-books-and-author-schema.xml" relativeToChangelogFile="true"/>
10+
<include file="changelog/02-insert-data-authors.xml" relativeToChangelogFile="true"/>
11+
<include file="changelog/02-insert-data-books.xml" relativeToChangelogFile="true"/>
12+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.hhimanshu.business;
2+
3+
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
6+
7+
import com.hhimanshu.persistence.entities.Author;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest
18+
public class AuthorsManagerTest {
19+
20+
@Autowired
21+
private AuthorsManager authorsManager;
22+
23+
@Test
24+
public void getAllAuthorsReturnDataFromDatabase() throws Exception {
25+
List<Author> authors = authorsManager.getAllAuthors().collect(Collectors.toList());
26+
assertFalse(authors.isEmpty());
27+
assertEquals(5, authors.size());
28+
29+
// If you want to compare all the authors to what we inserted in '02-insert-data-authors.xml'
30+
// authors.forEach(System.out::println);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.hhimanshu.business;
2+
3+
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
6+
7+
import com.hhimanshu.persistence.entities.Book;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest
18+
public class BooksManagerTest {
19+
20+
@Autowired
21+
private BooksManager booksManager;
22+
23+
@Test
24+
public void getAllBooksReturnsDataFromDatabase() throws Exception {
25+
List<Book> books = booksManager.getAllBooks().collect(Collectors.toList());
26+
assertFalse(books.isEmpty());
27+
assertEquals(5, books.size());
28+
29+
// If you want to compare all the authors to what we inserted in '02-insert-data-authors.xml'
30+
books.forEach(System.out::println);
31+
}
32+
}

0 commit comments

Comments
 (0)