package it.test;
import it.cdq.MainService;
import it.cdq.model.db.Trip;
import it.cdq.service.CodoquService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
@TransactionConfiguration(defaultRollback = true)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/context.xml")
public class ServiceTest {
static Logger logger = Logger.getLogger(MainService.class);
@Resource(name = CodoquService.QUALIFIER)
private CodoquService codoquService;
@Test
public void generatesUpcomingTripsListTest() {
List<Trip> list = codoquService.generatesUpcomingTripsList();
Assert.assertTrue(list.size() > 0);
}
@Test
public void persistOnlyNewTripListTest() {
List<Trip> list = codoquService.generatesUpcomingTripsList();
for (Trip trip : list) {
logger.info(trip);
}
int max = list.size();
codoquService.persistOnlyNewTripList(list);
Assert.assertTrue(codoquService.getUnprocessedElements().size() == max);
codoquService.persistOnlyNewTripList(list);
Assert.assertTrue(codoquService.getUnprocessedElements().size() == max);
}
@Test
public void consumeAllTest() {
List<Trip> upcomingTripsList = codoquService.generatesUpcomingTripsList();
codoquService.persistOnlyNewTripList(upcomingTripsList);
List<Trip> unprocessedElements = codoquService.getUnprocessedElements();
Assert.assertTrue(unprocessedElements.size() == upcomingTripsList.size());
String operator = "RYANAIR";
List<Trip> filtered = new ArrayList<Trip>();
for (Trip t : unprocessedElements) {
if (t.getArrival().getOperator().equals(operator)) {
filtered.add(t);
}
}
while (true) {
Trip trip = codoquService.getNextElement(operator);
if (trip == null)
break;
}
Assert
.assertTrue(codoquService.getUnprocessedElements().size() == upcomingTripsList
.size() - filtered.size());
}
}