Search in sources :

Example 1 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class TestCustomConstraint method testCreateMinMaxConstraintInvalid.

@Test
public void testCreateMinMaxConstraintInvalid() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelMinMaxInvalid" + System.currentTimeMillis();
    final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    // Create aspect
    String aspectName = "testAspect" + System.currentTimeMillis();
    createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
    // Update the Aspect by adding property
    CustomAspect aspectPayload = new CustomAspect();
    aspectPayload.setName(aspectName);
    final String aspectPropName = "testAspect1Prop" + System.currentTimeMillis();
    CustomModelProperty aspectProp = new CustomModelProperty();
    aspectProp.setName(aspectPropName);
    aspectProp.setTitle("property title");
    aspectProp.setDataType("d:text");
    String minMaxConstraintName = "testMinMaxConstraint" + System.currentTimeMillis();
    CustomModelConstraint minMaxConstraint = new CustomModelConstraint();
    minMaxConstraint.setType("MINMAX");
    minMaxConstraint.setName(minMaxConstraintName);
    minMaxConstraint.setTitle("test MinMax title");
    minMaxConstraint.setDescription("test MinMax desc");
    // 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
    minMaxConstraint.setParameters(parameters);
    // Add inline constraint
    aspectProp.setConstraints(Arrays.asList(minMaxConstraint));
    List<CustomModelProperty> props = new ArrayList<>(1);
    props.add(aspectProp);
    aspectPayload.setProperties(props);
    // Try to create constraint as a Model Administrator
    // MINMAX constraint can only be used with numeric data type.
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
    // Change type
    aspectProp.setDataType("d:datetime");
    // MINMAX constraint can only be used with numeric data type.
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
    // SHA-1126
    {
        // Change type
        aspectProp.setDataType("d:double");
        parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("maxValue", "0.0"));
        parameters.add(buildNamedValue("minValue", "-5.0"));
        // Add the parameters into the constraint
        minMaxConstraint.setParameters(parameters);
        // Add inline constraint
        aspectProp.setConstraints(Arrays.asList(minMaxConstraint));
        props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Maximum value of the MINMAX constraint must be a positive nonzero value.
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 400);
    }
}
Also used : CustomModelNamedValue(org.alfresco.rest.api.model.CustomModelNamedValue) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ArrayList(java.util.ArrayList) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) Test(org.junit.Test)

Example 2 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class TestCustomConstraint method testCreateInlineConstraint.

