Search in sources :

Example 11 with CustomModelConstraint

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

the class TestCustomConstraint method testCreateConstraintAndAddToProperty.

@Test
public void testCreateConstraintAndAddToProperty() throws Exception {
    setRequestContext(customModelAdmin);
    String modelName = "testModelConstraint" + 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 = "testFileNameRegEx" + System.currentTimeMillis();
    CustomModelConstraint regExConstraint = new CustomModelConstraint();
    regExConstraint.setName(regExConstraintName);
    regExConstraint.setType("REGEX");
    regExConstraint.setTitle("test RegEx title");
    regExConstraint.setDescription("test RegEx desc");
    // 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);
    // Create constraint as a Model Administrator
    post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 201);
    // Retrieve the created constraint
    HttpResponse response = getSingle("cmm/" + modelName + "/constraints", regExConstraintName, 200);
    CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
    // Retrieve all the model's constraints
    Paging paging = getPaging(0, Integer.MAX_VALUE);
    response = getAll("cmm/" + modelName + "/constraints", paging, 200);
    List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
    assertEquals(1, constraints.size());
    // Create aspect
    String aspectName = "testAspect1" + System.currentTimeMillis();
    createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
    // Update the Aspect by adding property
    CustomAspect payload = new CustomAspect();
    payload.setName(aspectName);
    final String aspectPropName = "testAspect1Prop1" + System.currentTimeMillis();
    CustomModelProperty aspectProp = new CustomModelProperty();
    aspectProp.setName(aspectPropName);
    aspectProp.setTitle("property title");
    aspectProp.setDataType("d:text");
    // Add the constraint ref
    aspectProp.setConstraintRefs(Arrays.asList(returnedConstraint.getPrefixedName()));
    List<CustomModelProperty> props = new ArrayList<>(1);
    props.add(aspectProp);
    payload.setProperties(props);
    // Create the property
    put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(payload), SELECT_PROPS_QS, 200);
    // Activate the model
    CustomModel updatePayload = new CustomModel();
    updatePayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
    // Retrieve all the model's constraints
    // Test to see if the API took care of duplicate constraints when referencing a constraint within a property.
    response = getAll("cmm/" + modelName + "/constraints", paging, 200);
    constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
    assertEquals(1, constraints.size());
    // Test RegEx constrain enforcement
    {
        final NodeService nodeService = repoService.getNodeService();
        final QName aspectQName = QName.createQName("{" + namespacePair.getFirst() + "}" + aspectName);
        TestNetwork testNetwork = getTestFixture().getRandomNetwork();
        TestPerson person = testNetwork.createUser();
        final String siteName = "site" + System.currentTimeMillis();
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
                TestSite site = repoService.createSite(null, siteInfo);
                NodeRef nodeRef = repoService.createDocument(site.getContainerNodeRef("documentLibrary"), "Test Doc", "Test Content");
                nodeService.addAspect(nodeRef, aspectQName, null);
                assertTrue(nodeService.hasAspect(nodeRef, aspectQName));
                try {
                    QName propQName = QName.createQName("{" + namespacePair.getFirst() + "}" + aspectPropName);
                    nodeService.setProperty(nodeRef, propQName, "Invalid$Char.");
                    fail("Invalid property value. Should have caused integrity violations.");
                } catch (Exception e) {
                // Expected
                }
                // Permanently remove model from repository
                nodeService.addAspect(nodeRef, ContentModel.ASPECT_TEMPORARY, null);
                nodeService.deleteNode(nodeRef);
                return null;
            }
        }, person.getId(), testNetwork.getId());
    }
    setRequestContext(customModelAdmin);
    // Deactivate the model
    updatePayload = new CustomModel();
    updatePayload.setStatus(ModelStatus.DRAFT);
    put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
    // Test update the namespace prefix (test to see if the API updates the constraints refs with this new prefix)
    CustomModel updateModelPayload = new CustomModel();
    String modifiedPrefix = namespacePair.getSecond() + "Modified";
    updateModelPayload.setNamespacePrefix(modifiedPrefix);
    updateModelPayload.setNamespaceUri(namespacePair.getFirst());
    response = put("cmm", modelName, RestApiUtil.toJsonAsString(updateModelPayload), null, 200);
    CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
    assertEquals(modifiedPrefix, returnedModel.getNamespacePrefix());
    assertEquals("The namespace URI shouldn't have changed.", namespacePair.getFirst(), returnedModel.getNamespaceUri());
    // Test update the namespace URI
    updateModelPayload = new CustomModel();
    updateModelPayload.setNamespacePrefix(modifiedPrefix);
    String modifiedURI = namespacePair.getFirst() + "Modified";
    updateModelPayload.setNamespaceUri(modifiedURI);
    response = put("cmm", modelName, RestApiUtil.toJsonAsString(updateModelPayload), null, 200);
    returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
    assertEquals(modifiedURI, returnedModel.getNamespaceUri());
    assertEquals("The namespace prefix shouldn't have changed.", modifiedPrefix, returnedModel.getNamespacePrefix());
}
Also used : QName(org.alfresco.service.namespace.QName) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) NodeService(org.alfresco.service.cmr.repository.NodeService) ArrayList(java.util.ArrayList) CustomAspect(org.alfresco.rest.api.model.CustomAspect) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) CustomModelProperty(org.alfresco.rest.api.model.CustomModelProperty) CustomModel(org.alfresco.rest.api.model.CustomModel) ConstraintException(org.alfresco.service.cmr.dictionary.ConstraintException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) CustomModelNamedValue(org.alfresco.rest.api.model.CustomModelNamedValue) SiteInformation(org.alfresco.rest.api.tests.RepoService.SiteInformation) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) Test(org.junit.Test)

