use of org.alfresco.rest.api.model.CustomModelProperty 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());
}
}
use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.
the class TestCustomTypeAspect method testCreateAspectsAndTypesWithProperties.
@Test
public void testCreateAspectsAndTypesWithProperties() 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);
{
// Add aspect with property
String aspectName = "testAspect1" + System.currentTimeMillis();
CustomAspect aspect = new CustomAspect();
aspect.setName(aspectName);
String aspectPropName = "testAspect1Prop1" + System.currentTimeMillis();
CustomModelProperty aspectProp = new CustomModelProperty();
aspectProp.setName(aspectPropName);
aspectProp.setTitle("property title");
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(aspectProp);
aspect.setProperties(props);
// Create aspect as a Model Administrator
post("cmm/" + modelName + "/aspects", RestApiUtil.toJsonAsString(aspect), 201);
// Retrieve the created aspect
HttpResponse response = getSingle("cmm/" + modelName + "/aspects", aspect.getName(), 200);
CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
compareCustomTypesAspects(aspect, returnedAspect, "prefixedName", "dataType", "indexTokenisationMode");
assertEquals(1, returnedAspect.getProperties().size());
CustomModelProperty customModelProperty = returnedAspect.getProperties().get(0);
assertEquals(aspectPropName, customModelProperty.getName());
assertEquals("property title", customModelProperty.getTitle());
assertEquals(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + aspectPropName, customModelProperty.getPrefixedName());
assertEquals("Default data type is 'd:text'.", "d:text", customModelProperty.getDataType());
// Test default indexing options
assertTrue(customModelProperty.isIndexed());
assertEquals(Facetable.UNSET, customModelProperty.getFacetable());
assertEquals(IndexTokenisationMode.TRUE, customModelProperty.getIndexTokenisationMode());
}
{
// Add type with properties
String typeName = "testType1" + System.currentTimeMillis();
CustomType type = new CustomType();
type.setName(typeName);
type.setDescription("test type1 Desc");
type.setTitle("test type1 title");
type.setParentName("cm:content");
String typePropName = "testType1Prop1" + System.currentTimeMillis();
CustomModelProperty typeProp = new CustomModelProperty();
typeProp.setName(typePropName);
// data type without dictionary prefix
typeProp.setDataType("int");
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(typeProp);
type.setProperties(props);
// Create type as a Model Administrator
post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type), 400);
typeProp.setDataType("d:int");
post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type), 201);
// Retrieve the created type
HttpResponse response = getSingle("cmm/" + modelName + "/types", type.getName(), 200);
CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
compareCustomTypesAspects(type, returnedType, "prefixedName", "indexTokenisationMode");
assertEquals("Shouldn't list the inherited properties from 'cm:content'.", 1, returnedType.getProperties().size());
CustomModelProperty customModelProperty = returnedType.getProperties().get(0);
assertEquals(typePropName, customModelProperty.getName());
assertEquals("d:int", customModelProperty.getDataType());
assertEquals(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typePropName, customModelProperty.getPrefixedName());
// Test default indexing options
assertTrue(customModelProperty.isIndexed());
assertEquals(Facetable.UNSET, customModelProperty.getFacetable());
assertEquals(IndexTokenisationMode.TRUE, customModelProperty.getIndexTokenisationMode());
}
{
// Add more types with the same parent
// Test to make sure the inherited properties are excluded properly.
// As without parent’s property exclusion, we won’t be able to
// create multiple types|aspects with the same parent.
String typeName2 = "testType2" + System.currentTimeMillis();
CustomType type2 = new CustomType();
type2.setName(typeName2);
type2.setDescription("test type2 Desc");
type2.setTitle("test type2 title");
type2.setParentName("cm:content");
post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type2), 201);
String typeName3 = "testType3" + System.currentTimeMillis();
CustomType type3 = new CustomType();
type3.setName(typeName3);
type3.setDescription("test type3 Desc");
type3.setTitle("test type3 title");
type3.setParentName("cm:content");
post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type3), 201);
}
{
// Retrieve the created model
HttpResponse response = getSingle("cmm", modelName, 200);
CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertNull(returnedModel.getTypes());
assertNull(returnedModel.getAspects());
// Retrieve the created model with its types and aspects
response = getSingle("cmm", modelName + SELECT_ALL, 200);
returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
assertNotNull(returnedModel.getTypes());
assertEquals(3, returnedModel.getTypes().size());
assertNotNull(returnedModel.getAspects());
assertEquals(1, returnedModel.getAspects().size());
}
}
use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.
the class TestCustomTypeAspect method testDeleteTypeAspect.
@Test
public void testDeleteTypeAspect() throws Exception {
setRequestContext(customModelAdmin);
final String modelName = "testDeleteTypeModel" + System.currentTimeMillis();
Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
createCustomModel(modelName, namespacePair, ModelStatus.DRAFT, null, "Joe Bloggs");
// Add type
final String typeName = "testType" + System.currentTimeMillis();
final String typeNameWithPrefix = namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typeName;
CustomType type = createTypeAspect(CustomType.class, modelName, typeName, "test type title", "test type Desc", "cm:content");
// Add aspect
final String aspectName = "testAspect" + System.currentTimeMillis();
final String aspectNameWithPrefix = namespacePair.getSecond() + QName.NAMESPACE_PREFIX + aspectName;
CustomAspect aspect = createTypeAspect(CustomAspect.class, modelName, aspectName, null, null, null);
// Delete type
{
setRequestContext(nonAdminUserName);
// Try to delete the model's type as a non Admin user
delete("cmm/" + modelName + "/types", typeName, 403);
setRequestContext(customModelAdmin);
// Delete the model's type as a Model Administrator
delete("cmm/" + modelName + "/types", typeName, 204);
// Try to retrieve the deleted type
getSingle("cmm/" + modelName + "/types", typeName, 404);
}
// Delete Aspect
{
setRequestContext(nonAdminUserName);
// Try to delete the model's aspect as a non Admin user
delete("cmm/" + modelName + "/aspects", aspectName, 403);
setRequestContext(customModelAdmin);
// Delete the model's aspect as a Model Administrator
delete("cmm/" + modelName + "/aspects", aspectName, 204);
// Try to retrieve the deleted aspect
getSingle("cmm/" + modelName + "/aspects", aspectName, 404);
}
setRequestContext(customModelAdmin);
// Create the type again
post("cmm/" + modelName + "/types", RestApiUtil.toJsonAsString(type), 201);
// Retrieve the created type
HttpResponse response = getSingle("cmm/" + modelName + "/types", type.getName(), 200);
CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
compareCustomTypesAspects(type, returnedType, "prefixedName");
// Create the aspect again
post("cmm/" + modelName + "/aspects", RestApiUtil.toJsonAsString(aspect), 201);
// Retrieve the created aspect
response = getSingle("cmm/" + modelName + "/aspects", aspect.getName(), 200);
CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
compareCustomTypesAspects(aspect, returnedAspect, "prefixedName");
// Update the Type by adding property
CustomType payload = new CustomType();
payload.setName(typeName);
String typePropName = "testType1Prop1" + System.currentTimeMillis();
CustomModelProperty typeProp = new CustomModelProperty();
typeProp.setName(typePropName);
typeProp.setTitle("property title");
typeProp.setDataType("d:int");
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(typeProp);
payload.setProperties(props);
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 200);
// Activate the model
CustomModel statusPayload = new CustomModel();
statusPayload.setStatus(ModelStatus.ACTIVE);
// Activate the model as a Model Administrator
put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
// Test for SHA-703
// Add another type with 'typeName' as its parent
final String childTypeName = "testChildType" + System.currentTimeMillis();
createTypeAspect(CustomType.class, modelName, childTypeName, "test child type title", null, typeNameWithPrefix);
// Add another aspect with 'aspectName' as its parent
final String childAspectName = "testChildAspect" + System.currentTimeMillis();
createTypeAspect(CustomAspect.class, modelName, childAspectName, "test child aspect title", null, aspectNameWithPrefix);
// Delete the model's type as a Model Administrator
// Cannot delete a type of an active model
delete("cmm/" + modelName + "/types", typeName, 409);
// Delete the model's aspect as a Model Administrator
// Cannot delete an aspect of an active model
delete("cmm/" + modelName + "/aspects", childAspectName, 409);
// Deactivate the model
statusPayload = new CustomModel();
statusPayload.setStatus(ModelStatus.DRAFT);
// Deactivate the model as a Model Administrator
put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
// Delete type
{
// Try to delete the model's type (parent) as a Model Administrator
// conflict: childTypeName depends on typeName
delete("cmm/" + modelName + "/types", typeName, 409);
// Delete the child type first
delete("cmm/" + modelName + "/types", childTypeName, 204);
// Try to retrieve the deleted child type
getSingle("cmm/" + modelName + "/types", childTypeName, 404);
// Now delete the parent type
delete("cmm/" + modelName + "/types", typeName, 204);
// Try to retrieve the deleted parent type
getSingle("cmm/" + modelName + "/types", typeName, 404);
}
// Delete Aspect
{
// Try to delete the model's aspect (parent) as a Model Administrator
// conflict: childAspectName depends on aspectName
delete("cmm/" + modelName + "/aspects", aspectName, 409);
// Delete the child aspect first
delete("cmm/" + modelName + "/aspects", childAspectName, 204);
// Try to retrieve the deleted child aspect
getSingle("cmm/" + modelName + "/aspects", childAspectName, 404);
// Now delete the parent aspect
delete("cmm/" + modelName + "/aspects", aspectName, 204);
// Try to retrieve the deleted parent aspect
getSingle("cmm/" + modelName + "/aspects", aspectName, 404);
}
}
Aggregations