-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from jogrimst/master
#307 Change Student id property to String
- Loading branch information
Showing
7 changed files
with
179 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.literacyapp.dao; | ||
|
||
import java.util.List; | ||
import org.literacyapp.model.Student; | ||
import org.literacyapp.model.enums.Locale; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
|
||
public interface StudentDao extends GenericDao<Student> { | ||
|
||
Student read(String uniqueId) throws DataAccessException; | ||
|
||
List<Student> readAll(Locale locale) throws DataAccessException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.literacyapp.dao.jpa; | ||
|
||
import java.util.List; | ||
import javax.persistence.NoResultException; | ||
import org.literacyapp.dao.StudentDao; | ||
|
||
import org.springframework.dao.DataAccessException; | ||
|
||
import org.literacyapp.model.Student; | ||
import org.literacyapp.model.enums.Locale; | ||
|
||
public class StudentDaoJpa extends GenericDaoJpa<Student> implements StudentDao { | ||
|
||
@Override | ||
public Student read(String uniqueId) throws DataAccessException { | ||
try { | ||
return (Student) em.createQuery( | ||
"SELECT s " + | ||
"FROM Student s " + | ||
"WHERE s.uniqueId = :uniqueId") | ||
.setParameter("uniqueId", uniqueId) | ||
.getSingleResult(); | ||
} catch (NoResultException e) { | ||
logger.warn("Student \"" + uniqueId + "\" was not found"); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public List<Student> readAll(Locale locale) throws DataAccessException { | ||
return em.createQuery( | ||
"SELECT s " + | ||
"FROM Student s " + | ||
"WHERE s.locale = :locale") | ||
.setParameter("locale", locale) | ||
.getResultList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.literacyapp.dao; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.apache.log4j.Logger; | ||
import static org.junit.Assert.*; | ||
import static org.hamcrest.CoreMatchers.*; | ||
import org.hibernate.exception.ConstraintViolationException; | ||
import org.junit.Ignore; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.literacyapp.model.Device; | ||
import org.literacyapp.model.Student; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.orm.jpa.JpaSystemException; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/applicationContext-jpa.xml") | ||
public class StudentDaoTest { | ||
|
||
private Logger logger = Logger.getLogger(getClass()); | ||
|
||
@Autowired | ||
private StudentDao studentDao; | ||
|
||
@Autowired | ||
private DeviceDao deviceDao; | ||
|
||
@Test(expected = JpaSystemException.class) | ||
public void testCreateStudentsWithNonUniqueIds() { | ||
Student student1 = new Student(); | ||
student1.setUniqueId("4113947bec18b7ad_1"); | ||
studentDao.create(student1); | ||
|
||
Student student2 = new Student(); | ||
student2.setUniqueId("4113947bec18b7ad_1"); | ||
studentDao.create(student2); | ||
} | ||
|
||
@Test | ||
public void testStoreStudentWithOneDevice() { | ||
Set<Device> devices = new HashSet<>(); | ||
|
||
Device device = new Device(); | ||
device.setDeviceId("4113947bec18b7ad"); | ||
deviceDao.create(device); | ||
devices.add(device); | ||
|
||
Student student = new Student(); | ||
student.setUniqueId("4113947bec18b7ad_1"); | ||
student.setDevices(devices); | ||
studentDao.create(student); | ||
|
||
Student studentStoredInDatabase = studentDao.read("4113947bec18b7ad_1"); | ||
assertThat(studentStoredInDatabase.getDevices().size(), is(1)); | ||
} | ||
|
||
@Test | ||
public void testStoreStudentWithTWoDevices() { | ||
Set<Device> devices = new HashSet<>(); | ||
|
||
Device device1 = new Device(); | ||
device1.setDeviceId("2223947bec18b7ad"); | ||
deviceDao.create(device1); | ||
devices.add(device1); | ||
|
||
Device device2 = new Device(); | ||
device2.setDeviceId("679b35bb2322c6e4"); | ||
deviceDao.create(device2); | ||
devices.add(device2); | ||
|
||
Student student = new Student(); | ||
student.setUniqueId("2223947bec18b7ad_1"); | ||
student.setDevices(devices); | ||
studentDao.create(student); | ||
|
||
Student studentStoredInDatabase = studentDao.read("2223947bec18b7ad_1"); | ||
assertThat(studentStoredInDatabase.getDevices().size(), is(2)); | ||
} | ||
} |