Skip to content

Commit b767e7d

Browse files
authored
Merge pull request #458 from ncats/cv_and_mixtures
Preventing leading and trailing blanks in text CV terms and in mixture component type
2 parents ae1fbab + e388a19 commit b767e7d

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

gsrs-module-substance-example/src/test/java/example/substance/validation/MixtureValidationTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
import org.springframework.beans.factory.annotation.Autowired;
1919
import org.springframework.security.test.context.support.WithMockUser;
2020

21+
import java.util.UUID;
2122
import java.util.stream.Stream;
2223

23-
import static org.junit.Assert.assertFalse;
24-
import static org.junit.Assert.assertTrue;
24+
import static org.junit.Assert.*;
2525

2626
/**
2727
* Created by katzelda on 7/20/18.
@@ -199,4 +199,36 @@ public void mixtureMustAtLeast2OneOfComponents() throws Exception{
199199

200200

201201
}
202+
203+
@Test
204+
public void mixtureComponentsTypeMustHaveNoTrailingBlanks() throws Exception{
205+
206+
String inputComponentType = "MAY_BE_PRESENT_ONE_OF ";
207+
String componentTypeTrimmed =inputComponentType.trim();
208+
String inputComponentType2 = " MUST_BE_PRESENT";
209+
String componentType2Trimmed = inputComponentType2.trim();
210+
JsonNode toSubmit = new MixtureSubstanceBuilder()
211+
.addName("foo " + UUID.randomUUID())
212+
.addComponents(inputComponentType, s1)
213+
.addComponents(inputComponentType2, s2)
214+
.addComponents(componentTypeTrimmed, s3)
215+
.buildJson();
216+
217+
GsrsEntityService.CreationResult<Substance> result = substanceEntityService.createEntity(toSubmit);
218+
219+
ValidationResponse response = result.getValidationResponse();
220+
assertTrue(response.isValid());
221+
//this is split up and stored as a variable for java 8 type inference to work...
222+
Stream<ValidationMessage>s1 = response.getValidationMessages().stream();
223+
224+
assertEquals(2, s1
225+
.filter(m->m.getMessageType() == ValidationMessage.MESSAGE_TYPE.WARNING)
226+
.map(ValidationMessage::getMessage)
227+
.filter(m-> m.contains(MixtureValidator.NO_LEAD_TRAIL_SPACES_IN_TYPE))
228+
.count());
229+
MixtureSubstance completed = (MixtureSubstance) result.getCreatedEntity();
230+
assertEquals(2, completed.mixture.components.stream().map(component -> component.type).filter(t->t.equals(componentTypeTrimmed)).count());
231+
assertEquals(1, completed.mixture.components.stream().map(component -> component.type).filter(t->t.equals(componentType2Trimmed)).count());
232+
}
233+
202234
}

gsrs-module-substances-core/src/main/java/ix/ginas/utils/validation/validators/CVValidator.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import ix.core.validator.GinasProcessingMessage;
44
import ix.core.validator.ValidatorCallback;
55
import ix.ginas.models.v1.ControlledVocabulary;
6+
import ix.ginas.models.v1.VocabularyTerm;
67
import ix.ginas.utils.validation.AbstractValidatorPlugin;
78
import lombok.extern.slf4j.Slf4j;
89

10+
import java.util.ArrayList;
911
import java.util.List;
1012
import java.util.Map;
1113
import java.util.stream.Collectors;
@@ -50,5 +52,33 @@ public void validate(ControlledVocabulary objnew, ControlledVocabulary objold, V
5052
callback.setInvalid();
5153
log.info("found duplicate vocabulary displays: {}", String.join(", ", duplicateDisplays));
5254
}
55+
List<String> displaysWithBlanks = new ArrayList<>();
56+
List<String> valuesWithBlanks = new ArrayList<>();
57+
for(VocabularyTerm term : objnew.terms) {
58+
if (term.display != null && term.display.length() != term.display.trim().length()) {
59+
log.trace("found display with space: {}", term.display);
60+
displaysWithBlanks.add(term.display);
61+
term.display = term.display.trim();
62+
}
63+
if (term.value != null && term.value.length() != term.value.trim().length()) {
64+
log.trace("found value with space: {}", term.value);
65+
valuesWithBlanks.add(term.value);
66+
term.value = term.value.trim();
67+
}
68+
}
69+
if( !displaysWithBlanks.isEmpty()) {
70+
GinasProcessingMessage displaysMessage =
71+
GinasProcessingMessage.WARNING_MESSAGE("One or more vocabulary term displays have leading or trailing spaces. These spaces were removed")
72+
.appliableChange(true);
73+
callback.addMessage(displaysMessage);
74+
log.trace("added warning about display value(s) with blanks");
75+
}
76+
if( !valuesWithBlanks.isEmpty()){
77+
GinasProcessingMessage valuesMessage =
78+
GinasProcessingMessage.WARNING_MESSAGE("One or more vocabulary term values have leading or trailing spaces. These spaces were removed")
79+
.appliableChange(true);
80+
callback.addMessage(valuesMessage);
81+
log.trace("added warning about value(s) with blanks");
82+
}
5383
}
5484
}

gsrs-module-substances-core/src/main/java/ix/ginas/utils/validation/validators/MixtureValidator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class MixtureValidator extends AbstractValidatorPlugin<Substance> {
2727
@Autowired
2828
private ReferenceRepository referenceRepository;
2929

30+
public final static String NO_LEAD_TRAIL_SPACES_IN_TYPE = "Mixture component type must not start or end with a space";
3031
@Override
3132
@Transactional(readOnly = true)
3233
public void validate(Substance objnew, Substance objold, ValidatorCallback callback) {
@@ -47,10 +48,14 @@ public void validate(Substance objnew, Substance objold, ValidatorCallback callb
4748
if (c.substance == null) {
4849
callback.addMessage(GinasProcessingMessage
4950
.ERROR_MESSAGE("Mixture components must reference a substance record, found:\"null\""));
50-
}else if(c.type == null || c.type.length()<=0){
51+
}else if(c.type == null || c.type.length()<=0) {
5152
callback.addMessage(GinasProcessingMessage.ERROR_MESSAGE("Mixture components must specify a type"));
53+
} else if(c.type.length() != c.type.trim().length()) {
54+
c.type = c.type.trim();
55+
GinasProcessingMessage message = GinasProcessingMessage.WARNING_MESSAGE(NO_LEAD_TRAIL_SPACES_IN_TYPE);
56+
message.appliableChange(true);
57+
callback.addMessage(message);
5258
}else {
53-
// Substance comp = SubstanceFactory.getFullSubstance(c.substance);
5459
if (!substanceRepository.exists(c.substance)) {
5560
callback.addMessage(GinasProcessingMessage
5661
.WARNING_MESSAGE("Mixture substance references \"%s\" which is not yet registered",

0 commit comments

Comments
 (0)