/**
* TestCube is an enterprise Test management tool.
* Copyright (C) 2011 JatakaSource Ltd.
*
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TestCube 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TestCube. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jatakasource.testcube.web.controller.testcase;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jatakasource.common.model.security.UserPojo;
import org.jatakasource.common.svc.CRUDService;
import org.jatakasource.testcube.model.testcase.ITestCase;
import org.jatakasource.testcube.model.testcase.TestCase;
import org.jatakasource.testcube.model.testplan.ITestPlan;
import org.jatakasource.testcube.model.testplan.TestPlanPojo;
import org.jatakasource.testcube.svc.security.UserService;
import org.jatakasource.testcube.svc.testcase.TestCaseService;
import org.jatakasource.testcube.svc.testplan.TestPlanService;
import org.jatakasource.testcube.web.controller.ApplicationMessagesController;
import org.jatakasource.testcube.web.controller.GeneralProperties;
import org.jatakasource.testcube.web.xml.testcase.TestCaseGridParameters;
import org.jatakasource.testcube.web.xml.testcase.TestCaseRendered;
import org.jatakasource.testcube.web.xml.testcase.TestCaseRenderedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestCaseController extends ApplicationMessagesController<ITestCase, Long> {
private static final Logger logger = Logger.getLogger(TestCaseController.class);
private static final String PARAMETER_TESTCASE_ID = "PARAMETER_TESTCASE_ID";
private static final String PARAMETER_TESTPLAN_ID = "PARAMETER_TESTPLAN_ID";
@Autowired
private TestCaseService testCaseService;
@Autowired
private TestPlanService testPlanService;
@Autowired
private UserService userService;
@Override
protected CRUDService<ITestCase, Long> getCrudService() {
return testCaseService;
}
@Override
protected String getModelName() {
return getMessages().getMessage(TestCaseProperties.class.getName() + "." + TestCaseProperties.MODEL_NAME.name());
}
@RequestMapping(value = PROTECTED_SERVICE + "/testcase/search")
protected ModelAndView search(@RequestParam(value = GRID_PARAMETERS, required = true) String xmlGridParameters) {
logger.trace("Request for search test-cases with parameters " + xmlGridParameters + "!!!");
TestCaseGridParameters gridParameters = null;
String keyword = null;
Long productId = null;
Long testPlanId = null;
if (StringUtils.isNotEmpty(xmlGridParameters)) {
gridParameters = getGridParameters(xmlGridParameters, TestCaseGridParameters.class);
keyword = gridParameters.getKeyword();
productId = gridParameters.getProductId();
testPlanId = gridParameters.getTestPlanId();
}
PagingAndSorting pagingAndSorting = getPagingAndSorting(gridParameters);
// Return all cases according to keyword search
List<ITestCase> cases = testCaseService.getAll(pagingAndSorting.getPaging(), pagingAndSorting.getSorting(), keyword, productId, testPlanId);
// Convert all cases to TestCaseRendered
TestCaseRenderedList testCaseRenderedList = getAsRenderer(cases, TestCaseRendered.class, ITestCase.class, TestCaseRenderedList.class, gridParameters);
return getXMLViewer(testCaseRenderedList);
}
@Override
@RequestMapping(value = PROTECTED_SERVICE + "/testcase/delete")
protected ModelAndView delete(@RequestParam(value = CRUD_PARAMETERS, required = true) Long id) {
return super.delete(id);
}
@RequestMapping(value = PROTECTED_SERVICE + "/testcase/update")
protected ModelAndView update(@RequestParam(value = CRUD_PARAMETERS, required = true) String xmlCrudParameters) {
return super.update(xmlCrudParameters, TestCaseRendered.class, TestCase.class);
}
@RequestMapping(value = PROTECTED_SERVICE + "/testcase/create")
protected ModelAndView create(@RequestParam(value = CRUD_PARAMETERS, required = true) String xmlCrudParameters) {
return super.create(xmlCrudParameters, TestCaseRendered.class, TestCase.class);
}
@RequestMapping(value = PROTECTED_SERVICE + "/testcase/copy")
protected ModelAndView copy(@RequestParam(value = PARAMETER_TESTCASE_ID, required = true) Long testCaseId,
@RequestParam(value = PARAMETER_TESTPLAN_ID, required = true) Long testPlanId) {
// Get requested testcase
ITestCase testCase = testCaseService.get(testCaseId);
if (testCase == null)
return getXMLViewer(getError(getMessages().getMessage(GeneralProperties.class.getName() + "." + GeneralProperties.ERROR_TITLE.name()),
getMessages().getMessage(TestCaseProperties.class.getName() + "." + TestCaseProperties.COPY_MISSING_TESTCASE.name(), getModelName()), testCaseId.toString()));
// Get requested testplan
ITestPlan testPlan = testPlanService.get(testPlanId);
if (testPlan == null)
return getXMLViewer(getError(getMessages().getMessage(GeneralProperties.class.getName() + "." + GeneralProperties.ERROR_TITLE.name()),
getMessages().getMessage(TestCaseProperties.class.getName() + "." + TestCaseProperties.COPY_MISSING_TESTPLAN.name(), getModelName()), testPlanId.toString()));
testCase.setTestPlan((TestPlanPojo) testPlan);
// Verify testcase name is unique in testPlan.
// If not unique: add prefix to testcase name and test again.
while (true){
if (!testCaseService.isNameUniqe(testCase, testPlan))
testCase.setName(getMessages().getMessage(TestCaseProperties.class.getName() + "." + TestCaseProperties.COPY_NAME_PREFIX.name()) + testCase.getName());
else
break;
}
testCase.setCreatedBy((UserPojo) userService.getCurrentUser());
testCase.setCreatedDate(new Date());
return super.copy(testCase);
}
@Override
protected ITestCase beforeCreate(ITestCase model) {
return setDate(setUser(model));
}
private ITestCase setUser(ITestCase testCase) {
testCase.setCreatedBy((UserPojo) userService.getCurrentUser());
return testCase;
}
private ITestCase setDate(ITestCase testCase) {
testCase.setCreatedDate(new Date());
return testCase;
}
}