From bf9b401757fc09ef25c4da876776bab96c99bb84 Mon Sep 17 00:00:00 2001 From: Radovan Semancik Date: Thu, 8 Dec 2016 12:49:39 +0100 Subject: [PATCH] Fixing OpenLDAP conntests (hopehully) --- .../midpoint/prism/util/PrismAsserts.java | 45 ++++++++++++++++--- .../midpoint/prism/util/PrismUtil.java | 17 ++++++- .../midpoint/prism/PrismInternalTestUtil.java | 6 +-- .../test/AbstractIntegrationTest.java | 41 ++++++++++++++++- .../testing/conntest/AbstractLdapTest.java | 2 +- 5 files changed, 99 insertions(+), 12 deletions(-) diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/util/PrismAsserts.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/util/PrismAsserts.java index 878ccf02f2e..b0147c92a15 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/util/PrismAsserts.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/util/PrismAsserts.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2015 Evolveum + * Copyright (c) 2010-2016 Evolveum * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -102,6 +102,12 @@ public static void assertPropertyValue(PrismContainer container, QName pr assertSame("Wrong parent for value of container "+container, container, containerValue.getParent()); assertPropertyValue(containerValue, propQName, realPropValues); } + + public static void assertPropertyValueMatch(PrismContainer container, QName propQName, MatchingRule matchingRule, T... realPropValues) throws SchemaException { + PrismContainerValue containerValue = container.getValue(); + assertSame("Wrong parent for value of container "+container, container, containerValue.getParent()); + assertPropertyValueMatch(containerValue, propQName, matchingRule, realPropValues); + } public static void assertPropertyValue(PrismContainerValue containerValue, QName propQName, T... realPropValues) { PrismProperty property = containerValue.findProperty(propQName); @@ -110,6 +116,13 @@ public static void assertPropertyValue(PrismContainerValue containerValue assertPropertyValueDesc(property, containerValue.getParent().toString(), realPropValues); } + public static void assertPropertyValueMatch(PrismContainerValue containerValue, QName propQName, MatchingRule matchingRule, T... realPropValues) throws SchemaException { + PrismProperty property = containerValue.findProperty(propQName); + assertNotNull("Property " + propQName + " not found in " + containerValue.getParent(), property); + assertSame("Wrong parent for property " + property, containerValue, property.getParent()); + assertPropertyValueDesc(property, matchingRule, containerValue.getParent().toString(), realPropValues); + } + public static void assertPropertyValue(PrismContainer container, ItemPath propPath, T... realPropValues) { PrismContainerValue containerValue = container.getValue(); assertSame("Wrong parent for value of container "+container, container, containerValue.getParent()); @@ -125,13 +138,22 @@ public static void assertPropertyValue(PrismContainerValue containerValue public static void assertPropertyValue(PrismProperty property, T... expectedPropValues) { assertPropertyValueDesc(property, null, expectedPropValues); } - + public static void assertPropertyValueDesc(PrismProperty property, String contextDescrition, T... expectedPropValues) { + try { + assertPropertyValueDesc(property, null, contextDescrition, expectedPropValues); + } catch (SchemaException e) { + // null matching rule, cannot happen + throw new RuntimeException(e.getMessage(), e); + } + } + + public static void assertPropertyValueDesc(PrismProperty property, MatchingRule matchingRule, String contextDescrition, T... expectedPropValues) throws SchemaException { Collection> pvals = property.getValues(); QName propQName = property.getElementName(); assert pvals != null && !pvals.isEmpty() : "Empty property "+propQName; assertSet("property "+propQName + (contextDescrition == null ? "" : " in " + contextDescrition), - "value", pvals, expectedPropValues); + "value", matchingRule, pvals, expectedPropValues); } public static void assertPropertyValues(String message, Collection expected, Collection> results) { @@ -811,20 +833,33 @@ public static void assertEquivalent(String message, Prism assert false: message + ": " + suffix; } + private static void assertSet(String inMessage, String setName, MatchingRule matchingRule, Collection> actualPValues, T[] expectedValues) throws SchemaException { + assertValues(setName + " set in " + inMessage, matchingRule, actualPValues, expectedValues); + } + private static void assertSet(String inMessage, String setName, Collection> actualPValues, T[] expectedValues) { assertValues(setName + " set in " + inMessage, actualPValues, expectedValues); } public static void assertValues(String message, Collection> actualPValues, T... expectedValues) { + try { + assertValues(message, null, actualPValues, expectedValues); + } catch (SchemaException e) { + // null matching rule, cannot happen + throw new RuntimeException(e.getMessage(), e); + } + } + + public static void assertValues(String message, MatchingRule matchingRule, Collection> actualPValues, T... expectedValues) throws SchemaException { assertNotNull("Null set in " + message, actualPValues); if (expectedValues.length != actualPValues.size()) { fail("Wrong number of values in " + message+ "; expected "+expectedValues.length+" (real values) " +PrettyPrinter.prettyPrint(expectedValues)+"; has "+actualPValues.size()+" (pvalues) "+actualPValues); } - for (PrismPropertyValue actualPValue: actualPValues) { + for (PrismPropertyValue actualPValue: actualPValues) { boolean found = false; for (T value: expectedValues) { - if (value.equals(actualPValue.getValue())) { + if (PrismUtil.equals(value, actualPValue.getValue(), matchingRule)) { found = true; } } diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/util/PrismUtil.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/util/PrismUtil.java index 6f751ccb70d..a0223e425a4 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/util/PrismUtil.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/util/PrismUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2015 Evolveum + * Copyright (c) 2010-2016 Evolveum * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import com.evolveum.midpoint.prism.delta.ItemDelta; import com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor; import com.evolveum.midpoint.prism.marshaller.PrismUnmarshaller; +import com.evolveum.midpoint.prism.match.MatchingRule; import com.evolveum.midpoint.prism.polystring.PolyString; import com.evolveum.midpoint.prism.polystring.PolyStringNormalizer; import com.evolveum.midpoint.prism.xml.XsdTypeMapper; @@ -190,4 +191,18 @@ public static void setDeltaOldValue(PrismObject oldObj } } + public static boolean equals(T a, T b, MatchingRule matchingRule) throws SchemaException { + if (a == null && b == null) { + return true; + } + if (a == null || b == null) { + return false; + } + if (matchingRule == null) { + return a.equals(b); + } else { + return matchingRule.match(a, b); + } + } + } diff --git a/infra/prism/src/test/java/com/evolveum/midpoint/prism/PrismInternalTestUtil.java b/infra/prism/src/test/java/com/evolveum/midpoint/prism/PrismInternalTestUtil.java index 349bec0f039..5621b2f3309 100644 --- a/infra/prism/src/test/java/com/evolveum/midpoint/prism/PrismInternalTestUtil.java +++ b/infra/prism/src/test/java/com/evolveum/midpoint/prism/PrismInternalTestUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2013 Evolveum + * Copyright (c) 2010-2016 Evolveum * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -425,7 +425,7 @@ public static void assertUserJackContent(PrismObject user, boolean exp } - private static void assertUserJackExtension(PrismObject user) { + private static void assertUserJackExtension(PrismObject user) throws SchemaException { PrismContainer extension = user.getExtension(); assertContainerDefinition(extension, "extension", DOMUtil.XSD_ANY, 0, 1); @@ -463,7 +463,7 @@ private static void assertUserJackExtension(PrismObject user) { } - public static void assertPropertyValue(PrismContainer container, String propName, Object propValue) { + public static void assertPropertyValue(PrismContainer container, String propName, Object propValue) throws SchemaException { QName propQName = new QName(NS_FOO, propName); PrismAsserts.assertPropertyValue(container, propQName, propValue); } diff --git a/repo/repo-test-util/src/main/java/com/evolveum/midpoint/test/AbstractIntegrationTest.java b/repo/repo-test-util/src/main/java/com/evolveum/midpoint/test/AbstractIntegrationTest.java index a30932fc20a..584155df712 100644 --- a/repo/repo-test-util/src/main/java/com/evolveum/midpoint/test/AbstractIntegrationTest.java +++ b/repo/repo-test-util/src/main/java/com/evolveum/midpoint/test/AbstractIntegrationTest.java @@ -632,7 +632,12 @@ protected QName getGroupObjectClass(ResourceType resourceType) { } protected void assertShadowCommon(PrismObject shadow, String oid, String username, ResourceType resourceType, - QName objectClass, MatchingRule nameMatchingRule, boolean requireNormalizedIdentfiers) throws SchemaException { + QName objectClass, MatchingRule nameMatchingRule, boolean requireNormalizedIdentfiers) throws SchemaException { + assertShadowCommon(shadow, oid, username, resourceType, objectClass, nameMatchingRule, requireNormalizedIdentfiers, false); + } + + protected void assertShadowCommon(PrismObject shadow, String oid, String username, ResourceType resourceType, + QName objectClass, final MatchingRule nameMatchingRule, boolean requireNormalizedIdentfiers, boolean useMatchingRuleForShadowName) throws SchemaException { assertShadow(shadow); if (oid != null) { assertEquals("Shadow OID mismatch (prism)", oid, shadow.getOid()); @@ -647,7 +652,39 @@ protected void assertShadowCommon(PrismObject shadow, String oid, St assertNotNull("Null attributes in shadow for "+username, attributesContainer); assertFalse("Empty attributes in shadow for "+username, attributesContainer.isEmpty()); - PrismAsserts.assertPropertyValue(shadow, ShadowType.F_NAME, PrismTestUtil.createPolyString(username)); + if (useMatchingRuleForShadowName) { + MatchingRule polyMatchingRule = new MatchingRule() { + + @Override + public QName getName() { + return nameMatchingRule.getName(); + } + + @Override + public boolean isSupported(QName xsdType) { + return nameMatchingRule.isSupported(xsdType); + } + + @Override + public boolean match(PolyString a, PolyString b) throws SchemaException { + return nameMatchingRule.match(a.getOrig(), b.getOrig()); + } + + @Override + public boolean matchRegex(PolyString a, String regex) throws SchemaException { + return nameMatchingRule.matchRegex(a.getOrig(), regex); + } + + @Override + public PolyString normalize(PolyString original) throws SchemaException { + return new PolyString(nameMatchingRule.normalize(original.getOrig())); + } + + }; + PrismAsserts.assertPropertyValueMatch(shadow, ShadowType.F_NAME, polyMatchingRule, PrismTestUtil.createPolyString(username)); + } else { + PrismAsserts.assertPropertyValue(shadow, ShadowType.F_NAME, PrismTestUtil.createPolyString(username)); + } RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.getRefinedSchema(resourceType); ObjectClassComplexTypeDefinition ocDef = rSchema.findObjectClassDefinition(objectClass); diff --git a/testing/conntest/src/test/java/com/evolveum/midpoint/testing/conntest/AbstractLdapTest.java b/testing/conntest/src/test/java/com/evolveum/midpoint/testing/conntest/AbstractLdapTest.java index 6d964c2b994..ae8de32999a 100644 --- a/testing/conntest/src/test/java/com/evolveum/midpoint/testing/conntest/AbstractLdapTest.java +++ b/testing/conntest/src/test/java/com/evolveum/midpoint/testing/conntest/AbstractLdapTest.java @@ -902,7 +902,7 @@ protected void assertAccountRepoShadow(PrismObject shadow, String dn } protected void assertGroupShadow(PrismObject shadow, String dn) throws SchemaException { - assertShadowCommon(shadow, null, dn, resourceType, getGroupObjectClass(), ciMatchingRule, false); + assertShadowCommon(shadow, null, dn, resourceType, getGroupObjectClass(), ciMatchingRule, false, true); } protected long roundTsDown(long ts) {