Skip to content

Commit

Permalink
Fixing big bug concerning refined schema. Fixing several tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Jun 13, 2017
1 parent 0a8037b commit 634a2c7
Show file tree
Hide file tree
Showing 26 changed files with 336 additions and 103 deletions.
Expand Up @@ -409,6 +409,10 @@ public void addAuxiliaryObjectClass(String name, DummyObjectClass objectClass) {
auxiliaryObjectClassMap.put(name, objectClass);
}

public int getNumberOfObjectclasses() {
return 4 + auxiliaryObjectClassMap.size();
}

public Collection<DummyAccount> listAccounts() throws ConnectException, FileNotFoundException, SchemaViolationException, ConflictException {
checkBlockOperations();
breakIt(getBreakMode, "get");
Expand Down
Expand Up @@ -605,4 +605,10 @@ public void revive(PrismContext prismContext) {
refinedObjectClassDefinition.revive(prismContext);
}

@Override
public String toString() {
return "LROCDef("+layer+": "
+ refinedObjectClassDefinition + ")";
}

}
Expand Up @@ -139,6 +139,7 @@ default PrismObject<ShadowType> createBlankShadow() {

ResourceShadowDiscriminator getShadowDiscriminator();

@Override
boolean matches(ShadowType shadowType);
//endregion

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2016 Evolveum
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,6 @@
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.schema.PrismSchema;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.processor.ObjectClassComplexTypeDefinition;
import com.evolveum.midpoint.schema.processor.ResourceSchema;
Expand Down Expand Up @@ -97,6 +96,6 @@ static RefinedResourceSchema getRefinedSchema(PrismObject<ResourceType> resource

static ResourceSchema getResourceSchema(PrismObject<ResourceType> resource, PrismContext prismContext)
throws SchemaException {
return RefinedResourceSchemaImpl.getRefinedSchema(resource, prismContext);
return RefinedResourceSchemaImpl.getResourceSchema(resource, prismContext);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2016 Evolveum
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,25 +35,33 @@
import java.util.*;

/**
* TODO: this whole class would benefit from more refactoring.
* TODO: especially the parsing part.
*
* @author semancik
*
* @author mederly
*/
public class RefinedResourceSchemaImpl implements RefinedResourceSchema {

private static final String USER_DATA_KEY_PARSED_RESOURCE_SCHEMA = RefinedResourceSchema.class.getName()+".parsedResourceSchema";
private static final String USER_DATA_KEY_REFINED_SCHEMA = RefinedResourceSchema.class.getName()+".refinedSchema";

// TODO really don't remember why we include originalResourceSchema here instead of simply extending ResourceSchemaImpl ...
// TODO Maybe that's because we need to create new RefinedResourceSchema(s) based on existing ResourceSchema(s)?
// Original resource schema is there to make parsing easier.
// But it is also useful in some cases, e.g. we do not need to pass both refined schema and
// original schema as a metod parameter.
private ResourceSchema originalResourceSchema;

// This object contains the real data of the refined schema
private ResourceSchema resourceSchema;

private RefinedResourceSchemaImpl(@NotNull ResourceSchema originalResourceSchema) {
this.originalResourceSchema = originalResourceSchema;
this.resourceSchema = new ResourceSchemaImpl(originalResourceSchema.getNamespace(), originalResourceSchema.getPrismContext());
}

@Override
public List<? extends RefinedObjectClassDefinition> getRefinedDefinitions() {
return originalResourceSchema.getDefinitions(RefinedObjectClassDefinition.class);
return resourceSchema.getDefinitions(RefinedObjectClassDefinition.class);
}

@Override
Expand Down Expand Up @@ -401,6 +409,7 @@ public static RefinedResourceSchema parse(ResourceType resourceType, PrismContex
// return false;
// }

// TODO: The whole parsing is a big mess. TODO: refactor parsing
private static void parseObjectTypeDefsFromSchemaHandling(RefinedResourceSchemaImpl rSchema, ResourceType resourceType,
SchemaHandlingType schemaHandling, Collection<ResourceObjectTypeDefinitionType> resourceObjectTypeDefs,
ShadowKindType impliedKind, PrismContext prismContext, String contextDescription) throws SchemaException {
Expand Down Expand Up @@ -466,106 +475,106 @@ private static void parseObjectTypesFromSchema(RefinedResourceSchemaImpl rSchema
//endregion

private void add(RefinedObjectClassDefinition rOcDef) {
((ResourceSchemaImpl) originalResourceSchema).add(rOcDef); // TODO FIXME
((ResourceSchemaImpl) resourceSchema).add(rOcDef);
}

//region Delegations
@Override
public ObjectClassComplexTypeDefinition findObjectClassDefinition(QName objectClassQName) {
return originalResourceSchema.findObjectClassDefinition(objectClassQName);
return resourceSchema.findObjectClassDefinition(objectClassQName);
}

@Override
public ObjectClassComplexTypeDefinition findObjectClassDefinition(ShadowKindType kind, String intent) {
return originalResourceSchema.findObjectClassDefinition(kind, intent);
return resourceSchema.findObjectClassDefinition(kind, intent);
}

@Override
public ObjectClassComplexTypeDefinition findDefaultObjectClassDefinition(ShadowKindType kind) {
return originalResourceSchema.findDefaultObjectClassDefinition(kind);
return resourceSchema.findDefaultObjectClassDefinition(kind);
}

@Override
public String getNamespace() {
return originalResourceSchema.getNamespace();
return resourceSchema.getNamespace();
}

@Override
@NotNull
public Collection<Definition> getDefinitions() {
return originalResourceSchema.getDefinitions();
return resourceSchema.getDefinitions();
}

@Override
@NotNull
public <T extends Definition> List<T> getDefinitions(@NotNull Class<T> type) {
return originalResourceSchema.getDefinitions(type);
return resourceSchema.getDefinitions(type);
}

@Override
public PrismContext getPrismContext() {
return originalResourceSchema.getPrismContext();
return resourceSchema.getPrismContext();
}

@Override
@NotNull
public Document serializeToXsd() throws SchemaException {
return originalResourceSchema.serializeToXsd();
return resourceSchema.serializeToXsd();
}

@Override
public boolean isEmpty() {
return originalResourceSchema.isEmpty();
return resourceSchema.isEmpty();
}

@NotNull
@Override
public <ID extends ItemDefinition> List<ID> findItemDefinitionsByCompileTimeClass(
@NotNull Class<?> compileTimeClass, @NotNull Class<ID> definitionClass) {
return originalResourceSchema.findItemDefinitionsByCompileTimeClass(compileTimeClass, definitionClass);
return resourceSchema.findItemDefinitionsByCompileTimeClass(compileTimeClass, definitionClass);
}

@Nullable
@Override
public <TD extends TypeDefinition> TD findTypeDefinitionByCompileTimeClass(@NotNull Class<?> compileTimeClass, @NotNull Class<TD> definitionClass) {
return originalResourceSchema.findTypeDefinitionByCompileTimeClass(compileTimeClass, definitionClass);
return resourceSchema.findTypeDefinitionByCompileTimeClass(compileTimeClass, definitionClass);
}

@Override
@Nullable
public <TD extends TypeDefinition> TD findTypeDefinitionByType(@NotNull QName typeName, @NotNull Class<TD> definitionClass) {
return originalResourceSchema.findTypeDefinitionByType(typeName, definitionClass);
return resourceSchema.findTypeDefinitionByType(typeName, definitionClass);
}

@Override
public String debugDump() {
return originalResourceSchema.debugDump();
return resourceSchema.debugDump();
}

@Override
public String debugDump(int indent) {
return originalResourceSchema.debugDump(indent);
return resourceSchema.debugDump(indent);
}

@Nullable
@Override
public <ID extends ItemDefinition> ID findItemDefinitionByType(
@NotNull QName typeName, @NotNull Class<ID> definitionType) {
return originalResourceSchema.findItemDefinitionByType(typeName, definitionType);
return resourceSchema.findItemDefinitionByType(typeName, definitionType);
}

@Override
@NotNull
public <ID extends ItemDefinition> List<ID> findItemDefinitionsByElementName(@NotNull QName elementName,
@NotNull Class<ID> definitionClass) {
return originalResourceSchema.findItemDefinitionsByElementName(elementName, definitionClass);
return resourceSchema.findItemDefinitionsByElementName(elementName, definitionClass);
}

@NotNull
@Override
public <TD extends TypeDefinition> Collection<? extends TD> findTypeDefinitionsByType(@NotNull QName typeName,
@NotNull Class<TD> definitionClass) {
return originalResourceSchema.findTypeDefinitionsByType(typeName, definitionClass);
return resourceSchema.findTypeDefinitionsByType(typeName, definitionClass);
}

//endregion
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2016 Evolveum
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,11 +37,13 @@
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import com.evolveum.midpoint.common.ResourceObjectPattern;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.Definition;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
Expand All @@ -68,6 +70,7 @@
/**
* @author semancik
*/
@Listeners({com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class})
public class TestRefinedSchema {

public static final String TEST_DIR_NAME = "src/test/resources/refinery";
Expand All @@ -88,8 +91,8 @@ public void setup() throws SchemaException, SAXException, IOException {
}

@Test
public void testParseFromResourceComplex() throws Exception {
final String TEST_NAME = "testParseFromResourceComplex";
public void test010ParseFromResourceComplex() throws Exception {
final String TEST_NAME = "test010ParseFromResourceComplex";
TestUtil.displayTestTile(TEST_NAME);

// GIVEN
Expand Down Expand Up @@ -128,8 +131,9 @@ private void assertLayerRefinedSchema(ResourceType resourceType, RefinedResource
}

@Test
public void testParseFromResourceSimple() throws JAXBException, SchemaException, SAXException, IOException {
System.out.println("\n===[ testParseFromResourceSimple ]===\n");
public void test020ParseFromResourceSimple() throws Exception {
final String TEST_NAME = "test020ParseFromResourceSimple";
TestUtil.displayTestTile(TEST_NAME);

// GIVEN
PrismContext prismContext = createInitializedPrismContext();
Expand All @@ -151,6 +155,15 @@ public void testParseFromResourceSimple() throws JAXBException, SchemaException,

private void assertRefinedSchema(ResourceType resourceType, RefinedResourceSchema rSchema,
LayerType sourceLayer, LayerType validationLayer, boolean assertEntitlements) {

assertEquals("Unexpected number of object classes in refined schema", 2, rSchema.getDefinitions().size());

for (Definition def: rSchema.getDefinitions()) {
if (!(def instanceof RefinedObjectClassDefinition)) {
AssertJUnit.fail("Non-refined definition sneaked into resource schema: "+def);
}
}

assertFalse("No account definitions", rSchema.getRefinedDefinitions(ShadowKindType.ACCOUNT).isEmpty());

RefinedObjectClassDefinition rAccountDef = rSchema.getRefinedDefinition(ShadowKindType.ACCOUNT, (String)null);
Expand Down Expand Up @@ -269,8 +282,9 @@ private void assertRefinedToLayer(ResourceAttributeDefinition attrDef, LayerType
}

@Test
public void testParseAccount() throws JAXBException, SchemaException, SAXException, IOException {
System.out.println("\n===[ testParseAccount ]===\n");
public void test100ParseAccount() throws Exception {
final String TEST_NAME = "test100ParseAccount";
TestUtil.displayTestTile(TEST_NAME);

// GIVEN
PrismContext prismContext = createInitializedPrismContext();
Expand Down Expand Up @@ -302,31 +316,34 @@ public void testParseAccount() throws JAXBException, SchemaException, SAXExcepti
}

@Test
public void testApplyAttributeDefinition() throws JAXBException, SchemaException, SAXException, IOException {
System.out.println("\n===[ testApplyAttributeDefinition ]===\n");
public void test110ApplyAttributeDefinition() throws Exception {
final String TEST_NAME = "test110ApplyAttributeDefinition";
TestUtil.displayTestTile(TEST_NAME);

// GIVEN
PrismContext prismContext = createInitializedPrismContext();

PrismObject<ResourceType> resource = prismContext.parseObject(RESOURCE_COMPLEX_FILE);

RefinedResourceSchema rSchema = RefinedResourceSchemaImpl.parse(resource, prismContext);
System.out.println("Refined schema:");
System.out.println(rSchema.debugDump(1));
RefinedObjectClassDefinition defaultAccountDefinition = rSchema.getDefaultRefinedDefinition(ShadowKindType.ACCOUNT);
assertNotNull("No refined default account definition in "+rSchema, defaultAccountDefinition);
System.out.println("Refined account definition:");
System.out.println(defaultAccountDefinition.debugDump());
System.out.println(defaultAccountDefinition.debugDump(1));

PrismObject<ShadowType> accObject = prismContext.parseObject(new File(TEST_DIR_NAME, "account-jack.xml"));
PrismContainer<Containerable> attributesContainer = accObject.findContainer(ShadowType.F_ATTRIBUTES);
System.out.println("Attributes container:");
System.out.println(attributesContainer.debugDump());
System.out.println(attributesContainer.debugDump(1));

// WHEN
attributesContainer.applyDefinition((PrismContainerDefinition)defaultAccountDefinition.toResourceAttributeContainerDefinition(), true);

// THEN
System.out.println("Parsed account:");
System.out.println(accObject.debugDump());
System.out.println(accObject.debugDump(1));

assertAccountShadow(accObject, resource, prismContext);
}
Expand Down Expand Up @@ -376,8 +393,9 @@ private QName getAttrQName(PrismObject<ResourceType> resource, String localPart)
}

@Test
public void testCreateShadow() throws JAXBException, SchemaException, SAXException, IOException {
System.out.println("\n===[ testCreateShadow ]===\n");
public void test120CreateShadow() throws Exception {
final String TEST_NAME = "test120CreateShadow";
TestUtil.displayTestTile(TEST_NAME);

// GIVEN
PrismContext prismContext = createInitializedPrismContext();
Expand Down Expand Up @@ -405,8 +423,9 @@ public void testCreateShadow() throws JAXBException, SchemaException, SAXExcepti
}

@Test
public void testProtectedAccount() throws JAXBException, SchemaException, SAXException, IOException {
System.out.println("\n===[ testProtectedAccount ]===\n");
public void test130ProtectedAccount() throws Exception {
final String TEST_NAME = "test130ProtectedAccount";
TestUtil.displayTestTile(TEST_NAME);

// GIVEN
PrismContext prismContext = createInitializedPrismContext();
Expand Down Expand Up @@ -531,8 +550,8 @@ private ResourceAttribute<String> createStringAttribute(QName attrName, String v
}

@Test
public void testParseFromResourcePosix() throws Exception {
final String TEST_NAME = "testParseFromResourcePosix";
public void test140ParseFromResourcePosix() throws Exception {
final String TEST_NAME = "test140ParseFromResourcePosix";
TestUtil.displayTestTile(TEST_NAME);

// GIVEN
Expand Down

0 comments on commit 634a2c7

Please sign in to comment.