Skip to content

Commit

Permalink
Add first test for metadata mapping
Browse files Browse the repository at this point in the history
(Mapping is not implemented yet.)

Related to MID-6275.
  • Loading branch information
mederly committed Jun 17, 2020
1 parent 0797209 commit 1a4ec87
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 45 deletions.
Expand Up @@ -49,6 +49,13 @@ public interface PrismValue extends Visitable, PathVisitable, Serializable, Debu
@Experimental
Optional<ValueMetadata> valueMetadata() throws SchemaException;

/**
* Maybe it is better to expect empty value metadata if these are absent.
* Client code would be simpler. HIGHLY EXPERIMENTAL.
*/
@Experimental
ValueMetadata getValueMetadata() throws SchemaException;

@Experimental
default void createLiveMetadata() {
}
Expand Down
Expand Up @@ -10,6 +10,7 @@
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.equivalence.ParameterizedEquivalenceStrategy;
import com.evolveum.midpoint.prism.impl.metadata.ValueMetadataAdapter;
import com.evolveum.midpoint.prism.metadata.ValueMetadataMockUpFactory;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.DebugUtil;
Expand Down Expand Up @@ -393,6 +394,22 @@ public Optional<ValueMetadata> valueMetadata() throws SchemaException {
}
}

@Override
public ValueMetadata getValueMetadata() throws SchemaException {
if (valueMetadata != null) {
return valueMetadata;
} else {
Optional<ValueMetadata> mockup = createMockUpValueMetadata();
if (mockup.isPresent()) {
return mockup.get();
} else if (prismContext != null && prismContext.getValueMetadataFactory() != null) {
return prismContext.getValueMetadataFactory().createEmpty();
} else {
return ValueMetadataAdapter.holding(new PrismContainerValueImpl<>());
}
}
}

