Skip to content

Commit

Permalink
Filter in compiles object views (+tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Dec 13, 2018
1 parent c787b82 commit 86003d8
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 28 deletions.
Expand Up @@ -23,6 +23,7 @@

import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.QNameUtil;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class CompiledObjectCollectionView implements DebugDumpable, Serializable
private DistinctSearchOptionType distinct;
private Boolean disableSorting;
private SearchBoxConfigurationType searchBoxConfiguration;
private ObjectFilter filter;

// Only used to construct "default" view definition. May be not needed later on.
public CompiledObjectCollectionView() {
Expand Down Expand Up @@ -141,6 +143,14 @@ public void setSearchBoxConfiguration(SearchBoxConfigurationType searchBoxConfig
this.searchBoxConfiguration = searchBoxConfiguration;
}

public ObjectFilter getFilter() {
return filter;
}

public void setFilter(ObjectFilter filter) {
this.filter = filter;
}

public boolean match(QName expectedObjectType, String expectedViewName) {
if (!QNameUtil.match(objectType, expectedObjectType)) {
return false;
Expand Down Expand Up @@ -176,7 +186,7 @@ public String debugDump(int indent) {
DebugUtil.debugDumpWithLabelToStringLn(sb, "distinct", distinct, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "disableSorting", disableSorting, indent + 1);
DebugUtil.debugDumpWithLabelToStringLn(sb, "searchBoxConfiguration", searchBoxConfiguration, indent + 1);
// TODO
DebugUtil.debugDumpWithLabel(sb, "filter", filter, indent + 1);
return sb.toString();
}

Expand Down
Expand Up @@ -51,6 +51,7 @@
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.PlusMinusZero;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.util.ItemDeltaItem;
import com.evolveum.midpoint.prism.util.ObjectDeltaObject;
import com.evolveum.midpoint.repo.api.RepositoryService;
Expand All @@ -76,6 +77,8 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.AbstractObjectTypeConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AdminGuiConfigurationRoleManagementType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AdminGuiConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ArchetypeType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CollectionSpecificationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DashboardWidgetType;
Expand All @@ -92,6 +95,7 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectFormsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectPolicyConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OtherPrivilegesLimitationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SearchBoxConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;
Expand Down Expand Up @@ -455,8 +459,22 @@ private void compileCollection(CompiledObjectCollectionView existingView, GuiObj
if (existingView.getCollection() != null) {
LOGGER.debug("Redefining collection in view {}", existingView.getViewName());
}
// TODO: resolve collection, apply filter
existingView.setCollection(collection);

// Compute and apply filter
ObjectReferenceType collectionRef = collection.getCollectionRef();
QName collectionRefType = collectionRef.getType();
ObjectFilter filter = null;

// TODO: support more cases
if (QNameUtil.match(ArchetypeType.COMPLEX_TYPE, collectionRefType)) {
filter = prismContext.queryFor(AssignmentHolderType.class)
.item(AssignmentHolderType.F_ARCHETYPE_REF).ref(collectionRef.getOid())
.buildFilter();
}

// TODO: resolve (read) collection if needed
existingView.setFilter(filter);
}

private void compileColumns(CompiledObjectCollectionView existingView, GuiObjectListViewType objectListViewType) {
Expand Down Expand Up @@ -505,8 +523,6 @@ private void compileSearchBox(CompiledObjectCollectionView existingView, GuiObje
existingView.setSearchBoxConfiguration(newSearchBoxConfig);
}



private void joinForms(ObjectFormsType objectForms, ObjectFormType newForm) {
objectForms.getObjectForm().removeIf(currentForm -> isTheSameObjectForm(currentForm, newForm));
objectForms.getObjectForm().add(newForm.clone());
Expand Down
Expand Up @@ -24,17 +24,23 @@
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

import com.evolveum.midpoint.model.api.authentication.CompiledUserProfile;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.schema.SearchResultList;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectQueryUtil;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ArchetypeType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentPolicyEnforcementType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RichHyperlinkType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
Expand All @@ -50,6 +56,7 @@ public class TestArchetypes extends AbstractInitializedModelIntegrationTest {
public static final File TEST_DIR = new File("src/test/resources/archetypes");

public static final File SYSTEM_CONFIGURATION_ARCHETYPES_FILE = new File(TEST_DIR, "system-configuration-archetypes.xml");
public static final String VIEW_ALL_EMPLOYEES_NAME = "All employees";

public static final File ARCHETYPE_EMPLOYEE_FILE = new File(TEST_DIR, "archetype-employee.xml");
protected static final String ARCHETYPE_EMPLOYEE_OID = "7135e68c-ee53-11e8-8025-170b77da3fd6";
Expand All @@ -61,13 +68,17 @@ public class TestArchetypes extends AbstractInitializedModelIntegrationTest {

public static final File ROLE_EMPLOYEE_BASE_FILE = new File(TEST_DIR, "role-employee-base.xml");
protected static final String ROLE_EMPLOYEE_BASE_OID = "e869d6c4-f6ef-11e8-b51f-df3e51bba129";

public static final File ROLE_USER_ADMINISTRATOR_FILE = new File(TEST_DIR, "role-user-administrator.xml");
protected static final String ROLE_USER_ADMINISTRATOR_OID = "6ae02e34-f8b0-11e8-9c40-87e142b606fe";

@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
super.initSystem(initTask, initResult);

repoAddObjectFromFile(ROLE_EMPLOYEE_BASE_FILE, initResult);
repoAddObjectFromFile(ARCHETYPE_TEST_FILE, initResult);
repoAddObjectFromFile(ROLE_USER_ADMINISTRATOR_FILE, initResult);
repoAddObjectFromFile(ARCHETYPE_EMPLOYEE_FILE, initResult);

addObject(SHADOW_GROUP_DUMMY_TESTERS_FILE, initTask, initResult);
}
Expand All @@ -78,23 +89,42 @@ protected File getSystemConfigurationFile() {
}

@Test
public void test050AddArchetypeEmployee() throws Exception {
final String TEST_NAME = "test050SetupJack";
public void test050AddArchetypeTest() throws Exception {
final String TEST_NAME = "test050AddArchetypeTest";
displayTestTitle(TEST_NAME);

Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

// WHEN
displayWhen(TEST_NAME);
addObject(ARCHETYPE_EMPLOYEE_FILE, task, result);
addObject(ARCHETYPE_TEST_FILE, task, result);

// THEN
displayThen(TEST_NAME);
assertSuccess(result);

PrismObject<ArchetypeType> archetypeEmployee = modelService.getObject(ArchetypeType.class, ARCHETYPE_EMPLOYEE_OID, null, task, result);
display("Archetype employee", archetypeEmployee);
PrismObject<ArchetypeType> archetypeTest = modelService.getObject(ArchetypeType.class, ARCHETYPE_TEST_OID, null, task, result);
display("Archetype test", archetypeTest);
}

@Test
public void test060AssignGuybrushUserAdministrator() throws Exception {
final String TEST_NAME = "test060AssignGuybrushUserAdministrator";
displayTestTitle(TEST_NAME);

Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

// WHEN
displayWhen(TEST_NAME);
assignRole(USER_GUYBRUSH_OID, ROLE_USER_ADMINISTRATOR_OID, task, result);

// THEN
displayThen(TEST_NAME);
assertSuccess(result);

// TODO: assert guybrush
}


Expand Down Expand Up @@ -134,11 +164,93 @@ public void test100AssignJackArchetypeEmployee() throws Exception {
.assertPluralLabel(ARCHETYPE_EMPLOYEE_DISPLAY_PLURAL_LABEL);
}

@Test
public void test102SearchEmployeeArchetypeRef() throws Exception {
final String TEST_NAME = "test102SearchEmployeeArchetypeRef";
displayTestTitle(TEST_NAME);

Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

ObjectQuery query = queryFor(UserType.class)
.item(UserType.F_ARCHETYPE_REF).ref(ARCHETYPE_EMPLOYEE_OID)
.build();

// WHEN
displayWhen(TEST_NAME);

SearchResultList<PrismObject<UserType>> searchResults = modelService.searchObjects(UserType.class, query, null, task, result);

// THEN
displayThen(TEST_NAME);
assertSuccess(result);
display("Search results", searchResults);
assertEquals("Wrong number of search results", 1, searchResults.size());
PrismObject<UserType> foundUser = searchResults.get(0);
assertUser(foundUser, "found user")
.assertName(USER_JACK_USERNAME)
.assertOid(USER_JACK_OID)
.assignments()
.assertAssignments(1)
.assertArchetype(ARCHETYPE_EMPLOYEE_OID)
.end()
.assertArchetypeRef(ARCHETYPE_EMPLOYEE_OID)
.roleMembershipRefs()
.assertRoleMemberhipRefs(1)
.assertArchetype(ARCHETYPE_EMPLOYEE_OID)
.end()
.getObject();
}

@Test
public void test104GetGuybryshCompiledUserProfile() throws Exception {
final String TEST_NAME = "test104GetGuybryshCompiledUserProfile";
displayTestTitle(TEST_NAME);

// GIVEN
login(USER_GUYBRUSH_USERNAME);

Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

// WHEN
CompiledUserProfile compiledUserProfile = modelInteractionService.getCompiledUserProfile(task, result);

// THEN
assertSuccess(result);

loginAdministrator();

ObjectFilter viewFilter = assertCompiledUserProfile(compiledUserProfile)
.assertAdditionalMenuLinks(0)
.assertUserDashboardLinks(0)
.assertObjectForms(0)
.assertUserDashboardWidgets(0)
.objectCollectionViews()
.single()
.assertName(VIEW_ALL_EMPLOYEES_NAME)
.assertFilter()
.getFilter();

ObjectQuery viewQuery = prismContext.queryFactory().createQuery(viewFilter, null);
SearchResultList<PrismObject<UserType>> searchResults = modelService.searchObjects(UserType.class, viewQuery, null, task, result);

display("Search results", searchResults);
assertEquals("Wrong number of search results", 1, searchResults.size());
PrismObject<UserType> foundUser = searchResults.get(0);
assertUser(foundUser, "found user")
.assertName(USER_JACK_USERNAME)
.assertOid(USER_JACK_OID);

}

@Test
public void test109UnassignJackArchetypeEmployee() throws Exception {
final String TEST_NAME = "test109UnassignJackArchetypeEmployee";
displayTestTitle(TEST_NAME);

loginAdministrator();

Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

Expand Down Expand Up @@ -274,19 +386,17 @@ public void test129UnassignJackArchetypeTest() throws Exception {
assertSuccess(result);

assertUserAfter(USER_JACK_OID)
.assignments()
.assertAssignments(0)
.end()
.assertNoArchetypeRef()
.roleMembershipRefs()
.assertRoleMemberhipRefs(0)
.end()
.links()
.assertNone();
.assignments()
.assertAssignments(0)
.end()
.assertNoArchetypeRef()
.roleMembershipRefs()
.assertRoleMemberhipRefs(0)
.end()
.links()
.assertNone();
}

// TODO: search by archetypeRef

// TODO: object template in archetype
// TODO: correct application of object template for new object (not yet stored)

Expand Down
Expand Up @@ -411,8 +411,8 @@ private void assertAddAccount(ModelContext<UserType> modelContext, boolean expec
}

@Test
public void test130GetAdminGuiConfig() throws Exception {
final String TEST_NAME = "test130GetAdminGuiConfig";
public void test130GetCompiledUserProfile() throws Exception {
final String TEST_NAME = "test130GetCompiledUserProfile";
displayTestTitle(TEST_NAME);

// GIVEN
Expand Down
@@ -0,0 +1,33 @@
<!--
~ Copyright (c) 2018 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<objectCollection oid="f61bcb4a-f8ae-11e8-9f5c-c3e7f27ee878"
xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:c="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"
xmlns:piracy="http://midpoint.evolveum.com/xml/ns/samples/piracy">
<name>Active employees</name>
<type>UserType</type>
<filter>
<q:equal>
<q:path>activation/effectiveStatus</q:path>
<q:value>enabled</q:value>
</q:equal>
</filter>
<baseCollection>
<collectionRef oid="7135e68c-ee53-11e8-8025-170b77da3fd6" type="ArchetypeType"/> <!-- Employee archetype -->
</baseCollection>
</objectCollection>

0 comments on commit 86003d8

Please sign in to comment.