@Test
public void testCreateInlineConstraint() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelInlineConstraint" + System.currentTimeMillis();
    final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    String regExConstraintName = "testInlineFileNameRegEx" + System.currentTimeMillis();
    {
        // Create RegEx constraint
        CustomModelConstraint inlineRegExConstraint = new CustomModelConstraint();
        inlineRegExConstraint.setName(regExConstraintName);
        inlineRegExConstraint.setType("REGEX");
        // Create the inline RegEx constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("expression", "(.*[\\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)"));
        parameters.add(buildNamedValue("requiresMatch", "false"));
        // Add the parameters into the constraint
        inlineRegExConstraint.setParameters(parameters);
        // Create aspect
        String aspectName = "testAspect1" + System.currentTimeMillis();
        createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
        // Update the Aspect by adding property
        CustomAspect aspectPayload = new CustomAspect();
        aspectPayload.setName(aspectName);
        final String aspectPropName = "testAspect1Prop1" + System.currentTimeMillis();
        CustomModelProperty aspectProp = new CustomModelProperty();
        aspectProp.setName(aspectPropName);
        aspectProp.setTitle("property title");
        aspectProp.setDataType("d:text");
        // Add inline constraint
        aspectProp.setConstraints(Arrays.asList(inlineRegExConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Create the property
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 200);
        // Retrieve all the model's constraints
        Paging paging = getPaging(0, Integer.MAX_VALUE);
        HttpResponse response = getAll("cmm/" + modelName + "/constraints", paging, 200);
        List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
        assertEquals("Inline constraints should not be included with the model defined constraints.", 0, constraints.size());
        // Retrieve the updated aspect
        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());
        CustomModelProperty customModelProperty = returnedAspect.getProperties().get(0);
        assertEquals(aspectPropName, customModelProperty.getName());
        assertEquals(0, customModelProperty.getConstraintRefs().size());
        List<CustomModelConstraint> inlineConstraints = customModelProperty.getConstraints();
        assertEquals(1, inlineConstraints.size());
        compareCustomModelConstraints(inlineRegExConstraint, inlineConstraints.get(0), "prefixedName");
    }
    // Create inline and referenced constraint
    {
        // Create RegEx constraint
        CustomModelConstraint regExConstraint = new CustomModelConstraint();
        // duplicate name
        regExConstraint.setName(regExConstraintName);
        regExConstraint.setType("REGEX");
        regExConstraint.setTitle("test RegEx title");
        // Create the RegEx constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("expression", "(.*[\\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)"));
        parameters.add(buildNamedValue("requiresMatch", "false"));
        // Add the parameters into the constraint
        regExConstraint.setParameters(parameters);
        // Try to create constraint as a Model Administrator
        // duplicate name
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 409);
        String newRegExConstraintName = "testFileNameRegEx" + System.currentTimeMillis();
        regExConstraint.setName(newRegExConstraintName);
        // Create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 201);
        // Retrieve the created RegEx constraint
        HttpResponse response = getSingle("cmm/" + modelName + "/constraints", newRegExConstraintName, 200);
        CustomModelConstraint returnedRegExConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        // Create inline anonymous LENGTH constraint
        CustomModelConstraint inlineAnonymousLengthConstraint = new CustomModelConstraint();
        inlineAnonymousLengthConstraint.setType("LENGTH");
        inlineAnonymousLengthConstraint.setTitle("test Length title");
        // Create the Length constraint's parameters
        parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("maxLength", "256"));
        parameters.add(buildNamedValue("minLength", "0"));
        // Add the parameters into the constraint
        inlineAnonymousLengthConstraint.setParameters(parameters);
        // Create type
        String typeName = "testType1" + System.currentTimeMillis();
        CustomType type = createTypeAspect(CustomType.class, modelName, typeName, "test type1 title", "test type1 Desc", "cm:content");
        // Update the Type by adding property
        CustomType typePayload = new CustomType();
        typePayload.setName(typeName);
        String typePropName = "testType1Prop1" + System.currentTimeMillis();
        CustomModelProperty typeProp = new CustomModelProperty();
        typeProp.setName(typePropName);
        typeProp.setTitle("property title");
        typeProp.setDataType("d:int");
        // Constraint Ref
        typeProp.setConstraintRefs(Arrays.asList(returnedRegExConstraint.getPrefixedName()));
        // inline constraint
        typeProp.setConstraints(Arrays.asList(inlineAnonymousLengthConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        typePayload.setProperties(props);
        // Try to create the property - LENGTH constraint can only be used with textual data type
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 400);
        typeProp.setDataType("d:double");
        // CTry to create the property - LENGTH constraint can only be used with textual data type
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 400);
        typeProp.setDataType("d:text");
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 200);
        // Retrieve the updated type
        response = getSingle("cmm/" + modelName + "/types", type.getName(), 200);
        CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        // Check the type's added property
        assertEquals(1, returnedType.getProperties().size());
        CustomModelProperty customModelProperty = returnedType.getProperties().get(0);
        assertEquals(typePropName, customModelProperty.getName());
        assertEquals(1, customModelProperty.getConstraintRefs().size());
        assertEquals(returnedRegExConstraint.getPrefixedName(), customModelProperty.getConstraintRefs().get(0));
        assertEquals(1, customModelProperty.getConstraints().size());
        // M2PropertyDefinition will add a name
        assertNotNull(customModelProperty.getConstraints().get(0).getName());
        compareCustomModelConstraints(inlineAnonymousLengthConstraint, customModelProperty.getConstraints().get(0), "prefixedName", "name");
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) Test(org.junit.Test)

Example 3 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class TestCustomProperty method testCreateProperties.

