ItEr35S11ArquitecturaServidorItEr34S11: Improvements to REST criterion service and (after changes in validation architecture) and a test case provided.

This commit is contained in:
Fernando Bellas Permuy 2009-11-18 21:25:07 +01:00 committed by Javier Moran Rua
parent 657ea4afd0
commit 3741d44f2a
3 changed files with 228 additions and 5 deletions

View file

@ -147,7 +147,7 @@ public final class CriterionConverter {
CriterionTypeDTO criterionTypeDTO) {
CriterionType criterionType = CriterionType.createUnvalidated(
criterionTypeDTO.name, criterionTypeDTO.description,
trim(criterionTypeDTO.name), trim(criterionTypeDTO.description),
criterionTypeDTO.allowHierarchy,
criterionTypeDTO.allowSimultaneousCriterionsPerResource,
criterionTypeDTO.enabled,
@ -194,11 +194,15 @@ public final class CriterionConverter {
CriterionDTO childDTO, CriterionType criterionType,
Criterion criterionParent) {
Criterion criterion = Criterion.createUnvalidated(childDTO.name,
Criterion criterion = Criterion.createUnvalidated(trim(childDTO.name),
criterionType, criterionParent, childDTO.active);
return criterion;
}
private static String trim(String s) {
return s == null ? null : s.trim();
}
}

View file

@ -20,8 +20,12 @@
package org.navalplanner.ws.resources.criterion.impl;
import static org.navalplanner.web.I18nHelper._;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@ -32,6 +36,7 @@ import javax.ws.rs.Produces;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.resources.daos.ICriterionTypeDAO;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.ws.common.api.ConstraintViolationDTO;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
import org.navalplanner.ws.common.impl.ConstraintViolationConverter;
@ -72,21 +77,79 @@ public class CriterionServiceREST implements ICriterionService {
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList =
new ArrayList<InstanceConstraintViolationsDTO>();
int instanceNumber = 1;
Set<String> criterionTypeNames = new HashSet<String>();
for (CriterionTypeDTO criterionTypeDTO :
criterionTypes.criterionTypes) {
CriterionType criterionType =
CriterionConverter.toEntity(criterionTypeDTO);
InstanceConstraintViolationsDTO instanceConstraintViolationsDTO =
null;
boolean criterionTypeNameRepeated =
criterionTypeNames.contains(criterionType.getName());
try {
criterionTypeDAO.save(criterionType);
/*
* "validate" is executed before "save", since "save" first
* adds the object to the underlying ORM session and then
* validates. So, if "validate" method is not called
* explicitly before "save", an invalid criterion type
* would be added to the underlying ORM session, causing
* the invalid criterion type to be added to the database
* when the ORM commits the transaction. As a side effect,
* validations are executed twice. Note also, that
* "CriterionType::checkConstraintUniqueCriterionTypeName"
* only checks if a criterion type with the same name already
* exists in the *database*, and that the criterion types
* being imported are inserted in the database when the
* transaction is committed. In consequence, we can only call
* "save" if the criterion type is valid according to "validate"
* method and its name is not used by another previously
* *imported* (not in the database yet) criterion type.
*/
criterionType.validate();
if (!criterionTypeNameRepeated) {
criterionTypeDAO.save(criterionType);
}
} catch (ValidationException e) {
instanceConstraintViolationsList.add(
instanceConstraintViolationsDTO =
ConstraintViolationConverter.toDTO(
generateInstanceId(instanceNumber,
criterionTypeDTO.name),
e.getInvalidValues()));
e.getInvalidValues());
}
/*
* If criterion type name is repeated, add it to the list of
* constraint violations.
*/
if (criterionTypeNameRepeated) {
if (instanceConstraintViolationsDTO == null) {
instanceConstraintViolationsDTO =
new InstanceConstraintViolationsDTO(
generateInstanceId(instanceNumber,
criterionTypeDTO.name),
new ArrayList<ConstraintViolationDTO>());
}
instanceConstraintViolationsDTO.constraintViolations.add(
new ConstraintViolationDTO(
CriterionType.class.getSimpleName() + ":name",
_("criterion type name is used by another criterion " +
"type being imported")));
} else {
if (criterionType.getName() != null) {
criterionTypeNames.add(criterionType.getName());
}
}
/*
* Add constraint violations of this criterion type to the returned
* list of constraint violations.
*/
if (instanceConstraintViolationsDTO != null) {
instanceConstraintViolationsList.add(
instanceConstraintViolationsDTO);
}
instanceNumber++;

View file

@ -0,0 +1,156 @@
package org.navalplanner.web.test.ws.resources.criterion.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
import static org.navalplanner.web.WebappGlobalNames.WEBAPP_SPRING_CONFIG_FILE;
import static org.navalplanner.web.test.WebappGlobalNames.WEBAPP_SPRING_CONFIG_TEST_FILE;
import java.util.ArrayList;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
import org.navalplanner.ws.resources.criterion.api.CriterionDTO;
import org.navalplanner.ws.resources.criterion.api.CriterionTypeDTO;
import org.navalplanner.ws.resources.criterion.api.CriterionTypeListDTO;
import org.navalplanner.ws.resources.criterion.api.ICriterionService;
import org.navalplanner.ws.resources.criterion.api.ResourceEnumDTO;
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;
/**
* Tests for <code>ICriterionService</code>.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE })
@Transactional
public class CriterionServiceTest {
@Autowired
private ICriterionService criterionService;
@BeforeClass
public static void populateDb() throws Throwable {
}
@AfterClass
public static void cleanDb() throws Throwable {
}
/**
* Tests <code>ICriterionService:addCriterionTypes</code> (indirectly,
* <code>ICriterionService:getCriterionTypes</code> is also tested).
*/
@Test
public void testAddCriterionTypes() {
/* Build criterion type "ct1" (4 constraint violations). */
CriterionDTO ct1c1 = new CriterionDTO(null, true, // Missing criterion
// name.
new ArrayList<CriterionDTO>());
CriterionDTO ct1c2c1 = new CriterionDTO("c2-1", true,
new ArrayList<CriterionDTO>());
List<CriterionDTO> ct1c2Criterions = new ArrayList<CriterionDTO>();
ct1c2Criterions.add(ct1c2c1);
CriterionDTO ct1c2 = new CriterionDTO("c2", true, // Criterion
// hierarchy is not
// allowed in the
// criterion type
// (see above).
ct1c2Criterions);
CriterionDTO ct1c3 = new CriterionDTO("c3", true,
new ArrayList<CriterionDTO>());
CriterionDTO ct1c4 = new CriterionDTO("c3", true,
new ArrayList<CriterionDTO>()); // Repeated criterion name.
List<CriterionDTO> ct1Criterions = new ArrayList<CriterionDTO>();
ct1Criterions.add(ct1c1);
ct1Criterions.add(ct1c2);
ct1Criterions.add(ct1c3);
ct1Criterions.add(ct1c4);
CriterionTypeDTO ct1 = new CriterionTypeDTO(null, "ct-1 desc",
false, true, true, ResourceEnumDTO.RESOURCE, // Missing criterion
ct1Criterions); // type name.
/* Build criterion type "ct2" (2 constraint violations). */
CriterionDTO ct2c1 = new CriterionDTO("c1", true, // Its criterion type
new ArrayList<CriterionDTO>()); // is not enabled.
CriterionDTO ct2c2c1 = new CriterionDTO("c2-1",
true, new ArrayList<CriterionDTO>()); // Its criterion father is
// not active.
List<CriterionDTO> ct2c2Criterions = new ArrayList<CriterionDTO>();
ct2c2Criterions.add(ct2c2c1);
CriterionDTO ct2c2 = new CriterionDTO("c2", false,
ct2c2Criterions);
List<CriterionDTO> ct2Criterions = new ArrayList<CriterionDTO>();
ct2Criterions.add(ct2c1);
ct2Criterions.add(ct2c2);
CriterionTypeDTO ct2 = new CriterionTypeDTO("ct-2", "ct-2 desc",
true, true, false, ResourceEnumDTO.RESOURCE, ct2Criterions);
/* Build criterion type "ct3" (OK). */
CriterionDTO ct3c1 = new CriterionDTO("c1", true,
new ArrayList<CriterionDTO>());
CriterionDTO ct3c2c1 = new CriterionDTO("c2-1",
true, new ArrayList<CriterionDTO>());
List<CriterionDTO> ct3c2Criterions = new ArrayList<CriterionDTO>();
ct3c2Criterions.add(ct3c2c1);
CriterionDTO ct3c2 = new CriterionDTO("c2", true,
ct3c2Criterions);
List<CriterionDTO> ct3Criterions = new ArrayList<CriterionDTO>();
ct3Criterions.add(ct3c1);
ct3Criterions.add(ct3c2);
CriterionTypeDTO ct3 = new CriterionTypeDTO("ct-3", "ct-3 desc",
true, true, true, ResourceEnumDTO.RESOURCE, ct3Criterions);
/* Build criterion type "ct4" (2 constraint violations). */
CriterionDTO ct4c1 = new CriterionDTO(null, true, // Missing criterion
new ArrayList<CriterionDTO>()); // name.
CriterionDTO ct4c2 = new CriterionDTO("c2", true,
new ArrayList<CriterionDTO>());
List<CriterionDTO> ct4Criterions = new ArrayList<CriterionDTO>();
ct4Criterions.add(ct4c1);
ct4Criterions.add(ct4c2);
CriterionTypeDTO ct4 = new CriterionTypeDTO("ct-3", // Repeated
"ct-4 desc", true, true, true, // criterion type
ResourceEnumDTO.RESOURCE, ct4Criterions); // name.
/* Criterion type list. */
List<CriterionTypeDTO> criterionTypes =
new ArrayList<CriterionTypeDTO>();
criterionTypes.add(ct1);
criterionTypes.add(ct2);
criterionTypes.add(ct3);
criterionTypes.add(ct4);
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList =
criterionService.addCriterionTypes(
new CriterionTypeListDTO(criterionTypes)).
instanceConstraintViolationsList;
assertTrue(instanceConstraintViolationsList.size() == 3);
assertTrue(instanceConstraintViolationsList.get(0).
constraintViolations.size() == 4);
assertTrue(instanceConstraintViolationsList.get(1).
constraintViolations.size() == 2);
assertTrue(instanceConstraintViolationsList.get(2).
constraintViolations.size() == 2);
/* Find criterion types. */
List<CriterionTypeDTO> returnedCriterionTypes =
criterionService.getCriterionTypes().criterionTypes;
assertTrue(returnedCriterionTypes.size() == 1);
assertEquals(returnedCriterionTypes.get(0).name, "ct-3");
}
}