Commit ff01bc5b authored by Maxym Borodenko's avatar Maxym Borodenko

AccessionData @Validation

Fix bean
parent 5af68cd0
......@@ -631,6 +631,11 @@
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
<dependency>
<groupId>cz.jirutka.validator</groupId>
<artifactId>validator-collection</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
<build>
......
/**
/*
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.model.genesys;
......@@ -28,6 +28,7 @@ import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Version;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
......@@ -84,15 +85,19 @@ public class AccessionAlias extends BasicModel implements AccessionRelated, Self
@JoinColumn(name = "accessionId", nullable = false, updatable = false)
private AccessionId accession;
@Size(max = 150)
@Column(name = "name", length = 150)
private String name;
@Column
private AliasType aliasType = AliasType.ACCENAME;
@Size(max = 2)
@Column(length = 2)
private String lang;
@javax.validation.constraints.Pattern(regexp = "[A-Z]{3}\\d{3,4}")
@Size(max = 64)
@Column(length = 64)
private String usedBy;
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.model.genesys;
......@@ -31,7 +31,10 @@ import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Version;
import javax.validation.constraints.Size;
import cz.jirutka.validator.collection.constraints.EachPattern;
import cz.jirutka.validator.collection.constraints.EachSize;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.BasicModel;
......@@ -54,25 +57,32 @@ public class AccessionCollect extends BasicModel implements AccessionRelated, Se
@OneToOne(mappedBy = "coll", optional = false, fetch = FetchType.LAZY, cascade = {})
private AccessionId accession;
@Size(max = 8)
@Column(name = "collDate", length = 8)
private String collDate;
@Size(max = 64)
@Column(name = "collNumb", length = 64)
private String collNumb;
@Size(max = 128)
@Column(name = "collMissId", length = 128)
private String collMissId;
@EachSize(max = 128)
@EachPattern(regexp = "[A-Z]{3}\\d{3,4}")
@Column(name = "collCode", nullable = false, length = 128)
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "accession_collect_code", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
private Set<String> collCode;
@EachSize(max = 250)
@Column(name = "collName", nullable = false, length = 250)
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "accession_collect_name", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
private Set<String> collName;
@EachSize(max = 128)
@Column(name = "collInstAddress", nullable = false, length = 128)
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "accession_collect_instaddr", joinColumns = @JoinColumn(name = "collectId", referencedColumnName = "id"))
......
/**
/*
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.model.genesys;
......@@ -34,6 +34,7 @@ import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.genesys.blocks.model.AuditedVersionedModel;
......@@ -176,6 +177,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
private String otherIds;
@Size(max = 9)
@Pattern(regexp = "[A-Z]{3}\\d{3,4}")
@Column(name = "donorCode", length = 9)
private String donorCode;
......
......@@ -39,6 +39,7 @@ import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
......@@ -124,6 +125,7 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
@Field(type = FieldType.Object)
private AccessionCollect coll;
@Valid
@OneToMany(mappedBy = "accession", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true)
@JsonIgnoreProperties({ "accession" })
@Field(type = FieldType.Nested)
......
......@@ -24,12 +24,14 @@ import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.commons.text.WordUtils;
import org.genesys.blocks.model.BasicModel;
import org.genesys2.server.api.PleaseRetryException;
import org.genesys2.server.api.model.AccessionHeaderJson;
import org.genesys2.server.exception.InvalidApiUsageException;
......@@ -71,6 +73,8 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.collect.Iterators;
import javassist.util.proxy.Proxy;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
/**
* The Class AccessionUploader.
......@@ -100,6 +104,9 @@ public class AccessionUploader implements InitializingBean {
@Autowired
private CropService cropService;
@Autowired
private Validator validator;
private static final String[] ACCESSIONID_FIELDS = { "breederCode", "storage", "duplSite" };
@Override
......@@ -219,7 +226,7 @@ public class AccessionUploader implements InitializingBean {
PDCICalculator.updatePdci(pdci, accession);
LOG.trace("Updated PDCI to 1 accession after {}ms", stopWatch.getTime());
toSave.add(accession);
toSave.add(validate(accession));
} catch (PleaseRetryException e) {
throw e;
} catch (InvalidApiUsageException e) {
......@@ -374,10 +381,10 @@ public class AccessionUploader implements InitializingBean {
if (jsonNode.isArray()) {
ArrayNode ns = (ArrayNode) jsonNode;
ns.forEach(n -> newaliases.add(new AccessionAlias(aliasType, n.textValue())));
ns.forEach(n -> newaliases.add(validate(new AccessionAlias(aliasType, n.textValue()))));
} else if (jsonNode.isTextual()) {
newaliases.add(new AccessionAlias(aliasType, jsonNode.textValue()));
newaliases.add(validate(new AccessionAlias(aliasType, jsonNode.textValue())));
} else if (jsonNode.isNull()) {
// NOOP
} else {
......@@ -478,6 +485,7 @@ public class AccessionUploader implements InitializingBean {
try {
copy(AccessionCollect.class, source, coll, jsonNode.fieldNames());
validate(coll);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
throw new InvalidApiUsageException(e.getMessage(), e);
}
......@@ -601,6 +609,23 @@ public class AccessionUploader implements InitializingBean {
throw new RuntimeException("No setter for field " + fieldName + " in " + clazz.getName());
}
private <T extends BasicModel> T validate(T object) {
Set<ConstraintViolation<T>> violations = validator.validate(object);
if (violations != null && violations.size() > 0) {
StringBuilder message = new StringBuilder("Validation failed: ");
for (ConstraintViolation<T> cv : violations) {
message.append(cv.getRootBeanClass().getSimpleName());
message.append(".");
message.append(cv.getPropertyPath().toString());
message.append(" -> ");
message.append(cv.getMessage());
message.append("; ");
}
throw new InvalidApiUsageException(message.toString());
}
return object;
}
@Transactional(timeout = 250, isolation = Isolation.READ_UNCOMMITTED, rollbackFor = Throwable.class)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE')")
public List<AccessionOpResponse> deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> identifiers) {
......
......@@ -138,4 +138,5 @@ public class ApplicationConfig {
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
}
......@@ -22,6 +22,8 @@ import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import org.genesys2.server.mvc.AddStuffInterceptor;
import org.genesys2.spring.RequestAttributeLocaleResolver;
......@@ -101,6 +103,12 @@ public class WebConfiguration extends WebMvcConfigurerAdapter {
return validator;
}
@Bean
public javax.validation.Validator getJavaxValidator() {
final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
return factory.getValidator();
}
@Bean
public RequestAttributeLocaleResolver localeResolver() {
final RequestAttributeLocaleResolver resolver = new RequestAttributeLocaleResolver();
......
......@@ -56,6 +56,10 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module.Feature;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
public final class ApplicationConfig {
/**
......@@ -137,6 +141,12 @@ public final class ApplicationConfig {
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
@Bean
public Validator getJavaxValidator() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
return factory.getValidator();
}
@Bean
public AsAdminInvoker asAdminInvoker() {
......
......@@ -335,6 +335,57 @@ public class AccessionControllerTest extends AbstractApiTest {
assertThat("acceName must be an empty array", result.getAccessionId().getAliases().size(), is(0));
}
/**
* Expecting an error - alias is invalid
*/
@Test
public void createAccessionWithInvalidAlias() throws Exception {
ObjectNode accessionJson = setUpAccession();
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
// TEST012 is invalid value
accessionJson.put("acceName", "INVALID001:IDC0057");
upsertWithErrors(accessionJson);
}
/**
* Expecting an error - collCode is invalid
*/
@Test
public void createAccessionWithInvalidCollCode() throws Exception {
ObjectNode accessionJson = setUpAccession();
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
ArrayNode codes = objectMapper.createArrayNode();
// add one valid code
codes.add("TET012");
// add one invalid code
codes.add("INVALID002");
ObjectNode coll = accessionJson.putObject("coll");
coll.putPOJO("collCode", codes);
upsertWithErrors(accessionJson);
}
/**
* Expecting an error - donorCode is invalid
*/
@Test
public void createAccessionWithInvalidDonorCode() throws Exception {
ObjectNode accessionJson = setUpAccession();
// ATATA414 is invalid value
accessionJson.put("donorCode", "INVALID003");
upsertWithErrors(accessionJson);
}
@Test
public void testCollNumb() throws Exception {
ObjectNode accessionJson = setUpAccession();
......@@ -383,6 +434,19 @@ public class AccessionControllerTest extends AbstractApiTest {
/*@formatter:on*/
}
private ResultActions upsertWithErrors(ObjectNode accessionJson) throws Exception, JsonProcessingException {
LOG.debug("Upsering {}", verboseMapper.writerWithDefaultPrettyPrinter().writeValueAsString(accessionJson));
/*@formatter:off*/
return mockMvc.perform(post(AccessionController.API_BASE + "/" + institute.getCode() + "/upsert")
.contentType(MediaType.APPLICATION_JSON)
.content("[" + verboseMapper.writeValueAsString(accessionJson) + "]"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$[0].error", not(nullValue())));
/*@formatter:on*/
}
private Accession fetch(UUID uuid) throws Exception, JsonProcessingException {
/*@formatter:off*/
String responseBody = mockMvc.perform(get(org.genesys2.server.api.v1.AccessionController.CONTROLLER_URL+ "/" + uuid))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment