code 2

2019-02-06 02:50:25.743

package com.accenture.adf.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.accenture.adf.businesstier.dao.EventDAO;
import com.accenture.adf.businesstier.entity.Event;
import com.accenture.adf.businesstier.entity.EventCoordinator;
import com.accenture.adf.businesstier.entity.Visitor;
import com.accenture.adf.businesstier.service.EventServiceImpl;
import com.accenture.adf.exceptions.FERSGenericException;
import com.accenture.adf.helper.FERSDataConnection;

/**
 * Junit test case to test class EventServiceImpl
 *
 */
public class TestEventServiceImpl {

private List<Object[]> eventList;
private Visitor visitor;
private EventServiceImpl eventServiceImpl;
private static Connection connection = null;
private static PreparedStatement statement = null;
private static ResultSet resultSet = null;
private EventDAO dao;
private Event event;
private List<EventCoordinator> showAllEvents1;
private ArrayList<Object[]> showAllEvents;


/**
* Set up the objects required before execution of every method
*
* @throws Exception
*/
@Before
public void setUp() throws Exception {
eventServiceImpl = new EventServiceImpl();
visitor = new Visitor();
}

/**
* Deallocates the objects after execution of every method
*
* @throws Exception
*/
@After
public void tearDown() throws Exception {
/**
* @TODO: Release all the objects here by assigning them null 
*/
eventServiceImpl=null;
visitor=null;
}

/**
* Test case to test the method getAllEvents
*/
@Test
public void testGetAllEvents() {
/**
* @TODO: Call getAllEvents method and assert it for the size of returned array
*/
eventList = eventServiceImpl.getAllEvents();
if(eventList.size()==0) {
assertTrue("no elements", eventList.size()==0);
}
else if(eventList.size()==1) {
assertTrue("1 record fetched", eventList.size()==1);
}
else if (eventList.size()>1) {
assertTrue("more than 1 record fetched", eventList.size()>1);
}
}

/**
* Test case to test the method checkEventsofVisitor
*/
@Test
public void testCheckEventsofVisitor() {
/**
* @TODO: Call checkEventsofVisitor and assert the returned type of this method
* for appropriate return type
*/  try {
visitor.setUserName("bsmith");
visitor.setPassword("password");
visitor.setFirstName("Bob");
visitor.setLastName("Smith");
visitor.setEmail("bsmith@email.com");
visitor.setPhoneNumber("748937487");
visitor.setVisitorId(1001);
boolean status= eventServiceImpl.checkEventsofVisitor(visitor, 1003, 10003);
assertTrue(status==true);

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

/**
* Test case to test the method updateEventDeletions
*/
@Test
public void testUpdateEventDeletions() {
/**
* @TODO: Call updateEventDeletions and assert the return type of this method
*/  
 try {
  connection=FERSDataConnection.createConnection();
  statement=connection.prepareStatement("SELECT SEATSAVAILABLE FROM EVENTSESSION WHERE EVENTID = ?");
  statement.setInt(1, 1001);
  resultSet= statement.executeQuery();
  int seatsbefore=0;
  while(resultSet.next())
  {
  seatsbefore=resultSet.getInt(1);
  }
  dao.updateEventDeletions(1001, 10001);
  connection=FERSDataConnection.createConnection();
  statement=connection.prepareStatement("SELECT SEATSAVAILABLE FROM EVENTSESSION WHERE EVENTID = ?");
  statement.setInt(1, 1001);
  resultSet=statement.executeQuery();
  int seatsafter=0;
  while(resultSet.next())
  {
  seatsafter=resultSet.getInt(1);
  }
  assertTrue("YES seats availabe before is 1 more then seats available after", seatsbefore+1==seatsafter);
  }catch(SQLException e) {
  e.printStackTrace();
  }catch(ClassNotFoundException e) {
  e.printStackTrace();
  }catch(Exception e) {
  e.printStackTrace();
  }
  }
 

/**
* Junit test case for getEventCoordinator
*/
@Test
public void testGetEventCoordinator() {
/**
* @TODO: Call getAllEventCoordinators and assert the size of return type of this method
*/
 try {
  showAllEvents1=dao.getEventCoordinator();
  if(showAllEvents1.size()==0) {
  assertTrue("No event found", showAllEvents1.size()==0);
  }
  else if(showAllEvents1.size()==1) {
  assertTrue("One event found", showAllEvents1.size()==1);
  }
  else if (showAllEvents1.size()>1) {
  assertTrue("more then one event found", showAllEvents1.size()>1);
  }
  }catch(SQLException e) {
  e.printStackTrace();
  }catch(ClassNotFoundException e) {
  e.printStackTrace();
  }catch(Exception e) {
  e.printStackTrace();
  }
}

/**
* Junit test case for getEvent
*/
@Test
public void testGetEvent()
{
event=eventServiceImpl.getEvent(1003, 10003);
 assertEquals(1003, event.getEventid());
  
}

/**
* Junit test case for updateEvent
*/
@Test
public void testInsertEvent() {
/**
* @TODO: Call insertEvent
* Create event object by setting appropriate values
* Assert the status of insertEvent method
*/
 try {
  Event event=new Event();
  //'1001','bsmith','password','Bob','Smith','bsmith@email.com','748937487'
  event.setName("bsmith");
  event.setDescription("jf");
  event.setPlace("ff");
  event.setDuration("h");
  event.setEventtype("gh");
  event.setEventid(1001);
  event.setSessionId(10001);
  int i=dao.insertEvent(event);
  assertTrue("Data inserted",i==1);
  }catch(ClassNotFoundException e) {
  e.printStackTrace();
  }
  catch (Exception e) {
  e.printStackTrace();
  }
  
}

/**
* Junit test case for updateEvent
*/


@Test
public void testUpdateEvent() {
 event=new Event();
 event.setName("gani");
 event.setDescription("jf");
 event.setPlace("ffdsff");
 event.setDuration("h");
 event.setEventtype("gh");
 event.setEventid(1003);
 int i= eventServiceImpl.updateEvent(event);
 assertTrue("Valid ", i==0);

}

/**
* Junit test case for deleteEvent
*/
@Ignore
@Test
public void testDeleteEvent()
{
/**
* @TODO: Fetch Event object by calling getAllEvents method
* Update event object by setting appropriate values
* Call deleteEvent method
* Assert the status of deleteEvent method
*/
  int i;
   i =eventServiceImpl.deleteEvent(1003, 10003);
   assertTrue(i==1);
}
}

Tags: