Skip to content

Commit

Permalink
Temporary workaround for MID-3249 Error when object has extra extensi…
Browse files Browse the repository at this point in the history
…on attributes in repo

We allow fixing such situations via Repository objects.
  • Loading branch information
mederly committed Jul 4, 2016
1 parent 04c6232 commit b459027
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 4 deletions.
Expand Up @@ -132,6 +132,7 @@ private ObjectViewDto loadObject() {
GetOperationOptions rootOptions = GetOperationOptions.createRaw();

rootOptions.setResolveNames(true);
rootOptions.setTolerateRawData(true);
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(rootOptions);
// FIXME: ObjectType.class will not work well here. We need more specific type.
//todo on page debug list create page params, put there oid and class for object type and send that to this page....read it here
Expand All @@ -155,7 +156,6 @@ private ObjectViewDto loadObject() {
GetOperationOptions.createRetrieve(RetrieveOption.INCLUDE)));
}


PrismObject<ObjectType> object = getModelService().getObject(type, objectOid.toString(), options, task, result);

PrismContext context = application.getPrismContext();
Expand Down
Expand Up @@ -201,6 +201,12 @@ public <T extends Objectable> PrismObject<T> parseObject(File file) throws Schem
return xnodeProcessor.parseObject(xnode, newParsingContext());
}

public <T extends Objectable> PrismObject<T> parseObject(File file, ParsingContext context) throws SchemaException, IOException {
Parser parser = findParser(file);
XNode xnode = parser.parse(file);
return xnodeProcessor.parseObject(xnode, context);
}

/**
* Parses a file and creates a prism from it.
*/
Expand Down
@@ -0,0 +1,115 @@
/*
* 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.
* 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.
*/
package com.evolveum.midpoint.prism;

import com.evolveum.midpoint.prism.foo.UserType;
import com.evolveum.midpoint.prism.parser.XNodeProcessorEvaluationMode;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import java.io.File;

import static com.evolveum.midpoint.prism.PrismInternalTestUtil.DEFAULT_NAMESPACE_PREFIX;
import static com.evolveum.midpoint.prism.PrismInternalTestUtil.constructInitializedPrismContext;
import static org.testng.AssertJUnit.assertNotNull;

/**
* See MID-3249.
*
* @author mederly
*/
public class TestUnknownItems {

public static final String TEST_DIR = "src/test/resources/common/xml";

public static final File WRONG_ITEM_FILE = new File(TEST_DIR + "/user-wrong-item.xml");
public static final File WRONG_NAMESPACE_FILE = new File(TEST_DIR + "/user-wrong-namespace.xml");

@BeforeSuite
public void setupDebug() {
PrettyPrinter.setDefaultNamespacePrefix(DEFAULT_NAMESPACE_PREFIX);
}

@Test(expectedExceptions = SchemaException.class)
public void test010ParseWrongItemStrict() throws Exception {
final String TEST_NAME = "testParseWrongItemStrict";
PrismInternalTestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = constructInitializedPrismContext();

// WHEN+THEN
try {
prismContext.parseObject(WRONG_ITEM_FILE);
} catch (SchemaException e) {
e.printStackTrace();
throw e;
}
}

@Test
public void test020ParseWrongItemCompat() throws Exception {
final String TEST_NAME = "testParseWrongItemCompat";
PrismInternalTestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = constructInitializedPrismContext();

// WHEN
PrismObject<UserType> user = prismContext.parseObject(WRONG_ITEM_FILE, ParsingContext.forMode(XNodeProcessorEvaluationMode.COMPAT));

// THEN
System.out.println("User:");
System.out.println(user.debugDump());
assertNotNull(user);
}

// Currently we simply mark the unknown value as raw.
// This might or might not be correct.
// (We should probably throw SchemaException instead.)
// TODO discuss this
@Test(enabled = false, expectedExceptions = SchemaException.class)
public void test110ParseWrongNamespaceStrict() throws Exception {
final String TEST_NAME = "test110ParseWrongNamespaceStrict";
PrismInternalTestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = constructInitializedPrismContext();

// WHEN+THEN
PrismObject<UserType> user = prismContext.parseObject(WRONG_NAMESPACE_FILE);
System.out.println("User:");
System.out.println(user.debugDump());
assertNotNull(user);
}

@Test
public void test120ParseWrongNamespaceCompat() throws Exception {
final String TEST_NAME = "test120ParseWrongNamespaceCompat";
PrismInternalTestUtil.displayTestTitle(TEST_NAME);
// GIVEN
PrismContext prismContext = constructInitializedPrismContext();

// WHEN
PrismObject<UserType> user = prismContext.parseObject(WRONG_NAMESPACE_FILE, ParsingContext.forMode(XNodeProcessorEvaluationMode.COMPAT));

// THEN
System.out.println("User:");
System.out.println(user.debugDump());
assertNotNull(user);
}


}
36 changes: 36 additions & 0 deletions infra/prism/src/test/resources/common/xml/user-wrong-item.xml
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~ 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.
-->
<user oid="c0c010c0-d34d-b33f-f00d-111111113333"
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='http://midpoint.evolveum.com/xml/ns/test/foo-1.xsd'
xmlns:a="http://prism.evolveum.com/xml/ns/public/annotation-3"
xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
xmlns:adhoc="http://midpoint.evolveum.com/xml/ns/test/adhoc-1.xsd"
xmlns:ext="http://midpoint.evolveum.com/xml/ns/test/extension">

