From 1ce99a9475aa3a26ae0c3ff75a76fdeb70c810f1 Mon Sep 17 00:00:00 2001 From: Katarina Valalikova Date: Mon, 12 Feb 2018 22:31:25 +0100 Subject: [PATCH] some fixes for rpc generate/validate value... adding more tests --- .../midpoint/model/impl/ModelRestService.java | 8 +- .../ModelInteractionServiceImpl.java | 112 ++++++++++++------ .../testing/rest/TestAbstractRestService.java | 55 +++++++++ .../repo/json/policy-generate-bad-path.json | 3 +- .../repo/json/policy-generate-explicit.json | 11 ++ ...icy-validate-explicit-no-value-policy.json | 10 ++ .../repo/json/policy-validate-explicit.json | 3 - .../repo/xml/policy-generate-bad-path.xml | 1 + .../repo/xml/policy-generate-explicit.xml | 11 ++ ...licy-validate-explicit-no-value-policy.xml | 12 ++ .../repo/xml/policy-validate-explicit.xml | 3 - .../repo/yaml/policy-generate-bad-path.yml | 3 +- .../repo/yaml/policy-generate-explicit.yml | 6 + ...licy-validate-explicit-no-value-policy.yml | 4 + .../repo/yaml/policy-validate-explicit.yml | 4 +- 15 files changed, 197 insertions(+), 49 deletions(-) create mode 100644 testing/rest/src/test/resources/repo/json/policy-generate-explicit.json create mode 100644 testing/rest/src/test/resources/repo/json/policy-validate-explicit-no-value-policy.json create mode 100644 testing/rest/src/test/resources/repo/xml/policy-generate-explicit.xml create mode 100644 testing/rest/src/test/resources/repo/xml/policy-validate-explicit-no-value-policy.xml create mode 100644 testing/rest/src/test/resources/repo/yaml/policy-generate-explicit.yml create mode 100644 testing/rest/src/test/resources/repo/yaml/policy-validate-explicit-no-value-policy.yml diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelRestService.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelRestService.java index f6c868226f1..47363914140 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelRestService.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ModelRestService.java @@ -172,7 +172,10 @@ public Response generateValue(PolicyItemsDefinitionType p Task task = RestServiceUtil.initRequest(mc); OperationResult parentResult = task.getResult().createSubresult(OPERATION_GENERATE_VALUE_RPC); - return generateValue(null, policyItemsDefinition, task, parentResult); + Response response = generateValue(null, policyItemsDefinition, task, parentResult); + finishRequest(task); + + return response; } private Response generateValue(PrismObject object, PolicyItemsDefinitionType policyItemsDefinition, Task task, OperationResult parentResult){ @@ -183,7 +186,6 @@ private Response generateValue(PrismObject object, Pol try { modelInteraction.generateValue(object, policyItemsDefinition, task, parentResult); parentResult.computeStatusIfUnknown(); - if (parentResult.isSuccess()) { response = RestServiceUtil.createResponse(Response.Status.OK, policyItemsDefinition, parentResult, true); } else { @@ -191,7 +193,7 @@ private Response generateValue(PrismObject object, Pol } } catch (Exception ex) { - parentResult.computeStatus(); + parentResult.recordFatalError("Failed to generate value, " + ex.getMessage(), ex); response = RestServiceUtil.handleException(parentResult, ex); } } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/controller/ModelInteractionServiceImpl.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/controller/ModelInteractionServiceImpl.java index 45722e8e679..bbefdde8d25 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/controller/ModelInteractionServiceImpl.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/controller/ModelInteractionServiceImpl.java @@ -874,10 +874,10 @@ public String generateValue(ValuePolicyType policy, int d public void generateValue(PrismObject object, PolicyItemsDefinitionType policyItemsDefinition, Task task, OperationResult parentResult) throws ObjectAlreadyExistsException, ExpressionEvaluationException, SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, PolicyViolationException { - String oid = object.getOid(); + OperationResult result = parentResult.createSubresult(OPERATION_GENERATE_VALUE); - Class clazz = (Class) object.asObjectable().getClass(); + ValuePolicyType valuePolicy = null; try { valuePolicy = getValuePolicy(object, task, result); @@ -892,23 +892,7 @@ public void generateValue(PrismObject object, PolicyIt Collection> deltasToExecute = new ArrayList<>(); for (PolicyItemDefinitionType policyItemDefinition : policyItemsDefinition.getPolicyItemDefinition()) { OperationResult generateValueResult = parentResult.createSubresult(OPERATION_GENERATE_VALUE); - - ItemPath path = getPath(policyItemDefinition); - if (path == null) { - LOGGER.error("No item path defined in the target for policy item definition. Cannot generate value"); - generateValueResult.recordFatalError("No item path defined in the target for policy item definition. Cannot generate value"); - continue; - } - - result.addArbitraryObjectAsParam("policyItemPath", path); - - PrismPropertyDefinition propertyDef = getItemDefinition(object, path); - if (propertyDef == null) { - LOGGER.error("No definition for property {} in object. Is the path referencing prism property?" + path, object); - generateValueResult.recordFatalError("No definition for property " + path + " in object " + object + ". Is the path referencing prism property?"); - continue; - } - + LOGGER.trace("Default value policy: {}" , valuePolicy); try { generateValue(object, valuePolicy, policyItemDefinition, task, generateValueResult); @@ -919,7 +903,37 @@ public void generateValue(PrismObject object, PolicyIt policyItemDefinition.setResult(generateValueResult.createOperationResultType()); continue; } - collectDeltasForGeneratedValuesIfNeeded(object, policyItemDefinition, deltasToExecute, path, propertyDef); + + //TODO: not sure about the bulk actions here + ItemPath path = getPath(policyItemDefinition); + if (path == null) { + if (isExecute(policyItemDefinition)) { + LOGGER.error("No item path defined in the target for policy item definition. Cannot generate value"); + generateValueResult.recordFatalError( + "No item path defined in the target for policy item definition. Cannot generate value"); + continue; + } + } + + PrismPropertyDefinition propertyDef = null; + if (path != null) { + result.addArbitraryObjectAsParam("policyItemPath", path); + + propertyDef = getItemDefinition(object, path); + if (propertyDef == null) { + if (isExecute(policyItemDefinition)) { + LOGGER.error("No definition for property {} in object. Is the path referencing prism property?" + path, + object); + generateValueResult.recordFatalError("No definition for property " + path + " in object " + object + + ". Is the path referencing prism property?"); + continue; + } + + } + } + // end of not sure + + collectDeltasForGeneratedValuesIfNeeded(object, policyItemDefinition, deltasToExecute, path, propertyDef, generateValueResult); generateValueResult.computeStatusIfUnknown(); } result.computeStatus(); @@ -928,8 +942,15 @@ public void generateValue(PrismObject object, PolicyIt } try { if (!deltasToExecute.isEmpty()) { - - modelCrudService.modifyObject(clazz, oid, deltasToExecute, null, task, result); + if (object == null) { + LOGGER.error("Cannot execute changes for generated values, no object specified in request."); + result.recordFatalError("Cannot execute changes for generated values, no object specified in request."); + throw new SchemaException("Cannot execute changes for generated values, no object specified in request."); + } + String oid = object.getOid(); + Class clazz = (Class) object.asObjectable().getClass(); + modelCrudService.modifyObject(clazz, oid, deltasToExecute, null, task, result); + } } catch (ObjectNotFoundException | SchemaException | ExpressionEvaluationException | CommunicationException | ConfigurationException | ObjectAlreadyExistsException @@ -942,6 +963,13 @@ public void generateValue(PrismObject object, PolicyIt } + private boolean isExecute(PolicyItemDefinitionType policyItemDefinition) { + if (policyItemDefinition.isExecute() == null) { + return false; + } + + return policyItemDefinition.isExecute().booleanValue(); + } private ItemPath getPath(PolicyItemDefinitionType policyItemDefinition){ PolicyItemTargetType target = policyItemDefinition.getTarget(); @@ -970,22 +998,32 @@ private PrismPropertyDefinition getItemDefinition(Pris private void collectDeltasForGeneratedValuesIfNeeded(PrismObject object, PolicyItemDefinitionType policyItemDefinition, Collection> deltasToExecute, ItemPath path, - PrismPropertyDefinition itemDef) throws SchemaException { + PrismPropertyDefinition itemDef, OperationResult result) throws SchemaException { Object value = policyItemDefinition.getValue(); - if (ProtectedStringType.COMPLEX_TYPE.equals(itemDef.getTypeName())) { - ProtectedStringType pst = new ProtectedStringType(); - pst.setClearValue((String) value); - value = pst; - } else if (PolyStringType.COMPLEX_TYPE.equals(itemDef.getTypeName())) { - value = new PolyString((String) value); + if (itemDef != null){ + if (ProtectedStringType.COMPLEX_TYPE.equals(itemDef.getTypeName())) { + ProtectedStringType pst = new ProtectedStringType(); + pst.setClearValue((String) value); + value = pst; + } else if (PolyStringType.COMPLEX_TYPE.equals(itemDef.getTypeName())) { + value = new PolyString((String) value); + } + } + if (object == null && isExecute(policyItemDefinition)) { + LOGGER.warn("Cannot apply generated changes and cannot execute them becasue there is no target object specified."); + result.recordFatalError("Cannot apply generated changes and cannot execute them becasue there is no target object specified."); + return; } - PropertyDelta propertyDelta = PropertyDelta.createModificationReplaceProperty(path, object.getDefinition(), value); - propertyDelta.applyTo(object); // in bulk actions we need to modify original objects - hope that REST is OK with this - if (BooleanUtils.isTrue(policyItemDefinition.isExecute())) { - deltasToExecute.add(propertyDelta); + if (object != null) { + PropertyDelta propertyDelta = PropertyDelta.createModificationReplaceProperty(path, object.getDefinition(), value); + propertyDelta.applyTo(object); // in bulk actions we need to modify original objects - hope that REST is OK with this + if (BooleanUtils.isTrue(policyItemDefinition.isExecute())) { + deltasToExecute.add(propertyDelta); + } } + } private void generateValue(PrismObject object, ValuePolicyType defaultPolicy, @@ -994,11 +1032,15 @@ private void generateValue(PrismObject object, ValuePo ConfigurationException, SecurityViolationException { PolicyItemTargetType target = policyItemDefinition.getTarget(); - if (target == null || ItemPath.isNullOrEmpty(target.getPath())) { + if ((target == null || ItemPath.isNullOrEmpty(target.getPath())) && isExecute(policyItemDefinition)) { LOGGER.error("Target item path must be defined"); throw new SchemaException("Target item path must be defined"); } - ItemPath targetPath = target.getPath().getItemPath(); + ItemPath targetPath = null; + + if (target != null) { + targetPath = target.getPath().getItemPath(); + } ValuePolicyType valuePolicy = resolveValuePolicy(policyItemDefinition, defaultPolicy, task, result); LOGGER.trace("Value policy used for generating new value : {}", valuePolicy); diff --git a/testing/rest/src/test/java/com/evolveum/midpoint/testing/rest/TestAbstractRestService.java b/testing/rest/src/test/java/com/evolveum/midpoint/testing/rest/TestAbstractRestService.java index 051838adcc6..0c155d13955 100644 --- a/testing/rest/src/test/java/com/evolveum/midpoint/testing/rest/TestAbstractRestService.java +++ b/testing/rest/src/test/java/com/evolveum/midpoint/testing/rest/TestAbstractRestService.java @@ -89,7 +89,10 @@ public abstract class TestAbstractRestService extends RestServiceInitializer{ public static final String POLICY_ITEM_DEFINITION_GENERATE_EXECUTE = "policy-generate-execute"; public static final String POLICY_ITEM_DEFINITION_GENERATE_PASSWORD_EXECUTE = "policy-generate-password-execute"; public static final String POLICY_ITEM_DEFINITION_GENERATE_HONORIFIC_PREFIX_EXECUTE = "policy-generate-honorific-prefix-execute"; + public static final String POLICY_ITEM_DEFINITION_GENERATE_EXPLICIT = "policy-generate-explicit"; + public static final String POLICY_ITEM_DEFINITION_GENERATE_EXPLICIT_NO_VALUE_POLICY = "policy-generate-explicit-no-value-policy"; public static final String POLICY_ITEM_DEFINITION_VALIDATE_EXPLICIT = "policy-validate-explicit"; + public static final String POLICY_ITEM_DEFINITION_VALIDATE_EXPLICIT_NO_VALUE_POLICY = "policy-validate-explicit-no-value-policy"; public static final String POLICY_ITEM_DEFINITION_VALIDATE_EXPLICIT_CONFLICT = "policy-validate-explicit-conflict"; public static final String POLICY_ITEM_DEFINITION_VALIDATE_IMPLICIT_SINGLE = "policy-validate-implicit-single"; public static final String POLICY_ITEM_DEFINITION_VALIDATE_IMPLICIT_PASSWORD = "policy-validate-implicit-password"; @@ -1133,7 +1136,59 @@ public void test515validateValueImplicitPassword() throws Exception { getDummyAuditService().assertLoginLogout(SchemaConstants.CHANNEL_REST_URI); } + @Test + public void test516validateValueExplicitNoValuePolicy() throws Exception { + final String TEST_NAME = "test516validateValueExplicitNoValuePolicy"; + displayTestTitle(this, TEST_NAME); + + WebClient client = prepareClient(); + client.path("/rpc/validate"); + + getDummyAuditService().clear(); + + TestUtil.displayWhen(TEST_NAME); + Response response = client.post(getRepoFile(POLICY_ITEM_DEFINITION_VALIDATE_EXPLICIT_NO_VALUE_POLICY)); + + TestUtil.displayThen(TEST_NAME); + displayResponse(response); + + traceResponse(response); + + assertEquals("Expected 200 but got " + response.getStatus(), 200, response.getStatus()); + IntegrationTestTools.display("Audit", getDummyAuditService()); + getDummyAuditService().assertRecords(2); + getDummyAuditService().assertLoginLogout(SchemaConstants.CHANNEL_REST_URI); + + + } + + + @Test + public void test517generateValueExplicit() throws Exception { + final String TEST_NAME = "test517generateValueExplicit"; + displayTestTitle(this, TEST_NAME); + + WebClient client = prepareClient(); + client.path("/rpc/generate"); + + getDummyAuditService().clear(); + + TestUtil.displayWhen(TEST_NAME); + Response response = client.post(getRepoFile(POLICY_ITEM_DEFINITION_GENERATE_EXPLICIT)); + + TestUtil.displayThen(TEST_NAME); + displayResponse(response); + + traceResponse(response); + + assertEquals("Expected 200 but got " + response.getStatus(), 200, response.getStatus()); + + IntegrationTestTools.display("Audit", getDummyAuditService()); + getDummyAuditService().assertRecords(2); + getDummyAuditService().assertLoginLogout(SchemaConstants.CHANNEL_REST_URI); + } + @Test public void test600modifySecurityQuestionAnswer() throws Exception { final String TEST_NAME = "test600modifySecurityQuestionAnswer"; diff --git a/testing/rest/src/test/resources/repo/json/policy-generate-bad-path.json b/testing/rest/src/test/resources/repo/json/policy-generate-bad-path.json index 6ddde0bafa5..e22e046afb4 100644 --- a/testing/rest/src/test/resources/repo/json/policy-generate-bad-path.json +++ b/testing/rest/src/test/resources/repo/json/policy-generate-bad-path.json @@ -9,7 +9,8 @@ "valuePolicyRef": { "type": "http://midpoint.evolveum.com/xml/ns/public/common/common-3#ValuePolicyType", "oid": "00000000-0000-0000-1111-000000000003" - } + }, + "execute" : "true" } ] } diff --git a/testing/rest/src/test/resources/repo/json/policy-generate-explicit.json b/testing/rest/src/test/resources/repo/json/policy-generate-explicit.json new file mode 100644 index 00000000000..00e29802896 --- /dev/null +++ b/testing/rest/src/test/resources/repo/json/policy-generate-explicit.json @@ -0,0 +1,11 @@ +{ + "@ns": "http://midpoint.evolveum.com/xml/ns/public/common/api-types-3", + "policyItemsDefinition": { + "policyItemDefinition": [{ + "valuePolicyRef": { + "type": "http://midpoint.evolveum.com/xml/ns/public/common/common-3#ValuePolicyType", + "oid": "00000000-0000-0000-1111-000000000003" + } + }] + } +} \ No newline at end of file diff --git a/testing/rest/src/test/resources/repo/json/policy-validate-explicit-no-value-policy.json b/testing/rest/src/test/resources/repo/json/policy-validate-explicit-no-value-policy.json new file mode 100644 index 00000000000..8d933368fd0 --- /dev/null +++ b/testing/rest/src/test/resources/repo/json/policy-validate-explicit-no-value-policy.json @@ -0,0 +1,10 @@ +{ + "@ns": "http://midpoint.evolveum.com/xml/ns/public/common/api-types-3", + "policyItemsDefinition": { + "policyItemDefinition": [ + { + "value": "Ad123456" + } + ] + } +} \ No newline at end of file diff --git a/testing/rest/src/test/resources/repo/json/policy-validate-explicit.json b/testing/rest/src/test/resources/repo/json/policy-validate-explicit.json index 19aae9f9b29..0473067b368 100644 --- a/testing/rest/src/test/resources/repo/json/policy-validate-explicit.json +++ b/testing/rest/src/test/resources/repo/json/policy-validate-explicit.json @@ -3,9 +3,6 @@ "policyItemsDefinition": { "policyItemDefinition": [ { - "target": { - "path": "employeeNumber" - }, "valuePolicyRef": { "type": "http://midpoint.evolveum.com/xml/ns/public/common/common-3#ValuePolicyType", "oid": "00000000-0000-0000-1111-000000000003" diff --git a/testing/rest/src/test/resources/repo/xml/policy-generate-bad-path.xml b/testing/rest/src/test/resources/repo/xml/policy-generate-bad-path.xml index ff4374b801d..087abc23800 100644 --- a/testing/rest/src/test/resources/repo/xml/policy-generate-bad-path.xml +++ b/testing/rest/src/test/resources/repo/xml/policy-generate-bad-path.xml @@ -10,5 +10,6 @@ parentOrgRef + true \ No newline at end of file diff --git a/testing/rest/src/test/resources/repo/xml/policy-generate-explicit.xml b/testing/rest/src/test/resources/repo/xml/policy-generate-explicit.xml new file mode 100644 index 00000000000..57f00f268c2 --- /dev/null +++ b/testing/rest/src/test/resources/repo/xml/policy-generate-explicit.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/testing/rest/src/test/resources/repo/xml/policy-validate-explicit-no-value-policy.xml b/testing/rest/src/test/resources/repo/xml/policy-validate-explicit-no-value-policy.xml new file mode 100644 index 00000000000..c46837e81a4 --- /dev/null +++ b/testing/rest/src/test/resources/repo/xml/policy-validate-explicit-no-value-policy.xml @@ -0,0 +1,12 @@ + + + + Ad123456 + + + \ No newline at end of file diff --git a/testing/rest/src/test/resources/repo/xml/policy-validate-explicit.xml b/testing/rest/src/test/resources/repo/xml/policy-validate-explicit.xml index 886b0e50c18..8a731416a81 100644 --- a/testing/rest/src/test/resources/repo/xml/policy-validate-explicit.xml +++ b/testing/rest/src/test/resources/repo/xml/policy-validate-explicit.xml @@ -6,9 +6,6 @@ xmlns:icfs="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3" xmlns:ri="http://midpoint.evolveum.com/xml/ns/public/resource/instance-3"> - - employeeNumber - 123456 diff --git a/testing/rest/src/test/resources/repo/yaml/policy-generate-bad-path.yml b/testing/rest/src/test/resources/repo/yaml/policy-generate-bad-path.yml index 171ebcb664d..d2b91e2cbee 100644 --- a/testing/rest/src/test/resources/repo/yaml/policy-generate-bad-path.yml +++ b/testing/rest/src/test/resources/repo/yaml/policy-generate-bad-path.yml @@ -5,4 +5,5 @@ policyItemsDefinition: path: "parentOrgRef" valuePolicyRef: type: "http://midpoint.evolveum.com/xml/ns/public/common/common-3#ValuePolicyType" - oid: "00000000-0000-0000-1111-000000000003" \ No newline at end of file + oid: "00000000-0000-0000-1111-000000000003" + execute: true \ No newline at end of file diff --git a/testing/rest/src/test/resources/repo/yaml/policy-generate-explicit.yml b/testing/rest/src/test/resources/repo/yaml/policy-generate-explicit.yml new file mode 100644 index 00000000000..ca78572896e --- /dev/null +++ b/testing/rest/src/test/resources/repo/yaml/policy-generate-explicit.yml @@ -0,0 +1,6 @@ +'@ns': "http://midpoint.evolveum.com/xml/ns/public/common/api-types-3" +policyItemsDefinition: + policyItemDefinition: + - valuePolicyRef: + type: "http://midpoint.evolveum.com/xml/ns/public/common/common-3#ValuePolicyType" + oid: "00000000-0000-0000-1111-000000000003" \ No newline at end of file diff --git a/testing/rest/src/test/resources/repo/yaml/policy-validate-explicit-no-value-policy.yml b/testing/rest/src/test/resources/repo/yaml/policy-validate-explicit-no-value-policy.yml new file mode 100644 index 00000000000..7d2059a1ffb --- /dev/null +++ b/testing/rest/src/test/resources/repo/yaml/policy-validate-explicit-no-value-policy.yml @@ -0,0 +1,4 @@ +'@ns': "http://midpoint.evolveum.com/xml/ns/public/common/api-types-3" +policyItemsDefinition: + policyItemDefinition: + - value: "Ad123456" \ No newline at end of file diff --git a/testing/rest/src/test/resources/repo/yaml/policy-validate-explicit.yml b/testing/rest/src/test/resources/repo/yaml/policy-validate-explicit.yml index eca89dcd2ee..97054f8e1c4 100644 --- a/testing/rest/src/test/resources/repo/yaml/policy-validate-explicit.yml +++ b/testing/rest/src/test/resources/repo/yaml/policy-validate-explicit.yml @@ -1,9 +1,7 @@ '@ns': "http://midpoint.evolveum.com/xml/ns/public/common/api-types-3" policyItemsDefinition: policyItemDefinition: - - target: - path: "employeeNumber" - valuePolicyRef: + - valuePolicyRef: type: "http://midpoint.evolveum.com/xml/ns/public/common/common-3#ValuePolicyType" oid: "00000000-0000-0000-1111-000000000003" value: "123456" \ No newline at end of file