Search in sources :

Example 11 with CustomType

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

the class TestCustomConstraint method testPropDefaultValueWithConstraintRef.

@Test
public void testPropDefaultValueWithConstraintRef() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelConstraintRef" + System.currentTimeMillis();
    final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
    {
        // Create List constraint
        String listConstraintName = "testListConstraint" + System.currentTimeMillis();
        CustomModelConstraint listConstraint = new CustomModelConstraint();
        listConstraint.setName(listConstraintName);
        listConstraint.setType("LIST");
        // Create the List constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(3);
        // list value
        parameters.add(buildNamedValue("allowedValues", null, "London", "Paris", "New York"));
        parameters.add(buildNamedValue("sorted", "false"));
        // Add the parameters into the constraint
        listConstraint.setParameters(parameters);
        // Create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(listConstraint), 201);
        // Retrieve the created List constraint
        HttpResponse response = getSingle("cmm/" + modelName + "/constraints", listConstraintName, 200);
        CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        // 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 with LIST constraint ref");
        aspectProp.setDataType("d:text");
        // Not in the list
        aspectProp.setDefaultValue("Berlin");
        // constrain ref
        aspectProp.setConstraintRefs(Arrays.asList(returnedConstraint.getPrefixedName()));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 409);
    }
    {
        // Create MINMAX constraint
        String minMaxConstraintName = "testMinMaxConstraint" + System.currentTimeMillis();
        CustomModelConstraint minMaxConstraint = new CustomModelConstraint();
        minMaxConstraint.setName(minMaxConstraintName);
        minMaxConstraint.setType("MINMAX");
        // Create the MinMax constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("maxValue", "100"));
        parameters.add(buildNamedValue("minValue", "50"));
        // Add the parameters into the constraint
        minMaxConstraint.setParameters(parameters);
        // Create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(minMaxConstraint), 201);
        // Retrieve the created MinMax constraint
        HttpResponse response = getSingle("cmm/" + modelName + "/constraints", minMaxConstraintName, 200);
        CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        // Create type
        String typeName = "testType1" + System.currentTimeMillis();
        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 with MINMAX constraint ref");
        typeProp.setDataType("d:int");
        // Not in the defined range [50,100]
        typeProp.setDefaultValue("35");
        // constrain ref
        typeProp.setConstraintRefs(Arrays.asList(returnedConstraint.getPrefixedName()));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        typePayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 409);
    }
}
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) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) Test(org.junit.Test)

Example 12 with CustomType

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

the class TestCustomConstraint method testPropDefaultValueWithInlineConstraint.

