/*
* This file is part of LibrePlan
*
* Copyright (C) 2013 St. Antoniusziekenhuis
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.libreplan.importers;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.libreplan.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
import static org.libreplan.web.WebappGlobalNames.WEBAPP_SPRING_CONFIG_FILE;
import static org.libreplan.web.WebappGlobalNames.WEBAPP_SPRING_SECURITY_CONFIG_FILE;
import static org.libreplan.web.test.WebappGlobalNames.WEBAPP_SPRING_CONFIG_TEST_FILE;
import static org.libreplan.web.test.WebappGlobalNames.WEBAPP_SPRING_SECURITY_CONFIG_TEST_FILE;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import javax.annotation.Resource;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.libreplan.business.IDataBootstrap;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.IOnTransaction;
import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.orders.daos.IOrderDAO;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.scenarios.IScenarioManager;
import org.libreplan.business.scenarios.entities.OrderVersion;
import org.libreplan.business.scenarios.entities.Scenario;
import org.libreplan.importers.jira.IssueDTO;
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 JiraOrderElementSynchronizer }
*
* @author Miciele Ghiorghis <m.ghiorghis@antoniusziekenhuis.nl>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE,
WEBAPP_SPRING_SECURITY_CONFIG_FILE,
WEBAPP_SPRING_SECURITY_CONFIG_TEST_FILE })
public class JiraOrderElementSynchronizerTest {
@Resource
private IDataBootstrap defaultAdvanceTypesBootstrapListener;
@Resource
private IDataBootstrap scenariosBootstrap;
@Resource
private IDataBootstrap configurationBootstrap;
@Autowired
private IAdHocTransactionService transactionService;
@Autowired
private IConfigurationDAO configurationDAO;
@Autowired
private IScenarioManager scenarioManager;
private List<IssueDTO> issues;
@Autowired
private IOrderDAO orderDAO;
@Autowired
private IJiraOrderElementSynchronizer jiraOrderElementSynchronizer;
@Before
public void loadRequiredaData() {
IOnTransaction<Void> load = new IOnTransaction<Void>() {
@Override
public Void execute() {
defaultAdvanceTypesBootstrapListener.loadRequiredData();
configurationBootstrap.loadRequiredData();
scenariosBootstrap.loadRequiredData();
issues = getJiraIssues();
return null;
}
};
transactionService.runOnAnotherTransaction(load);
}
private List<IssueDTO> getJiraIssues() {
List<IssueDTO> issues = new ArrayList<IssueDTO>();
try {
Properties properties = loadProperties();
issues = JiraRESTClient.getIssues(properties.getProperty("url"),
properties.getProperty("username"),
properties.getProperty("password"),
JiraRESTClient.PATH_SEARCH,
getJiraLabel(properties.getProperty("label")));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return issues;
}
private Properties loadProperties() throws FileNotFoundException,
IOException {
String filename = System.getProperty("user.dir")
+ "/../scripts/jira-connector/jira-conn.properties";
Properties properties = new Properties();
properties.load(new FileInputStream(filename));
return properties;
}
private String getJiraLabel(String label) {
return "labels=" + label;
}
private Order givenOrder() {
return transactionService
.runOnAnotherTransaction(new IOnTransaction<Order>() {
@Override
public Order execute() {
return givenValidOrderAlreadyStored();
}
});
}
private Order givenValidOrderAlreadyStored() {
Order order = Order.create();
order.setCode(UUID.randomUUID().toString());
order.setName("Order name " + UUID.randomUUID());
order.setInitDate(new Date());
order.setCalendar(configurationDAO.getConfiguration()
.getDefaultCalendar());
OrderVersion version = setupVersionUsing(scenarioManager, order);
order.useSchedulingDataFor(version);
orderDAO.save(order);
orderDAO.flush();
try {
return orderDAO.find(order.getId());
} catch (InstanceNotFoundException e) {
return null;
}
}
private Order givenOrderWithValidOrderLines() {
return transactionService
.runOnAnotherTransaction(new IOnTransaction<Order>() {
@Override
public Order execute() {
return givenValidOrderWithValidOrderLinesAlreadyStored();
}
});
}
private Order givenValidOrderWithValidOrderLinesAlreadyStored() {
Order order = givenOrder();
jiraOrderElementSynchronizer.syncOrderElementsWithJiraIssues(issues, order);
order.dontPoseAsTransientObjectAnymore();
orderDAO.saveWithoutValidating(order);
orderDAO.flush();
try {
return orderDAO.find(order.getId());
} catch (InstanceNotFoundException e) {
return null;
}
}
private OrderVersion setupVersionUsing(IScenarioManager scenarioManager,
Order order) {
Scenario current = scenarioManager.getCurrent();
OrderVersion result = OrderVersion.createInitialVersion(current);
order.setVersionForScenario(current, result);
return result;
}
@Test
@Transactional
@Ignore("Only working if you have a JIRA server configured")
public void testSyncOrderElementsOfAnExistingOrderWithNoOrderLines() {
Order order = givenOrder();
jiraOrderElementSynchronizer.syncOrderElementsWithJiraIssues(issues, order);
assertEquals(order.getOrderElements().size(), issues.size());
assertTrue(order.getOrderElements().get(0).getHoursGroups().size() > 0);
assertTrue(!order.getAdvancePercentage().equals(BigDecimal.ZERO));
}
@Test
@Transactional
@Ignore("Only working if you have a JIRA server configured")
public void testReSyncOrderElementsOfAnExistingOrderWithOrderLines() {
Order order = givenOrderWithValidOrderLines();
Integer workingHours = order.getWorkHours();
jiraOrderElementSynchronizer.syncOrderElementsWithJiraIssues(issues, order);
assertEquals(order.getOrderElements().size(), issues.size());
assertEquals(workingHours.intValue(), order.getWorkHours().intValue());
}
}