Search in sources :

Example 26 with ValidationErrors

use of com.enonic.xp.content.ValidationErrors in project xp by enonic.

the class ValidateContentDataCommandTest method test_empty_displayName.

@Test
public void test_empty_displayName() {
    // setup
    final FieldSet fieldSet = FieldSet.create().label("My layout").name("myLayout").addFormItem(FormItemSet.create().name("mySet").required(true).addFormItem(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).build()).build()).build();
    final ContentType contentType = ContentType.create().superType(ContentTypeName.structured()).name("myapplication:my_type").addFormItem(fieldSet).build();
    Mockito.when(contentTypeService.getByName(Mockito.isA(GetContentTypeParams.class))).thenReturn(contentType);
    final Content content = Content.create().path("/mycontent").type(contentType.getName()).displayName("").build();
    content.getData().setString("mySet.myInput", "thing");
    // exercise
    final ValidationErrors result = executeValidation(content.getData(), contentType.getName(), content.getName(), content.getDisplayName());
    assertThat(result.stream()).hasSize(1);
}
Also used : GetContentTypeParams(com.enonic.xp.schema.content.GetContentTypeParams) FieldSet(com.enonic.xp.form.FieldSet) ContentType(com.enonic.xp.schema.content.ContentType) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) Test(org.junit.jupiter.api.Test)

Example 27 with ValidationErrors

use of com.enonic.xp.content.ValidationErrors in project xp by enonic.

the class ContentDataSerializerTest method update_validationErrors.

@Test
public void update_validationErrors() {
    final ContentDataSerializer contentDataSerializer = createContentDataSerializer();
    final String binaryName = "myName";
    final String binaryLabel = "myLabel";
    final String binaryMimeType = "myMimeType";
    final byte[] binaryData = "my binary".getBytes();
    final ValidationErrors validationErrors = ValidationErrors.create().add(ValidationError.attachmentError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "SOME_CODE"), BinaryReference.from("myName")).message("someError").build()).add(ValidationError.dataError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "SOME_OTHER_CODE"), PropertyPath.from("")).message("someDataError").build()).add(ValidationError.generalError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "SERIOUS_ERROR")).message("someError").build()).build();
    final UpdateContentTranslatorParams params = UpdateContentTranslatorParams.create().editedContent(Content.create().name("myContent").parentPath(ContentPath.ROOT).creator(PrincipalKey.ofAnonymous()).validationErrors(validationErrors).build()).modifier(PrincipalKey.ofAnonymous()).createAttachments(CreateAttachments.from(CreateAttachment.create().byteSource(ByteSource.wrap(binaryData)).label(binaryLabel).mimeType(binaryMimeType).name(binaryName).build())).build();
    final PropertyTree data = contentDataSerializer.toUpdateNodeData(params);
    final Iterable<PropertySet> validationErrorsData = data.getSets("validationErrors");
    assertThat(validationErrorsData).hasSize(3).extracting(propertySet -> propertySet.getString("errorCode")).containsExactly("system:SOME_CODE", "system:SOME_OTHER_CODE", "system:SERIOUS_ERROR");
    assertThat(validationErrorsData).extracting(propertySet -> propertySet.getString("attachment")).containsExactly("myName", null, null);
}
Also used : ChildOrder(com.enonic.xp.index.ChildOrder) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) WorkflowInfo(com.enonic.xp.content.WorkflowInfo) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) WorkflowState(com.enonic.xp.content.WorkflowState) ValidationErrors(com.enonic.xp.content.ValidationErrors) PageDescriptorService(com.enonic.xp.page.PageDescriptorService) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) ExtraData(com.enonic.xp.content.ExtraData) WorkflowCheckState(com.enonic.xp.content.WorkflowCheckState) Map(java.util.Map) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) PropertyPath(com.enonic.xp.data.PropertyPath) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) ValidationError(com.enonic.xp.content.ValidationError) XDataName(com.enonic.xp.schema.xdata.XDataName) PropertyTree(com.enonic.xp.data.PropertyTree) PartDescriptorService(com.enonic.xp.region.PartDescriptorService) BinaryReference(com.enonic.xp.util.BinaryReference) ContentPropertyNames(com.enonic.xp.content.ContentPropertyNames) ValidationErrorCode(com.enonic.xp.content.ValidationErrorCode) ContentPath(com.enonic.xp.content.ContentPath) PropertySet(com.enonic.xp.data.PropertySet) Content(com.enonic.xp.content.Content) LayoutDescriptorService(com.enonic.xp.region.LayoutDescriptorService) ExtraDatas(com.enonic.xp.content.ExtraDatas) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) PrincipalKey(com.enonic.xp.security.PrincipalKey) CreateContentTranslatorParams(com.enonic.xp.content.CreateContentTranslatorParams) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) ValidationErrors(com.enonic.xp.content.ValidationErrors) PropertyTree(com.enonic.xp.data.PropertyTree) PropertySet(com.enonic.xp.data.PropertySet) Test(org.junit.jupiter.api.Test)

