From 48b61d4518147d652029da1a68521bbf7aad944b Mon Sep 17 00:00:00 2001 From: maximthomas Date: Mon, 2 Nov 2026 11:30:30 +0300 Subject: [PATCH 1/5] [#212] Write an attribute given in another case under the schema's name in create and update --- .../connectors/xml/XMLHandlerImpl.java | 60 +++++----- .../connectors/xml/XMLHandlerReloadTests.java | 111 +++++++++++++++--- 2 files changed, 127 insertions(+), 44 deletions(-) diff --git a/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java b/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java index e0bb96f2..5f5d235a 100644 --- a/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java +++ b/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java @@ -191,24 +191,21 @@ public Uid create(final ObjectClass objClass, final Set attributes) { ObjectClassInfo objInfo = connSchema.findObjectClassInfo(objClass.getObjectClassValue()); Map objAttributes = AttributeInfoUtil.toMap(objInfo.getAttributeInfo()); - Map providedAttributesMap = null; String uidValue = null; - if (attributes != null) { - providedAttributesMap = new HashMap(AttributeUtil.toMap(attributes)); - } - - // Check if __NAME__ is defined - if (providedAttributesMap == null || !providedAttributesMap.containsKey(Name.NAME) || providedAttributesMap.get(Name.NAME).getValue().isEmpty()) { + // Check if __NAME__ is defined. The framework names attributes in any case, so __name__ defines it too (#212). + Attribute givenName = AttributeUtil.find(Name.NAME, attributes); + if (givenName == null || givenName.getValue() == null || givenName.getValue().isEmpty()) { throw new IllegalArgumentException(Name.NAME + " must be defined."); } // Every given attribute is checked as update checks it, so one the schema does not have is refused rather than - // dropped (#192). AttributeUtil.toMap is case-insensitive: of two names that differ only in case it keeps the - // first spelling as the key and the last attribute as the value (#212). + // dropped (#192). It is kept under the schema's name, which the checks below and the file use, whatever case it + // was given in (#212). Map> checked = new HashMap>(); - for (Map.Entry provided : providedAttributesMap.entrySet()) { - checked.put(provided.getKey(), checkAttribute(provided.getValue(), objAttributes, Change.CREATE)); + for (Attribute attribute : attributes) { + CheckedAttribute c = checkAttribute(attribute, objAttributes, Change.CREATE); + checked.put(c.name, c.values); } for (AttributeInfo attributeInfo : objInfo.getAttributeInfo()) { if (attributeInfo.isRequired() && !checked.containsKey(attributeInfo.getName())) { @@ -216,12 +213,13 @@ public Uid create(final ObjectClass objClass, final Set attributes) { } } - Name name = AttributeUtil.getNameFromAttributes(attributes); + // Not AttributeUtil.getNameFromAttributes: it casts the attribute to Name, which only an exact __NAME__ is. + String name = checked.get(Name.NAME).get(0); // Check if entry already exists - if (entryExists(objClass, new Uid(name.getNameValue()), ElementIdentifierFieldType.BY_NAME)) { + if (entryExists(objClass, new Uid(name), ElementIdentifierFieldType.BY_NAME)) { throw new AlreadyExistsException("Could not create entry. An entry with the " + Uid.NAME + " of " - + name.getNameValue() + " already exists."); + + name + " already exists."); } // Create or get UID. The lookup is case-insensitive, but the loop below writes the uid only into __UID__. @@ -229,7 +227,7 @@ public Uid create(final ObjectClass objClass, final Set attributes) { if (uidInfo != null && uidInfo.getName().equals(Uid.NAME)) { uidValue = UUID.randomUUID().toString(); } else { - uidValue = name.getNameValue(); + uidValue = name; } // Create object type element @@ -291,11 +289,10 @@ public Uid update(ObjectClass objClass, Uid uid, Set replaceAttribute Uid newUid = null; for (Attribute attribute : replaceAttributes) { - String attributeName = attribute.getName(); - List values = checkAttribute(attribute, objAttributes, Change.UPDATE); + CheckedAttribute checked = checkAttribute(attribute, objAttributes, Change.UPDATE); - if (Name.NAME.equals(attributeName)) { - for (String value : values) { + if (Name.NAME.equals(checked.name)) { + for (String value : checked.values) { if (nameTakenByAnother(objClass, entry, value)) { throw new AlreadyExistsException("Could not update entry. An entry with the " + Name.NAME + " of " + value + " already exists."); @@ -307,7 +304,7 @@ public Uid update(ObjectClass objClass, Uid uid, Set replaceAttribute } } - checkedAttributes.add(new CheckedAttribute(attributeName, values)); + checkedAttributes.add(checked); } try { @@ -832,17 +829,20 @@ String refusal(String attributeName) { } /** - * Checks one attribute given to create or update and returns its values as text. Both operations check an attribute - * with this method; only the operation's own schema rule differs (#192). The values are read once, so a guarded - * value is decrypted once. + * Checks one attribute given to create or update and returns its values as text, under the schema's name. Both + * operations check an attribute with this method; only the operation's own schema rule differs (#192). The values + * are read once, so a guarded value is decrypted once. + *

+ * The schema lookup is case-insensitive, as the framework names attributes, while the file and the XSD name them + * exactly: so from here on the attribute is named, written and reported as the schema spells it (#212). */ - private static List checkAttribute(Attribute attribute, Map objAttributes, + private static CheckedAttribute checkAttribute(Attribute attribute, Map objAttributes, Change change) { - String attributeName = attribute.getName(); - AttributeInfo attributeInfo = objAttributes.get(attributeName); + AttributeInfo attributeInfo = objAttributes.get(attribute.getName()); if (attributeInfo == null) { - throw new IllegalArgumentException("Data field: " + attributeName + " is not supported."); + throw new IllegalArgumentException("Data field: " + attribute.getName() + " is not supported."); } + String attributeName = attributeInfo.getName(); if (!change.allows(attributeInfo)) { throw new IllegalArgumentException(change.refusal(attributeName)); } @@ -864,12 +864,12 @@ private static List checkAttribute(Attribute attribute, Map 1) { throw new IllegalArgumentException("Data field: " + attributeName + " is not multivalued can not have more than one value"); } - return values; + return new CheckedAttribute(attributeName, values); } /** - * An attribute of an update that passed every check, with the values to write. Kept in a list, not a map, so a - * name given twice is applied twice, in order. + * An attribute that passed every check, under the schema's name, with the values to write. update keeps them in a + * list, not a map, so a name given twice is applied twice, in order. */ private static final class CheckedAttribute { final String name; diff --git a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java index e8b734ca..41833886 100644 --- a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java +++ b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java @@ -40,7 +40,6 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -155,6 +154,9 @@ public Object[][] rejectedUpdates() { {attributes(changedFirstName, AttributeBuilder.build(Name.NAME, " ")), "Parameter '__NAME__' must not be blank."}, {attributes(changedFirstName, AttributeBuilder.build(ATTR_ACCOUNT_IS_DELETED, Boolean.TRUE)), "is-deleted is not updatable."}, + // The message names the attribute as the schema spells it, not as it was given (#212). + {attributes(changedFirstName, AttributeBuilder.build("IS-DELETED", Boolean.TRUE)), + "is-deleted is not updatable."}, {attributes(changedFirstName, AttributeBuilder.build(ATTR_ACCOUNT_AGE, "12")), "age contains invalid type. Value(s) should be of type java.lang.Integer"}, {attributes(changedFirstName, AttributeBuilder.build("no-such-attribute", "x")), @@ -210,6 +212,12 @@ public Object[][] refusedAttributes() { {AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, "a@example.com", null), "email contains a null value"}, {AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "A", "B"), "Data field: lastname is not multivalued can not have more than one value"}, + // An attribute named in another case is checked, and named in the message, as the schema spells it (#212). + {AttributeBuilder.build("LastName"), "No values provided for required attribute: lastname"}, + {AttributeBuilder.build("LASTNAME", " "), "Parameter 'lastname' must not be blank."}, + {AttributeBuilder.build("LastName", "A", "B"), + "Data field: lastname is not multivalued can not have more than one value"}, + {AttributeBuilder.build("__name__", " "), "Parameter '__NAME__' must not be blank."}, }; } @@ -220,15 +228,12 @@ public void createAndUpdateRefuseAnAttributeAlike(Attribute refused, String mess FileTime past = FileTime.fromMillis(now() - HOUR); Files.setLastModifiedTime(file.toPath(), past); byte[] before = Files.readAllBytes(file.toPath()); - Map carol = new LinkedHashMap(); - for (Attribute a : attributes(AttributeBuilder.build(Name.NAME, "carol"), + Set carol = withAttributes(attributes(AttributeBuilder.build(Name.NAME, "carol"), AttributeBuilder.buildPassword("secret-carol".toCharArray()), - AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "Last-carol"), refused)) { - carol.put(a.getName(), a); // the row's attribute replaces carol's attribute of the same name - } + AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "Last-carol")), refused); IllegalArgumentException onCreate = expectThrows(IllegalArgumentException.class, - () -> call(h -> h.create(ObjectClass.ACCOUNT, new LinkedHashSet(carol.values())))); + () -> call(h -> h.create(ObjectClass.ACCOUNT, carol))); IllegalArgumentException onUpdate = expectThrows(IllegalArgumentException.class, () -> call(h -> h.update(ObjectClass.ACCOUNT, new Uid("uid-alice"), attributes(refused)))); @@ -267,10 +272,6 @@ public void eachOperationAppliesItsOwnRule() throws Exception { public Object[][] attributesWithoutLastName() { return new Object[][] { {attributes(AttributeBuilder.build(Name.NAME, "alice"), AttributeBuilder.buildPassword("secret-x".toCharArray()))}, - // A required attribute is looked up by the schema's spelling, so another case does not stand in for it - // (unchanged, see #212). - {attributes(AttributeBuilder.build(Name.NAME, "alice"), AttributeBuilder.buildPassword("secret-x".toCharArray()), - AttributeBuilder.build("LASTNAME", "X"))}, }; } @@ -293,6 +294,66 @@ public void createRefusesAMissingRequiredAttribute(Set attributes) th assertEquals(Files.readAllBytes(file.toPath()), before); } + @DataProvider + public Object[][] attributesWithoutAName() { + return new Object[][] { + {null}, + // __name__ is __NAME__ in another case; with no values its value is null, not an empty list (#212). + {attributes(AttributeBuilder.build("__name__"), AttributeBuilder.buildPassword("secret-x".toCharArray()), + AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "X"))}, + }; + } + + /** create looks for a name before it checks any attribute, so attributes without a name are refused for the name. */ + @Test(dataProvider = "attributesWithoutAName") + public void createRefusesAttributesWithoutAName(Set attributes) throws Exception { + writeAccounts(file, "alice"); + + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> call(h -> h.create(ObjectClass.ACCOUNT, attributes))); + + assertEquals(e.getMessage(), Name.NAME + " must be defined."); + } + + /** An attribute named in another case than the schema's; the schema's name; the value it carries. */ + @DataProvider + public Object[][] otherCaseSpellings() { + return new Object[][] { + {AttributeBuilder.build("LastName", "L2"), ATTR_ACCOUNT_LAST_NAME, "L2"}, + {AttributeBuilder.build("FirstName", "F1"), ATTR_ACCOUNT_FIRST_NAME, "F1"}, + {AttributeBuilder.build("__name__", "carol"), Name.NAME, "carol"}, + {AttributeBuilder.build("__password__", new GuardedString("p1".toCharArray())), ATTR_PASSWORD, "p1"}, + }; + } + + /** + * The schema and the file name an attribute exactly, the framework in any case: create writes an attribute named in + * another case under the schema's name (#212). Reads skip empty elements, so the file is checked, not a search. + */ + @Test(dataProvider = "otherCaseSpellings") + public void createWritesAnAttributeUnderTheSchemaName(Attribute given, String schemaName, String value) + throws Exception { + writeAccounts(file, "alice"); + + call(h -> h.create(ObjectClass.ACCOUNT, withAttributes(account("carol"), given))); + + List written = valuesInFile(file, schemaName); + assertEquals(written.get(written.size() - 1), value); // carol comes after alice + assertEquals(valuesInFile(file, given.getName()), List.of()); + } + + /** update replaces the schema's element with an attribute named in another case, as create writes it (#212). */ + @Test(dataProvider = "otherCaseSpellings") + public void updateWritesAnAttributeUnderTheSchemaName(Attribute given, String schemaName, String value) + throws Exception { + writeAccounts(file, "alice"); + + call(h -> h.update(ObjectClass.ACCOUNT, new Uid("uid-alice"), attributes(given))); + + assertEquals(valuesInFile(file, schemaName), List.of(value)); + assertEquals(valuesInFile(file, given.getName()), List.of()); + } + /** * Of two attributes with exactly one name, create keeps the last, as AttributeUtil.toMap does, and writes only that * one. The framework's CreateImpl refuses such a pair before it reaches the connector; this calls the handler. @@ -309,8 +370,14 @@ public void createKeepsOneOfTwoSameNamedAttributes() throws Exception { assertEquals(valuesInFile(file, ATTR_ACCOUNT_EMAIL), List.of("b")); } - @Test - public void renameIntoATakenNameLeavesTheEntryAndTheFileAlone() throws Exception { + @DataProvider + public Object[][] nameSpellings() { + return new Object[][] {{Name.NAME}, {"__name__"}}; + } + + /** The name is checked for a clash in any case, as it is written under the schema's name in any case (#212). */ + @Test(dataProvider = "nameSpellings") + public void renameIntoATakenNameLeavesTheEntryAndTheFileAlone(String nameAttribute) throws Exception { writeAccounts(file, "alice", "bob"); FileTime past = FileTime.fromMillis(now() - HOUR); Files.setLastModifiedTime(file.toPath(), past); @@ -319,7 +386,7 @@ public void renameIntoATakenNameLeavesTheEntryAndTheFileAlone() throws Exception // The name comes after an attribute that would be applied first. AlreadyExistsException e = expectThrows(AlreadyExistsException.class, () -> call(h -> h.update(ObjectClass.ACCOUNT, new Uid("uid-alice"), attributes(AttributeBuilder.build(ATTR_ACCOUNT_FIRST_NAME, "CHANGED"), - AttributeBuilder.build(Name.NAME, "bob"))))); + AttributeBuilder.build(nameAttribute, "bob"))))); assertEquals(e.getMessage(), "Could not update entry. An entry with the __NAME__ of bob already exists."); assertEquals(Files.getLastModifiedTime(file.toPath()), past); @@ -365,6 +432,22 @@ private static Set attributes(Attribute... attributes) { return new LinkedHashSet(Arrays.asList(attributes)); } + /** base without its attributes named like a replacement, in any case, then the replacements in order. */ + private static Set withAttributes(Set base, Attribute... replacements) { + Set result = new LinkedHashSet(); + for (Attribute a : base) { + boolean replaced = false; + for (Attribute r : replacements) { + replaced |= a.is(r.getName()); + } + if (!replaced) { + result.add(a); + } + } + result.addAll(Arrays.asList(replacements)); + return result; + } + @Test public void deleteIsInTheFileWhenTheLastUserLeaves() throws Exception { writeAccounts(file, "alice", "bobby"); From ec72c847cdf442eddf4e279382e6ebd71ac4b082 Mon Sep 17 00:00:00 2001 From: maximthomas Date: Mon, 2 Nov 2026 11:33:04 +0300 Subject: [PATCH 2/5] [#212] Refuse an attribute that create or update is given more than once, in any case --- .../connectors/xml/XMLHandlerImpl.java | 36 ++++++++---- .../xml/XMLFilterTranslatorTests.java | 4 +- .../connectors/xml/XMLHandlerReloadTests.java | 56 ++++++++++++------- 3 files changed, 64 insertions(+), 32 deletions(-) diff --git a/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java b/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java index 5f5d235a..c0f7606e 100644 --- a/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java +++ b/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java @@ -203,8 +203,7 @@ public Uid create(final ObjectClass objClass, final Set attributes) { // dropped (#192). It is kept under the schema's name, which the checks below and the file use, whatever case it // was given in (#212). Map> checked = new HashMap>(); - for (Attribute attribute : attributes) { - CheckedAttribute c = checkAttribute(attribute, objAttributes, Change.CREATE); + for (CheckedAttribute c : checkAttributes(attributes, objAttributes, Change.CREATE)) { checked.put(c.name, c.values); } for (AttributeInfo attributeInfo : objInfo.getAttributeInfo()) { @@ -285,12 +284,10 @@ public Uid update(ObjectClass objClass, Uid uid, Set replaceAttribute } // Every attribute is checked before the entry changes, so a rejected update leaves the document as it was (#158). - List checkedAttributes = new ArrayList(); + // The name is checked for a clash once every attribute has passed, as create() checks it. + List checkedAttributes = checkAttributes(replaceAttributes, objAttributes, Change.UPDATE); Uid newUid = null; - for (Attribute attribute : replaceAttributes) { - - CheckedAttribute checked = checkAttribute(attribute, objAttributes, Change.UPDATE); - + for (CheckedAttribute checked : checkedAttributes) { if (Name.NAME.equals(checked.name)) { for (String value : checked.values) { if (nameTakenByAnother(objClass, entry, value)) { @@ -303,8 +300,6 @@ public Uid update(ObjectClass objClass, Uid uid, Set replaceAttribute } } } - - checkedAttributes.add(checked); } try { @@ -868,9 +863,28 @@ private static CheckedAttribute checkAttribute(Attribute attribute, Map checkAttributes(Set attributes, + Map objAttributes, Change change) { + List checkedAttributes = new ArrayList(); + Map givenNames = new HashMap(); + for (Attribute attribute : attributes) { + CheckedAttribute checked = checkAttribute(attribute, objAttributes, change); + String firstGiven = givenNames.put(checked.name, attribute.getName()); + if (firstGiven != null) { + throw new IllegalArgumentException("Data field: " + checked.name + " is given more than once, as " + + firstGiven + " and as " + attribute.getName() + "."); + } + checkedAttributes.add(checked); + } + return checkedAttributes; + } + + /** An attribute that passed every check, under the schema's name, with the values to write. */ private static final class CheckedAttribute { final String name; final List values; diff --git a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLFilterTranslatorTests.java b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLFilterTranslatorTests.java index 4753505f..0d34f6a1 100644 --- a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLFilterTranslatorTests.java +++ b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLFilterTranslatorTests.java @@ -86,8 +86,8 @@ public static void setUpClass() throws Exception { attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_MS_EMPLOYED, ATTR_ACCOUNT_VALUE_MS_EMPLOYED)); attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_SIXTH_SENSE, ATTR_ACCOUNT_VALUE_SIXTH_SENSE)); attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_FIRST_NAME, ATTR_ACCOUNT_VALUE_FIRST_NAME)); - attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, ATTR_ACCOUNT_VALUE_EMAIL_1)); - attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, ATTR_ACCOUNT_VALUE_EMAIL_2)); + // One attribute with both values: create refuses an attribute given twice (#212). + attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, ATTR_ACCOUNT_VALUE_EMAIL_1, ATTR_ACCOUNT_VALUE_EMAIL_2)); attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_SECRET_PIN, new GuardedString(ATTR_ACCOUNT_VALUE_SECRET_PIN.toCharArray()))); attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_PERMANTENT_EMPLOYEE, ATTR_ACCOUNT_VALUE_PERMANENT_EPLOYEE)); attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_YEARS_EMPLOYED, ATTR_ACCOUNT_VALUE_YEARS_EPLOYED)); diff --git a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java index 41833886..ac83e959 100644 --- a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java +++ b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java @@ -354,20 +354,48 @@ public void updateWritesAnAttributeUnderTheSchemaName(Attribute given, String sc assertEquals(valuesInFile(file, given.getName()), List.of()); } + /** A schema attribute given twice, in any case; the message that refuses it. */ + @DataProvider + public Object[][] attributesGivenTwice() { + return new Object[][] { + {AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "L4"), AttributeBuilder.build("LastName", "X4"), + "Data field: lastname is given more than once, as lastname and as LastName."}, + {AttributeBuilder.build("LastName", "X4"), AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "L4"), + "Data field: lastname is given more than once, as LastName and as lastname."}, + // Attribute.equals compares the values too, so a set can hold exactly one name twice. + {AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, "a"), AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, "b"), + "Data field: email is given more than once, as email and as email."}, + {AttributeBuilder.build(Name.NAME, "carol"), AttributeBuilder.build("__name__", "dave"), + "Data field: __NAME__ is given more than once, as __NAME__ and as __name__."}, + }; + } + /** - * Of two attributes with exactly one name, create keeps the last, as AttributeUtil.toMap does, and writes only that - * one. The framework's CreateImpl refuses such a pair before it reaches the connector; this calls the handler. + * Of a schema attribute given twice, in any case, either value may be the one meant, so create and update refuse it + * before they change anything (#212). The framework's CreateImpl refuses only exactly one name given twice, and + * UpdateImpl neither. */ - @Test - public void createKeepsOneOfTwoSameNamedAttributes() throws Exception { + @Test(dataProvider = "attributesGivenTwice") + public void createAndUpdateRefuseAnAttributeGivenTwice(Attribute first, Attribute second, String message) + throws Exception { writeAccounts(file, "alice"); - - call(h -> h.create(ObjectClass.ACCOUNT, attributes(AttributeBuilder.build(Name.NAME, "carol"), + FileTime past = FileTime.fromMillis(now() - HOUR); + Files.setLastModifiedTime(file.toPath(), past); + byte[] before = Files.readAllBytes(file.toPath()); + Set carol = withAttributes(attributes(AttributeBuilder.build(Name.NAME, "carol"), AttributeBuilder.buildPassword("secret-carol".toCharArray()), - AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "Last-carol"), - AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, "a"), AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, "b")))); + AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "Last-carol")), first, second); - assertEquals(valuesInFile(file, ATTR_ACCOUNT_EMAIL), List.of("b")); + IllegalArgumentException onCreate = expectThrows(IllegalArgumentException.class, + () -> call(h -> h.create(ObjectClass.ACCOUNT, carol))); + IllegalArgumentException onUpdate = expectThrows(IllegalArgumentException.class, + () -> call(h -> h.update(ObjectClass.ACCOUNT, new Uid("uid-alice"), attributes(first, second)))); + + assertEquals(onCreate.getMessage(), message); + assertEquals(onUpdate.getMessage(), message); + assertEquals(Files.getLastModifiedTime(file.toPath()), past); + assertEquals(Files.readAllBytes(file.toPath()), before); + assertEquals(call(h -> firstAndLastNames(h.search(allAccounts(), ObjectClass.ACCOUNT))), List.of("null|Last-alice")); } @DataProvider @@ -417,16 +445,6 @@ public void updateWithNoAttributesLeavesTheFileAlone() throws Exception { assertEquals(Files.readAllBytes(file.toPath()), before); } - @Test - public void repeatedAttributeKeepsTheLastValue() throws Exception { - writeAccounts(file, "alice"); - setModified(file, now() - HOUR); - // Attribute.equals compares the values too, so a set can hold the same name twice: update applies both, in order. - call(h -> h.update(ObjectClass.ACCOUNT, new Uid("uid-alice"), attributes( - AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "A"), AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "B")))); - assertEquals(firstAndLastNamesIn(file), List.of("null|B")); - } - /** The attributes in the order given, which is the order update() goes through them. */ private static Set attributes(Attribute... attributes) { return new LinkedHashSet(Arrays.asList(attributes)); From df4fc2c81c846e001da3618b59864b73f072f9d0 Mon Sep 17 00:00:00 2001 From: maximthomas Date: Mon, 2 Nov 2026 11:44:17 +0300 Subject: [PATCH 3/5] [#212] Pin the blank-name check for __name__ when the XSD makes __NAME__ optional --- .../connectors/xml/XMLHandlerIndexTests.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerIndexTests.java b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerIndexTests.java index 0d449ced..4bb35404 100644 --- a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerIndexTests.java +++ b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerIndexTests.java @@ -182,11 +182,14 @@ public void clearingAnOptionalNameIsRefused() throws Exception { optional.init(); // create() requires a name whatever the XSD says, and an object without one cannot be built: - // a cleared entry would fail every search of the class. - for (String blank : Arrays.asList("", " ")) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> optional.update( - ObjectClass.ACCOUNT, new Uid("uid-alice"), Collections.singleton(AttributeBuilder.build(Name.NAME, blank)))); - assertEquals(e.getMessage(), "Parameter '__NAME__' must not be blank."); + // a cleared entry would fail every search of the class. __name__ is the name in another case (#212). + for (String nameAttribute : Arrays.asList(Name.NAME, "__name__")) { + for (String blank : Arrays.asList("", " ")) { + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> optional.update( + ObjectClass.ACCOUNT, new Uid("uid-alice"), + Collections.singleton(AttributeBuilder.build(nameAttribute, blank)))); + assertEquals(e.getMessage(), "Parameter '__NAME__' must not be blank."); + } } assertEquals(describe(optional.search((Query) null, ObjectClass.ACCOUNT)), Arrays.asList("uid-alice|alice", "uid-bob|bob")); From 919e50eaf09335a1305b930584fe89ba93f0fb5b Mon Sep 17 00:00:00 2001 From: maximthomas Date: Mon, 2 Nov 2026 12:48:14 +0300 Subject: [PATCH 4/5] [#212] Look a given attribute up by its exact name first, so an XSD with two names that differ only in case keeps both apart --- .../connectors/xml/XMLHandlerImpl.java | 39 ++++++++++++------- .../connectors/xml/XMLHandlerIndexTests.java | 39 +++++++++++++++++++ 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java b/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java index c0f7606e..06bf5f6b 100644 --- a/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java +++ b/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java @@ -203,7 +203,7 @@ public Uid create(final ObjectClass objClass, final Set attributes) { // dropped (#192). It is kept under the schema's name, which the checks below and the file use, whatever case it // was given in (#212). Map> checked = new HashMap>(); - for (CheckedAttribute c : checkAttributes(attributes, objAttributes, Change.CREATE)) { + for (CheckedAttribute c : checkAttributes(attributes, objInfo, Change.CREATE)) { checked.put(c.name, c.values); } for (AttributeInfo attributeInfo : objInfo.getAttributeInfo()) { @@ -276,7 +276,6 @@ public Uid update(ObjectClass objClass, Uid uid, Set replaceAttribute XmlHandlerUtil.checkObjectType(objClass, riSchema); ObjectClassInfo objInfo = connSchema.findObjectClassInfo(objClass.getObjectClassValue()); - Map objAttributes = AttributeInfoUtil.toMap(objInfo.getAttributeInfo()); Element entry = getEntry(objClass, uid, ElementIdentifierFieldType.AUTO); if (entry == null) { @@ -285,7 +284,7 @@ public Uid update(ObjectClass objClass, Uid uid, Set replaceAttribute // Every attribute is checked before the entry changes, so a rejected update leaves the document as it was (#158). // The name is checked for a clash once every attribute has passed, as create() checks it. - List checkedAttributes = checkAttributes(replaceAttributes, objAttributes, Change.UPDATE); + List checkedAttributes = checkAttributes(replaceAttributes, objInfo, Change.UPDATE); Uid newUid = null; for (CheckedAttribute checked : checkedAttributes) { if (Name.NAME.equals(checked.name)) { @@ -828,12 +827,17 @@ String refusal(String attributeName) { * operations check an attribute with this method; only the operation's own schema rule differs (#192). The values * are read once, so a guarded value is decrypted once. *

- * The schema lookup is case-insensitive, as the framework names attributes, while the file and the XSD name them - * exactly: so from here on the attribute is named, written and reported as the schema spells it (#212). + * A name the schema spells exactly is that attribute. Any other name is looked up without regard to case, as the + * framework names attributes, while the file and the XSD name them exactly: so from here on the attribute is named, + * written and reported as the schema spells it (#212). An XSD may declare two names that differ only in case, and + * the case-insensitive map holds one of the two for both, so the exact lookup comes first. */ - private static CheckedAttribute checkAttribute(Attribute attribute, Map objAttributes, - Change change) { - AttributeInfo attributeInfo = objAttributes.get(attribute.getName()); + private static CheckedAttribute checkAttribute(Attribute attribute, Map exactAttributes, + Map anyCaseAttributes, Change change) { + AttributeInfo attributeInfo = exactAttributes.get(attribute.getName()); + if (attributeInfo == null) { + attributeInfo = anyCaseAttributes.get(attribute.getName()); + } if (attributeInfo == null) { throw new IllegalArgumentException("Data field: " + attribute.getName() + " is not supported."); } @@ -864,16 +868,23 @@ private static CheckedAttribute checkAttribute(Attribute attribute, Map checkAttributes(Set attributes, - Map objAttributes, Change change) { + private static List checkAttributes(Set attributes, ObjectClassInfo objInfo, + Change change) { + Map exactAttributes = new HashMap(); + for (AttributeInfo attributeInfo : objInfo.getAttributeInfo()) { + exactAttributes.put(attributeInfo.getName(), attributeInfo); + } + Map anyCaseAttributes = AttributeInfoUtil.toMap(objInfo.getAttributeInfo()); + List checkedAttributes = new ArrayList(); Map givenNames = new HashMap(); for (Attribute attribute : attributes) { - CheckedAttribute checked = checkAttribute(attribute, objAttributes, change); + CheckedAttribute checked = checkAttribute(attribute, exactAttributes, anyCaseAttributes, change); String firstGiven = givenNames.put(checked.name, attribute.getName()); if (firstGiven != null) { throw new IllegalArgumentException("Data field: " + checked.name + " is given more than once, as " diff --git a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerIndexTests.java b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerIndexTests.java index 4bb35404..3acafbb6 100644 --- a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerIndexTests.java +++ b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerIndexTests.java @@ -237,6 +237,45 @@ public void clearingTheOptionalNameOfAnEntryWithoutUidIsRefused() throws Excepti } } + /** + * An XSD may declare two attributes whose names differ only in case, and the framework's case-insensitive map holds + * one of them for both names. A name given exactly as the schema spells it is that attribute, so create and update + * write each value into its own element, as before #212. + */ + @Test + public void attributesWhoseNamesDifferOnlyInCaseAreWrittenApart() throws Exception { + File casePairXsd = getTestFile("case-pair.xsd"); + String xsd = new String(Files.readAllBytes(XSD_SCHEMA_FILEPATH.toPath()), StandardCharsets.UTF_8); + String lastName = ""; + // __ACCOUNT__'s required lastname gets an optional sibling LastName. + Files.write(casePairXsd.toPath(), xsd.replace(lastName, + "" + lastName) + .getBytes(StandardCharsets.UTF_8)); + try { + File file = getRandomXMLFile(); + writeAccounts(file, "alice"); + XMLConfiguration config = new XMLConfiguration(); + config.setXmlFilePath(file); + config.setXsdFilePath(casePairXsd); + SchemaParser parser = new SchemaParser(XMLConnector.class, casePairXsd); + XMLHandlerImpl pair = new XMLHandlerImpl(config, parser.parseSchema(), parser.getXsdSchema()); + pair.init(); + + Set carol = account("carol"); + carol.add(AttributeBuilder.build("LastName", "Other-carol")); + pair.create(ObjectClass.ACCOUNT, carol); + pair.update(ObjectClass.ACCOUNT, new Uid("uid-alice"), new LinkedHashSet(Arrays.asList( + AttributeBuilder.build("LastName", "Other-alice"), + AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "New-alice")))); + pair.dispose(); + + assertEquals(valuesInFile(file, ATTR_ACCOUNT_LAST_NAME), Arrays.asList("New-alice", "Last-carol")); + assertEquals(valuesInFile(file, "LastName"), Arrays.asList("Other-alice", "Other-carol")); + } finally { + Files.delete(casePairXsd.toPath()); + } + } + /** * Renames to {@code newName} along with an unknown attribute. update() rejects the unknown attribute * before it changes the entry, so the rename is not applied. A two-valued __NAME__ cannot be used: From e22ced1c17cfe57274429db7511a471b35f9f54f Mon Sep 17 00:00:00 2001 From: maximthomas Date: Mon, 2 Nov 2026 12:48:15 +0300 Subject: [PATCH 5/5] [#212] Pin update's late name-clash check and create's empty-name check; note how create refuses a valueless second name --- .../connectors/xml/XMLHandlerImpl.java | 2 ++ .../connectors/xml/XMLHandlerReloadTests.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java b/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java index 06bf5f6b..e47c3ac1 100644 --- a/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java +++ b/OpenICF-xml-connector/src/main/java/org/forgerock/openicf/connectors/xml/XMLHandlerImpl.java @@ -194,6 +194,8 @@ public Uid create(final ObjectClass objClass, final Set attributes) { String uidValue = null; // Check if __NAME__ is defined. The framework names attributes in any case, so __name__ defines it too (#212). + // Only the first spelling found is looked at. So __NAME__ given along with a __name__ without a value is refused + // for the missing value, here or in checkAttribute as the order of the set falls, not as a name given twice. Attribute givenName = AttributeUtil.find(Name.NAME, attributes); if (givenName == null || givenName.getValue() == null || givenName.getValue().isEmpty()) { throw new IllegalArgumentException(Name.NAME + " must be defined."); diff --git a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java index ac83e959..cd9d8286 100644 --- a/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java +++ b/OpenICF-xml-connector/src/test/java/org/forgerock/openicf/connectors/xml/XMLHandlerReloadTests.java @@ -301,6 +301,10 @@ public Object[][] attributesWithoutAName() { // __name__ is __NAME__ in another case; with no values its value is null, not an empty list (#212). {attributes(AttributeBuilder.build("__name__"), AttributeBuilder.buildPassword("secret-x".toCharArray()), AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "X"))}, + // An empty list is no value either; it reaches the isEmpty check, not the null check (#212). + {attributes(AttributeBuilder.build("__name__", Collections.emptyList()), + AttributeBuilder.buildPassword("secret-x".toCharArray()), + AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "X"))}, }; } @@ -423,6 +427,24 @@ public void renameIntoATakenNameLeavesTheEntryAndTheFileAlone(String nameAttribu List.of("null|Last-alice", "null|Last-bob")); } + /** update checks the name for a clash only once every attribute has passed its checks, as create does (#212). */ + @Test + public void renameIntoATakenNameWithABadAttributeIsRefusedForTheAttribute() throws Exception { + writeAccounts(file, "alice", "bob"); + FileTime past = FileTime.fromMillis(now() - HOUR); + Files.setLastModifiedTime(file.toPath(), past); + byte[] before = Files.readAllBytes(file.toPath()); + + // The taken name comes first, so a clash check made as the name is reached would throw AlreadyExistsException. + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> call(h -> h.update( + ObjectClass.ACCOUNT, new Uid("uid-alice"), attributes(AttributeBuilder.build(Name.NAME, "bob"), + AttributeBuilder.build("no-such-attribute", "x"))))); + + assertEquals(e.getMessage(), "Data field: no-such-attribute is not supported."); + assertEquals(Files.getLastModifiedTime(file.toPath()), past); + assertEquals(Files.readAllBytes(file.toPath()), before); + } + @Test public void updateIsInTheFileWhenTheLastUserLeaves() throws Exception { writeAccounts(file, "alice");