use of org.alfresco.rest.api.model.CustomModel in project alfresco-remote-api by Alfresco.
the class TestCustomModel method testActivateCustomModel.
@Test
public void testActivateCustomModel() throws Exception {
setRequestContext(customModelAdmin);
String modelNameOne = "testActivateModelOne" + System.currentTimeMillis();
Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
CustomModel customModelOne = createCustomModel(modelNameOne, namespacePair, ModelStatus.DRAFT, "Test model description", "Jane Doe");
// Retrieve the created model and check its status (the default is DRAFT)
HttpResponse response = getSingle("cmm", modelNameOne, 200);
CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(ModelStatus.DRAFT, returnedModel.getStatus());
// We only want to update the status, so ignore the other properties
CustomModel updatePayload = new CustomModel();
updatePayload.setStatus(ModelStatus.ACTIVE);
setRequestContext(nonAdminUserName);
// Try to activate the model as a non Admin user
put("cmm", modelNameOne, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 403);
setRequestContext(customModelAdmin);
// Activate the model as a Model Administrator
put("cmm", modelNameOne, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
response = getSingle("cmm", modelNameOne, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(ModelStatus.ACTIVE, returnedModel.getStatus());
// Check other properties have not been modified
compareCustomModels(customModelOne, returnedModel, "status");
// Try to activate the already activated model as a Model Administrator
put("cmm", modelNameOne, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 500);
// Create another Model
String modelNameTwo = "testActivateModelTwo" + System.currentTimeMillis();
Pair<String, String> namespacePairTwo = getTestNamespaceUriPrefixPair();
CustomModel customModelTwo = createCustomModel(modelNameTwo, namespacePairTwo, ModelStatus.DRAFT, null, "John Doe");
// Activate the model as a Model Administrator
customModelTwo.setStatus(ModelStatus.ACTIVE);
put("cmm", modelNameTwo, RestApiUtil.toJsonAsString(customModelTwo), SELECT_STATUS_QS, 200);
response = getSingle("cmm", modelNameTwo, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(ModelStatus.ACTIVE, returnedModel.getStatus());
// Check other properties have not been modified
compareCustomModels(customModelTwo, returnedModel, "status");
}
use of org.alfresco.rest.api.model.CustomModel in project alfresco-remote-api by Alfresco.
the class TestCustomModel method testCreateBasicModel_Invalid.
@Test
public void testCreateBasicModel_Invalid() throws Exception {
String modelName = "testModel" + System.currentTimeMillis();
Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
CustomModel customModel = new CustomModel();
customModel.setName(modelName);
customModel.setNamespaceUri(namespacePair.getFirst());
customModel.setNamespacePrefix(namespacePair.getSecond());
setRequestContext(customModelAdmin);
// Test invalid inputs
{
customModel.setName(modelName + "<script>alert('oops')</script>");
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
customModel.setName("prefix:" + modelName);
// Invalid name. Contains ':'
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
customModel.setName("prefix " + modelName);
// Invalid name. Contains space
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
customModel.setName(modelName);
customModel.setNamespacePrefix(namespacePair.getSecond() + " space");
// Invalid prefix. Contains space
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
customModel.setNamespacePrefix(namespacePair.getSecond() + "invalid/");
// Invalid prefix. Contains '/'
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
customModel.setNamespacePrefix(namespacePair.getSecond());
customModel.setNamespaceUri(namespacePair.getFirst() + " space");
// Invalid URI. Contains space
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
customModel.setNamespaceUri(namespacePair.getFirst() + "\\");
// Invalid URI. Contains '\'
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
}
// Test mandatory properties of the model
{
customModel.setName("");
customModel.setNamespacePrefix(namespacePair.getSecond());
customModel.setNamespaceUri(namespacePair.getFirst());
// name is mandatory
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
customModel.setName(modelName);
customModel.setNamespaceUri(null);
// namespaceUri is mandatory
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
customModel.setName(modelName);
customModel.setNamespaceUri(namespacePair.getFirst());
customModel.setNamespacePrefix(null);
// namespacePrefix is mandatory
post("cmm", RestApiUtil.toJsonAsString(customModel), 400);
}
// Test duplicate model name
{
// Test create a model with the same name as the bootstrapped model
customModel.setName("contentmodel");
customModel.setNamespaceUri(namespacePair.getFirst());
customModel.setNamespacePrefix(namespacePair.getSecond());
post("cmm", RestApiUtil.toJsonAsString(customModel), 409);
// Create the model
customModel.setName(modelName);
post("cmm", RestApiUtil.toJsonAsString(customModel), 201);
// Create a duplicate model
// Set a new namespace to make sure the 409 status code is returned
// because of a name conflict rather than namespace URI
namespacePair = getTestNamespaceUriPrefixPair();
customModel.setNamespaceUri(namespacePair.getFirst());
customModel.setNamespacePrefix(namespacePair.getSecond());
post("cmm", RestApiUtil.toJsonAsString(customModel), 409);
}
// Test duplicate namespaceUri
{
String modelNameTwo = "testModelTwo" + System.currentTimeMillis();
Pair<String, String> namespacePairTwo = getTestNamespaceUriPrefixPair();
CustomModel customModelTwo = new CustomModel();
customModelTwo.setName(modelNameTwo);
customModelTwo.setNamespaceUri(namespacePairTwo.getFirst());
customModelTwo.setNamespacePrefix(namespacePairTwo.getSecond());
post("cmm", RestApiUtil.toJsonAsString(customModelTwo), 201);
String modelNameThree = "testModelThree" + System.currentTimeMillis();
Pair<String, String> namespacePairThree = getTestNamespaceUriPrefixPair();
CustomModel customModelThree = new CustomModel();
customModelThree.setName(modelNameThree);
// duplicate URI
customModelThree.setNamespaceUri(namespacePairTwo.getFirst());
customModelThree.setNamespacePrefix(namespacePairThree.getSecond());
// Try to create a model with a namespace uri which has already been used.
post("cmm", RestApiUtil.toJsonAsString(customModelThree), 409);
customModelThree.setNamespaceUri(namespacePairThree.getFirst());
// duplicate prefix
customModelThree.setNamespacePrefix(namespacePairTwo.getSecond());
// Try to create a model with a namespace prefix which has already been used.
post("cmm", RestApiUtil.toJsonAsString(customModelThree), 409);
}
}
use of org.alfresco.rest.api.model.CustomModel in project alfresco-remote-api by Alfresco.
the class TestCustomModel method testUpdateBasicModel.
@Test
public void testUpdateBasicModel() throws Exception {
setRequestContext(customModelAdmin);
String modelName = "testModel" + System.currentTimeMillis();
Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
createCustomModel(modelName, namespacePair, ModelStatus.DRAFT, "Test model description", null);
// Test update name
CustomModel updatePayload = new CustomModel();
String newName = modelName + "Modified";
updatePayload.setName(newName);
// Cannot update the model name
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 400);
// Test update the namespace URI (already in-use)
updatePayload = new CustomModel();
updatePayload.setNamespaceUri("http://www.alfresco.org/model/content/1.0");
updatePayload.setNamespacePrefix("newPrefix");
// The namespace uri has already been used
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 409);
// Test update the namespace Prefix (already in-use)
updatePayload = new CustomModel();
updatePayload.setNamespaceUri(getTestNamespaceUriPrefixPair().getFirst());
updatePayload.setNamespacePrefix("cm");
// The namespace prefix has already been used
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 409);
// Test update the namespace URI (without sending the namespace prefix)
updatePayload = new CustomModel();
updatePayload.setNamespaceUri(getTestNamespaceUriPrefixPair().getFirst());
// The namespace prefix is mandatory
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 400);
// Test update the namespace URI only
updatePayload = new CustomModel();
updatePayload.setNamespacePrefix(namespacePair.getSecond());
Pair<String, String> newURI = getTestNamespaceUriPrefixPair();
updatePayload.setNamespaceUri(newURI.getFirst());
HttpResponse response = put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 200);
CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(newURI.getFirst(), returnedModel.getNamespaceUri());
assertEquals("The namespace prefix shouldn't have changed.", namespacePair.getSecond(), returnedModel.getNamespacePrefix());
// Test update the namespace prefix (without sending the namespace URI)
updatePayload = new CustomModel();
updatePayload.setNamespacePrefix("newPrefix");
// The namespce uri is mandatory
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 400);
// Test update the namespace prefix only
updatePayload = new CustomModel();
updatePayload.setNamespaceUri(namespacePair.getFirst());
Pair<String, String> newPrefix = getTestNamespaceUriPrefixPair();
updatePayload.setNamespacePrefix(newPrefix.getSecond());
response = put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(newPrefix.getSecond(), returnedModel.getNamespacePrefix());
assertEquals("The namespace URI shouldn't have changed.", namespacePair.getFirst(), returnedModel.getNamespaceUri());
// Test a valid update
updatePayload = new CustomModel();
Pair<String, String> newNamespacePair = getTestNamespaceUriPrefixPair();
updatePayload.setNamespaceUri(newNamespacePair.getFirst());
updatePayload.setNamespacePrefix(newNamespacePair.getSecond());
updatePayload.setDescription("Test model description Modified");
updatePayload.setAuthor("John Moe");
// This should be ignored
updatePayload.setStatus(ModelStatus.ACTIVE);
setRequestContext(nonAdminUserName);
// Try to update the model as a non Admin user
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 403);
setRequestContext(customModelAdmin);
// Update the model as a Model Administrator
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 200);
// Retrieve the updated model
response = getSingle("cmm", modelName, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
compareCustomModels(updatePayload, returnedModel, "name", "status");
assertEquals("The model status should only be updated via '?select=status' request.", ModelStatus.DRAFT, returnedModel.getStatus());
// Activate the model as a Model Administrator
updatePayload = new CustomModel();
updatePayload.setStatus(ModelStatus.ACTIVE);
response = put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(ModelStatus.ACTIVE, returnedModel.getStatus());
// Try to update the ACTIVE model's namespace URI
updatePayload = new CustomModel();
newNamespacePair = getTestNamespaceUriPrefixPair();
updatePayload.setNamespaceUri(newNamespacePair.getFirst());
updatePayload.setNamespacePrefix(returnedModel.getNamespacePrefix());
// Cannot update the namespace uri and/or namespace prefix when the model is Active.
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 409);
// Try to update the ACTIVE model's namespace Prefix
updatePayload = new CustomModel();
updatePayload.setNamespaceUri(returnedModel.getNamespaceUri());
updatePayload.setNamespacePrefix("myNewPrefix");
// Cannot update the namespace uri and/or namespace prefix when the model is Active.
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 409);
// Test a valid update of an Active model (you can only update desc and author)
updatePayload = new CustomModel();
updatePayload.setNamespaceUri(returnedModel.getNamespaceUri());
updatePayload.setNamespacePrefix(returnedModel.getNamespacePrefix());
updatePayload.setDescription("Test modifying active model description");
updatePayload.setAuthor("Mark Miller");
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 200);
// Retrieve the updated active model
response = getSingle("cmm", modelName, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
compareCustomModels(updatePayload, returnedModel, "name", "status");
}
use of org.alfresco.rest.api.model.CustomModel in project alfresco-remote-api by Alfresco.
the class TestCustomModel method testDeactivateCustomModel.
@Test
public void testDeactivateCustomModel() throws Exception {
setRequestContext(customModelAdmin);
String modelNameOne = "testDeactivateModelOne" + System.currentTimeMillis();
Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
CustomModel customModelOne = createCustomModel(modelNameOne, namespacePair, ModelStatus.ACTIVE, null, "Mark Moe");
// Retrieve the created model and check its status
HttpResponse response = getSingle("cmm", modelNameOne, 200);
CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(ModelStatus.ACTIVE, returnedModel.getStatus());
// We only want to update the status (Deactivate), so ignore the other properties
CustomModel updatePayload = new CustomModel();
updatePayload.setStatus(ModelStatus.DRAFT);
setRequestContext(nonAdminUserName);
// Try to deactivate the model as a non Admin user
put("cmm", modelNameOne, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 403);
setRequestContext(customModelAdmin);
// Deactivate the model as a Model Administrator
put("cmm", modelNameOne, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
response = getSingle("cmm", modelNameOne, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(ModelStatus.DRAFT, returnedModel.getStatus());
// Check other properties have not been modified
compareCustomModels(customModelOne, returnedModel, "status");
// Try to deactivate the already deactivated model as a Model Administrator
put("cmm", modelNameOne, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 500);
// Activate/Deactivate a model with an aspect
{
// Create another Model
final String modelNameTwo = "testDeactivateModelTwo" + System.currentTimeMillis();
Pair<String, String> namespacePairTwo = getTestNamespaceUriPrefixPair();
CustomModel customModelTwo = createCustomModel(modelNameTwo, namespacePairTwo, ModelStatus.DRAFT, null, "Mark Moe");
// Aspect
CustomAspect aspect = new CustomAspect();
aspect.setName("testMarkerAspect");
post("cmm/" + modelNameTwo + "/aspects", RestApiUtil.toJsonAsString(aspect), 201);
// Retrieve the created aspect
getSingle("cmm/" + modelNameTwo + "/aspects", aspect.getName(), 200);
// Activate the model as a Model Administrator
customModelTwo.setStatus(ModelStatus.ACTIVE);
put("cmm", modelNameTwo, RestApiUtil.toJsonAsString(customModelTwo), SELECT_STATUS_QS, 200);
response = getSingle("cmm", modelNameTwo, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(ModelStatus.ACTIVE, returnedModel.getStatus());
updatePayload = new CustomModel();
updatePayload.setStatus(ModelStatus.DRAFT);
// Deactivate the model as a Model Administrator
put("cmm", modelNameTwo, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
response = getSingle("cmm", modelNameTwo, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertEquals(ModelStatus.DRAFT, returnedModel.getStatus());
}
}
use of org.alfresco.rest.api.model.CustomModel in project alfresco-remote-api by Alfresco.
the class TestCustomProperty method testUpdateProperty.
@Test
public void testUpdateProperty() throws Exception {
setRequestContext(customModelAdmin);
String modelName = "testModelUpdateProp" + System.currentTimeMillis();
Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
/*
* Create aspect and update it by adding a property
*/
String aspectName = "testAspect1" + System.currentTimeMillis();
createTypeAspect(CustomAspect.class, modelName, aspectName, null, null, null);
// Update the Aspect by adding property
CustomAspect aspectPayload = new CustomAspect();
aspectPayload.setName(aspectName);
String aspectPropName = "testAspect1Prop" + System.currentTimeMillis();
CustomModelProperty aspectProp = new CustomModelProperty();
aspectProp.setName(aspectPropName);
aspectProp.setTitle("aspect property title");
aspectProp.setMultiValued(true);
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(aspectProp);
aspectPayload.setProperties(props);
// create property
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 200);
// Retrieve the updated aspect
HttpResponse response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
// Check the aspect's added property
assertEquals(1, returnedAspect.getProperties().size());
/*
* Create type and update it by adding a property
*/
String typeName = "testType1" + System.currentTimeMillis();
createTypeAspect(CustomType.class, modelName, typeName, "test type1 title", null, "cm:content");
// Update the Type by adding property - property one
CustomType typePayload = new CustomType();
typePayload.setName(typeName);
// Create inline MINMAX constraint
CustomModelConstraint inlineMinMaxConstraint = new CustomModelConstraint();
inlineMinMaxConstraint.setType("MINMAX");
inlineMinMaxConstraint.setTitle("test MINMAX title");
// Create the MinMax constraint's parameters
List<CustomModelNamedValue> parameters = new ArrayList<>(2);
parameters.add(buildNamedValue("maxValue", "100.0"));
parameters.add(buildNamedValue("minValue", "0.0"));
// Add the parameters into the constraint
inlineMinMaxConstraint.setParameters(parameters);
String typePropName = "testType1Prop" + System.currentTimeMillis();
CustomModelProperty typeProp = new CustomModelProperty();
typeProp.setName(typePropName);
typeProp.setDataType("d:int");
typeProp.setTitle("type property title");
typeProp.setDefaultValue("0");
// add the inline constraint
typeProp.setConstraints(Arrays.asList(inlineMinMaxConstraint));
props = new ArrayList<>(1);
props.add(typeProp);
typePayload.setProperties(props);
// create property
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 200);
// Retrieve the updated type
response = getSingle("cmm/" + modelName + "/types", typeName, 200);
CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
// Check the type's added property
assertEquals(1, returnedType.getProperties().size());
// Update aspect's property - model is inactive
{
final String updatePropOneAspectQS = getPropDeleteUpdateQS(aspectPropName, false);
// Try to update property from aspect
// missing payload
put("cmm/" + modelName + "/aspects", aspectName, null, updatePropOneAspectQS, 400);
CustomAspect updatePropAspectPayload = new CustomAspect();
CustomModelProperty propertyAspect = new CustomModelProperty();
propertyAspect.setTitle("new Title");
propertyAspect.setDescription("new Desc");
// the original value was d:text
propertyAspect.setDataType("d:int");
// the original value was true
propertyAspect.setMultiValued(false);
// the original value was false
propertyAspect.setMandatory(true);
propertyAspect.setDefaultValue("10");
List<CustomModelProperty> modifiedProp = new ArrayList<>(1);
modifiedProp.add(propertyAspect);
updatePropAspectPayload.setProperties(modifiedProp);
// missing aspect name
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(updatePropAspectPayload), updatePropOneAspectQS, 400);
// set a random name
updatePropAspectPayload.setName(aspectName + System.currentTimeMillis());
// Aspect not found
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(updatePropAspectPayload), updatePropOneAspectQS, 404);
// set the correct name
updatePropAspectPayload.setName(aspectName);
// the requested property name dose not match the payload
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(updatePropAspectPayload), updatePropOneAspectQS, 400);
// set the property name that matches the requested property
propertyAspect.setName(aspectPropName);
setRequestContext(nonAdminUserName);
// unauthorised
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(updatePropAspectPayload), updatePropOneAspectQS, 403);
setRequestContext(customModelAdmin);
// Update as a Model Administrator
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(updatePropAspectPayload), updatePropOneAspectQS, 200);
// Check the property has been updated
response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
assertEquals(1, returnedAspect.getProperties().size());
CustomModelProperty modifiedAspectProperty = returnedAspect.getProperties().get(0);
compareCustomModelProperties(propertyAspect, modifiedAspectProperty, "prefixedName", "indexTokenisationMode");
}
// Activate the model
CustomModel statusPayload = new CustomModel();
statusPayload.setStatus(ModelStatus.ACTIVE);
put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
// Update type's property - model is active
{
final String updatePropTwoTypeQS = getPropDeleteUpdateQS(typePropName, false);
CustomType updatePropTypePayload = new CustomType();
updatePropTypePayload.setName(typeName);
CustomModelProperty propertyType = new CustomModelProperty();
propertyType.setName(typePropName);
propertyType.setTitle("new Title");
propertyType.setDescription("new Desc");
// the original value was d:int
propertyType.setDataType("d:long");
propertyType.setDefaultValue("5");
List<CustomModelProperty> modifiedProp = new ArrayList<>(1);
modifiedProp.add(propertyType);
updatePropTypePayload.setProperties(modifiedProp);
setRequestContext(nonAdminUserName);
// Unauthorised
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 403);
setRequestContext(customModelAdmin);
// Try to update an active model as a Model Administrator - Cannot change the data type of the property of an active model
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 409);
// Set the data type with its original value
propertyType.setDataType("d:int");
// the original value was false
propertyType.setMultiValued(true);
// Cannot change the multi-valued option of the property of an active model
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 409);
propertyType.setMultiValued(false);
// the original value was false
propertyType.setMandatory(true);
// Cannot change the mandatory option of the property of an active model
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 409);
propertyType.setMandatory(false);
// the original value was false
propertyType.setMandatoryEnforced(true);
// Cannot change the mandatory-enforced option of the property of an active model
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 409);
// Set the mandatory-enforced with its original value
propertyType.setMandatoryEnforced(false);
// Update the MinMax constraint's parameters
parameters = new ArrayList<>(2);
// the original value was 100.0
parameters.add(buildNamedValue("maxValue", "120.0"));
// the original value was 0.0
parameters.add(buildNamedValue("minValue", "20.0"));
// Add the parameters into the constraint
inlineMinMaxConstraint.setParameters(parameters);
// add the updated inline constraint
propertyType.setConstraints(Arrays.asList(inlineMinMaxConstraint));
// Try to Update - constraint violation. The default value is 5 which is not in the MinMax range [20, 120]
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 409);
// we changed the MinMax constraint to be [20, 120]
propertyType.setDefaultValue("25");
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 200);
// Check the property has been updated
response = getSingle("cmm/" + modelName + "/types", typeName, 200);
returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
assertEquals(1, returnedType.getProperties().size());
CustomModelProperty modifiedTypeProperty = returnedType.getProperties().get(0);
assertEquals("new Title", modifiedTypeProperty.getTitle());
assertEquals("new Desc", modifiedTypeProperty.getDescription());
assertEquals("25", modifiedTypeProperty.getDefaultValue());
assertEquals("Shouldn't be able to change the data type of the property of an active model.", "d:int", modifiedTypeProperty.getDataType());
assertFalse(modifiedTypeProperty.isMandatory());
assertFalse(modifiedTypeProperty.isMultiValued());
assertFalse(modifiedTypeProperty.isMandatoryEnforced());
assertEquals(1, modifiedTypeProperty.getConstraints().size());
CustomModelConstraint modifiedConstraint = modifiedTypeProperty.getConstraints().get(0);
assertEquals("MINMAX", modifiedConstraint.getType());
assertEquals("120.0", getParameterSimpleValue(modifiedConstraint.getParameters(), "maxValue"));
assertEquals("20.0", getParameterSimpleValue(modifiedConstraint.getParameters(), "minValue"));
// Change the constraint type and parameter
inlineMinMaxConstraint.setType("LENGTH");
inlineMinMaxConstraint.setTitle("test LENGTH title");
parameters = new ArrayList<>(2);
parameters.add(buildNamedValue("maxLength", "256"));
parameters.add(buildNamedValue("minLength", "0"));
// Add the parameters into the constraint
inlineMinMaxConstraint.setParameters(parameters);
propertyType.setConstraints(Arrays.asList(inlineMinMaxConstraint));
// LENGTH can only be used with textual data type
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 400);
// update the property by removing the constraint
propertyType.setConstraints(Collections.<CustomModelConstraint>emptyList());
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(updatePropTypePayload), updatePropTwoTypeQS, 200);
response = getSingle("cmm/" + modelName + "/types", typeName, 200);
returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
assertEquals(1, returnedType.getProperties().size());
modifiedTypeProperty = returnedType.getProperties().get(0);
assertEquals(0, modifiedTypeProperty.getConstraints().size());
}
}
Aggregations