Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Mar 24, 2015
2 parents f72a5ac + 799896d commit a22b505
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 53 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package com.evolveum.midpoint.model.intest.gensync;

import com.evolveum.midpoint.model.api.ModelExecuteOptions;
import com.evolveum.midpoint.model.intest.TestModelServiceContract;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
Expand Down Expand Up @@ -58,6 +59,7 @@
import org.testng.AssertJUnit;
import org.testng.annotations.Test;

import java.io.File;
import java.util.Collection;

import static org.testng.AssertJUnit.assertEquals;
Expand All @@ -72,7 +74,9 @@
@ContextConfiguration(locations = {"classpath:ctx-model-intest-test-main.xml"})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestEditSchema extends AbstractGenericSyncTest {


public static final File LOOKUP_LANGUAGES_REPLACEMENT_FILE = new File(TEST_DIR, "lookup-languages-replacement.xml");

@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
super.initSystem(initTask, initResult);
Expand Down Expand Up @@ -819,10 +823,7 @@ public void test168LookupLanguagesDeleteRowByKey() throws Exception {
assertSteadyResources();
}

/**
* todo probably enable this test, implementation in repository is not available yet.
*/
@Test(enabled = false)
@Test
public void test170LookupLanguagesReplaceRows() throws Exception {
final String TEST_NAME="test170LookupLanguagesReplaceRows";
TestUtil.displayTestTile(this, TEST_NAME);
Expand All @@ -841,8 +842,13 @@ public void test170LookupLanguagesReplaceRows() throws Exception {
row2.setValue("ja");
row2.setLabel(PrismTestUtil.createPolyStringType("Mumbojumbo"));

LookupTableRowType row3 = new LookupTableRowType();
row3.setKey("en_PR"); // existing key
row3.setValue("en1");
row3.setLabel(PrismTestUtil.createPolyStringType("English (pirate1)"));

ObjectDelta<LookupTableType> delta = ObjectDelta.createModificationReplaceContainer(LookupTableType.class,
LOOKUP_LANGUAGES_OID, LookupTableType.F_ROW, prismContext, row1, row2);
LOOKUP_LANGUAGES_OID, LookupTableType.F_ROW, prismContext, row1, row2, row3);

// WHEN
TestUtil.displayWhen(TEST_NAME);
Expand All @@ -864,14 +870,56 @@ public void test170LookupLanguagesReplaceRows() throws Exception {

PrismContainer<LookupTableRowType> tableContainer = lookup.findContainer(LookupTableType.F_ROW);
assertNotNull("Table container missing", tableContainer);
assertEquals("Unexpected table container size", 2, tableContainer.size());
assertEquals("Unexpected table container size", 3, tableContainer.size());

assertLookupRow(tableContainer, "ja_JA", "ja", "Jabber");
assertLookupRow(tableContainer, "ja_MJ", "ja", "Mumbojumbo");
assertLookupRow(tableContainer, "en_PR", "en1", "English (pirate1)");

assertSteadyResources();
}


@Test
public void test180LookupLanguagesReplaceObject() throws Exception {
final String TEST_NAME="test180LookupLanguagesReplaceObject";
TestUtil.displayTestTile(this, TEST_NAME);

// GIVEN
Task task = taskManager.createTaskInstance(TestModelServiceContract.class.getName() + "." + TEST_NAME);
OperationResult result = task.getResult();

PrismObject<LookupTableType> replacement = PrismTestUtil.parseObject(LOOKUP_LANGUAGES_REPLACEMENT_FILE);
ObjectDelta<LookupTableType> delta = ObjectDelta.createAddDelta(replacement);

// WHEN
TestUtil.displayWhen(TEST_NAME);
ModelExecuteOptions options = ModelExecuteOptions.createOverwrite();
options.setRaw(true);
modelService.executeChanges(MiscSchemaUtil.createCollection(delta), options, task, result);

// THEN
TestUtil.displayThen(TEST_NAME);
result.computeStatus();
TestUtil.assertSuccess(result);

PrismObject<LookupTableType> lookup = getLookupTableAll(LOOKUP_LANGUAGES_OID, task, result);

result.computeStatus();
TestUtil.assertSuccess(result);

IntegrationTestTools.display("Languages", lookup);

assertEquals("Wrong lang lookup name", "Languages Replaced", lookup.asObjectable().getName().getOrig());

PrismContainer<LookupTableRowType> tableContainer = lookup.findContainer(LookupTableType.F_ROW);
assertNotNull("Table container missing", tableContainer);
assertEquals("Unexpected table container size", 1, tableContainer.size());

assertLookupRow(tableContainer, "fr_FR", "fr", "Francais");
assertSteadyResources();
}


private void assertLookupRow(PrismContainer<LookupTableRowType> tableContainer, String key, String value,
String label) {
for (PrismContainerValue<LookupTableRowType> row: tableContainer.getValues()) {
Expand Down
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010-2015 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.
-->

<lookupTable oid="70000000-0000-0000-1111-000000000001"
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
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>Languages Replaced</name>

<row>
<key>fr_FR</key>
<value>fr</value>
<label>Francais</label>
</row>

</lookupTable>
Expand Up @@ -554,7 +554,6 @@ private <T extends ObjectType> String overwriteAddObjectAttempt(PrismObject<T> o
updateFullObject(rObject, object);
RObject merged = (RObject) session.merge(rObject);
addLookupTableRows(session, rObject, modifications != null);
//todo handle lookuptable rows merge [lazyman]

if (getClosureManager().isEnabled()) {
OrgClosureManager.Operation operation;
Expand Down Expand Up @@ -724,18 +723,27 @@ private void addLookupTableRows(Session session, RObject object, boolean merge)
if (!(object instanceof RLookupTable)) {
return;
}

RLookupTable table = (RLookupTable) object;
if (table.getRows() == null) {
return;
}

if (!merge) {
if (merge) {
deleteLookupTableRows(session, table.getOid());
}
if (table.getRows() != null) {
for (RLookupTableRow row : table.getRows()) {
session.save(row);
}
} else {
//todo implement lookup table rows merge !!!!!!!!!
}
}

private void addLookupTableRows(Session session, String tableOid, Collection<PrismContainerValue> values, int currentId) {
for (PrismContainerValue value : values) {
LookupTableRowType rowType = new LookupTableRowType();
rowType.setupContainerValue(value);

RLookupTableRow row = RLookupTableRow.toRepo(tableOid, rowType);
row.setId(currentId);
currentId++;
session.save(row);
}
}

Expand All @@ -747,63 +755,45 @@ private void updateLookupTableData(Session session, RObject object, Collection<?
if (!(object instanceof RLookupTable)) {
throw new IllegalStateException("Object being modified is not a LookupTable; it is " + object.getClass());
}
RLookupTable rLookupTable = (RLookupTable) object;

// todo compute only if necessary
Query query = session.getNamedQuery("get.lookupTableLastId");
query.setString("oid", object.getOid());
Integer lastId = (Integer) query.uniqueResult();
if (lastId == null) {
lastId = 0;
}
final RLookupTable rLookupTable = (RLookupTable) object;
final String tableOid = object.getOid();

for (ItemDelta delta : modifications) {
if (!(delta instanceof ContainerDelta) || delta.getPath().size() != 1) {
throw new IllegalStateException("Wrong table delta sneaked into updateLookupTableData: class=" + delta.getClass() + ", path=" + delta.getPath());
}
// one "table" container modification
ContainerDelta containerDelta = (ContainerDelta) delta;

if (containerDelta.getValuesToDelete() != null) {
for (PrismContainerValue value : (Collection<PrismContainerValue>) containerDelta.getValuesToDelete()) {
query = session.getNamedQuery("delete.lookupTableDataRow");
query.setString("oid", object.getOid());
Query query = session.getNamedQuery("delete.lookupTableDataRow");
query.setString("oid", tableOid);
query.setInteger("id", RUtil.toInteger(value.getId()));
query.executeUpdate();
}
}
if (containerDelta.getValuesToAdd() != null) {
for (PrismContainerValue value : (Collection<PrismContainerValue>) containerDelta.getValuesToAdd()) {
lastId++;

LookupTableRowType table = new LookupTableRowType();
table.setupContainerValue(value);

RLookupTableRow row = RLookupTableRow.toRepo(object.getOid(), table);
row.setId(lastId);
session.save(row);
}
int currentId = findLastIdInRepo(session, tableOid) + 1;
addLookupTableRows(session, tableOid, containerDelta.getValuesToAdd(), currentId);
}
if (containerDelta.getValuesToReplace() != null) {
// rLookupTable.getRows().clear();
// for (PrismContainerValue rowValue : (Collection<PrismContainerValue>) containerDelta.getValuesToReplace()) {
//
// LookupTableRowType rowType = new LookupTableRowType();
// rowType.setupContainerValue(rowValue);
//
// if (rowType.getId() != null) {
// // TODO
// }
//
// RLookupTableRow rRow = RLookupTableRow.toRepo(object.getOid(), rowType);
// lastId++;
//
// rRow.setId(lastId);
// session.save(rRow);
// }
deleteLookupTableRows(session, tableOid);
addLookupTableRows(session, tableOid, containerDelta.getValuesToReplace(), 1);
}
}
}

private int findLastIdInRepo(Session session, String tableOid) {
Query query = session.getNamedQuery("get.lookupTableLastId");
query.setString("oid", tableOid);
Integer lastId = (Integer) query.uniqueResult();
if (lastId == null) {
lastId = 0;
}
return lastId;
}

@Override
public <T extends ObjectType> int countObjects(Class<T> type, ObjectQuery query, OperationResult result) {
Validate.notNull(type, "Object type must not be null.");
Expand Down

0 comments on commit a22b505

Please sign in to comment.