Skip to content

Commit 717cdf4

Browse files
committed
data model for Author, Repository, Manager and Test
1 parent 3259400 commit 717cdf4

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

pom.xml

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
<groupId>org.springframework.boot</groupId>
3838
<artifactId>spring-boot-starter-web</artifactId>
3939
</dependency>
40-
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-data-jpa</artifactId>
43+
</dependency>
4144
<dependency>
4245
<groupId>com.h2database</groupId>
4346
<artifactId>h2</artifactId>
@@ -48,7 +51,7 @@
4851
<artifactId>spring-boot-starter-test</artifactId>
4952
<scope>test</scope>
5053
</dependency>
51-
</dependencies>
54+
</dependencies>
5255

5356
<build>
5457
<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,43 @@
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+
9+
@Entity(name = "authors")
10+
public class Author {
11+
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.AUTO)
14+
private Integer id;
15+
16+
@Column(nullable = false, unique = true)
17+
private String name;
18+
19+
protected Author() {
20+
// for JPA
21+
}
22+
23+
public Author(Integer id, String name) {
24+
this.id = id;
25+
this.name = name;
26+
}
27+
28+
public Integer getId() {
29+
return id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return "Author{" +
39+
"id=" + id +
40+
", name='" + name + '\'' +
41+
'}';
42+
}
43+
}
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,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+
}

0 commit comments

Comments
 (0)