Skip to content

Commit caa6632

Browse files
authored
muasif80@gmail.com (#13262)
* BAEL-6063 * Updated the unit test class name * Fixed the test * Made updates to method names for tests and some refactor of code * Updated the formatting using Baeldung formatter for Java * Removed the space around = and made line continuations 2 spaces
1 parent fd1c286 commit caa6632

5 files changed

Lines changed: 223 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.spring.data.persistence.search;
2+
3+
import java.util.Objects;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
10+
@Entity
11+
public class Student {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.AUTO)
15+
private long id;
16+
17+
private String name;
18+
private int score;
19+
20+
public Student() {
21+
}
22+
23+
public Student(String name, int score) {
24+
25+
this.name = name;
26+
this.score = score;
27+
}
28+
29+
public long getId() {
30+
return id;
31+
}
32+
33+
public void setId(long id) {
34+
this.id = id;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
45+
public int getScore() {
46+
return score;
47+
}
48+
49+
public void setScore(int score) {
50+
this.score = score;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "Student [id=" + id + ", name=" + name + ", score=" + score + "]";
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return Objects.hash(id, name, score);
61+
}
62+
63+
@Override
64+
public boolean equals(Object obj) {
65+
if (this == obj)
66+
return true;
67+
if (obj == null)
68+
return false;
69+
if (getClass() != obj.getClass())
70+
return false;
71+
Student other = (Student) obj;
72+
return id == other.id && Objects.equals(name, other.name) && score == other.score;
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.spring.data.persistence.search;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
@SpringBootApplication
9+
public class StudentApplication {
10+
11+
private static final Logger log = LoggerFactory.getLogger(StudentApplication.class);
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(StudentApplication.class, args);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.spring.data.persistence.search;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.domain.Sort;
6+
import org.springframework.data.jpa.repository.JpaRepository;
7+
import org.springframework.stereotype.Repository;
8+
9+
@Repository
10+
public interface StudentRepository extends JpaRepository<Student, Long> {
11+
12+
Student findFirstByOrderByScoreDesc();
13+
14+
Student findFirstBy(Sort sort);
15+
16+
Student findFirstByNameLike(String name, Sort sort);
17+
18+
List<Student> findFirst3ByOrderByScoreDesc();
19+
20+
List<Student> findFirst2ByScoreBetween(int startScore, int endScore, Sort sort);
21+
22+
Student findTopByOrderByScoreDesc();
23+
24+
Student findTopBy(Sort sort);
25+
26+
Student findTopByNameLike(String name, Sort sort);
27+
28+
List<Student> findTop3ByOrderByScoreDesc();
29+
30+
List<Student> findTop2ByScoreBetween(int startScore, int endScore, Sort sort);
31+
}

persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ spring.datasource.username=sa
33
spring.datasource.password=sa
44

55
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
6+
logging.level.com.baeldung.spring.data.persistence.search=debug
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.baeldung.spring.data.persistence.search;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import java.util.ArrayList;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
import java.util.Random;
10+
import java.util.stream.Collectors;
11+
12+
import org.junit.After;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.boot.test.context.SpringBootTest;
20+
import org.springframework.data.domain.Sort;
21+
import org.springframework.test.context.junit4.SpringRunner;
22+
23+
@RunWith(SpringRunner.class)
24+
@SpringBootTest
25+
public class StudentApplicationUnitTest {
26+
27+
@Autowired
28+
private StudentRepository studentRepo;
29+
private List<Student> students;
30+
31+
@Before
32+
public void fillData() {
33+
students = new ArrayList<>();
34+
int count = 10;
35+
Random r = new Random();
36+
List<Integer> scores = r.ints(0, 101)
37+
.distinct()
38+
.limit(count)
39+
.boxed()
40+
.collect(Collectors.toList());
41+
42+
for (int i = 0; i < count; i++) {
43+
Integer score = scores.get(i);
44+
Student s = new Student("Student-" + i, score);
45+
students.add(s);
46+
}
47+
48+
studentRepo.saveAll(students);
49+
Comparator<Student> c = Comparator.comparing(a -> a.getScore());
50+
c = c.reversed();
51+
students.sort(c);
52+
}
53+
54+
@After
55+
public void clearData() {
56+
studentRepo.deleteAll();
57+
}
58+
59+
@Test
60+
public void givenStudentScores_whenMoreThanOne_thenFindFirst() {
61+
62+
Student student = studentRepo.findFirstByOrderByScoreDesc();
63+
Student s = students.get(0);
64+
assertEquals(student, s);
65+
}
66+
67+
@Test
68+
public void givenStudentScores_whenMoreThan3_thenFindFirstThree() {
69+
70+
List<Student> firstThree = studentRepo.findFirst3ByOrderByScoreDesc();
71+
List<Student> sList = students.subList(0, 3);
72+
assertArrayEquals(firstThree.toArray(), sList.toArray());
73+
}
74+
75+
@Test
76+
public void givenStudentScores_whenNameMatches_thenFindFirstStudent() {
77+
78+
String matchString = "3";
79+
Student student = studentRepo.findFirstByNameLike("%" + matchString + "%", Sort.by("score")
80+
.descending());
81+
Student s = students.stream()
82+
.filter(a -> a.getName()
83+
.contains(matchString))
84+
.findFirst()
85+
.orElse(null);
86+
assertEquals(student, s);
87+
}
88+
89+
@Test
90+
public void givenStudentScores_whenBetweenRange_thenFindFirstTwoStudents() {
91+
92+
List<Student> topTwoBetweenRange = studentRepo.findFirst2ByScoreBetween(50, 60, Sort.by("score")
93+
.descending());
94+
List<Student> _students = students.stream()
95+
.filter(a -> a.getScore() >= 50 && a.getScore() <= 60)
96+
.limit(2)
97+
.collect(Collectors.toList());
98+
assertArrayEquals(_students.toArray(), topTwoBetweenRange.toArray());
99+
}
100+
}

0 commit comments

Comments
 (0)