Skip to content

Commit 7eb04b4

Browse files
authored
Added Foo stored procedures tests
1 parent 95e6cb1 commit 7eb04b4

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.baeldung.persistence.service;
2+
3+
import java.util.List;
4+
5+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
6+
import static org.junit.Assert.assertEquals;
7+
8+
import org.hibernate.Query;
9+
import org.hibernate.Session;
10+
import org.hibernate.SessionFactory;
11+
import org.hibernate.exception.SQLGrammarException;
12+
import org.junit.After;
13+
import org.junit.Assume;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.test.context.ContextConfiguration;
19+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
20+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
21+
22+
import com.baeldung.persistence.model.Foo;
23+
import com.baeldung.spring.PersistenceConfig;
24+
25+
@RunWith(SpringJUnit4ClassRunner.class)
26+
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
27+
public class FooStoredProceduresIntegrationTest {
28+
29+
@Autowired
30+
private SessionFactory sessionFactory;
31+
32+
@Autowired
33+
private IFooService fooService;
34+
35+
private Session session;
36+
37+
@Before
38+
public final void before() {
39+
session = sessionFactory.openSession();
40+
Assume.assumeTrue(getAllFoosExists());
41+
Assume.assumeTrue(getFoosByNameExists());
42+
}
43+
44+
private boolean getFoosByNameExists() {
45+
try {
46+
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
47+
.addEntity(Foo.class);
48+
sqlQuery.list();
49+
return true;
50+
} catch (SQLGrammarException e) {
51+
System.out
52+
.println("WARNING : GetFoosByName() Procedure is may be missing ");
53+
return false;
54+
}
55+
}
56+
57+
private boolean getAllFoosExists() {
58+
try {
59+
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
60+
.addEntity(Foo.class);
61+
sqlQuery.list();
62+
return true;
63+
} catch (SQLGrammarException e) {
64+
System.out
65+
.println("WARNING : GetAllFoos() Procedure is may be missing ");
66+
return false;
67+
}
68+
}
69+
70+
@After
71+
public final void after() {
72+
session.close();
73+
}
74+
75+
@Test
76+
public final void getAllFoosUsingStoredProcedures() {
77+
78+
fooService.create(new Foo(randomAlphabetic(6)));
79+
80+
// Stored procedure getAllFoos using createSQLQuery
81+
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(
82+
Foo.class);
83+
@SuppressWarnings("unchecked")
84+
List<Foo> allFoos = sqlQuery.list();
85+
for (Foo foo : allFoos) {
86+
System.out.println("getAllFoos() SQL Query result : "
87+
+ foo.getName());
88+
}
89+
assertEquals(allFoos.size(), fooService.findAll().size());
90+
91+
// Stored procedure getAllFoos using a Named Query
92+
Query namedQuery = session.getNamedQuery("callGetAllFoos");
93+
@SuppressWarnings("unchecked")
94+
List<Foo> allFoos2 = namedQuery.list();
95+
for (Foo foo : allFoos2) {
96+
System.out.println("getAllFoos() NamedQuery result : "
97+
+ foo.getName());
98+
}
99+
assertEquals(allFoos2.size(), fooService.findAll().size());
100+
}
101+
102+
@Test
103+
public final void getFoosByNameUsingStoredProcedures() {
104+
105+
fooService.create(new Foo("NewFooName"));
106+
107+
// Stored procedure getFoosByName using createSQLQuery()
108+
Query sqlQuery = session.createSQLQuery("CALL GetFoosByName(:fooName)")
109+
.addEntity(Foo.class).setParameter("fooName", "NewFooName");
110+
@SuppressWarnings("unchecked")
111+
List<Foo> allFoosByName = sqlQuery.list();
112+
for (Foo foo : allFoosByName) {
113+
System.out.println("getFoosByName() using SQL Query : found => "
114+
+ foo.toString());
115+
}
116+
117+
// Stored procedure getFoosByName using getNamedQuery()
118+
Query namedQuery = session.getNamedQuery("callGetFoosByName")
119+
.setParameter("fooName", "NewFooName");
120+
@SuppressWarnings("unchecked")
121+
List<Foo> allFoosByName2 = namedQuery.list();
122+
for (Foo foo : allFoosByName2) {
123+
System.out.println("getFoosByName() using Native Query : found => "
124+
+ foo.toString());
125+
}
126+
127+
}
128+
}

0 commit comments

Comments
 (0)