Example 12 with CustomModelConstraint

use of org.alfresco.rest.api.model.CustomModelConstraint 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 CustomModelConstraint

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

the class TestCustomConstraint method testCreateConstraints.

@Test
public void testCreateConstraints() throws Exception {
    setRequestContext(customModelAdmin);
    final Paging paging = getPaging(0, Integer.MAX_VALUE);
    String modelName = "testModelConstraint" + 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 = "testFileNameRegEx" + System.currentTimeMillis();
        CustomModelConstraint regExConstraint = new CustomModelConstraint();
        regExConstraint.setName(regExConstraintName);
        regExConstraint.setType("REGEX");
        regExConstraint.setTitle("test RegEx title");
        regExConstraint.setDescription("test RegEx desc");
        // 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);
        setRequestContext(nonAdminUserName);
        // Try to create constraint as a non Admin user
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 403);
        setRequestContext(customModelAdmin);
        // 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", regExConstraintName, 200);
        CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        compareCustomModelConstraints(regExConstraint, returnedConstraint, "prefixedName");
        // Try to create a duplicate constraint
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 409);
        // Retrieve all the model's constraints
        response = getAll("cmm/" + modelName + "/constraints", paging, 200);
        List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
        assertEquals(1, constraints.size());
    }
    setRequestContext(customModelAdmin);
    // Try to create invalid RegEx constraint
    {
        String regExConstraintName = "testFileNameInvalidRegEx" + System.currentTimeMillis();
        CustomModelConstraint regExConstraint = new CustomModelConstraint();
        regExConstraint.setName(regExConstraintName);
        regExConstraint.setType("REGEX");
        // 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
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 400);
    }
    // Create MINMAX constraint
    {
        String minMaxConstraintName = "testMinMaxConstraint" + System.currentTimeMillis();
        CustomModelConstraint minMaxConstraint = new CustomModelConstraint();
        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);
        // Try to create constraint as a Model Administrator
        // constraint's type is mandatory
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(minMaxConstraint), 400);
        minMaxConstraint.setType("MINMAX");
        parameters.clear();
        // invalid number
        parameters.add(buildNamedValue("maxValue", "abc"));
        parameters.add(buildNamedValue("minValue", "0.0"));
        // Add the parameters into the constraint
        minMaxConstraint.setParameters(parameters);
        // Try to create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(minMaxConstraint), 400);
        parameters.clear();
        parameters.add(buildNamedValue("maxValue", "100"));
        // invalid number
        parameters.add(buildNamedValue("minValue", "text"));
        // Add the parameters into the constraint
        minMaxConstraint.setParameters(parameters);
        // Try to create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(minMaxConstraint), 400);
        parameters.clear();
        parameters.add(buildNamedValue("maxValue", "100.0"));
        parameters.add(buildNamedValue("minValue", "0.0"));
        // 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);
        compareCustomModelConstraints(minMaxConstraint, returnedConstraint, "prefixedName");
        // Retrieve all the model's constraints
        response = getAll("cmm/" + modelName + "/constraints", paging, 200);
        List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
        assertEquals(2, constraints.size());
    }
    // Create LENGTH constraint
    {
        String lengthConstraintName = "testLengthConstraint" + System.currentTimeMillis();
        CustomModelConstraint lengthConstraint = new CustomModelConstraint();
        lengthConstraint.setName(lengthConstraintName);
        lengthConstraint.setType("LENGTH");
        lengthConstraint.setTitle("test Length title");
        lengthConstraint.setDescription("test Length desc");
        // Create the Length constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(2);
        // invalid number
        parameters.add(buildNamedValue("maxLength", "text"));
        parameters.add(buildNamedValue("minLength", "0"));
        // Add the parameters into the constraint
        lengthConstraint.setParameters(parameters);
        // Try to create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(lengthConstraint), 400);
        parameters.clear();
        parameters.add(buildNamedValue("maxLength", "256"));
        // double number
        parameters.add(buildNamedValue("minLength", "1.0"));
        // Add the parameters into the constraint
        lengthConstraint.setParameters(parameters);
        // Try to create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(lengthConstraint), 400);
        parameters.clear();
        parameters.add(buildNamedValue("maxLength", "256"));
        parameters.add(buildNamedValue("minLength", "0"));
        // Add the parameters into the constraint
        lengthConstraint.setParameters(parameters);
        // Create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(lengthConstraint), 201);
        // Retrieve the created LENGTH constraint
        HttpResponse response = getSingle("cmm/" + modelName + "/constraints", lengthConstraintName, 200);
        CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        compareCustomModelConstraints(lengthConstraint, returnedConstraint, "prefixedName");
        // Retrieve all the model's constraints
        response = getAll("cmm/" + modelName + "/constraints", paging, 200);
        List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
        assertEquals(3, constraints.size());
    }
    // Create LIST constraint
    {
        String listConstraintName = "testListConstraint" + System.currentTimeMillis();
        CustomModelConstraint listConstraint = new CustomModelConstraint();
        listConstraint.setName(listConstraintName);
        listConstraint.setType("LIST");
        listConstraint.setTitle("test List title");
        listConstraint.setDescription("test List desc");
        // Create the List constraint's parameters
        List<CustomModelNamedValue> parameters = new ArrayList<>(3);
        // list value
        parameters.add(buildNamedValue("allowedValues", null, "High", "Normal", "Low"));
        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);
        compareCustomModelConstraints(listConstraint, returnedConstraint, "prefixedName", "parameters");
        String sorted = getParameterSimpleValue(returnedConstraint.getParameters(), "sorted");
        assertEquals("false", sorted);
        List<String> listValues = getParameterListValue(returnedConstraint.getParameters(), "allowedValues");
        assertNotNull(listValues);
        assertEquals(3, listValues.size());
        assertEquals("High", listValues.get(0));
        assertEquals("Normal", listValues.get(1));
        assertEquals("Low", listValues.get(2));
        // Retrieve all the model's constraints
        response = getAll("cmm/" + modelName + "/constraints", paging, 200);
        List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
        assertEquals(4, constraints.size());
    }
    // Create authorityName constraint
    {
        String authorityNameConstraintName = "authorityNameConstraint" + System.currentTimeMillis();
        CustomModelConstraint authorityNameConstraint = new CustomModelConstraint();
        authorityNameConstraint.setName(authorityNameConstraintName);
        authorityNameConstraint.setType("org.alfresco.repo.dictionary.constraint.AuthorityNameConstraint");
        // Create constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(authorityNameConstraint), 201);
        // Retrieve the created authorityName constraint
        HttpResponse response = getSingle("cmm/" + modelName + "/constraints", authorityNameConstraintName, 200);
        CustomModelConstraint returnedConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
        compareCustomModelConstraints(authorityNameConstraint, returnedConstraint, "prefixedName");
        // Retrieve all the model's constraints
        response = getAll("cmm/" + modelName + "/constraints", paging, 200);
        List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
        assertEquals(5, constraints.size());
    }
    // Create Invalid constraint
    {
        String invalidConstraintName = "testInvalidConstraint" + System.currentTimeMillis();
        CustomModelConstraint invalidConstraint = new CustomModelConstraint();
        invalidConstraint.setName(invalidConstraintName);
        invalidConstraint.setType("InvalidConstraintType" + System.currentTimeMillis());
        invalidConstraint.setTitle("test Invalid title");
        invalidConstraint.setDescription("test Invalid 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
        invalidConstraint.setParameters(parameters);
        // Try to create an invalid constraint as a Model Administrator
        post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(invalidConstraint), 400);
        // Retrieve all the model's constraints
        HttpResponse response = getAll("cmm/" + modelName + "/constraints", paging, 200);
        List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
        assertEquals(5, constraints.size());
    }
    // Activate the model
    CustomModel updatePayload = new CustomModel();
    updatePayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
    // Retrieve all the model's constraints
    HttpResponse response = getAll("cmm/" + modelName + "/constraints", paging, 200);
    List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
    assertEquals(5, constraints.size());
    // Deactivate the model
    updatePayload = new CustomModel();
    updatePayload.setStatus(ModelStatus.DRAFT);
    put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
    // Retrieve all the model's constraints
    response = getAll("cmm/" + modelName + "/constraints", paging, 200);
    constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
    assertEquals(5, constraints.size());
}
Also used : Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) CustomModel(org.alfresco.rest.api.model.CustomModel) Test(org.junit.Test)

Example 14 with CustomModelConstraint

use of org.alfresco.rest.api.model.CustomModelConstraint 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)

Aggregations

CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)14 CustomAspect (org.alfresco.rest.api.model.CustomAspect)10 ArrayList (java.util.ArrayList)9 CustomModelProperty (org.alfresco.rest.api.model.CustomModelProperty)9 Test (org.junit.Test)9 CustomType (org.alfresco.rest.api.model.CustomType)7 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)6 List (java.util.List)5 CustomModel (org.alfresco.rest.api.model.CustomModel)5 CustomModelNamedValue (org.alfresco.rest.api.model.CustomModelNamedValue)5 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)3 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)3 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)3 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)3 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 CustomModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException)2 InvalidCustomModelException (org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException)2 QName (org.alfresco.service.namespace.QName)2