Commit 0f6e1048 authored by Matija Obreza's avatar Matija Obreza

Merge branch '374-validated-to-subsets' into 'master'

Resolve "@Validated to Subsets"

Closes #374

See merge request genesys-pgr/genesys-server!290
parents 0337d424 111d7321
......@@ -80,7 +80,7 @@
<querydsl.version>4.1.4</querydsl.version>
<hibernate.version>4.3.11.Final</hibernate.version>
<hibernate.validator.version>4.3.2.Final</hibernate.validator.version>
<hibernate.validator.version>5.2.5.Final</hibernate.validator.version>
<hsqldb.version>2.3.6</hsqldb.version>
<ehcache.version>2.7.4</ehcache.version>
......@@ -624,6 +624,11 @@
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
......
......@@ -16,6 +16,8 @@
package org.genesys.catalog.model.dataset;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Set;
......@@ -70,6 +72,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "partnerId", updatable = false)
@JsonView({ JsonViews.Public.class })
@NotNull
private Partner owner;
/** The accession identifiers. */
......@@ -112,10 +115,12 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
private List<DatasetLocation> locations;
/** The min start date of all locations */
@Size(max = 8)
@Column(length = 8)
private String startDate;
/** The max end date of all locations */
@Size(max = 8)
@Column(length = 8)
private String endDate;
......@@ -176,6 +181,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
private String source;
/** Information about time of creation of the dataset. */
@Size(max = 200)
@Column(nullable = true, length = 200)
private String created;
......
......@@ -21,6 +21,8 @@ import org.genesys.catalog.annotations.PublishValidation;
import org.genesys.catalog.service.PublishValidationInterface;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Map;
/**
......@@ -57,6 +59,8 @@ public class DatasetCreator extends UuidModel implements PublishValidationInterf
/** The full name. */
@PublishValidation
@NotNull
@Size(max = 200)
@Column(length = 200, nullable = false)
private String fullName;
......
......@@ -22,6 +22,7 @@ import org.genesys.catalog.service.PublishValidationInterface;
import org.genesys2.util.MCPDUtil;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.Map;
/**
......@@ -68,14 +69,17 @@ public class DatasetLocation extends UuidModel implements PublishValidationInter
private Double decimalLongitude;
/** ISO3 country code of the location */
@Size(max = 3)
@Column(length = 3)
private String countryCode;
/** The start date */
@Size(max = 8)
@Column(length = 8)
private String startDate;
/** The end date */
@Size(max = 8)
@Column(length = 8)
private String endDate;
......
......@@ -40,6 +40,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
/**
* The Interface DatasetService.
*/
......@@ -54,7 +56,7 @@ public interface DatasetService {
* @param input new Dataset
* @return saved Dataset in db.
*/
Dataset createDataset(Dataset input);
Dataset createDataset(@Valid Dataset input);
/**
* Method updating Dataset.
......@@ -62,7 +64,7 @@ public interface DatasetService {
* @param input new Dataset
* @return updated Dataset in db.
*/
Dataset updateDataset(Dataset input);
Dataset updateDataset(@Valid Dataset input);
/**
* Method updating accessionRefs in Dataset.
......@@ -71,7 +73,7 @@ public interface DatasetService {
* @param accessionRefs new accessionRefs
* @return updated Dataset in db.
*/
Dataset updateAccessionRefs(Dataset dataset, Set<AccessionRef> accessionRefs);
Dataset updateAccessionRefs(Dataset dataset, @Valid Set<AccessionRef> accessionRefs);
/**
* Method adding new descriptor to Dataset.
......@@ -219,7 +221,7 @@ public interface DatasetService {
* @return updated Dataset
* @throws NotFoundElement the not found element
*/
Dataset upsertAccessions(Dataset dataset, Set<AccessionRef> accessionRefs) throws NotFoundElement;
Dataset upsertAccessions(Dataset dataset, @Valid Set<AccessionRef> accessionRefs) throws NotFoundElement;
/**
* Puts the dataset into the Review state to be reviewed by admin.
......@@ -293,7 +295,7 @@ public interface DatasetService {
* @return created DatasetCreator
* @throws NotFoundElement throws if don't match the version
*/
DatasetCreator createDatasetCreator(Dataset dataset, DatasetCreator input) throws NotFoundElement;
DatasetCreator createDatasetCreator(Dataset dataset, @Valid DatasetCreator input) throws NotFoundElement;
/**
* Remove DatasetCreator of Dataset.
......@@ -349,7 +351,7 @@ public interface DatasetService {
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
DatasetCreator updateDatasetCreator(Dataset dataset, DatasetCreator datasetCreator) throws NotFoundElement;
DatasetCreator updateDatasetCreator(Dataset dataset, @Valid DatasetCreator datasetCreator) throws NotFoundElement;
/**
* Autocomplete creators.
......@@ -368,7 +370,7 @@ public interface DatasetService {
* @return added DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation createLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
DatasetLocation createLocation(Dataset dataset, @Valid DatasetLocation input) throws NotFoundElement;
/**
* Method for remove DatasetLocation of Dataset.
......@@ -424,7 +426,7 @@ public interface DatasetService {
* @return updated location
* @throws NotFoundElement the not found element
*/
DatasetLocation updateLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
DatasetLocation updateLocation(Dataset dataset, @Valid DatasetLocation input) throws NotFoundElement;
/**
* Clear accession references across all Datasets for specified accessions.
......
......@@ -85,17 +85,21 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import javax.validation.Valid;
/**
* The Class DatasetServiceImpl.
*/
@Service("catalogDatasetService")
@Transactional(readOnly = true)
@Validated
public class DatasetServiceImpl implements DatasetService {
/** The dataset versions repository. */
......@@ -151,7 +155,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#source.owner, 'write')")
public Dataset createDataset(final Dataset source) {
public Dataset createDataset(@Valid final Dataset source) {
LOG.info("Create Dataset. Input data {}", source);
final DatasetVersions datasetVersions = new DatasetVersions();
datasetVersionsRepository.save(datasetVersions);
......@@ -199,7 +203,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#input, 'write')")
public Dataset updateDataset(final Dataset input) {
public Dataset updateDataset(@Valid final Dataset input) {
LOG.info("Update Dataset. Input data {}", input);
final Dataset dataset = loadDataset(input);
......@@ -224,7 +228,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset updateAccessionRefs(Dataset dataset, final Set<AccessionRef> accessionRefs) {
public Dataset updateAccessionRefs(Dataset dataset, @Valid final Set<AccessionRef> accessionRefs) {
LOG.info("Update AccessionIdentifiers. Input dataset: {}, input accessionRefs {}", dataset, accessionRefs);
dataset = loadDataset(dataset);
if (dataset.getAccessionRefs() != null && !dataset.getAccessionRefs().isEmpty()) {
......@@ -538,7 +542,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset upsertAccessions(final Dataset dataset, final Set<AccessionRef> accessionRefs) throws NotFoundElement {
public Dataset upsertAccessions(final Dataset dataset, @Valid final Set<AccessionRef> accessionRefs) throws NotFoundElement {
final Dataset loadedDataset = datasetRepository.findByUuidAndVersion(dataset.getUuid(), dataset.getVersion());
if (loadedDataset == null) {
throw new NotFoundElement("Dataset doesn't exist");
......@@ -699,7 +703,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetCreator createDatasetCreator(Dataset dataset, final DatasetCreator input) throws NotFoundElement {
public DatasetCreator createDatasetCreator(Dataset dataset, @Valid final DatasetCreator input) throws NotFoundElement {
dataset = loadDataset(dataset);
input.setDataset(dataset);
return datasetCreatorRepository.save(input);
......@@ -774,7 +778,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetCreator updateDatasetCreator(final Dataset dataset, final DatasetCreator input) throws NotFoundElement {
public DatasetCreator updateDatasetCreator(final Dataset dataset, @Valid final DatasetCreator input) throws NotFoundElement {
final DatasetCreator datasetCreator = loadDatasetCreator(input);
if (!datasetCreator.getDataset().getUuid().equals(dataset.getUuid())) {
throw new InvalidApiUsageException("Creator does not belong to dataset");
......@@ -932,7 +936,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetLocation createLocation(Dataset dataset, final DatasetLocation input) throws NotFoundElement {
public DatasetLocation createLocation(Dataset dataset, @Valid final DatasetLocation input) throws NotFoundElement {
dataset = datasetRepository.findByUuidAndVersion(dataset.getUuid(), dataset.getVersion());
LOG.info("Create DatasetLocation {} for dataset {}", input, dataset.getUuid());
input.setDataset(dataset);
......@@ -1027,7 +1031,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetLocation updateLocation(Dataset dataset, final DatasetLocation input) throws NotFoundElement {
public DatasetLocation updateLocation(Dataset dataset, @Valid final DatasetLocation input) throws NotFoundElement {
dataset = loadDataset(dataset);
final DatasetLocation datasetLocation = loadLocation(input);
if (!datasetLocation.getDataset().getUuid().equals(dataset.getUuid())) {
......
......@@ -18,12 +18,15 @@ package org.genesys2.server.api;
import java.io.EOFException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.genesys.blocks.security.NotUniqueUserException;
import org.genesys.filerepository.FileRepositoryException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.ConcurrencyFailureException;
......@@ -110,6 +113,31 @@ public class ApiExceptionHandler {
return new ApiError<>(e);
}
/**
* Handle javax validation error.
*
* @param e the e
* @param request the request
* @return the api error
*/
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
public ApiError<Exception> handleValidationError(final ConstraintViolationException e, final HttpServletRequest request) {
final StringBuilder message = new StringBuilder("Validation failed: ");
if (e.getConstraintViolations() != null) {
for (ConstraintViolation cv : e.getConstraintViolations()) {
final PathImpl path = (PathImpl) cv.getPropertyPath();
message.append(path.getLeafNode().getName());
message.append(" -> ");
message.append(cv.getMessage());
message.append("; ");
}
}
LOG.warn("{} for {} {}", message.toString(), request.getMethod(), request.getRequestURL(), e);
return new ApiError<>(new Exception(message.toString(), e));
}
/**
* Handle converter error.
*
......
......@@ -22,6 +22,8 @@ import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.genesys.blocks.model.JsonViews;
import org.genesys2.server.model.impl.AccessionIdentifier3;
......@@ -43,22 +45,30 @@ public class AccessionRef implements AccessionIdentifier3, Serializable {
private static final long serialVersionUID = 1L;
/** The DOI. */
@Size(max = 100)
@Column(length = 100)
private String doi;
/** The inst code. */
@NotNull
@Size(max = 10)
@Column(length = 10, nullable = false)
private String instCode;
/** The acce numb. */
@NotNull
@Size(max = 50)
@Column(nullable = false, length = 50)
private String acceNumb;
/** The genus. */
@NotNull
@Size(max = 100)
@Column(nullable = false, length = 100)
private String genus;
/** The species. */
@Size(max = 100)
@Column(nullable = true, length = 100)
private String species;
......
......@@ -35,6 +35,8 @@ import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
......@@ -61,6 +63,8 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
private static final long serialVersionUID = 7021405309572916429L;
/** The title. */
@NotNull
@Size(max = 250)
@Column(length = 250, nullable = false)
protected String title;
......@@ -71,15 +75,18 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
protected String description;
/** The publisher. */
@Size(max = 250)
@Column(length = 250)
protected String publisher;
/** The date created. */
@Size(max = 100)
@Column(length = 100)
protected String dateCreated;
/** The wiews code. */
@NotNull
@Size(max = 8)
@Column(name = "wiews_code", length = 8, nullable = false)
private String wiewsCode;
......@@ -99,6 +106,7 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
private Set<AccessionRef> accessionRefs;
/** The accession count. */
@NotNull
@Column(name = "accession_count", nullable = false)
private int accessionCount = 0;
......@@ -120,10 +128,12 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
private Set<String> crops;
/** The date when subset was authored. */
@Size(max = 8)
@Column(length = 8)
private String date;
/** The source. */
@Size(max = 200)
@Column(length = 200)
private String source;
......
......@@ -21,6 +21,8 @@ import org.genesys.catalog.annotations.PublishValidation;
import org.genesys.catalog.service.PublishValidationInterface;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Map;
/**
......@@ -57,6 +59,8 @@ public class SubsetCreator extends UuidModel implements PublishValidationInterfa
/** The full name. */
@PublishValidation
@NotNull
@Size(max = 200)
@Column(length = 200, nullable = false)
private String fullName;
......
......@@ -28,6 +28,8 @@ import org.genesys2.server.model.kpi.Observation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import javax.validation.Valid;
public interface KPIService {
KPIParameter save(KPIParameter parameter);
......@@ -38,7 +40,7 @@ public interface KPIService {
KPIParameter delete(KPIParameter parameter);
<T extends Dimension<?>> T save(T dimension);
<T extends Dimension<?>> T save(@Valid T dimension);
Dimension<?> getDimension(long id);
......@@ -48,7 +50,7 @@ public interface KPIService {
<T> Set<T> getValues(Dimension<T> loadedJpa);
Execution save(Execution execution);
Execution save(@Valid Execution execution);
Execution getExecution(long id);
......
......@@ -31,6 +31,8 @@ import org.genesys2.server.service.filter.SubsetFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import javax.validation.Valid;
/**
* @author Maxym Borodenko
*/
......@@ -78,7 +80,7 @@ public interface SubsetService {
* @param source the source
* @return saved Subset in db.
*/
Subset create(FaoInstitute institute, Subset source);
Subset create(FaoInstitute institute, @Valid Subset source);
/**
* Load subset based on identifiers and version provided in the input.
......@@ -111,7 +113,7 @@ public interface SubsetService {
* @param subset new Subset
* @return updated Subset in db.
*/
Subset update(Subset subset);
Subset update(@Valid Subset subset);
/**
* Remove subset.
......@@ -175,7 +177,7 @@ public interface SubsetService {
* @return created SubsetCreator
* @throws NotFoundElement throws if don't match the version
*/
SubsetCreator createSubsetCreator(Subset Subset, SubsetCreator input) throws NotFoundElement;
SubsetCreator createSubsetCreator(Subset Subset, @Valid SubsetCreator input) throws NotFoundElement;
/**
* Remove SubsetCreator of Subset.
......@@ -241,7 +243,7 @@ public interface SubsetService {
* @return the Subset creator
* @throws NotFoundElement the not found element
*/
SubsetCreator updateSubsetCreator(Subset Subset, SubsetCreator SubsetCreator) throws NotFoundElement;
SubsetCreator updateSubsetCreator(Subset Subset, @Valid SubsetCreator SubsetCreator) throws NotFoundElement;
/**
* Autocomplete creators.
......
......@@ -52,6 +52,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
......@@ -83,6 +84,7 @@ public class AccessionServiceImpl implements AccessionService {
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Lazy
@Autowired
private DatasetService datasetService;
......
......@@ -60,6 +60,7 @@ import org.springframework.validation.annotation.Validated;
@Service
@Transactional(readOnly = true)
@Validated
public class KPIServiceImpl implements KPIService {
public static final Logger LOG = LoggerFactory.getLogger(KPIServiceImpl.class);
......@@ -118,7 +119,6 @@ public class KPIServiceImpl implements KPIService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dimension, 'ADMINISTRATION')")
@Override
@Transactional
@Validated
public <T extends Dimension<?>> T save(@Valid T dimension) {
LOG.debug("Persising dimension {}", dimension);
return lazyLoad(dimensionRepository.save(dimension));
......@@ -174,7 +174,6 @@ public class KPIServiceImpl implements KPIService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#execution, 'ADMINISTRATION')")
@Override
@Transactional
@Validated
public Execution save(@Valid Execution execution) {
Execution target = executionRepository.findByName(execution.getName());
if (target == null) {
......
......@@ -65,10 +65,13 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import javax.validation.Valid;
/**
* The Class SubsetServiceImpl.
*
......@@ -76,6 +79,7 @@ import com.querydsl.core.types.Predicate;
*/
@Service
@Transactional(readOnly = true)
@Validated
public class SubsetServiceImpl implements SubsetService {
private static final Logger LOG = LoggerFactory.getLogger(SubsetServiceImpl.class);
......@@ -150,7 +154,7 @@ public class SubsetServiceImpl implements SubsetService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE')")
public Subset create(final FaoInstitute institute, final Subset source) {
public Subset create(final FaoInstitute institute, @Valid final Subset source) {
LOG.info("Create Subset.");
if (! StringUtils.equals(institute.getCode(), source.getWiewsCode())) {
throw new InvalidApiUsageException("Institute code of the subset does not match the code of Institute");
......@@ -399,7 +403,7 @@ public class SubsetServiceImpl implements SubsetService {
@Transactional
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#subset, 'write')")
public SubsetCreator createSubsetCreator(Subset subset, SubsetCreator input) throws NotFoundElement {
public SubsetCreator createSubsetCreator(Subset subset, @Valid SubsetCreator input) throws NotFoundElement {
subset = loadSubset(subset);
input.setSubset(subset);
return subsetCreatorRepository.save(input);
......@@ -470,7 +474,7 @@ public class SubsetServiceImpl implements SubsetService {
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#subset, 'write')")
public SubsetCreator updateSubsetCreator(Subset subset, SubsetCreator input) throws NotFoundElement {
public SubsetCreator updateSubsetCreator(Subset subset, @Valid SubsetCreator input) throws NotFoundElement {
final SubsetCreator subsetCreator = loadSubsetCreator(input);
if (!subsetCreator.getSubset().getUuid().equals(subset.getUuid())) {
throw new InvalidApiUsageException("Creator does not belong to subset");
......
......@@ -42,6 +42,7 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
@Configuration
@Import({ HazelcastConfig.class, CommonConfig.class, SchedulerConfig.class, DatabaseConfig.class, TemplatingConfig.class, MailConfig.class, OAuth2ServerConfig.class, SecurityConfig.class, CacheConfig.class,
......@@ -127,4 +128,9 @@ public class ApplicationConfig {
// This provides static access to current application context
return new CurrentApplicationContext();
}
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
}
......@@ -61,6 +61,8 @@ import org.springframework.security.test.context.support.WithMockUser;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import javax.validation.ConstraintViolationException;
@WithMockUser(username = "admin", password = "admin", roles = "ADMINISTRATOR")
public class DatasetServiceTest extends AbstractDatasetServiceTest {
......@@ -73,6 +75,16 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(input.getVersions().getCurrentVersion(), is(input));
}
/**
* Testing javax validation.
* Fail to create dataset when owner = null
*/
@Test(expected = ConstraintViolationException.class)
public void testCreateDatasetWithFailedValidation() {
// Try to create dataset without owner. Should be an error
buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, null, PublishState.DRAFT);
}
@Test
public void testCreateDatasetSelfCleaning() {
Dataset input = buildAndSaveDataset(DATASET_TITLE_1 + " ", " " + DATASET_DESCRIPTION_1, partner, PublishState.DRAFT);
......
......@@ -26,6 +26,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import javax.validation.ConstraintViolationException;
public class SubsetServiceTest extends CatalogServiceTest {
@Autowired
......@@ -94,6 +96,19 @@ public class SubsetServiceTest extends CatalogServiceTest {
assertThat(result.getEmail(), equalTo(newEmail));
}
/**
* Testing javax validation.
* Fail to update subset when title = null
*/
@Test(expected = ConstraintViolationException.class)
public void testUpdateSubsetWithFailedValidation() {
assertThat(subset.getTitle(), notNullValue());
subset.setTitle(null);
// title cannot be null
subsetService.update(subset);
}
@Test
public void testLoadMoreAccessions() {
final Accession accession1 = upsertAccession("instCode1", "acceNumb1", "genus1");
......@@ -196,7 +211,7 @@ public class SubsetServiceTest extends CatalogServiceTest {
assertThat(result.getSubset().getUuid(), is(SUBSET_UUID));
}
private SubsetCreator buildSubsetCreator(UUID uuid) {
private SubsetCreator buildSubsetCreator(final UUID uuid) {
SubsetCreator subsetCreator = new SubsetCreator();
subsetCreator.setFullName(SUBSET_CREATOR_FULL_NAME);
subsetCreator.setEmail(SUBSET_CREATOR_EMAIL);
......@@ -205,14 +220,14 @@ public class SubsetServiceTest extends CatalogServiceTest {
return subsetCreator;
}
private Subset createSubset(UUID uuid) {
private Subset createSubset(final UUID uuid) {
subset = new Subset();
subset.setTitle("Subset");
subset.setWiewsCode("TEST");
subset.setWiewsCode("0000");
subset.setInstitute(faoInstitute);
subset.setUuid(uuid);
return subsetRepository.save(subset);
return subsetService.create(faoInstitute, subset);
}
private FaoInstitute createInstitute() {
......
......@@ -210,6 +210,32 @@ public class DatasetControllerTest extends AbstractApiTest {
/*@formatter:on*/
}
/**
* Testing javax validation.
* Fail to create dataset when owner = null
*/
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void failToCreateDataset() throws Exception {
final Dataset dataset = setUpDataset(PublishState.DRAFT);
dataset.setOwner(null);
final String s = verboseMapper.writeValueAsString(dataset);