Skip to content

Commit

Permalink
Initial Work on Issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteMagnussen committed Sep 10, 2019
1 parent fc2f6b5 commit 4db1a09
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/entities/WhoDidWhat.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public WhoDidWhat() {
// FIELDS
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private List<String> done;
Expand Down
173 changes: 173 additions & 0 deletions src/main/java/facades/WhoDidWhatFacade.java
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();
// }
// }


}
178 changes: 178 additions & 0 deletions src/test/java/facades/WhoDidWhatFacadeTest.java
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();
// }
// }
}

0 comments on commit 4db1a09

Please sign in to comment.