@Test
public void testCreateProperties() 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);
    {
        // Create aspect
        String aspectName = "testAspect1" + System.currentTimeMillis();
        CustomAspect aspect = createTypeAspect(CustomAspect.class, modelName, aspectName, null, null, null);
        // Update the Aspect by adding property
        CustomAspect payload = new CustomAspect();
        String aspectPropName = "testAspect1Prop1" + System.currentTimeMillis();
        CustomModelProperty aspectProp = new CustomModelProperty();
        aspectProp.setName(aspectPropName);
        aspectProp.setTitle("property title");
        aspectProp.setMultiValued(true);
        aspectProp.setIndexed(true);
        aspectProp.setFacetable(Facetable.TRUE);
        aspectProp.setIndexTokenisationMode(IndexTokenisationMode.BOTH);
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(aspectProp);
        payload.setProperties(props);
        setRequestContext(nonAdminUserName);
        // Try to update the aspect as a non Admin user
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 403);
        setRequestContext(customModelAdmin);
        // Try to update the aspect as a Model Administrator
        // Type name is mandatory
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 400);
        // Add the mandatory aspect name to the payload
        payload.setName(aspectName);
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 200);
        // Retrieve the updated aspect
        HttpResponse response = getSingle("cmm/" + modelName + "/aspects", aspect.getName(), 200);
        CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        // Check the aspect's added property
        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());
        assertNull(customModelProperty.getDescription());
        assertTrue(customModelProperty.isMultiValued());
        assertFalse(customModelProperty.isMandatory());
        assertFalse(customModelProperty.isMandatoryEnforced());
        assertNull(customModelProperty.getDefaultValue());
        assertTrue(customModelProperty.isIndexed());
        assertEquals(Facetable.TRUE, customModelProperty.getFacetable());
        assertEquals(IndexTokenisationMode.BOTH, customModelProperty.getIndexTokenisationMode());
        // Test duplicate property name
        aspectProp = new CustomModelProperty();
        // Existing name
        aspectProp.setName(aspectPropName);
        aspectProp.setTitle("new property title");
        props = new ArrayList<>(1);
        props.add(aspectProp);
        payload.setProperties(props);
        // Try to update the aspect as a Model Administrator
        // property name already exists
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 409);
    }
    {
        // Create type
        String typeName = "testType1" + System.currentTimeMillis();
        CustomType type = createTypeAspect(CustomType.class, modelName, typeName, "test type1 title", "test type1 Desc", "cm:content");
        // Update the Type by adding property
        CustomType payload = new CustomType();
        String typePropName = "testType1Prop1" + System.currentTimeMillis();
        CustomModelProperty typeProp = new CustomModelProperty();
        typeProp.setName(typePropName);
        typeProp.setTitle("property title");
        typeProp.setDataType("d:int");
        typeProp.setIndexed(false);
        typeProp.setFacetable(Facetable.FALSE);
        typeProp.setIndexTokenisationMode(IndexTokenisationMode.FALSE);
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        payload.setProperties(props);
        setRequestContext(nonAdminUserName);
        // Try to update the type as a non Admin user
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 403);
        setRequestContext(customModelAdmin);
        // Try to update the type as a Model Administrator
        // Type name is mandatory
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 400);
        // Add the mandatory type name to the payload
        payload.setName(typeName);
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 200);
        // Retrieve the updated type
        HttpResponse response = getSingle("cmm/" + modelName + "/types", type.getName(), 200);
        CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        // Check the type's added property
        assertEquals(1, returnedType.getProperties().size());
        CustomModelProperty customModelProperty = returnedType.getProperties().get(0);
        assertEquals(typePropName, customModelProperty.getName());
        assertEquals("property title", customModelProperty.getTitle());
        assertEquals(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typePropName, customModelProperty.getPrefixedName());
        assertEquals("d:int", customModelProperty.getDataType());
        assertNull(customModelProperty.getDescription());
        assertFalse(customModelProperty.isMultiValued());
        assertFalse(customModelProperty.isMandatory());
        assertFalse(customModelProperty.isMandatoryEnforced());
        assertNull(customModelProperty.getDefaultValue());
        assertFalse(customModelProperty.isIndexed());
        assertEquals(Facetable.FALSE, customModelProperty.getFacetable());
        assertEquals(IndexTokenisationMode.FALSE, customModelProperty.getIndexTokenisationMode());
        // Retrieve the updated type with all the properties (include inherited)
        response = getSingle("cmm/" + modelName + "/types", type.getName() + SELECT_ALL_PROPS, 200);
        returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals(3, returnedType.getProperties().size());
        // Check for the inherited properties
        // cm:content
        assertNotNull(getProperty(returnedType.getProperties(), "content"));
        // cm:name
        assertNotNull(getProperty(returnedType.getProperties(), "name"));
        // Create another property and set all of its attributes
        payload = new CustomType();
        payload.setName(typeName);
        String typePropName2 = "testType1Prop2" + System.currentTimeMillis();
        typeProp = new CustomModelProperty();
        typeProp.setName(typePropName2);
        typeProp.setTitle("property2 title");
        typeProp.setDescription("property2 desciption");
        typeProp.setDataType("d:int");
        typeProp.setDefaultValue("0");
        typeProp.setMultiValued(false);
        typeProp.setMandatory(true);
        typeProp.setMandatoryEnforced(true);
        props = new ArrayList<>(1);
        props.add(typeProp);
        payload.setProperties(props);
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 200);
        // Retrieve the updated type
        response = getSingle("cmm/" + modelName + "/types", type.getName(), 200);
        returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        // Check the type's added property
        assertEquals(2, returnedType.getProperties().size());
        customModelProperty = getProperty(returnedType.getProperties(), typePropName2);
        assertNotNull(customModelProperty);
        assertEquals(typePropName2, customModelProperty.getName());
        assertEquals("property2 title", customModelProperty.getTitle());
        assertEquals(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typePropName2, customModelProperty.getPrefixedName());
        assertEquals("d:int", customModelProperty.getDataType());
        assertEquals("property2 desciption", customModelProperty.getDescription());
        assertFalse(customModelProperty.isMultiValued());
        assertTrue(customModelProperty.isMandatory());
        assertTrue(customModelProperty.isMandatoryEnforced());
        assertEquals("0", customModelProperty.getDefaultValue());
        // Test duplicate property name
        typeProp = new CustomModelProperty();
        // Existing name
        typeProp.setName(typePropName2);
        typeProp.setTitle("new property title");
        typeProp.setDataType("d:text");
        props = new ArrayList<>(1);
        props.add(typeProp);
        payload.setProperties(props);
        // property name already exists
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 409);
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) Test(org.junit.Test)