<name>will</name>

<extension>
<ext:stringType_Wrong>FOObar</ext:stringType_Wrong>
</extension>

<fullName>Will Turner</fullName>
<givenName>William</givenName>
<familyName>Turner</familyName>

</user>
36 changes: 36 additions & 0 deletions infra/prism/src/test/resources/common/xml/user-wrong-namespace.xml
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~ 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.
-->
<user oid="c0c010c0-d34d-b33f-f00d-111111113333"
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='http://midpoint.evolveum.com/xml/ns/test/foo-1.xsd'
xmlns:a="http://prism.evolveum.com/xml/ns/public/annotation-3"
xmlns:t="http://prism.evolveum.com/xml/ns/public/types-3"
xmlns:adhoc="http://midpoint.evolveum.com/xml/ns/test/adhoc-1.xsd"
xmlns:ext="http://midpoint.evolveum.com/xml/ns/test/extension-wrong-namespace">

<name>will</name>

<extension>
<ext:stringType>FOObar</ext:stringType>
</extension>

<fullName>Will Turner</fullName>
<givenName>William</givenName>
<familyName>Turner</familyName>

</user>
1 change: 1 addition & 0 deletions infra/prism/testng-unit.xml
Expand Up @@ -26,6 +26,7 @@
<class name="com.evolveum.midpoint.prism.TestPrismParsingXml"/>
<class name="com.evolveum.midpoint.prism.TestPrismParsingJson"/>
<class name="com.evolveum.midpoint.prism.TestPrismParsingYaml"/>
<class name="com.evolveum.midpoint.prism.TestUnknownItems"/>
<class name="com.evolveum.midpoint.prism.TestCompare"/>
<class name="com.evolveum.midpoint.prism.TestCompareXml"/>
<class name="com.evolveum.midpoint.prism.TestCompareJson"/>
Expand Down
Expand Up @@ -82,6 +82,13 @@ public class GetOperationOptions implements Serializable, Cloneable {
*/
private Boolean raw;

/**
* Tolerate "raw" data in returned object. In some cases, raw data are tolerated by default (e.g. if raw=true
* and the object is ResourceType or ShadowType). But generally, toleration of raw data can be explicitly requested
* by setting this flag to TRUE.
*/
private Boolean tolerateRawData;

/**
* Force to get object from the resource even if some of the error occurred.
* If the any copy of the shadow is fetched, we can't delete this object
Expand Down Expand Up @@ -245,6 +252,30 @@ public static GetOperationOptions createResolveNames() {
return opts;
}

public Boolean getTolerateRawData() {
return tolerateRawData;
}

public void setTolerateRawData(Boolean value) {
this.tolerateRawData = value;
}

public static boolean isTolerateRawData(GetOperationOptions options) {
if (options == null) {
return false;
}
if (options.tolerateRawData == null) {
return false;
}
return options.tolerateRawData;
}

public static GetOperationOptions createTolerateRawData() {
GetOperationOptions opts = new GetOperationOptions();
opts.setTolerateRawData(true);
return opts;
}

public Boolean getRaw() {
return raw;
}
Expand Down
Expand Up @@ -516,10 +516,10 @@ private <T extends ObjectType> void validateObject(PrismObject<T> object, GetOpe
return;
}
Class<T> type = object.getCompileTimeClass();
boolean tolerateRaw = false;
boolean tolerateRaw = GetOperationOptions.isTolerateRawData(options);
if (type == ResourceType.class || ShadowType.class.isAssignableFrom(type) || type == ReportType.class) {
// We tolarate raw values for resource and shadows in case the user has requested so
tolerateRaw = options.isRaw(options);
// We tolerate raw values for resource and shadows in case the user has requested so
tolerateRaw = GetOperationOptions.isRaw(options);
}
if (hasError(object, result)) {
// If there is an error then the object might not be complete.
Expand Down

0 comments on commit b459027

Please sign in to comment.