Skip to content

Commit

Permalink
Expand "dependent children" linked objects test
Browse files Browse the repository at this point in the history
Project sub-orgs (project_users, project_groups) are automatically
created/modified/deleted on project creation/rename/deletion.
  • Loading branch information
mederly committed May 30, 2020
1 parent 6bfaaf1 commit e70c84a
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 61 deletions.
Expand Up @@ -13,6 +13,7 @@
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
Expand Down Expand Up @@ -51,8 +52,10 @@ public class TestLinkedObjects extends AbstractEmptyModelIntegrationTest {
private static final TestResource<UserType> USER_GRAMMI = new TestResource<>(TEST_DIR, "user-grammi.xml", "041d0c03-c322-4e0d-89ba-a2d49b732674");
private static final TestResource<UserType> USER_CUBBY = new TestResource<>(TEST_DIR, "user-cubby.xml", "7b8f2e00-a49e-40ff-a4bd-11b70bac89d3");

// Separate scenario
// Separate scenario (projects and sub-orgs)
private static final TestResource<ArchetypeType> ARCHETYPE_PROJECT = new TestResource<>(TEST_DIR, "archetype-project.xml", "4d3280a1-6514-4984-ac2c-7e56c05af258");
private static final TestResource<ArchetypeType> ARCHETYPE_PROJECT_USERS = new TestResource<>(TEST_DIR, "archetype-project-users.xml", "3af67ba4-183f-45e7-887e-4ae5ddff4cdf");
private static final TestResource<ArchetypeType> ARCHETYPE_PROJECT_GROUPS = new TestResource<>(TEST_DIR, "archetype-project-groups.xml", "a85bddc9-4ff0-475f-8ccc-17f9038d4ce1");

@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
Expand All @@ -77,6 +80,8 @@ public void initSystem(Task initTask, OperationResult initResult) throws Excepti
addObject(USER_CUBBY, initTask, initResult);

addObject(ARCHETYPE_PROJECT, initTask, initResult);
addObject(ARCHETYPE_PROJECT_USERS, initTask, initResult);
addObject(ARCHETYPE_PROJECT_GROUPS, initTask, initResult);

// predefinedTestMethodTracing = PredefinedTestMethodTracing.MODEL_LOGGING;
}
Expand Down Expand Up @@ -482,7 +487,7 @@ public void test300CreateNewWhistleHolder() throws Exception {
* Creates a project. Two children should be created automatically.
*/
@Test
public void test900CreateProject() throws Exception {
public void test700CreateProject() throws Exception {
given();
Task task = getTestTask();
OperationResult result = task.getResult();
Expand All @@ -509,4 +514,57 @@ public void test900CreateProject() throws Exception {
.display()
.assertParentOrgRefs(ariane.getOid());
}

/**
* Renames a project. Children should be renamed automatically.
*/
@Test
public void test710RenameProject() throws Exception {
given();
Task task = getTestTask();
OperationResult result = task.getResult();

when();
PrismObject<OrgType> ariane = assertSingleObjectByName(OrgType.class, "ariane", task, result);
ObjectDelta<OrgType> delta = deltaFor(OrgType.class)
.item(OrgType.F_NAME).replace(PolyString.fromOrig("ariane5"))
.asObjectDelta(ariane.getOid());

executeChanges(delta, null, task, result);

then();
assertSuccess(result);

assertOrgAfter(ariane.getOid())
.assertName("ariane5");

assertOrgByName("ariane5_users", "after")
.display()
.assertParentOrgRefs(ariane.getOid());

assertOrgByName("ariane5_groups", "after")
.display()
.assertParentOrgRefs(ariane.getOid());
}

/**
* Deletes a project. Children should be deleted automatically.
*/
@Test
public void test720DeleteProject() throws Exception {
given();
Task task = getTestTask();
OperationResult result = task.getResult();

when();
PrismObject<OrgType> ariane5 = assertSingleObjectByName(OrgType.class, "ariane5", task, result);
deleteObject(OrgType.class, ariane5.getOid(), task, result);

then();
assertSuccess(result);

assertNoObjectByName(OrgType.class, "ariane5", task, result);
assertNoObjectByName(OrgType.class, "ariane5_users", task, result);
assertNoObjectByName(OrgType.class, "ariane5_groups", task, result);
}
}
@@ -0,0 +1,37 @@
<!--
~ Copyright (c) 2020 Evolveum and contributors
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->

<archetype xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
oid="a85bddc9-4ff0-475f-8ccc-17f9038d4ce1">
<name>project-groups</name>
<documentation>
"Groups" sub-org for project org.
</documentation>

<inducement>
<focusMappings>
<mapping>
<strength>strong</strength>
<expression>
<script>
<code>
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType

ARCHETYPE_PROJECT_OID = '4d3280a1-6514-4984-ac2c-7e56c05af258'
parent = midpoint.findLinkedTarget(OrgType.class, ARCHETYPE_PROJECT_OID)
parent ? parent.name + '_groups' : null
</code>
</script>
</expression>
<target>
<path>name</path>
</target>
</mapping>
</focusMappings>
</inducement>

</archetype>
@@ -0,0 +1,37 @@
<!--
~ Copyright (c) 2020 Evolveum and contributors
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->

<archetype xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
oid="3af67ba4-183f-45e7-887e-4ae5ddff4cdf">
<name>project-users</name>
<documentation>
"Users" sub-org for project org.
</documentation>

<inducement>
<focusMappings>
<mapping>
<strength>strong</strength>
<expression>
<script>
<code>
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType

ARCHETYPE_PROJECT_OID = '4d3280a1-6514-4984-ac2c-7e56c05af258'
parent = midpoint.findLinkedTarget(OrgType.class, ARCHETYPE_PROJECT_OID)
parent ? parent.name + '_users' : null
</code>
</script>
</expression>
<target>
<path>name</path>
</target>
</mapping>
</focusMappings>
</inducement>

</archetype>
122 changes: 63 additions & 59 deletions model/model-intest/src/test/resources/linked/archetype-project.xml
Expand Up @@ -7,11 +7,13 @@

<archetype xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:s="http://midpoint.evolveum.com/xml/ns/public/model/scripting-3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
oid="4d3280a1-6514-4984-ac2c-7e56c05af258">
<name>project</name>
<documentation>
A project has two child objects: users and groups. These are to be created when project is created.
A project has two child objects: users and groups.
1. These are to be created when project is created.
2. They need to be deleted when project is deleted.
3. They need to be renamed when project is renamed.
</documentation>

<inducement>
Expand All @@ -30,39 +32,37 @@
<!-- no object spec, so executed on the object itself -->
<executeScript>
<s:pipeline>
<s:action>
<!-- old style -->
<s:type>execute-script</s:type>
<s:parameter>
<s:name>script</s:name>
<value xsi:type="ScriptExpressionEvaluatorType">
<code>
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType
<s:execute>
<s:script>
<code>
import com.evolveum.midpoint.xml.ns._public.common.common_3.ArchetypeType
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType

users = new OrgType(prismContext)
.name(input.name + '_users')
.beginAssignment()
.targetRef(input.oid, OrgType.COMPLEX_TYPE)
.end()
ARCHETYPE_USERS_OID = '3af67ba4-183f-45e7-887e-4ae5ddff4cdf'
ARCHETYPE_GROUPS_OID = 'a85bddc9-4ff0-475f-8ccc-17f9038d4ce1'

groups = new OrgType(prismContext)
.name(input.name + '_groups')
.beginAssignment()
.targetRef(input.oid, OrgType.COMPLEX_TYPE)
.end()
users = new OrgType(prismContext)
.beginAssignment()
.targetRef(input.oid, OrgType.COMPLEX_TYPE)
.end()
.beginAssignment()
.targetRef(ARCHETYPE_USERS_OID, ArchetypeType.COMPLEX_TYPE)
.end()

[users, groups] // Returning two objects - they will be created in the second action
</code>
</value>
</s:parameter>
<s:parameter>
<s:name>outputItem</s:name>
<value>OrgType</value>
</s:parameter>
</s:action>
<s:action>
<s:type>add</s:type> <!-- creates objects prepared above -->
</s:action>
groups = new OrgType(prismContext)
.beginAssignment()
.targetRef(input.oid, OrgType.COMPLEX_TYPE)
.end()
.beginAssignment()
.targetRef(ARCHETYPE_GROUPS_OID, ArchetypeType.COMPLEX_TYPE)
.end()

[users, groups] // Returning two objects - they will be created in the second action
</code>
</s:script>
<s:outputTypeName>OrgType</s:outputTypeName>
</s:execute>
<s:add/> <!-- creates objects prepared above -->
</s:pipeline>
</executeScript>
</scriptExecution>
Expand All @@ -71,63 +71,67 @@
<order>1</order> <!-- assigned to project -->
</inducement>

<!-- 2b: user -> device -->

<!--
Actually, this one is fake: assignments/inducements are NOT evaluated on object deletion (yet).
So the same rule is included as a global policy rule in system configuration.
-->
<inducement>
<policyRule>
<name>recompute-device-on-user-name-change</name>
<documentation>Recomputes a device when user's name or fullName changes.</documentation>
<name>delete-children-on-project-deletion</name>
<documentation>
Deletes children when project is deleted.
</documentation>
<policyConstraints>
<or>
<modification>
<item>name</item>
</modification>
<modification>
<item>fullName</item>
</modification>
</or>
<modification>
<operation>delete</operation>
</modification>
</policyConstraints>
<policyActions>
<scriptExecution>
<object>
<linkTarget>
<!-- i.e. all objects that brought this policy rule to the focus -->
<matchesRuleAssignment>true</matchesRuleAssignment>
</linkTarget>
<linkSource>
<type>OrgType</type>
<archetypeRef oid="3af67ba4-183f-45e7-887e-4ae5ddff4cdf"/> <!-- users -->
<archetypeRef oid="a85bddc9-4ff0-475f-8ccc-17f9038d4ce1"/> <!-- groups -->
</linkSource>
</object>
<executeScript>
<s:recompute/>
<s:delete/>
</executeScript>
</scriptExecution>
</policyActions>
</policyRule>
<order>2</order> <!-- assigned to the user -->
<order>1</order> <!-- assigned to project -->
</inducement>

<inducement>
<policyRule>
<name>recompute-device-on-membership-change</name>
<name>recompute-children-on-project-rename</name>
<documentation>
Recomputes all devices whose membership has changed.
Recomputes children when the project is renamed.
</documentation>
<policyConstraints>
<alwaysTrue/>
<modification>
<operation>modify</operation>
<item>name</item>
</modification>
</policyConstraints>
<policyActions>
<scriptExecution>
<object>
<linkTarget>
<changeSituation>changed</changeSituation>
<!-- i.e. all objects that brought this policy rule to the focus -->
<matchesRuleAssignment>true</matchesRuleAssignment>
</linkTarget>
<linkSource>
<type>OrgType</type>
<archetypeRef oid="3af67ba4-183f-45e7-887e-4ae5ddff4cdf"/> <!-- users -->
<archetypeRef oid="a85bddc9-4ff0-475f-8ccc-17f9038d4ce1"/> <!-- groups -->
</linkSource>
</object>
<executeScript>
<s:recompute/>
</executeScript>
</scriptExecution>
</policyActions>
</policyRule>
<order>2</order> <!-- assigned the user -->
<order>1</order> <!-- assigned to project -->
</inducement>

</archetype>
Expand Up @@ -48,4 +48,35 @@
</scriptExecution>
</policyActions>
</globalPolicyRule>

<globalPolicyRule>
<focusSelector>
<type>OrgType</type>
<archetypeRef oid="4d3280a1-6514-4984-ac2c-7e56c05af258" /> <!-- project -->
</focusSelector>
<name>delete-children-on-project-deletion</name>
<documentation>
Deletes children when project is deleted.
</documentation>
<policyConstraints>
<modification>
<operation>delete</operation>
</modification>
</policyConstraints>
<policyActions>
<scriptExecution>
<object>
<linkSource>
<type>OrgType</type>
<archetypeRef oid="3af67ba4-183f-45e7-887e-4ae5ddff4cdf"/> <!-- users -->
<archetypeRef oid="a85bddc9-4ff0-475f-8ccc-17f9038d4ce1"/> <!-- groups -->
</linkSource>
</object>
<executeScript>
<s:delete/>
</executeScript>
</scriptExecution>
</policyActions>
</globalPolicyRule>

</systemConfiguration>

0 comments on commit e70c84a

Please sign in to comment.