Example 4 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class TestCustomProperty method testDeleteProperty.

@Test
public void testDeleteProperty() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelDeleteProp" + 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 two properties
         */
    String aspectName = "testAspect1" + System.currentTimeMillis();
    createTypeAspect(CustomAspect.class, modelName, aspectName, null, null, null);
    // Update the Aspect by adding property - property one
    CustomAspect aspectPayload = new CustomAspect();
    aspectPayload.setName(aspectName);
    String aspectPropNameOne = "testAspect1Prop1" + System.currentTimeMillis();
    CustomModelProperty aspectPropOne = new CustomModelProperty();
    aspectPropOne.setName(aspectPropNameOne);
    aspectPropOne.setTitle("aspect property one title");
    aspectPropOne.setMultiValued(true);
    List<CustomModelProperty> props = new ArrayList<>(1);
    props.add(aspectPropOne);
    aspectPayload.setProperties(props);
    // create property one
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 200);
    // Update the Aspect by adding another property - property two
    aspectPayload = new CustomAspect();
    aspectPayload.setName(aspectName);
    String aspectPropNameTwo = "testAspect1Prop2" + System.currentTimeMillis();
    CustomModelProperty aspectPropTwo = new CustomModelProperty();
    aspectPropTwo.setName(aspectPropNameTwo);
    aspectPropTwo.setTitle("aspect property two title");
    aspectPropTwo.setMandatory(true);
    aspectPropTwo.setDataType("d:int");
    aspectPropTwo.setDefaultValue("1");
    props = new ArrayList<>(1);
    props.add(aspectPropTwo);
    aspectPayload.setProperties(props);
    // create property two
    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 properties
    assertEquals(2, returnedAspect.getProperties().size());
    /*
         * Create type and update it by adding two properties
         */
    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);
    String typePropNameOne = "testType1Prop1" + System.currentTimeMillis();
    CustomModelProperty typePropOne = new CustomModelProperty();
    typePropOne.setName(typePropNameOne);
    typePropOne.setTitle("type property one title");
    props = new ArrayList<>(1);
    props.add(typePropOne);
    typePayload.setProperties(props);
    // create property one
    put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 200);
    // Update the Type by adding another property - property two
    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 typePropNameTwo = "testType1Prop2" + System.currentTimeMillis();
    CustomModelProperty typePropTwo = new CustomModelProperty();
    typePropTwo.setName(typePropNameTwo);
    typePropTwo.setTitle("type property two title");
    typePropTwo.setDataType("d:int");
    // add the inline constraint
    typePropTwo.setConstraints(Arrays.asList(inlineMinMaxConstraint));
    props = new ArrayList<>(1);
    props.add(typePropTwo);
    typePayload.setProperties(props);
    // create property one
    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 properties
    assertEquals(2, returnedType.getProperties().size());
    // Delete aspect's property one - model is inactive
    {
        final String deletePropOneAspectQS = getPropDeleteUpdateQS(aspectPropNameOne, true);
        // Try to delete propertyOne from aspect
        // missing payload
        put("cmm/" + modelName + "/aspects", aspectName, null, deletePropOneAspectQS, 400);
        CustomAspect deletePropAspectPayload = new CustomAspect();
        // missing aspect name
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropOneAspectQS, 400);
        setRequestContext(nonAdminUserName);
        deletePropAspectPayload.setName(aspectName);
        // unauthorised
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropOneAspectQS, 403);
        setRequestContext(customModelAdmin);
        // Delete as a Model Administrator
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropOneAspectQS, 200);
        // Check the property has been deleted
        response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
        returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        assertEquals(1, returnedAspect.getProperties().size());
        assertFalse("Property one should have been deleted.", aspectPropNameOne.equals(returnedAspect.getProperties().get(0).getName()));
        // Not found
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropOneAspectQS, 404);
    }
    // Delete type's property two - model is inactive
    {
        final String deletePropTwoTypeQS = getPropDeleteUpdateQS(typePropNameTwo, true);
        // Try to delete propertyOne from type
        // missing payload
        put("cmm/" + modelName + "/types", typeName, null, deletePropTwoTypeQS, 400);
        CustomType deletePropTypePayload = new CustomType();
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropTwoTypeQS, // missing type name
        400);
        setRequestContext(nonAdminUserName);
        deletePropTypePayload.setName(typeName);
        // unauthorised
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropTwoTypeQS, 403);
        setRequestContext(customModelAdmin);
        // Delete as a Model Administrator
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropTwoTypeQS, 200);
        // Check the property has been deleted
        response = getSingle("cmm/" + modelName + "/types", typeName, 200);
        returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals(1, returnedType.getProperties().size());
        assertFalse("Property two should have been deleted.", typePropNameTwo.equals(returnedType.getProperties().get(0).getName()));
        // Not found
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropTwoTypeQS, 404);
    }
    /*
         * APPSREPO-59: delete the last property of a Type / Aspect for an active model
         */
    // Activate the model
    CustomModel statusPayload = new CustomModel();
    statusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
    // Delete aspect's property two - model is active
    {
        setRequestContext(customModelAdmin);
        final String deletePropTwoAspectQS = getPropDeleteUpdateQS(aspectPropNameTwo, true);
        CustomAspect deletePropAspectPayload = new CustomAspect();
        deletePropAspectPayload.setName(aspectName);
        // Delete as a Model Administrator
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropTwoAspectQS, 200);
        // Check the property has been deleted
        response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
        returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        assertEquals("Property two should have been deleted.", 0, returnedAspect.getProperties().size());
        // Not found
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(deletePropAspectPayload), deletePropTwoAspectQS, 404);
    }
    // Delete type's property one - model is active
    {
        final String deletePropOneTypeQS = getPropDeleteUpdateQS(typePropNameOne, true);
        CustomType deletePropTypePayload = new CustomType();
        deletePropTypePayload.setName(typeName);
        // Delete as a Model Administrator
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropOneTypeQS, 200);
        // Check the property has been deleted
        response = getSingle("cmm/" + modelName + "/types", typeName, 200);
        returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals("Property one should have been deleted.", 0, returnedType.getProperties().size());
        // Not found
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(deletePropTypePayload), deletePropOneTypeQS, 404);
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) CustomModel(org.alfresco.rest.api.model.CustomModel) CustomModelNamedValue(org.alfresco.rest.api.model.CustomModelNamedValue) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) Test(org.junit.Test)