@Test
public void testPropDefaultValueWithInlineConstraint() 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);
    {
        // Create RegEx constraint
        String regExConstraintName = "testInlineFileNameRegEx" + System.currentTimeMillis();
        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 with REGEX constraint");
        aspectProp.setDataType("d:text");
        // invalid value
        aspectProp.setDefaultValue("invalid<defaultValue");
        // Add inline constraint
        aspectProp.setConstraints(Arrays.asList(inlineRegExConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 409);
    }
    {
        // Create inline anonymous LENGTH constraint
        CustomModelConstraint inlineAnonymousLengthConstraint = new CustomModelConstraint();
        inlineAnonymousLengthConstraint.setType("LENGTH");
        // Create the Length constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("maxLength", "4"));
        parameters.add(buildNamedValue("minLength", "0"));
        // Add the parameters into the constraint
        inlineAnonymousLengthConstraint.setParameters(parameters);
        // Create type
        String typeName = "testType1" + System.currentTimeMillis();
        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 with LENGTH constraint");
        typeProp.setDataType("d:text");
        // Invalid length
        typeProp.setDefaultValue("abcdef");
        // inline constraint
        typeProp.setConstraints(Arrays.asList(inlineAnonymousLengthConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        typePayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 409);
    }
    {
        // Create inline anonymous MINMAX constraint
        CustomModelConstraint inlineAnonymousMinMaxConstraint = new CustomModelConstraint();
        inlineAnonymousMinMaxConstraint.setType("MINMAX");
        // Create the MinMax constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        parameters.add(buildNamedValue("maxValue", "10"));
        parameters.add(buildNamedValue("minValue", "0"));
        // Add the parameters into the constraint
        inlineAnonymousMinMaxConstraint.setParameters(parameters);
        // Create type
        String typeName = "testType1" + System.currentTimeMillis();
        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 with MINMAX constraint");
        typeProp.setDataType("d:int");
        // Not in the defined range [0,10]
        typeProp.setDefaultValue("20");
        // inline constraint
        typeProp.setConstraints(Arrays.asList(inlineAnonymousMinMaxConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(typeProp);
        typePayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 409);
    }
    {
        // Create LIST constraint
        String listConstraintName = "testListConstraint" + System.currentTimeMillis();
        CustomModelConstraint inlineListConstraint = new CustomModelConstraint();
        inlineListConstraint.setName(listConstraintName);
        inlineListConstraint.setType("LIST");
        // Create the List constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(3);
        parameters.add(buildNamedValue("allowedValues", null, "one", "two", "three"));
        parameters.add(buildNamedValue("sorted", "false"));
        // Add the parameters into the constraint
        inlineListConstraint.setParameters(parameters);
        // 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 with LIST constraint");
        aspectProp.setDataType("d:text");
        // Not in the list
        aspectProp.setDefaultValue("four");
        // Add inline constraint
        aspectProp.setConstraints(Arrays.asList(inlineListConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 409);
    }
    {
        // Create Java Class constraint
        String inlineJavaClassConstraintName = "testJavaClassConstraint" + System.currentTimeMillis();
        CustomModelConstraint inlineListConstraint = new CustomModelConstraint();
        inlineListConstraint.setName(inlineJavaClassConstraintName);
        inlineListConstraint.setType("org.alfresco.rest.api.tests.TestCustomConstraint$DummyJavaClassConstraint");
        // 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 with Java Class constraint");
        aspectProp.setDataType("d:text");
        // Invalid default value
        aspectProp.setDefaultValue("invalid#value");
        // Add inline constraint
        aspectProp.setConstraints(Arrays.asList(inlineListConstraint));
        List<CustomModelProperty> props = new ArrayList<>(1);
        props.add(aspectProp);
        aspectPayload.setProperties(props);
        // Try to create the property - constraint violation
        put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 409);
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomAspect(org.alfresco.rest.api.model.CustomAspect) 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 13 with CustomType

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

the class TestCustomModel method testModelsCircularDependency.

// SHA-808
@Test
public void testModelsCircularDependency() throws Exception {
    setRequestContext(customModelAdmin);
    // Model One
    String modelNameOne = "testModelOne" + System.currentTimeMillis();
    Pair<String, String> namespacePairOne = getTestNamespaceUriPrefixPair();
    // Create the modelOne as a Model Administrator
    createCustomModel(modelNameOne, namespacePairOne, ModelStatus.DRAFT);
    // Add typeA_M1 into modelOne
    String typeA_M1 = "testTypeA_M1" + System.currentTimeMillis();
    final String typeA_M1_WithPrefix = namespacePairOne.getSecond() + QName.NAMESPACE_PREFIX + typeA_M1;
    createTypeAspect(CustomType.class, modelNameOne, typeA_M1, "test typeA_M1 title", null, "cm:content");
    // Activate modelOne
    CustomModel modelOneStatusPayload = new CustomModel();
    modelOneStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 200);
    // Add another type into modelOne with 'typeA_M1' as its parent
    String typeB_M1 = "testTypeB_M1" + System.currentTimeMillis();
    final String typeB_M1_WithPrefix = namespacePairOne.getSecond() + QName.NAMESPACE_PREFIX + typeB_M1;
    createTypeAspect(CustomType.class, modelNameOne, typeB_M1, "test typeB_M1 title", "test typeB_M1 Desc", typeA_M1_WithPrefix);
    // Model Two
    String modelNameTwo = "testModelTwo" + System.currentTimeMillis();
    Pair<String, String> namespacePairTwo = getTestNamespaceUriPrefixPair();
    // Create the modelTwo as a Model Administrator
    createCustomModel(modelNameTwo, namespacePairTwo, ModelStatus.DRAFT);
    // Add type1_M2 into modelTwo with 'typeB_M1' (from modelOne) as its parent
    String type1_M2 = "testType1_M2" + System.currentTimeMillis();
    final String type1_M2_WithPrefix = namespacePairTwo.getSecond() + QName.NAMESPACE_PREFIX + type1_M2;
    createTypeAspect(CustomType.class, modelNameTwo, type1_M2, "test type1_M2 title", null, typeB_M1_WithPrefix);
    // Activate modelTwo
    CustomModel modelTwoStatusPayload = new CustomModel();
    modelTwoStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelNameTwo, RestApiUtil.toJsonAsString(modelTwoStatusPayload), SELECT_STATUS_QS, 200);
    // Test that the API can handle "circular dependency" - (modelOne depends on modelTwo)
    {
        // Add another type into modelOne with 'type1_M2' (from modelTwo) as its parent
        String typeC_M1 = "testTypeC_M1" + System.currentTimeMillis();
        CustomType typeC_M1_Payload = new CustomType();
        typeC_M1_Payload.setName(typeC_M1);
        typeC_M1_Payload.setTitle("test typeC_M1 title");
        // => 'type1_M2' (from modelTwo)
        typeC_M1_Payload.setParentName(type1_M2_WithPrefix);
        // Try to create typeC_M1 which has 'circular dependency'
        // Constraint violation
        post("cmm/" + modelNameOne + "/types", RestApiUtil.toJsonAsString(typeC_M1_Payload), 409);
    }
    // Model Three
    String modelNameThree = "testModelThree" + System.currentTimeMillis();
    Pair<String, String> namespacePairThree = getTestNamespaceUriPrefixPair();
    // Create the modelThree as a Model Administrator
    createCustomModel(modelNameThree, namespacePairThree, ModelStatus.DRAFT);
    // Add type1_M3 into modelThree with 'type1_M2' (from modelTwo) as its parent
    String type1_M3 = "testType1_M3" + System.currentTimeMillis();
    final String type1_M3_WithPrefix = namespacePairThree.getSecond() + QName.NAMESPACE_PREFIX + type1_M3;
    createTypeAspect(CustomType.class, modelNameThree, type1_M3, "test type1_M3 title", null, type1_M2_WithPrefix);
    // Activate modelThree
    CustomModel modelThreeStatusPayload = new CustomModel();
    modelThreeStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelNameThree, RestApiUtil.toJsonAsString(modelThreeStatusPayload), SELECT_STATUS_QS, 200);
    // Test that the API can handle "circular dependency" - (modelOne depends on modelThree)
    {
        // Add another type into modelOne with 'type1_M3' (from modelThree) as its parent
        String typeC_M1 = "testTypeC_M1" + System.currentTimeMillis();
        CustomType typeC_M1_Payload = new CustomType();
        typeC_M1_Payload.setName(typeC_M1);
        typeC_M1_Payload.setTitle("test typeC_M1 title");
        // => 'type1_M3' (from modelThree)
        typeC_M1_Payload.setParentName(type1_M3_WithPrefix);
        // Try to create typeC_M1 which has 'circular dependency'
        // Constraint violation
        post("cmm/" + modelNameOne + "/types", RestApiUtil.toJsonAsString(typeC_M1_Payload), 409);
    }
    // Model Three
    String modelNameFour = "testModelFour" + System.currentTimeMillis();
    Pair<String, String> namespacePairFour = getTestNamespaceUriPrefixPair();
    // Create the modelFour as a Model Administrator
    createCustomModel(modelNameFour, namespacePairFour, ModelStatus.DRAFT);
    // Add type1_M4 into modelFour with 'type1_M3' (from modelThree) as its parent
    String type1_M4 = "testType1_M4" + System.currentTimeMillis();
    final String type1_M4_WithPrefix = namespacePairFour.getSecond() + QName.NAMESPACE_PREFIX + type1_M4;
    createTypeAspect(CustomType.class, modelNameFour, type1_M4, "test type1_M4 title", null, type1_M3_WithPrefix);
    // Activate modelFour
    CustomModel modelFourStatusPayload = new CustomModel();
    modelFourStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelNameFour, RestApiUtil.toJsonAsString(modelFourStatusPayload), SELECT_STATUS_QS, 200);
    // Test that the API can handle "circular dependency" - (modelOne depends on modelFour)
    {
        // Add another type into modelOne with 'type1_M4' (from modelFour) as its parent
        String typeC_M1 = "testTypeC_M1" + System.currentTimeMillis();
        CustomType typeC_M1_Payload = new CustomType();
        typeC_M1_Payload.setName(typeC_M1);
        typeC_M1_Payload.setTitle("test typeC_M1 title");
        // => 'type1_M4' (from modelFour)
        typeC_M1_Payload.setParentName(type1_M4_WithPrefix);
        // Try to create typeC_M1 which has 'circular dependency'
        // Constraint violation
        post("cmm/" + modelNameOne + "/types", RestApiUtil.toJsonAsString(typeC_M1_Payload), 409);
    }
    // Test that the API can handle "circular dependency" - (modelTwo depends on modelFour)
    {
        // Add another type into modelTwo with 'type1_M4' (from modelFour) as its parent
        String type2_M2 = "testType2_M2" + System.currentTimeMillis();
        CustomType type2_M2_Payload = new CustomType();
        type2_M2_Payload.setName(type2_M2);
        type2_M2_Payload.setTitle("test type2_M2 title");
        // => 'type1_M4' (from modelFour)
        type2_M2_Payload.setParentName(type1_M4_WithPrefix);
        // Try to create type2_M2 which has 'circular dependency'
        // Constraint violation
        post("cmm/" + modelNameTwo + "/types", RestApiUtil.toJsonAsString(type2_M2_Payload), 409);
    }
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) CustomModel(org.alfresco.rest.api.model.CustomModel) Test(org.junit.Test)