Example 28 with ValidationErrors

use of com.enonic.xp.content.ValidationErrors in project xp by enonic.

the class OccurrenceValidatorTest method given_optionset_with_required_selection_and_empty_selection_array_fails_multiselection_check.

@Test
public void given_optionset_with_required_selection_and_empty_selection_array_fails_multiselection_check() {
    contentType.getForm().getFormItems().add(makeOptionSet("myOptionSet", 0, 0, 1, 1));
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myOptionSet.option1.myUnrequiredData", "1");
    content.getData().setString("myOptionSet._selected", "1");
    content.getData().removeProperty("myOptionSet._selected");
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream().findFirst()).containsInstanceOf(DataValidationError.class).get().extracting(ValidationError::getArgs, LIST).containsExactly("myOptionSet", 1, 1, 0);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 29 with ValidationErrors

use of com.enonic.xp.content.ValidationErrors in project xp by enonic.

the class OccurrenceValidatorTest method given_optionset_with_required_selection_and_missing_selection_array_has_too_little_default_options.

@Test
public void given_optionset_with_required_selection_and_missing_selection_array_has_too_little_default_options() {
    contentType.getForm().getFormItems().add(makeOptionSet("myOptionSet", 2, 0, 1, 1));
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myOptionSet.option1.myUnrequiredData", "1");
    final ValidationErrors validationResults = validate(content);
    assertThat(validationResults.stream().findFirst()).containsInstanceOf(DataValidationError.class).get().extracting(ValidationError::getArgs, LIST).containsExactly("myOptionSet", 1, 1, 0);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 30 with ValidationErrors

use of com.enonic.xp.content.ValidationErrors in project xp by enonic.

the class OccurrenceValidatorTest method given_input_with_minOccur1_with_two_null_values_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError.

@Test
public void given_input_with_minOccur1_with_two_null_values_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError() {
    contentType.getForm().getFormItems().add(Input.create().name("myInput").label("Input").inputType(InputTypeName.TEXT_LINE).minimumOccurrences(1).build());
    Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
    content.getData().setString("myInput[0]", null);
    content.getData().setString("myInput[1]", null);
    // exercise
    final ValidationErrors validationResults = validate(content);
    assertTrue(validationResults.hasErrors());
    assertThat(validationResults.stream()).allMatch(ve -> ve instanceof DataValidationError);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationErrors (com.enonic.xp.content.ValidationErrors)51 Content (com.enonic.xp.content.Content)48 Test (org.junit.jupiter.api.Test)48 DataValidationError (com.enonic.xp.content.DataValidationError)21 ContentType (com.enonic.xp.schema.content.ContentType)10 FormOptionSet (com.enonic.xp.form.FormOptionSet)8 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)8 GetContentTypeParams (com.enonic.xp.schema.content.GetContentTypeParams)7 FormItemSet (com.enonic.xp.form.FormItemSet)5 PropertySet (com.enonic.xp.data.PropertySet)4 ApplicationKey (com.enonic.xp.app.ApplicationKey)3 PropertyTree (com.enonic.xp.data.PropertyTree)3 ContentDataValidationException (com.enonic.xp.content.ContentDataValidationException)2 UpdateContentTranslatorParams (com.enonic.xp.content.UpdateContentTranslatorParams)2 FieldSet (com.enonic.xp.form.FieldSet)2 Input (com.enonic.xp.form.Input)2 PageDescriptorService (com.enonic.xp.page.PageDescriptorService)2 LayoutDescriptorService (com.enonic.xp.region.LayoutDescriptorService)2 PartDescriptorService (com.enonic.xp.region.PartDescriptorService)2 ContentTypeName (com.enonic.xp.schema.content.ContentTypeName)2