Skip to content

Commit

Permalink
Added parent class for JpaDAO tests
Browse files Browse the repository at this point in the history
Added BallDrawJPADaoTest TCs
  • Loading branch information
nunoaac committed Jul 21, 2015
1 parent 2cc5702 commit b59358e
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class Program {

public static void main(String[] args) throws InterruptedException, Exception {


testBallDrawDao();
/*
Client user = new Client("nunoaac", "123qwe");
Expand All @@ -32,7 +33,7 @@ public static void main(String[] args) throws InterruptedException, Exception {
jpac.create(user);
jpac.getHashPasswordForClient("nunoaac");

*/
/*
BallDraw newDraw;
ManualBallDraw drawGen = new ManualBallDraw();
Expand Down Expand Up @@ -128,17 +129,29 @@ public static void testDaoAndPatterns() throws Exception {
}

public static void testBallDrawDao() throws Exception {

Client cl = new Client("nunoaac", "123qwe");
ClientJpaDAO clientDao = new ClientJpaDAO();
clientDao.create(cl);

BallDraw newDraw;
ManualBallDraw drawGen = new ManualBallDraw();
newDraw = drawGen.getBallDraw(50, 20, DrawAlgorithm.SIMPLERANDOM);

List<BallDraw>oldList = cl.getDraws();
oldList.add(newDraw);
cl.setDraws(oldList);
newDraw.setClient(cl);

BallDrawJpaDAO jpac = new BallDrawJpaDAO();
jpac.create(newDraw);

System.out.println(newDraw.toString());

BallDraw secondDraw = jpac.findBallDraw(newDraw.getUid());
System.out.println(secondDraw.toString());

clientDao.destroy(cl.getId());
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nunoaac.balldraw_core.balldraw.domain.beans;

import com.nunoaac.balldraw_core.balldraw.logic.algorithm.BallDrawAlgorithmInterface.DrawAlgorithm;
import com.nunoaac.balldraw_core.balldraw.tutorial.Customer;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -17,6 +16,7 @@
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
Expand Down Expand Up @@ -47,7 +47,6 @@ public class BallDraw implements Serializable {
private DrawAlgorithm algorithm;
private Integer size = 0;
private String uid;
@ManyToOne()
private Client client;

public BallDraw() {
Expand Down Expand Up @@ -79,6 +78,8 @@ public List<Integer> getDraw() {
return draw;
}

@ManyToOne()
@JoinColumn(nullable=false)
public Client getClient() {
return client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.eclipse.persistence.annotations.CascadeOnDelete;

/**
* Represents an Client, with username, hashPassword, id and list of generated draws
Expand All @@ -16,6 +18,7 @@
* @author support
*/
@Entity
@CascadeOnDelete
public class Client implements Serializable{

@Id
Expand All @@ -25,7 +28,8 @@ public class Client implements Serializable{
private String username;
@Column(nullable=false)
private String hashPassword;
@OneToMany(mappedBy = "client")
@OneToMany(mappedBy = "client", orphanRemoval=true, cascade={CascadeType.ALL})
@CascadeOnDelete
private List<BallDraw> draws;

public Client() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void edit(BallDraw ballDraw) throws NonexistentEntityException, Exception
}
}
}

/*
public void destroy(UUID id) throws NonexistentEntityException {
EntityManager em = null;
try {
Expand All @@ -93,7 +93,7 @@ public void destroy(UUID id) throws NonexistentEntityException {
}
}
}

*/
public List<BallDraw> findBallDrawEntities() {
return findBallDrawEntities(true, -1, -1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void destroy(ID id) throws NonexistentEntityException {
try {
em = getEntityManager();
em.getTransaction().begin();
System.out.println(id);
T returnedRow = em.getReference(entityClass, id);
if (returnedRow == null) {
throw new NonexistentEntityException("The " + entityClass.getSimpleName() + " with id " + id + " no longer exists.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,81 @@
package com.nunoaac.balldraw_core.balldraw.domain.daos;

import com.nunoaac.balldraw_core.balldraw.domain.beans.BallDraw;
import java.util.List;
import java.util.UUID;
import javax.persistence.EntityManager;
import com.nunoaac.balldraw_core.balldraw.domain.beans.Client;
import java.util.Map;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

/**
*
* @author nunocosta
*/
public class BallDrawJpaDAOTest {

static BallDrawJpaDAO bdDao;
public class BallDrawJpaDAOTest<ID> extends JpaDAOTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

public BallDrawJpaDAOTest() {
super();
}

@BeforeClass
public static void setUpClass() {
bdDao = new BallDrawJpaDAO();
@Override
@After
public void tearDown() throws Exception {

}

@AfterClass
public static void tearDownClass() {

@Test
public void testCreateBallDraw() throws Exception {

Client user = new Client("test" + RandomStringUtils.random(10, true, false), "123qwe");
auxiliaryPersistClient(user);

int numberOfDraws = bdDrawDao.getEntityCount();

BallDraw newDraw = auxiliaryRandomGenerateManualBallDraw(user);
bdDrawDao.create(newDraw);

toDelete.put(bdDrawDao, (ID) newDraw.getUid());
int newNumberOfDraws = bdDrawDao.getEntityCount();
assertEquals("testCreateBallDraw is not working correctly. Number of draw rows has not incremented with a createDraw call", (numberOfDraws + 1), newNumberOfDraws);
}

@Before
public void setUp() {

@Test
public void testCreateBallDrawWithoutUser() throws Exception {
int numberOfDraws = bdDrawDao.getEntityCount();

BallDraw newDraw = auxiliaryRandomGenerateManualBallDraw(null);

thrown.expect(javax.persistence.RollbackException.class);
thrown.expectMessage("ERROR: null value in column \"client_id\" violates not-null constraint");
auxiliaryPersistBallDraw(newDraw);

int newNumberOfDraws = bdDrawDao.getEntityCount();
assertEquals("testCreateBallDrawWithoutUser is not working correctly. Draw without client has been persisted.", numberOfDraws, newNumberOfDraws);
}

@After
public void tearDown() {
}
@Test
public void testDestroyBallDraw() throws Exception {

Client user = new Client("test" + RandomStringUtils.random(10, true, false), "123qwe");
auxiliaryPersistClient(user);

BallDraw newDraw = auxiliaryRandomGenerateManualBallDraw(user);
bdDrawDao.create(newDraw);

int numberOfDraws = bdDrawDao.getEntityCount();
bdDrawDao.destroy(newDraw.getUid());

//thrown.expect(javax.persistence.EntityNotFoundException.class);
BallDraw retrievedBallDraw = (BallDraw) bdDrawDao.findById(newDraw.getUid());
assertNull("testDestroy is not deleting the ball draw from the database", retrievedBallDraw);
int newNumberOfDraws = bdDrawDao.getEntityCount();
assertEquals("testDestroy is not working correctly. Number of ball draw rows is not the same", (numberOfDraws-1), newNumberOfDraws);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,76 +23,50 @@
*
* @author nunocosta
*/
public class ClientJpaDAOTest<ID> {

static ClientJpaDAO bdDao;
HashMap<GenericDAO, ID> toDelete;
public class ClientJpaDAOTest<ID> extends JpaDAOTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

public ClientJpaDAOTest() {
bdDao = new ClientJpaDAO();
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
toDelete = new HashMap<GenericDAO, ID>();
}

@After
public void tearDown() throws Exception {

for (Map.Entry<GenericDAO, ID> entry : toDelete.entrySet()) {
GenericDAO dao = entry.getKey();
ID id = entry.getValue();
dao.destroy(id);
}
super();
}

@Test
public void testCreateClient() throws Exception {
System.out.println("JUnit Test - ClientJpaDAO - Create normal client");

int numberOfClients = bdDao.getEntityCount();
int numberOfClients = bdClientDao.getEntityCount();

Client client = new Client("test" + RandomStringUtils.random(10, true, false), "123qwe");
bdDao.create(client);
bdClientDao.create(client);

assertNotNull("testCreate is not working correctly. ID of created row is null", client.getId());
toDelete.put(bdDao, (ID) client.getId());
int newNumberOfClients = bdDao.getEntityCount();
toDelete.put(bdClientDao, (ID) client.getId());
int newNumberOfClients = bdClientDao.getEntityCount();
assertEquals("testCreate is not working correctly. Number of client rows has not incremented with a createClient call", (numberOfClients + 1), newNumberOfClients);
}

@Test
public void testCreateClientWithSameUsername() throws Exception {
System.out.println("JUnit Test - ClientJpaDAO - Create client with same username");

int numberOfClients = bdDao.getEntityCount();
int numberOfClients = bdClientDao.getEntityCount();

String randomUsername = "test" + RandomStringUtils.random(10, true, false);
Client client = new Client(randomUsername, "123qwe");
bdDao.create(client);
bdClientDao.create(client);

assertNotNull("testCreate is not working correctly. ID of created row is null", client.getId());
toDelete.put(bdDao, (ID) client.getId());
toDelete.put(bdClientDao, (ID) client.getId());

client = new Client(randomUsername, "qwe123");

thrown.expect(javax.persistence.RollbackException.class);
thrown.expectMessage("ERROR: duplicate key value violates unique constraint \"client_username_key\"");
bdDao.create(client);
bdClientDao.create(client);

int newNumberOfClients = bdDao.getEntityCount();
int newNumberOfClients = bdClientDao.getEntityCount();
assertEquals("testCreateClientWithSameUsername is not working correctly. Number of client rows has not incremented only by 1", (numberOfClients + 1), newNumberOfClients);
}

Expand All @@ -103,7 +77,7 @@ public void testFindClientById() throws Exception {
Client client = new Client("test" + RandomStringUtils.random(10, true, false), "123qwe");
auxiliaryPersistClient(client);

Client retrievedClient = (Client) bdDao.findById(client.getId());
Client retrievedClient = (Client) bdClientDao.findById(client.getId());
assertTrue("testFindClientById is returning a different client that was expected", client.equals(retrievedClient));
}

Expand All @@ -115,9 +89,9 @@ public void testEditClient() throws Exception {
auxiliaryPersistClient(client);

client.setHashPassword("newPassword");
bdDao.edit(client, client.getId());
bdClientDao.edit(client, client.getId());

Client retrievedClient = (Client) bdDao.findById(client.getId());
Client retrievedClient = (Client) bdClientDao.findById(client.getId());
assertTrue("testFindClientById is returning a different client that was expected", client.equals(retrievedClient));

}
Expand All @@ -126,16 +100,16 @@ public void testEditClient() throws Exception {
public void testDestroy() throws Exception {
System.out.println("JUnit Test - ClientJpaDAO - Delete client");

int numberOfClients = bdDao.getEntityCount();
int numberOfClients = bdClientDao.getEntityCount();
Client client = new Client("test" + RandomStringUtils.random(10, true, false), "123qwe");
auxiliaryPersistClient(client);

bdDao.destroy(client.getId());
bdClientDao.destroy(client.getId());

Client retrievedClient = (Client) bdDao.findById(client.getId());
thrown.expect(javax.persistence.EntityNotFoundException.class);
Client retrievedClient = (Client) bdClientDao.findById(client.getId());
assertNull("testDestroy is not deleting the client from the database", retrievedClient);
int newNumberOfClients = bdDao.getEntityCount();
int newNumberOfClients = bdClientDao.getEntityCount();
assertEquals("testDestroy is not working correctly. Number of client rows is not the same", numberOfClients, newNumberOfClients);
}

Expand All @@ -148,7 +122,7 @@ public void testGetHashPassword() throws Exception {
Client client = new Client(randomUsername, randomHashPassword);
auxiliaryPersistClient(client);

assertEquals("testGetHashPassword not working correctly. Passwords don't match", bdDao.getHashPasswordForClient(client.getUsername()), randomHashPassword);
assertEquals("testGetHashPassword not working correctly. Passwords don't match", bdClientDao.getHashPasswordForClient(client.getUsername()), randomHashPassword);
}

@Test
Expand All @@ -157,12 +131,6 @@ public void testGetHasPasswordInvalidClient() throws Exception {

String randomUsername = RandomStringUtils.random(10, true, false);
thrown.expect(javax.persistence.NoResultException.class);
bdDao.getHashPasswordForClient(randomUsername);
}

public void auxiliaryPersistClient(Client client) throws Exception {
bdDao.create(client);
assertNotNull("auxiliaryCreateClient is not working correctly. ID of created row is null", client.getId());
toDelete.put(bdDao, (ID) client.getId());
bdClientDao.getHashPasswordForClient(randomUsername);
}
}
Loading

0 comments on commit b59358e

Please sign in to comment.