@Override
@Experimental
public void createLiveMetadata() {
Expand Down
Expand Up @@ -145,10 +145,17 @@ public void setParent(Itemable parent) {
delegate.setParent(parent);
}

// TODO is this correct?
public Optional<ValueMetadata> valueMetadata() {
return Optional.of(this);
}

// TODO is this correct?
@Override
public ValueMetadata getValueMetadata() {
return this;
}

public @NotNull ItemPath getPath() {
return delegate.getPath();
}
Expand Down
Expand Up @@ -9763,7 +9763,11 @@
</xsd:appinfo>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="tns:AbstractMappingType" />
<xsd:extension base="tns:AbstractMappingType">
<xsd:sequence>
<xsd:element name="metadataHandling" type="tns:MetadataHandlingType" minOccurs="0" />
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="mapping" type="tns:MappingType"/>
Expand Down
Expand Up @@ -13,8 +13,9 @@

import com.evolveum.midpoint.prism.*;

import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.*;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
Expand All @@ -31,25 +32,38 @@
import javax.xml.datatype.XMLGregorianCalendar;

/**
* Tests the value metadata handling. Currently the only "handling" is creation of metadata mock-up.
* Tests the value metadata handling.
*/
@ContextConfiguration(locations = {"classpath:ctx-model-intest-test-main.xml"})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestValueMetadata extends AbstractInitializedModelIntegrationTest {
public class TestValueMetadata extends AbstractEmptyModelIntegrationTest {

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

private static final String NS_EXT_METADATA = "http://midpoint.evolveum.com/xml/ns/samples/metadata";
private static final ItemName LOA_NAME = new ItemName(NS_EXT_METADATA, "loa");
private static final ItemPath LOA_PATH = ItemPath.create(ObjectType.F_EXTENSION, LOA_NAME);

private static final File SYSTEM_CONFIGURATION_FILE = new File(TEST_DIR, "system-configuration.xml");
private static final TestResource<ObjectTemplateType> TEMPLATE_REGULAR_USER = new TestResource<>(TEST_DIR, "template-regular-user.xml", "b1005d3d-6ef4-4347-b235-313666824ed8");
private static final TestResource<UserType> USER_ALICE = new TestResource<>(TEST_DIR, "user-alice.xml", "9fc389be-5b47-4e9d-90b5-33fffd87b3ca");
private static final TestResource<UserType> USER_BOB = new TestResource<>(TEST_DIR, "user-bob.xml", "cab2344d-06c0-4881-98ee-7075bf5d1309");

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

addObject(TEMPLATE_REGULAR_USER, initTask, initResult);
addObject(USER_ALICE, initTask, initResult);
}

@Override
protected File getSystemConfigurationFile() {
return SYSTEM_CONFIGURATION_FILE;
}

@Test
public void test010TestLiveMetadata() throws SchemaException {
public void test010KeepingLiveMetadata() throws SchemaException {
given();
UserType mark = new UserType(prismContext)
.name("mark");
Expand Down Expand Up @@ -77,7 +91,7 @@ public void test010TestLiveMetadata() throws SchemaException {
}

@Test
public void test100CheckValueMetadata() throws Exception {
public void test020GettingMockUpMetadata() throws Exception {
given();

when();
Expand Down Expand Up @@ -143,6 +157,33 @@ public void test100CheckValueMetadata() throws Exception {
displayDumpable("assignment[111] admin status metadata", assignmentAdminStatusMetadata.get());
}

@Test
public void test100SimpleMetadataMapping() throws Exception {
given();
Task task = getTestTask();
OperationResult result = task.getResult();

when();
addObject(USER_BOB, task, result);

then();
assertUserAfter(USER_BOB.oid)
.display()
.valueMetadata(UserType.F_GIVEN_NAME)
.display()
.assertPropertyValuesEqual(LOA_PATH, "low")
.end()
.valueMetadata(UserType.F_FAMILY_NAME)
.display()
.assertPropertyValuesEqual(LOA_PATH, "high")
.end()
.assertFullName("Bob Green")
.valueMetadata(UserType.F_FULL_NAME)
.display()
// .assertPropertyValuesEqual(LOA_PATH, "low")
.end();
}

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private ValueMetadataType cast(Optional<ValueMetadata> metadata) {
//noinspection OptionalGetWithoutIsPresent
Expand Down
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
~ 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.
-->

<systemConfiguration oid="00000000-0000-0000-0000-000000000001" version="0"
xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3">
<name>SystemConfiguration</name>
<defaultObjectPolicyConfiguration>
<type>UserType</type>
<subtype>regular</subtype>
<objectTemplateRef oid="b1005d3d-6ef4-4347-b235-313666824ed8"/> <!-- template-regular-user -->
</defaultObjectPolicyConfiguration>
</systemConfiguration>
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010-2017 Evolveum and contributors
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->
<objectTemplate oid="b1005d3d-6ef4-4347-b235-313666824ed8"
xmlns='http://midpoint.evolveum.com/xml/ns/public/common/common-3'>
<name>Template for regular users</name>

<mapping>
<strength>strong</strength>
<source>
<path>givenName</path>
</source>
<source>
<path>familyName</path>
</source>
<expression>
<script>
<code>
givenName + ' ' + familyName
</code>
</script>
</expression>
<target>
<path>fullName</path>
</target>
<metadataHandling>
<mapping>
<source>
<path>extension/loa</path>
</source>
<expression>
<script>
<code>'low'</code> <!-- todo -->
</script>
</expression>
<target>
<path>extension/loa</path>
</target>
</mapping>
</metadataHandling>
</mapping>
</objectTemplate>
35 changes: 35 additions & 0 deletions model/model-intest/src/test/resources/metadata/user-bob.xml
@@ -0,0 +1,35 @@
<!--
~ 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.
-->

<user xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:mm="http://midpoint.evolveum.com/xml/ns/public/common/extension-metadata-mockup-3"
xmlns:sm="http://midpoint.evolveum.com/xml/ns/samples/metadata"
oid="cab2344d-06c0-4881-98ee-7075bf5d1309">
<name>bob</name>
<extension>
<mm:attachedValueMetadata>
<mm:path>givenName</mm:path>
<mm:metadata>
<c:extension>
<sm:loa>low</sm:loa>
</c:extension>
</mm:metadata>
</mm:attachedValueMetadata>
<mm:attachedValueMetadata>
<mm:path>familyName</mm:path>
<mm:metadata>
<c:extension>
<sm:loa>high</sm:loa>
</c:extension>
</mm:metadata>
</mm:attachedValueMetadata>
</extension>
<subtype>regular</subtype>
<givenName>Bob</givenName>
<familyName>Green</familyName>
</user>
1 change: 1 addition & 0 deletions model/model-intest/src/test/resources/schema/metadata.xsd
Expand Up @@ -86,5 +86,6 @@
</xsd:enumeration>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="loa" type="tns:LevelOfAssuranceType" />

</xsd:schema>
Expand Up @@ -7,6 +7,7 @@
package com.evolveum.midpoint.test.asserter;

import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.test.asserter.prism.PrismContainerValueAsserter;
import com.evolveum.midpoint.test.asserter.prism.PrismObjectAsserter;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExtensionType;
Expand Down Expand Up @@ -56,14 +57,14 @@ public ExtensionAsserter<O,OA,RA> assertAny() {
}

@Override
public <T> ExtensionAsserter<O,OA,RA> assertPropertyValuesEqual(QName propName, T... expectedValues) {
super.assertPropertyValuesEqual(propName, expectedValues);
public <T> ExtensionAsserter<O,OA,RA> assertPropertyValuesEqual(ItemPath path, T... expectedValues) {
super.assertPropertyValuesEqual(path, expectedValues);
return this;
}

@Override
public <T> ExtensionAsserter<O,OA,RA> assertPropertyValuesEqualRaw(QName attrName, T... expectedValues) {
super.assertPropertyValuesEqualRaw(attrName, expectedValues);
public <T> ExtensionAsserter<O,OA,RA> assertPropertyValuesEqualRaw(ItemPath path, T... expectedValues) {
super.assertPropertyValuesEqualRaw(path, expectedValues);
return this;
}

Expand All @@ -74,8 +75,8 @@ public <T> ExtensionAsserter<O,OA,RA> assertNoItem(QName itemName) {
}

@Override
public ExtensionAsserter<O,OA,RA> assertTimestampBetween(QName propertyName, XMLGregorianCalendar startTs, XMLGregorianCalendar endTs) {
super.assertTimestampBetween(propertyName, startTs, endTs);
public ExtensionAsserter<O,OA,RA> assertTimestampBetween(ItemPath path, XMLGregorianCalendar startTs, XMLGregorianCalendar endTs) {
super.assertTimestampBetween(path, startTs, endTs);
return this;
}

Expand Down
Expand Up @@ -16,17 +16,13 @@
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.crypto.EncryptionException;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.test.IntegrationTestTools;
import com.evolveum.midpoint.test.asserter.prism.PolyStringAsserter;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CredentialsStorageTypeType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CredentialsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.PasswordType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;

/**
* @author semancik
Expand Down Expand Up @@ -367,6 +363,13 @@ public UserAsserter<RA> assertRoleMemberhipRefs(int expected) {
return asserter;
}

@Override
public ValueMetadataAsserter<UserType, ? extends UserAsserter<RA>, RA> valueMetadata(
ItemPath path) throws SchemaException {
//noinspection unchecked
return (ValueMetadataAsserter<UserType, ? extends UserAsserter<RA>, RA>) super.valueMetadata(path);
}

@Override
public TriggersAsserter<UserType, ? extends UserAsserter<RA>, RA> triggers() {
TriggersAsserter<UserType, ? extends UserAsserter<RA>, RA> asserter = new TriggersAsserter<>(this, getDetails());
Expand Down

0 comments on commit 1a4ec87

Please sign in to comment.