Example 14 with CustomType

use of org.alfresco.rest.api.model.CustomType 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());
    }
}
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) ArrayList(java.util.ArrayList) List(java.util.List) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) Test(org.junit.Test)

Example 15 with CustomType

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

the class TestCustomTypeAspect method testCustomModelTypesAspectsDependencies.

@Test
public // SHA-679
void testCustomModelTypesAspectsDependencies() throws Exception {
    setRequestContext(customModelAdmin);
    String modelNameOne = "testModel" + System.currentTimeMillis();
    Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelNameOne, namespacePair, ModelStatus.DRAFT, null, "Mark Moe");
    // Add type
    String typeBaseName = "testTypeBase" + System.currentTimeMillis();
    final String typeBaseNameWithPrefix = namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typeBaseName;
    createTypeAspect(CustomType.class, modelNameOne, typeBaseName, "test typeBase title", "test typeBase Desc", "cm:content");
    // Activate the model
    CustomModel modelOneStatusPayload = new CustomModel();
    modelOneStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 200);
    HttpResponse response = getSingle("cmm", modelNameOne, 200);
    CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
    assertEquals(ModelStatus.ACTIVE, returnedModel.getStatus());
    // Add another type with 'typeBaseName' as its parent
    String typeName2 = "testTypeChild" + System.currentTimeMillis();
    createTypeAspect(CustomType.class, modelNameOne, typeName2, "test typeChild title", "test typeChild Desc", typeBaseNameWithPrefix);
    Paging paging = getPaging(0, Integer.MAX_VALUE);
    response = getAll("cmm/" + modelNameOne + "/types", paging, 200);
    List<CustomType> returnedTypes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomType.class);
    assertEquals(2, returnedTypes.size());
    // Create another model
    String modelNameTwo = "testModelTwo" + System.currentTimeMillis();
    Pair<String, String> modelTwoNamespacePair = getTestNamespaceUriPrefixPair();
    createCustomModel(modelNameTwo, modelTwoNamespacePair, ModelStatus.DRAFT, null, "Admin");
    // Add a type with 'typeBaseName' from the modelOne as its parent
    String modelTwoTypeName = "testModelTwoChild" + System.currentTimeMillis();
    createTypeAspect(CustomType.class, modelNameTwo, modelTwoTypeName, "test model two type child title", null, typeBaseNameWithPrefix);
    // Try to deactivate modelOne
    modelOneStatusPayload = new CustomModel();
    modelOneStatusPayload.setStatus(ModelStatus.DRAFT);
    // ModelTwo depends on ModelOne
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 409);
    // Activate modelTwo
    CustomModel modelTwoStatusPayload = new CustomModel();
    modelTwoStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelNameTwo, RestApiUtil.toJsonAsString(modelTwoStatusPayload), SELECT_STATUS_QS, 200);
    // Try to deactivate modelOne again. The dependent model is Active now, however, the result should be the same.
    // ModelTwo depends on ModelOne
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 409);
    // Deactivate modelTwo
    modelTwoStatusPayload = new CustomModel();
    modelTwoStatusPayload.setStatus(ModelStatus.DRAFT);
    put("cmm", modelNameTwo, RestApiUtil.toJsonAsString(modelTwoStatusPayload), SELECT_STATUS_QS, 200);
    // Delete the modelTwo's type as a Model Administrator
    delete("cmm/" + modelNameTwo + "/types", modelTwoTypeName, 204);
    // Try to deactivate modelOne again. There is no dependency
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 200);
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) CustomModel(org.alfresco.rest.api.model.CustomModel) Test(org.junit.Test)

Aggregations

CustomType (org.alfresco.rest.api.model.CustomType)19 CustomAspect (org.alfresco.rest.api.model.CustomAspect)15 Test (org.junit.Test)14 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)12 ArrayList (java.util.ArrayList)9 CustomModel (org.alfresco.rest.api.model.CustomModel)9 CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)9 List (java.util.List)7 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)7 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)3 CustomModelNamedValue (org.alfresco.rest.api.model.CustomModelNamedValue)2 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 CompiledModel (org.alfresco.repo.dictionary.CompiledModel)1 M2Aspect (org.alfresco.repo.dictionary.M2Aspect)1 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)1 M2Model (org.alfresco.repo.dictionary.M2Model)1