Skip to content

Commit

Permalink
more tests, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Feb 6, 2018
1 parent 3644b6a commit ec117d6
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 48 deletions.
Expand Up @@ -25,6 +25,7 @@
import com.evolveum.midpoint.repo.sql.data.common.RObjectReference;
import com.evolveum.midpoint.repo.sql.data.common.RUser;
import com.evolveum.midpoint.repo.sql.data.common.any.RAnyValue;
import com.evolveum.midpoint.repo.sql.data.common.container.RAssignment;
import com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString;
import com.evolveum.midpoint.repo.sql.data.common.enums.RActivationStatus;
import com.evolveum.midpoint.repo.sql.testing.QueryCountInterceptor;
Expand All @@ -48,6 +49,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

//import com.evolveum.midpoint.repo.sql.helpers.EntityModificationRegistry;
Expand Down Expand Up @@ -101,6 +103,7 @@ public void test100UpdateGivenNameAndActivation() throws Exception {
ObjectDelta delta = ObjectDelta.createEmptyModifyDelta(UserType.class, userOid, prismContext);
delta.addModificationReplaceProperty(UserType.F_NAME, new PolyString("ášdf", "asdf"));
delta.addModificationReplaceProperty(UserType.F_GIVEN_NAME, new PolyString("ášdf", "asdf"));
delta.addModificationReplaceProperty(UserType.F_LOCALE, "en-US");
delta.addModificationReplaceProperty(
new ItemPath(UserType.F_ACTIVATION, ActivationType.F_ADMINISTRATIVE_STATUS), ActivationStatusType.DISABLED);

Expand All @@ -110,13 +113,20 @@ public void test100UpdateGivenNameAndActivation() throws Exception {
AssertJUnit.assertEquals(4, queryCountInterceptor.getQueryCount());

Session session = factory.openSession();
RUser u = session.get(RUser.class, userOid);
try {
RUser u = session.get(RUser.class, userOid);

AssertJUnit.assertEquals(new RPolyString("ášdf", "asdf"), u.getName());
AssertJUnit.assertEquals(new RPolyString("ášdf", "asdf"), u.getNameCopy());

AssertJUnit.assertEquals(new RPolyString("ášdf", "asdf"), u.getName());
AssertJUnit.assertEquals(new RPolyString("ášdf", "asdf"), u.getNameCopy());
AssertJUnit.assertEquals(new RPolyString("ášdf", "asdf"), u.getGivenName());

AssertJUnit.assertEquals(new RPolyString("ášdf", "asdf"), u.getGivenName());
AssertJUnit.assertEquals(RActivationStatus.DISABLED, u.getActivation().getAdministrativeStatus());
AssertJUnit.assertEquals(u.getLocale(), "en-US");

AssertJUnit.assertEquals(RActivationStatus.DISABLED, u.getActivation().getAdministrativeStatus());
} finally {
session.close();
}
}

@Test
Expand All @@ -132,9 +142,13 @@ public void test110ReplaceExtensionProperty() throws Exception {
AssertJUnit.assertEquals(2, queryCountInterceptor.getQueryCount());

Session session = factory.openSession();
RUser u = session.get(RUser.class, userOid);
try {
RUser u = session.get(RUser.class, userOid);

assertAnyValues(u.getLongs(), LOOT);
assertAnyValues(u.getLongs(), LOOT);
} finally {
session.close();
}
}

private void assertAnyValues(Collection<? extends RAnyValue> collection, QName name, Object... values) {
Expand Down Expand Up @@ -178,19 +192,61 @@ public void test120AddExtensionProperty() throws Exception {
AssertJUnit.assertEquals(5, queryCountInterceptor.getQueryCount());

Session session = factory.openSession();
RUser u = session.get(RUser.class, userOid);
try {
RUser u = session.get(RUser.class, userOid);

assertAnyValues(u.getStrings(), WEAPON, "weapon1", "weapon2");
assertAnyValues(u.getStrings(), WEAPON, "weapon1", "weapon2");
} finally {
session.close();
}
}

@Test
public void test140AddAssignment() throws Exception {
// todo implement
}
public void test140AddDeleteAssignment() throws Exception {
OperationResult result = new OperationResult("test140AddDeleteAssignment");

@Test
public void test150DeleteAssignment() throws Exception {
// todo impelment
ObjectDelta delta = ObjectDelta.createEmptyModifyDelta(UserType.class, userOid, prismContext);

AssignmentType ass = new AssignmentType();
ass.setId(1L);
delta.addModificationDeleteContainer(UserType.F_ASSIGNMENT, ass);

ass = new AssignmentType();
ass.setDescription("asdf");
ass.setTargetRef(createRef(OrgType.COMPLEX_TYPE, "444"));
MetadataType metadata = new MetadataType();
metadata.setCreateChannel("zzz");
metadata.getModifyApproverRef().add(createRef(UserType.COMPLEX_TYPE, "555"));
ass.setMetadata(metadata);
delta.addModificationAddContainer(UserType.F_ASSIGNMENT, ass);

queryCountInterceptor.startCounter();
repositoryService.modifyObject(UserType.class, userOid, delta.getModifications(), result);

// todo this should be only 7 queries, these two aren't expected:
// select createappr0_.owner_id as owner_id1_14_0_, createappr0_.owner_owner_oid as owner_ow2_14_0_, createappr0_.reference_type as referenc3_14_0_, createappr0_.relation as relation4_14_0_, createappr0_.targetOid as targetOi5_14_0_, createappr0_.owner_id as owner_id1_14_1_, createappr0_.owner_owner_oid as owner_ow2_14_1_, createappr0_.reference_type as referenc3_14_1_, createappr0_.relation as relation4_14_1_, createappr0_.targetOid as targetOi5_14_1_, createappr0_.targetType as targetTy6_14_1_ from m_assignment_reference createappr0_ where ( createappr0_.reference_type= 0) and createappr0_.owner_id=? and createappr0_.owner_owner_oid=?
// select modifyappr0_.owner_id as owner_id1_14_0_, modifyappr0_.owner_owner_oid as owner_ow2_14_0_, modifyappr0_.reference_type as referenc3_14_0_, modifyappr0_.relation as relation4_14_0_, modifyappr0_.targetOid as targetOi5_14_0_, modifyappr0_.owner_id as owner_id1_14_1_, modifyappr0_.owner_owner_oid as owner_ow2_14_1_, modifyappr0_.reference_type as referenc3_14_1_, modifyappr0_.relation as relation4_14_1_, modifyappr0_.targetOid as targetOi5_14_1_, modifyappr0_.targetType as targetTy6_14_1_ from m_assignment_reference modifyappr0_ where ( modifyappr0_.reference_type= 1) and modifyappr0_.owner_id=? and modifyappr0_.owner_owner_oid=?
AssertJUnit.assertEquals(9, queryCountInterceptor.getQueryCount());

Session session = factory.openSession();
try {
RUser u = session.get(RUser.class, userOid);

Set<RAssignment> assignments = u.getAssignments();
AssertJUnit.assertEquals(1, assignments.size());

RAssignment a = assignments.iterator().next();
AssertJUnit.assertEquals("zzz", a.getCreateChannel());

ObjectReferenceType targetRef = a.getTargetRef().toJAXB(prismContext);
AssertJUnit.assertEquals(createRef(OrgType.COMPLEX_TYPE, "444", SchemaConstants.ORG_DEFAULT), targetRef);

assertReferences((Collection) a.getModifyApproverRef(),
RObjectReference.copyFromJAXB(createRef(UserType.COMPLEX_TYPE, "555", SchemaConstants.ORG_DEFAULT), new RObjectReference())
);
} finally {
session.close();
}
}

@Test
Expand All @@ -210,11 +266,15 @@ public void test160AddDeleteParentRef() throws Exception {
AssertJUnit.assertEquals(5, queryCountInterceptor.getQueryCount());

Session session = factory.openSession();
RUser u = session.get(RUser.class, userOid);

assertReferences((Collection) u.getParentOrgRef(),
RObjectReference.copyFromJAXB(createRef(OrgType.COMPLEX_TYPE, "456", SchemaConstants.ORG_DEFAULT), new RObjectReference()),
RObjectReference.copyFromJAXB(createRef(OrgType.COMPLEX_TYPE, "789", SchemaConstants.ORG_DEFAULT), new RObjectReference()));
try {
RUser u = session.get(RUser.class, userOid);

assertReferences((Collection) u.getParentOrgRef(),
RObjectReference.copyFromJAXB(createRef(OrgType.COMPLEX_TYPE, "456", SchemaConstants.ORG_DEFAULT), new RObjectReference()),
RObjectReference.copyFromJAXB(createRef(OrgType.COMPLEX_TYPE, "789", SchemaConstants.ORG_DEFAULT), new RObjectReference()));
} finally {
session.close();
}
}

private ObjectReferenceType createRef(QName type, String oid) {
Expand All @@ -225,6 +285,7 @@ private ObjectReferenceType createRef(QName type, String oid, QName relation) {
ObjectReferenceType ref = new ObjectReferenceType();
ref.setType(type);
ref.setOid(oid);
ref.setRelation(relation);

return ref;
}
Expand All @@ -235,7 +296,9 @@ private void assertReferences(Collection<ObjectReference> collection, ObjectRefe
for (ObjectReference ref : collection) {
boolean found = false;
for (ObjectReference exp : expected) {
if (ref.equals(exp)) {
if (Objects.equals(exp.getRelation(), ref.getRelation())
&& Objects.equals(exp.getTargetOid(), ref.getTargetOid())
&& Objects.equals(exp.getType(), ref.getType())) {
found = true;
break;
}
Expand All @@ -247,6 +310,7 @@ private void assertReferences(Collection<ObjectReference> collection, ObjectRefe
}
}


@Test
public void test170ModifyEmployeeTypeAndMetadataCreateChannel() throws Exception {
OperationResult result = new OperationResult("test170ModifyEmployeeTypeAndMetadataCreateChannel");
Expand Down Expand Up @@ -294,8 +358,6 @@ public void test180ModifyMetadataChannel() throws Exception {
RObjectReference.copyFromJAXB(createRef(UserType.COMPLEX_TYPE, "111", SchemaConstants.ORG_DEFAULT), new RObjectReference()));
}

// todo activation status enum is probably not translated correctly

public <T extends ObjectType> void addLinkRef() throws Exception {
String oid = null;

Expand All @@ -307,22 +369,10 @@ public <T extends ObjectType> void addLinkRef() throws Exception {

// delta.addModificationReplaceProperty(
// new ItemPath(UserType.F_ACTIVATION, ActivationType.F_ADMINISTRATIVE_STATUS), ActivationStatusType.DISABLED);
// delta.addModificationReplaceProperty(UserType.F_LOCALE, "en-US");

// ActivationType activation = new ActivationType();
// activation.setAdministrativeStatus(ActivationStatusType.ENABLED);
// delta.addModificationAddContainer(
// new ItemPath(UserType.F_ASSIGNMENT, 1, AssignmentType.F_ACTIVATION), activation.asPrismContainerValue());

// AssignmentType ass = new AssignmentType();
// ass.setId(1L);
// delta.addModificationDeleteContainer(UserType.F_ASSIGNMENT, ass);
//
// ass = new AssignmentType();
// ass.setDescription("asdf");
// delta.addModificationAddContainer(UserType.F_ASSIGNMENT, ass);

// delta.addModificationReplaceProperty(
// new ItemPath(UserType.F_EXTENSION, new QName("http://example.com/p", "loot")), 34);
}
}
Expand Up @@ -299,6 +299,22 @@ public void setLifecycleState(String lifecycleState) {
@Override
public void setTransient(Boolean trans) {
this.trans = trans;

if (!Boolean.TRUE.equals(trans)) {
return;
}

if (getCreateApproverRef() != null) {
for (RContainerReference ref : getCreateApproverRef()) {
ref.setTransient(true);
}
}

if (getModifyApproverRef() != null) {
for (RContainerReference ref : getModifyApproverRef()) {
ref.setTransient(true);
}
}
}

public void setOwner(RObject owner) {
Expand Down
Expand Up @@ -43,7 +43,7 @@
@javax.persistence.Index(name = "iAssignmentReferenceTargetOid", columnList = "targetOid")
})
@Persister(impl = MidPointSingleTablePersister.class)
public class RAssignmentReference extends RContainerReference {
public class RAssignmentReference extends RContainerReference {

private RAssignment owner;

Expand Down
Expand Up @@ -24,10 +24,12 @@
import com.evolveum.midpoint.repo.sql.data.common.other.RObjectType;
import com.evolveum.midpoint.repo.sql.query2.definition.NotQueryable;
import com.evolveum.midpoint.repo.sql.util.ClassMapper;
import com.evolveum.midpoint.repo.sql.util.EntityState;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import org.apache.commons.lang.Validate;

import javax.persistence.Transient;
import java.util.Objects;

/**
Expand All @@ -41,12 +43,14 @@
* almost all the code.
*
*/
public abstract class RContainerReference extends RReference implements ObjectReference {
public abstract class RContainerReference extends RReference implements ObjectReference, EntityState {

public static final String REFERENCE_TYPE = "reference_type";

public static final String F_OWNER = "owner";

private Boolean trans;

private RCReferenceOwner referenceType;

//owner
Expand Down Expand Up @@ -93,6 +97,16 @@ public void setOwnerId(Integer ownerId) {
this.ownerId = ownerId;
}

@Transient
public Boolean isTransient() {
return trans;
}

@Override
public void setTransient(Boolean trans) {
this.trans = trans;
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Expand Up @@ -42,6 +42,7 @@
import com.evolveum.midpoint.repo.sql.helpers.modify.MapperContext;
import com.evolveum.midpoint.repo.sql.helpers.modify.PrismEntityMapper;
import com.evolveum.midpoint.repo.sql.helpers.modify.PrismEntityPair;
import com.evolveum.midpoint.repo.sql.util.EntityState;
import com.evolveum.midpoint.repo.sql.util.PrismIdentifierGenerator;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.schema.util.FullTextSearchConfigurationUtil;
Expand Down Expand Up @@ -740,6 +741,11 @@ private void handleOneToMany(Collection collection, ItemDelta delta, Attribute a
// handle delete
if (delta.isDelete()) {
Collection<PrismEntityPair<?>> valuesToDelete = processDeltaValues(delta.getValuesToDelete(), outputType, delta, bean);
valuesToDelete.stream().forEach(pair -> {
if (pair.getRepository() instanceof EntityState) {
((EntityState) pair.getRepository()).setTransient(false);
}
});
deleteValues(collection, valuesToDelete);
}
}
Expand Down
Expand Up @@ -458,17 +458,6 @@ public <T extends ObjectType> void modifyObjectAttempt(Class<T> type, String oid
LOGGER.trace("Save finished.");
// new implementation end

//todo remove, just for debugging
// LOGGER.trace(">>>> LOADING MODIFIED OBJECT");
// rObject = session.get(rObject.getClass(), oid);
// LOGGER.trace(">>>> MODIFIED OBJECT\n{}", ToStringBuilder.reflectionToString(rObject, ToStringStyle.MULTI_LINE_STYLE));

// LOGGER.trace(">>>> FAKE MERGE START");
// rObject = createDataObjectFromJAXB(prismObject, PrismIdentifierGenerator.Operation.MODIFY);
// updateFullObject(rObject, prismObject);
// session.merge(rObject);
// LOGGER.trace(">>>> FAKE MERGE FINISH");

if (closureManager.isEnabled()) {
closureManager.updateOrgClosure(originalObject, modifications, session, oid, type, OrgClosureManager.Operation.MODIFY, closureContext);
}
Expand Down

0 comments on commit ec117d6

Please sign in to comment.