Skip to content

Commit

Permalink
#26 adding validators for the ConsentFormCheckBox and GroupElement fi…
Browse files Browse the repository at this point in the history
…elds. And Studio-Labels for all validators
  • Loading branch information
Timo Lemke authored and TallenceJanHendrikPopp committed Jun 20, 2019
1 parent e28bcb7 commit 8ab9780
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 12 deletions.
13 changes: 13 additions & 0 deletions form-editor-studio-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@
<groupId>com.coremedia.cms</groupId>
<artifactId>coremedia-rest-plugins</artifactId>
</dependency>
<dependency>
<groupId>com.coremedia.blueprint.base</groupId>
<artifactId>bpbase-uapi-util</artifactId>
</dependency>

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
Expand All @@ -67,6 +75,11 @@
</dependency>

<!-- Testing -->
<dependency>
<groupId>com.coremedia.cms</groupId>
<artifactId>coremedia-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2018 Tallence AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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 com.tallence.formeditor.studio.validator.field;

import com.coremedia.blueprint.base.util.StructUtil;
import com.coremedia.cap.struct.Struct;
import com.coremedia.rest.validation.Issues;
import com.coremedia.rest.validation.Severity;
import com.tallence.formeditor.contentbeans.FormEditor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
* Validates, that ConsentFormCheckBoxes have a text and a link.
*/
@Component
public class ConsentFormValidator implements FieldValidator {
@Override
public List<String> resonsibleFor() {
return Collections.singletonList("ConsentFormCheckBox");
}

@Override
public void validateField(Struct fieldData, String action, Issues issues) {

String name = StructUtil.getString(fieldData, "name");
if (fieldData.get("linkTarget") == null) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_missing_linkTarget", name);
}

String hint = StructUtil.getString(fieldData, "hint");
if (StringUtils.isEmpty(hint)) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_missing_hint", name);
} else if (!hint.matches(".*%.+%.*")) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_invalid_hint", name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.tallence.formeditor.studio.validator.field;

import com.coremedia.blueprint.base.util.StructUtil;
import com.coremedia.cap.struct.Struct;
import com.coremedia.rest.validation.Issues;
import com.coremedia.rest.validation.Severity;
Expand All @@ -39,8 +40,8 @@ public List<String> resonsibleFor() {

@Override
public void validateField(Struct fieldData, String action, Issues issues) {
if (!StringUtils.hasText(fieldData.getString("name"))) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "missing_name");
if (!StringUtils.hasText(StructUtil.getString(fieldData, "name"))) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formField_missing_name", fieldData.get("type"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2018 Tallence AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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 com.tallence.formeditor.studio.validator.field;

import com.coremedia.blueprint.base.util.StructUtil;
import com.coremedia.cap.struct.Struct;
import com.coremedia.rest.validation.Issues;
import com.coremedia.rest.validation.Severity;
import com.tallence.formeditor.contentbeans.FormEditor;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
* Validates, that CheckBoxes, DropDowns and RadioButtons have at least one groupElement.
*/
@Component
public class OptionsNotEmptyValidator implements FieldValidator {
@Override
public List<String> resonsibleFor() {
return Arrays.asList("RadioButtons", "CheckBoxes", "SelectBox");
}

@Override
public void validateField(Struct fieldData, String action, Issues issues) {

boolean noElements = Optional.ofNullable(StructUtil.getSubstruct(fieldData, "groupElements"))
.map(s -> s.getProperties().isEmpty())
.orElse(true);
if (noElements) {

String messageKey = fieldData.getString("type").toLowerCase() + "_missing_options";

issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, messageKey, fieldData.getString("name"));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.tallence.formeditor.studio.validator.field;

import com.coremedia.blueprint.base.util.StructUtil;
import com.coremedia.cap.struct.Struct;
import com.coremedia.rest.validation.Issues;
import com.coremedia.rest.validation.Severity;
Expand Down Expand Up @@ -44,45 +45,45 @@ public List<String> resonsibleFor() {

@Override
public void validateField(Struct fieldData, String action, Issues issues) {
Struct validator = (Struct) fieldData.get(VALIDATOR);
Struct validator = StructUtil.getSubstruct(fieldData, VALIDATOR);
if (validator != null) {
validateFieldValidators(validator, issues);
}
}

private void validateFieldValidators(Struct validator, Issues issues) {
// Size constraints
Integer minSize = (Integer) validator.get("minSize");
Integer maxSize = (Integer) validator.get("maxSize");
Integer minSize = StructUtil.getInteger(validator, "minSize");
Integer maxSize = StructUtil.getInteger(validator, "maxSize");
validateMinSize(minSize, issues);
validateMaxSize(maxSize, minSize, issues);

// Regex
String regex = (String) validator.get(REGEX);
String regex = StructUtil.getString(validator, REGEX);
if (StringUtils.hasLength(regex)) {
validateRegex(regex, issues);
}
}

private void validateMinSize(Integer minSize, Issues issues) {
if (minSize != null && minSize < 0) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "invalid_minsize");
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formfield_validator_invalid_minsize", minSize);
}
}

private void validateMaxSize(Integer maxSize, Integer minSize, Issues issues) {
if (maxSize != null && maxSize < 0) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "invalid_maxsize");
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formfield_validator_invalid_maxsize", maxSize);
} else if (maxSize != null && minSize != null && maxSize < minSize) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "maxsize_smaller_minsize");
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formfield_validator_maxsize_smaller_minsize");
}
}

