Skip to content

Commit

Permalink
validation - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Apr 15, 2024
1 parent 4793095 commit f0632cf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,31 @@ private void visitContainer(PrismContainer<?> container, ValidationResult result
}

private void visitProperty(PrismProperty<?> property, ValidationResult result) {
if (check(ValidationItemType.PROTECTED_DATA_NOT_EXTERNAL) && ProtectedStringType.COMPLEX_TYPE.equals(property.getDefinition().getTypeName())) {
PrismPropertyDefinition<?> def = property.getDefinition();
PrismPropertyDefinition<?> def = property.getDefinition();

if (check(ValidationItemType.PROTECTED_DATA_NOT_EXTERNAL)
&& ProtectedStringType.COMPLEX_TYPE.equals(property.getDefinition().getTypeName())) {
Class<?> type = def.getTypeClass();
if (ProtectedDataType.class.isAssignableFrom(type)) {
checkProtectedString(property, result);
}
}

if (check(ValidationItemType.MULTIVALUE_BYTE_ARRAY)) {
if (!def.isMultiValue()) {
return;
}

if (!byte[].class.equals(def.getTypeClass())) {
return;
}

ItemPath path = property.getPath();

warn(
result, ValidationItemType.MULTIVALUE_BYTE_ARRAY,
"Multi-value byte array in " + property.getPath(), property, null);
}
}

private void checkProtectedString(PrismProperty<?> property, ValidationResult result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public enum ValidationItemType {
*/
MULTIVALUE_REF_WITHOUT_OID,

/**
* Multi-value byte array in extension container.
*
* Data type: null
*/
MULTIVALUE_BYTE_ARRAY,

/**
* Data type: {@link com.evolveum.midpoint.prism.Item}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import static org.testng.AssertJUnit.*;

import java.io.File;
import java.util.Map;
import java.util.stream.Collectors;
import javax.xml.namespace.QName;

import org.testng.AssertJUnit;
import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.PrismObject;
Expand All @@ -28,6 +31,8 @@ public class TestObjectValidator extends AbstractSchemaTest {

public static final File TEST_DIR = new File("src/test/resources/validator");

public static final File ROLE = new File(TEST_DIR, "role.xml");

// Contains elements that are deprecated and planned for removal, but still valid.
protected static final File ROLE_ONE_FILE = new File(TEST_DIR, "role-one.xml");
protected static final File ROLE_TWO_FILE = new File(TEST_DIR, "role-two.xml");
Expand Down Expand Up @@ -205,4 +210,29 @@ private ValidationItem findItem(ValidationResult validationResult, ItemPath expe
}
return null;
}

@Test
public void testNewValidations() throws Exception {
ObjectValidator validator = new ObjectValidator();
validator.setAllWarnings();
validator.setSummarizeItemLifecycleState(false);
validator.setWarnPlannedRemovalVersion("4.8");

PrismObject<RoleType> object = PrismTestUtil.getPrismContext().parseObject(ROLE);
ValidationResult result = validator.validate(object);

AssertJUnit.assertNotNull(result);
AssertJUnit.assertEquals(5, result.size());

Map<ValidationItemType, Long> countPerType = result.getItems().stream()
.collect(Collectors.groupingBy(
i -> i.type(),
Collectors.counting()));

AssertJUnit.assertEquals(1L, countPerType.get(ValidationItemType.PROTECTED_DATA_NOT_EXTERNAL).longValue());
AssertJUnit.assertEquals(2L, countPerType.get(ValidationItemType.INCORRECT_OID_FORMAT).longValue());
// AssertJUnit.assertEquals(1L, countPerType.get(ValidationItemType.MISSING_NATURAL_KEY));
AssertJUnit.assertEquals(1L, countPerType.get(ValidationItemType.MULTIVALUE_REF_WITHOUT_OID).longValue());
AssertJUnit.assertEquals(1L, countPerType.get(ValidationItemType.DEPRECATED_ITEM).longValue());
}
}
34 changes: 34 additions & 0 deletions infra/schema/src/test/resources/validator/role.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

<!-- invalid OID format -->
<role xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3"
oid="6d1a2464-db78-43e8-b1f1-cdf73cd54fxx">

<name>validation role</name>

<!-- deprecated item -->
<subtype>deprecated item</subtype>

<parentOrgRef oid="00000000-8888-6666-0000-100000000001" type="OrgType"/>
<parentOrgRef>
<filter>
<q:text>name = "sample"</q:text>
</filter>
</parentOrgRef>

<credentials>
<password>
<value>
<!-- clear value, external data shoud be there -->
<t:clearValue>qwe123</t:clearValue>
</value>
</password>
</credentials>

<!-- missing natural key -->
<inducement>
<!-- invalid OID format -->
<targetRef oid="73ca6aa2-e8ac-4097-a9b7-ca539dec45xx" type="RoleType"/>
</inducement>
</role>

0 comments on commit f0632cf

Please sign in to comment.