-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc2f6b5
commit 4db1a09
Showing
4 changed files
with
354 additions
and
3 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
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,173 @@ | ||
package facades; | ||
|
||
import dto.WhoDidWhatDTO; | ||
import entities.WhoDidWhat; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
/** | ||
* | ||
* Rename Class to a relevant name Add add relevant facade methods | ||
*/ | ||
public class WhoDidWhatFacade { | ||
|
||
private static WhoDidWhatFacade instance; | ||
private static EntityManagerFactory emf; | ||
|
||
//Private Constructor to ensure Singleton | ||
private WhoDidWhatFacade() { | ||
} | ||
|
||
/** | ||
* | ||
* @param _emf | ||
* @return an instance of this facade class. | ||
*/ | ||
public static WhoDidWhatFacade getWhoDidWhatFacade(EntityManagerFactory _emf) { | ||
if (instance == null) { | ||
emf = _emf; | ||
instance = new WhoDidWhatFacade(); | ||
} | ||
return instance; | ||
} | ||
|
||
private EntityManager getEntityManager() { | ||
return emf.createEntityManager(); | ||
} | ||
|
||
/** | ||
* Get Work Done by Name | ||
* @param name of the student. Either Camilla, Malte, Asger or Runì | ||
* @return WhoDidWhatDTO | ||
* @throws Exception | ||
*/ | ||
public WhoDidWhatDTO getWorkDoneByName(String name) throws Exception { | ||
EntityManager em = getEntityManager(); | ||
try { | ||
return new WhoDidWhatDTO(em.createNamedQuery("WhoDidWhat.getByName", WhoDidWhat.class).setParameter("name", name).getSingleResult()); | ||
} catch (Exception e) { | ||
throw new Exception("No Student found by that name."); | ||
} finally { | ||
em.close(); | ||
} | ||
} | ||
|
||
public List<WhoDidWhatDTO> getAllWorkDone() { | ||
EntityManager em = getEntityManager(); | ||
try { | ||
List<WhoDidWhat> whodidwhat = em.createNamedQuery("WhoDidWhat.getAll").getResultList(); | ||
List<WhoDidWhatDTO> result = new ArrayList<>(); | ||
whodidwhat.forEach((who) -> { | ||
result.add(new WhoDidWhatDTO(who)); | ||
}); | ||
return result; | ||
} finally { | ||
em.close(); | ||
} | ||
} | ||
|
||
// | ||
// /** | ||
// * Get Ticket Sales for a Movie by ID. To be used internally only. | ||
// * | ||
// * @param id of the Movie. | ||
// * @return Number of Ticket sales as a Long. | ||
// */ | ||
// public Long getTicketSales(int id) { | ||
// EntityManager em = getEntityManager(); | ||
// try { | ||
// return (Long) em.createQuery("SELECT r.ticketsSold FROM Movie r WHERE r.id = :id").setParameter("id", id).getSingleResult(); | ||
// } finally { | ||
// em.close(); | ||
// } | ||
// } | ||
// | ||
// /** | ||
// * Get all Movies in DTO format. | ||
// * | ||
// * @return | ||
// */ | ||
// public List<MovieDTO> getAllMoviesDTO() { | ||
// EntityManager em = getEntityManager(); | ||
// try { | ||
// List<Movie> movies = em.createNamedQuery("Movie.findAll").getResultList(); | ||
// List<MovieDTO> result = new ArrayList<>(); | ||
// movies.forEach((movie) -> { | ||
// result.add(new MovieDTO(movie)); | ||
// }); | ||
// return result; | ||
// } finally { | ||
// em.close(); | ||
// } | ||
// } | ||
// | ||
// /** | ||
// * Get MovieDTO by ID. | ||
// * | ||
// * @param id of the Movie | ||
// * @return MovieDTO | ||
// */ | ||
// public MovieDTO getMovieDTOById(Long id) { | ||
// EntityManager em = getEntityManager(); | ||
// try { | ||
// Movie movie = em.find(Movie.class, id); | ||
// return new MovieDTO(movie); | ||
// } finally { | ||
// em.close(); | ||
// } | ||
// | ||
// } | ||
// | ||
// public Movie makeMovie(Movie movie) { | ||
// EntityManager em = getEntityManager(); | ||
// try { | ||
// em.getTransaction().begin(); | ||
// em.persist(movie); | ||
// em.getTransaction().commit(); | ||
// return movie; | ||
// } catch (Exception e) { | ||
// em.getTransaction().rollback(); | ||
// } finally { | ||
// em.close(); | ||
// } | ||
// return null; | ||
// } | ||
// | ||
// /** | ||
// * Get number of movies in the database. | ||
// * | ||
// * @return | ||
// */ | ||
// public Long getMovieCount() { | ||
// EntityManager em = getEntityManager(); | ||
// try { | ||
// Long result = (long) em.createNamedQuery("Movie.count").getSingleResult(); | ||
// return result; | ||
// } finally { | ||
// em.close(); | ||
// } | ||
// } | ||
// | ||
// /** | ||
// * Returns MovieDTO by name. | ||
// * | ||
// * @param name | ||
// * @return | ||
// * @throws java.lang.Exception | ||
// */ | ||
// public MovieDTO getMovieDTOByName(String name) throws Exception { | ||
// EntityManager em = getEntityManager(); | ||
// try { | ||
// return em.createQuery("SELECT new dto.MovieDTO(m) FROM Movie m WHERE m.name = :name", MovieDTO.class).setParameter("name", name).getSingleResult(); | ||
// } catch (Exception e) { | ||
// e.printStackTrace(); | ||
// throw new Exception("No customer found by that name"); | ||
// } finally { | ||
// em.close(); | ||
// } | ||
// } | ||
|
||
|
||
} |
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,178 @@ | ||
package facades; | ||
|
||
import dto.WhoDidWhatDTO; | ||
import entities.WhoDidWhat; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import utils.EMF_Creator; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Query; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.AfterEach; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import utils.EMF_Creator.DbSelector; | ||
import utils.EMF_Creator.Strategy; | ||
|
||
//Uncomment the line below, to temporarily disable this test | ||
//@Disabled | ||
public class WhoDidWhatFacadeTest { | ||
|
||
private static EntityManagerFactory emf; | ||
private static WhoDidWhatFacade facade; | ||
private static WhoDidWhat testTask; | ||
private static List<WhoDidWhat> testList = new ArrayList<>(); | ||
|
||
public WhoDidWhatFacadeTest() { | ||
} | ||
|
||
@BeforeAll | ||
public static void setUpClass() { | ||
emf = EMF_Creator.createEntityManagerFactory(DbSelector.TEST, Strategy.DROP_AND_CREATE); | ||
} | ||
|
||
@AfterAll | ||
public static void tearDownClass() { | ||
|
||
} | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
facade = WhoDidWhatFacade.getWhoDidWhatFacade(emf); | ||
|
||
testTask = new WhoDidWhat("Malte"); | ||
testTask.addDone("This"); | ||
testTask.addDone("That"); | ||
testTask.addDone("The other"); | ||
|
||
WhoDidWhat testTwo = new WhoDidWhat("Asger"); | ||
testTwo.addDone("Everything"); | ||
testTwo.addDone("Even more"); | ||
testTwo.addDone("So much"); | ||
|
||
testList.add(testTask); | ||
testList.add(testTwo); | ||
|
||
EntityManager em = emf.createEntityManager(); | ||
|
||
em.getTransaction().begin(); | ||
em.createNativeQuery("DELETE FROM WHODIDWHAT").executeUpdate(); | ||
em.getTransaction().commit(); | ||
|
||
try { | ||
for (WhoDidWhat obj : testList) { | ||
em.getTransaction().begin(); | ||
em.persist(obj); | ||
em.getTransaction().commit(); | ||
} | ||
} finally { | ||
em.close(); | ||
} | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
|
||
} | ||
|
||
@Test | ||
public void testGetWorkDoneByName() throws Exception { | ||
// Arrange | ||
WhoDidWhatDTO expResult = new WhoDidWhatDTO(testTask); | ||
// Act | ||
WhoDidWhatDTO result = facade.getWorkDoneByName("Malte"); | ||
// Assert | ||
assertEquals(expResult, result); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetAllWorkDone() throws Exception { | ||
// Arrange | ||
List<WhoDidWhatDTO> expResult = new ArrayList<>(); | ||
for (WhoDidWhat whodidwhat: testList) { | ||
expResult.add(new WhoDidWhatDTO(whodidwhat)); | ||
} | ||
// Act | ||
List<WhoDidWhatDTO> result = facade.getAllWorkDone(); | ||
// Assert | ||
assertEquals(expResult, result); | ||
} | ||
|
||
|
||
|
||
// @Test | ||
// public void testGetMovieByName() throws Exception { | ||
// //Arrange | ||
// MovieDTO expResult = new MovieDTO(terminator); | ||
// //Act | ||
// MovieDTO result = facade.getMovieDTOByName("Terminator"); | ||
// //Assert | ||
// assertEquals(expResult, result); | ||
// } | ||
// | ||
// @Test | ||
// public void testGetMovieCount() { | ||
// // Arrange | ||
// Long expResult = 1l; | ||
// // Act | ||
// Long result = facade.getMovieCount(); | ||
// // Assert | ||
// assertEquals(expResult, result); | ||
// } | ||
// | ||
// @org.junit.jupiter.api.Test | ||
// public void testGetTicketSales() { | ||
// //Arrange | ||
// Long expResult = terminator.getTicketsSold(); | ||
// //Act | ||
// Long result = facade.getTicketSales(1); | ||
// //Assert | ||
// assertEquals(expResult, result); | ||
// } | ||
// | ||
// @org.junit.jupiter.api.Test | ||
// public void testGetAllMovies() { | ||
// //Arrange | ||
// List<MovieDTO> expResult = new ArrayList<>(); | ||
// expResult.add(new MovieDTO(terminator)); | ||
// //Act | ||
// List<MovieDTO> result = facade.getAllMoviesDTO(); | ||
// //Assert | ||
// assertEquals(expResult, result); | ||
// } | ||
// | ||
// @org.junit.jupiter.api.Test | ||
// public void testGetMovieDTO() { | ||
// //Arrange | ||
// MovieDTO expResult = new MovieDTO(terminator); | ||
// //Act | ||
// Long id = 1l; | ||
// MovieDTO result = facade.getMovieDTOById(id); | ||
// //Assert | ||
// assertEquals(expResult, result); | ||
// } | ||
// | ||
// @org.junit.jupiter.api.Test | ||
// public void testMakeMovie() { | ||
// //Arrange | ||
// Movie terminator2 = new Movie("Terminator 2: Judgment Day", Date.valueOf("1991-11-08"), 9, false, 200000); | ||
// //Act | ||
// Movie result = facade.makeMovie(terminator2); | ||
// //Assert | ||
// assertEquals(terminator2, result); | ||
// // Removing the user again so it doesn't mess up the other tests. | ||
// EntityManager em = emf.createEntityManager(); | ||
// try { | ||
// em = emf.createEntityManager(); | ||
// em.getTransaction().begin(); | ||
// em.remove(em.find(Movie.class, new Long(movies.size() + 1))); | ||
// em.getTransaction().commit(); | ||
// } finally { | ||
// em.close(); | ||
// } | ||
// } | ||
} |