From 1ef14738ce46d48e106cbe29493fae2b11d33c76 Mon Sep 17 00:00:00 2001 From: Manuel Rego Casasnovas Date: Thu, 22 Apr 2010 16:17:31 +0200 Subject: [PATCH] ItEr55S08RFInfraestucturaEscenariosItEr54S10: Very simple test for method getTasksByOrder at OrderDAO. NOTE: This test will fail in the scenarios branch, so the method in OrderDAO will be modified there. --- .../test/orders/daos/OrderDAOTest.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 navalplanner-business/src/test/java/org/navalplanner/business/test/orders/daos/OrderDAOTest.java diff --git a/navalplanner-business/src/test/java/org/navalplanner/business/test/orders/daos/OrderDAOTest.java b/navalplanner-business/src/test/java/org/navalplanner/business/test/orders/daos/OrderDAOTest.java new file mode 100644 index 000000000..6c09df4c4 --- /dev/null +++ b/navalplanner-business/src/test/java/org/navalplanner/business/test/orders/daos/OrderDAOTest.java @@ -0,0 +1,138 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.business.test.orders.daos; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE; +import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE; + +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import javax.annotation.Resource; + +import org.hibernate.SessionFactory; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.navalplanner.business.IDataBootstrap; +import org.navalplanner.business.common.daos.IConfigurationDAO; +import org.navalplanner.business.common.exceptions.ValidationException; +import org.navalplanner.business.orders.daos.IOrderDAO; +import org.navalplanner.business.orders.daos.OrderDAO; +import org.navalplanner.business.orders.entities.HoursGroup; +import org.navalplanner.business.orders.entities.Order; +import org.navalplanner.business.orders.entities.OrderLine; +import org.navalplanner.business.orders.entities.TaskSource; +import org.navalplanner.business.orders.entities.TaskSource.TaskSourceSynchronization; +import org.navalplanner.business.planner.daos.ITaskSourceDAO; +import org.navalplanner.business.planner.entities.Task; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +/** + * Test for {@link OrderDAO}. + * + * @author Manuel Rego Casasnovas + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE, + BUSINESS_SPRING_CONFIG_TEST_FILE }) +@Transactional +public class OrderDAOTest { + + @Resource + private IDataBootstrap defaultAdvanceTypesBootstrapListener; + + @Resource + private IDataBootstrap configurationBootstrap; + + @Before + public void loadRequiredaData() { + defaultAdvanceTypesBootstrapListener.loadRequiredData(); + configurationBootstrap.loadRequiredData(); + } + + @Autowired + private IOrderDAO orderDAO; + + @Autowired + private ITaskSourceDAO taskSourceDAO; + + @Autowired + private IConfigurationDAO configurationDAO; + + @Autowired + private SessionFactory sessionFactory; + + private Order order; + + private Task createValidTask() { + HoursGroup associatedHoursGroup = new HoursGroup(); + associatedHoursGroup.setCode("hours-group-code-" + UUID.randomUUID()); + OrderLine orderLine = createOrderLine(); + orderLine.addHoursGroup(associatedHoursGroup); + TaskSource taskSource = TaskSource.create(orderLine, Arrays + .asList(associatedHoursGroup)); + TaskSourceSynchronization mustAdd = TaskSource.mustAdd(taskSource); + mustAdd.apply(taskSourceDAO); + Task task = (Task) taskSource.getTask(); + return task; + } + + private OrderLine createOrderLine() { + OrderLine orderLine = OrderLine.create(); + orderLine.setName("bla"); + orderLine.setCode("code-" + UUID.randomUUID()); + HoursGroup hoursGroup = new HoursGroup(); + hoursGroup.setCode("hours-group-code-" + UUID.randomUUID()); + orderLine.addHoursGroup(hoursGroup); + order = Order.create(); + order.setName("bla"); + order.setInitDate(new Date()); + order.setCode("code-" + UUID.randomUUID()); + order.add(orderLine); + order.setCalendar(configurationDAO.getConfiguration() + .getDefaultCalendar()); + try { + orderDAO.save(order); + sessionFactory.getCurrentSession().flush(); + } catch (ValidationException e) { + throw new RuntimeException(e); + } + return orderLine; + } + + @Test + public void testGetTasksByOrder() { + Task task = createValidTask(); + List tasks = orderDAO.getTasksByOrder(order); + + assertThat(tasks.size(), equalTo(1)); + assertThat(tasks.get(0).getId(), equalTo(task.getId())); + } + +}