Example 5 with CustomModelProperty

use of org.alfresco.rest.api.model.CustomModelProperty in project alfresco-remote-api by Alfresco.

the class TestCustomTypeAspect method testUpdateAspectsTypes.

@Test
public void testUpdateAspectsTypes() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModeEditAspectType" + System.currentTimeMillis();
    final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    // Test update aspect
    {
        // Create aspect
        String aspectName = "testAspect" + System.currentTimeMillis();
        createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
        // Update the aspect
        CustomAspect aspectPayload = new CustomAspect();
        aspectPayload.setDescription(null);
        aspectPayload.setTitle("title modified");
        aspectPayload.setParentName("cm:titled");
        setRequestContext(nonAdminUserName);
        // Try to update the aspect as a non Admin user
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 403);
        setRequestContext(customModelAdmin);
        // Modify the name
        aspectPayload.setName(aspectName + "Modified");
        // Try to update the aspect as a Model Administrator
        // Note: aspect/type name cannot be modified
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 404);
        aspectPayload.setName(aspectName);
        // Update the aspect as a Model Administrator
        HttpResponse response = put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 200);
        CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
        compareCustomTypesAspects(aspectPayload, returnedAspect, "prefixedName");
        // Update the aspect with an invalid parent
        aspectPayload.setParentName("cm:titled" + System.currentTimeMillis());
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 409);
        // Activate the model
        CustomModel statusPayload = new CustomModel();
        statusPayload.setStatus(ModelStatus.ACTIVE);
        put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
        // Remove the aspect's parent
        // Note: cannot update the parent of an ACTIVE type/aspect.
        aspectPayload.setParentName(null);
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 409);
        statusPayload = new CustomModel();
        statusPayload.setStatus(ModelStatus.DRAFT);
        put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
        // now update the aspect's parent - model is inactive
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), null, 200);
    }
    // Test update type
    {
        // Create type
        String typeName = "testType" + System.currentTimeMillis();
        createTypeAspect(CustomType.class, modelName, typeName, "title", "desc", "cm:content");
        // Add property
        CustomType addPropertyPayload = new CustomType();
        addPropertyPayload.setName(typeName);
        String typePropName = "testTypeProp1" + System.currentTimeMillis();
        CustomModelProperty typeProp = new CustomModelProperty();
        typeProp.setName(typePropName);
        typeProp.setTitle("property title");
        typeProp.setDataType("d:text");
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        addPropertyPayload.setProperties(props);
        // Create the property
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(addPropertyPayload), SELECT_PROPS_QS, 200);
        // Update the type
        CustomType typePayload = new CustomType();
        typePayload.setDescription("desc modified");
        typePayload.setTitle("title modified");
        setRequestContext(nonAdminUserName);
        // Try to update the type as a non Admin user
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 403);
        setRequestContext(customModelAdmin);
        // Modify the name
        typePayload.setName(typeName + "Modified");
        // Try to update the type as a Model Administrator
        // Note: type/type name cannot be modified
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 404);
        typePayload.setName(typeName);
        // Update the type as a Model Administrator
        HttpResponse response = put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 200);
        CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals(typePayload.getDescription(), returnedType.getDescription());
        assertEquals(typePayload.getTitle(), returnedType.getTitle());
        // Check that properties are unmodified
        assertNotNull(returnedType.getProperties());
        assertEquals(1, returnedType.getProperties().size());
        assertEquals(typePropName, returnedType.getProperties().iterator().next().getName());
        // Update the type with an invalid parent
        typePayload.setParentName("cm:folder" + System.currentTimeMillis());
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 409);
        // Activate the model
        CustomModel statusPayload = new CustomModel();
        statusPayload.setStatus(ModelStatus.ACTIVE);
        put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
        // Remove the type's parent
        // Note: cannot update the parent of an ACTIVE type/type.
        typePayload.setParentName("cm:folder");
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 409);
        statusPayload = new CustomModel();
        statusPayload.setStatus(ModelStatus.DRAFT);
        put("cmm", modelName, RestApiUtil.toJsonAsString(statusPayload), SELECT_STATUS_QS, 200);
        // now update the type's parent - model is inactive
        response = put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), null, 200);
        returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
        assertEquals(typePayload.getParentName(), returnedType.getParentName());
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomAspect(org.alfresco.rest.api.model.CustomAspect) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) CustomModel(org.alfresco.rest.api.model.CustomModel) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) Test(org.junit.Test)

Aggregations

CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)18 ArrayList (java.util.ArrayList)15 CustomAspect (org.alfresco.rest.api.model.CustomAspect)14 Test (org.junit.Test)13 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)10 CustomType (org.alfresco.rest.api.model.CustomType)9 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)9 List (java.util.List)7 CustomModel (org.alfresco.rest.api.model.CustomModel)6 CustomModelNamedValue (org.alfresco.rest.api.model.CustomModelNamedValue)5 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)1 M2Property (org.alfresco.repo.dictionary.M2Property)1 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)1 AbstractClassModel (org.alfresco.rest.api.model.AbstractClassModel)1 SiteInformation (org.alfresco.rest.api.tests.RepoService.SiteInformation)1 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)1