private void validateRegex(String regex, Issues issues) {
try {
Pattern.compile(regex);
} catch (PatternSyntaxException e) {
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "form_action_invalid_regexp");
issues.addIssue(Severity.ERROR, FormEditor.FORM_ELEMENTS, "formfield_validator_invalid_regexp");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Collections;
import java.util.Set;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -88,6 +92,40 @@ public void testInvaildMailAction() {
assertThat(issues.getByProperty().get(FormEditor.FORM_ELEMENTS), hasItem(issue2));
}

@Test
public void testEmptyOptions() {
Content testContent = contentRepository.getContent("10");
IssuesImpl<Content> issues = new IssuesImpl<>(testContent, testContent.getProperties().keySet());
formEditorValidator.validate(testContent, issues);

Issue<Content> issue1 = new Issue<>(testContent, Severity.ERROR, FormEditor.FORM_ELEMENTS, "missing_options", Collections.singletonList("without buttons"));
Set<Issue<Content>> formElementIssues = issues.getByProperty().get(FormEditor.FORM_ELEMENTS);
assertThat(formElementIssues, hasItem(issue1));

assertThat(formElementIssues.size(), equalTo(1));
}

@Test
public void testConsentForm() {
Content testContent = contentRepository.getContent("12");
IssuesImpl<Content> issues = new IssuesImpl<>(testContent, testContent.getProperties().keySet());
formEditorValidator.validate(testContent, issues);

Set<Issue<Content>> formElementIssues = issues.getByProperty().get(FormEditor.FORM_ELEMENTS);

Issue<Content> issue1 = new Issue<>(testContent, Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_missing_linkTarget", Collections.singletonList("ConsentForm missing target"));
assertThat(formElementIssues, hasItem(issue1));

Issue<Content> issue2 = new Issue<>(testContent, Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_missing_hint", Collections.singletonList("ConsentForm missing hint"));
assertThat(formElementIssues, hasItem(issue2));

Issue<Content> issue3 = new Issue<>(testContent, Severity.ERROR, FormEditor.FORM_ELEMENTS, "consentForm_invalid_hint", Collections.singletonList("ConsentForm invalid hint"));
assertThat(formElementIssues, hasItem(issue3));


assertThat(formElementIssues.size(), equalTo(3));
}

@Test
public void testInvalidFieldName() {
Content testContent = contentRepository.getContent("8");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
~ Copyright 2018 Tallence AG
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ 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.
-->

<Struct xmlns="http://www.coremedia.com/2008/struct">
<StructProperty Name="formElements">
<Struct>
<StructProperty Name="Test Buttons without options">
<Struct>
<StringProperty Name="type">RadioButtons</StringProperty>
<StringProperty Name="name">without buttons</StringProperty>
</Struct>
</StructProperty>
<StructProperty Name="Test Buttons with options">
<Struct>
<StringProperty Name="type">RadioButtons</StringProperty>
<StringProperty Name="name">with button</StringProperty>
<StructProperty Name="groupElements">
<Struct>
<StructProperty Name="Button 1"/>
</Struct>
</StructProperty>
</Struct>
</StructProperty>
</Struct>
</StructProperty>
</Struct>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!--
~ Copyright 2018 Tallence AG
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ 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.
-->

<Struct xmlns="http://www.coremedia.com/2008/struct" xmlns:xlink="http://www.w3.org/1999/xlink">
<StructProperty Name="formElements">
<Struct>
<StructProperty Name="ConsentForm valid">
<Struct>
<StringProperty Name="type">ConsentFormCheckBox</StringProperty>
<StringProperty Name="name">ConsentForm valid</StringProperty>
<StringProperty Name="hint">That is the %target%</StringProperty>
<LinkProperty Name="linkTarget" LinkType="CMLinkable" xlink:href="coremedia:///cap/content/2"/>
</Struct>
</StructProperty>
<StructProperty Name="ConsentForm missing target">
<Struct>
<StringProperty Name="type">ConsentFormCheckBox</StringProperty>
<StringProperty Name="name">ConsentForm missing target</StringProperty>
<StringProperty Name="hint">That is the %target%</StringProperty>
</Struct>
</StructProperty>
<StructProperty Name="ConsentForm missing hint">
<Struct>
<StringProperty Name="type">ConsentFormCheckBox</StringProperty>
<StringProperty Name="name">ConsentForm missing hint</StringProperty>
<LinkProperty Name="linkTarget" LinkType="CMLinkable" xlink:href="coremedia:///cap/content/2"/>
</Struct>
</StructProperty>
<StructProperty Name="ConsentForm invalid hint">
<Struct>
<StringProperty Name="type">ConsentFormCheckBox</StringProperty>
<StringProperty Name="name">ConsentForm invalid hint</StringProperty>
<LinkProperty Name="linkTarget" LinkType="CMLinkable" xlink:href="coremedia:///cap/content/2"/>
<StringProperty Name="hint">Hint without placeholder</StringProperty>
</Struct>
</StructProperty>
</Struct>
</StructProperty>
</Struct>
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
<markupProperty name="formData" url="8formElements.xml" grammar="coremedia-struct-2008"/>
</version>
</document>
<document id="10" type="FormEditor" name="TestForm10" modificationDate="2009-06-01T20:59:42.000+01:00">
<version number="1">
<stringProperty name="formAction" value="default"/>
<markupProperty name="formData" url="10formElements.xml" grammar="coremedia-struct-2008"/>
</version>
</document>
<document id="12" type="FormEditor" name="TestForm12" modificationDate="2009-06-01T20:59:42.000+01:00">
<version number="1">
<stringProperty name="formAction" value="default"/>
<markupProperty name="formData" url="12formElements.xml" grammar="coremedia-struct-2008"/>
</version>
</document>
</folder>
</folder>
</content>
Expand Down
Loading

0 comments on commit 8ab9780

Please sign in to comment.