Search in sources :

Example 91 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class DictionaryModelTypeTest method testUpdateDictionaryModelConstraintDelete.

@Test
public void testUpdateDictionaryModelConstraintDelete() throws Exception {
    try {
        // Check that the model has not yet been loaded into the dictionary
        this.dictionaryService.getModel(TEST_MODEL_TWO);
        fail("This model has not yet been loaded into the dictionary service");
    } catch (DictionaryException exception) {
    // We expect this exception
    }
    // Check that the namespace is not yet in the namespace service
    String uri = this.namespaceService.getNamespaceURI("test2");
    assertNull(uri);
    // Create a model node
    PropertyMap properties = new PropertyMap(1);
    properties.put(ContentModel.PROP_MODEL_ACTIVE, true);
    final NodeRef modelNode = this.nodeService.createNode(this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"), ContentModel.TYPE_DICTIONARY_MODEL, properties).getChildRef();
    assertNotNull(modelNode);
    // Add the model content to the model node
    ContentWriter contentWriter = this.contentService.getWriter(modelNode, ContentModel.PROP_CONTENT, true);
    contentWriter.setEncoding("UTF-8");
    contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
    contentWriter.putContent(MODEL_TWO_XML);
    // End the transaction to force update
    TestTransaction.flagForCommit();
    TestTransaction.end();
    final NodeRef workingCopy = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Exception {
            // Check that the namespace is in the namespace service
            String uri = namespaceService.getNamespaceURI("test2");
            assertNotNull(uri);
            Collection<ConstraintDefinition> constraints = dictionaryService.getConstraints(TEST_MODEL_TWO, true);
            assertEquals(1, constraints.size());
            assertEquals("test2:con1", constraints.iterator().next().getName().getPrefixString());
            // Update model
            NodeRef workingCopy = DictionaryModelTypeTest.this.cociService.checkout(modelNode);
            ContentWriter contentWriter2 = DictionaryModelTypeTest.this.contentService.getWriter(workingCopy, ContentModel.PROP_CONTENT, true);
            contentWriter2.putContent(MODEL_TWO_INVALID_XML);
            return workingCopy;
        }
    });
    try {
        transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

            public Object execute() throws Exception {
                // Check that the policy has not been fired since we have updated a working copy
                assertEquals("1.0", DictionaryModelTypeTest.this.nodeService.getProperty(workingCopy, ContentModel.PROP_MODEL_VERSION));
                // Check-in the model change
                DictionaryModelTypeTest.this.cociService.checkin(workingCopy, null);
                return null;
            }
        });
        fail("Unexpected - should not be able to update model");
    } catch (AlfrescoRuntimeException are) {
        assertTrue(are.getMessage().contains("Failed to validate constraint delete"));
    }
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // Check that the policy has not been fired since the previous update was invalid
            assertEquals("1.0", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_VERSION));
            // Update model
            ContentWriter contentWriter2 = DictionaryModelTypeTest.this.contentService.getWriter(workingCopy, ContentModel.PROP_CONTENT, true);
            contentWriter2.putContent(MODEL_TWO_MODIFIED_XML);
            // Check-in the model change
            DictionaryModelTypeTest.this.cociService.checkin(workingCopy, null);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // Now check that the model has been updated
            Collection<ConstraintDefinition> constraints = dictionaryService.getConstraints(TEST_MODEL_TWO, true);
            assertEquals(0, constraints.size());
            assertEquals("1.1", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_VERSION));
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            DictionaryModelTypeTest.this.nodeService.deleteNode(modelNode);
            return null;
        }
    });
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap) Collection(java.util.Collection) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) ExpectedException(org.junit.rules.ExpectedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 92 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class DictionaryModelTypeTest method testOverrideMandatoryProperty.

/**
 * Test for MNT-11653
 */
@SuppressWarnings("deprecation")
@Test
public void testOverrideMandatoryProperty() throws Exception {
    try {
        // Check that the model has not yet been loaded into the dictionary
        this.dictionaryService.getModel(TEST_MODEL_THREE);
        fail("This model has not yet been loaded into the dictionary service");
    } catch (DictionaryException exception) {
    // We expect this exception
    }
    // Check that the namespace is not yet in the namespace service
    String uri = this.namespaceService.getNamespaceURI("test3");
    assertNull(uri);
    // Create a model node
    PropertyMap properties = new PropertyMap(1);
    properties.put(ContentModel.PROP_MODEL_ACTIVE, true);
    final NodeRef modelNode = this.nodeService.createNode(this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"), ContentModel.TYPE_DICTIONARY_MODEL, properties).getChildRef();
    assertNotNull(modelNode);
    // Add the model content to the model node
    ContentWriter contentWriter = this.contentService.getWriter(modelNode, ContentModel.PROP_CONTENT, true);
    contentWriter.setEncoding("UTF-8");
    contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
    contentWriter.putContent(MODEL_THREE_XML);
    // End the transaction to force update
    TestTransaction.flagForCommit();
    TestTransaction.end();
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // Validate the model
            ContentReader reader = DictionaryModelTypeTest.this.contentService.getReader(modelNode, ContentModel.PROP_CONTENT);
            Source transferReportSource = new StreamSource(reader.getContentInputStream());
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final String schemaLocation = "classpath:alfresco/model/modelSchema.xsd";
            Schema schema = sf.newSchema(ResourceUtils.getURL(schemaLocation));
            Validator validator = schema.newValidator();
            validator.validate(transferReportSource);
            return null;
        }
    });
    // create node using new type
    final NodeRef node1 = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Exception {
            Map<QName, Serializable> properties = new HashMap<>();
            properties.put(QName.createQName("http://www.alfresco.org/test/testmodel3/1.0", "prop1"), "testvalue");
            NodeRef node = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("http://www.alfresco.org/model/system/1.0", "node1"), QName.createQName("http://www.alfresco.org/test/testmodel3/1.0", "base-override"), properties).getChildRef();
            assertNotNull(node);
            return node;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // Delete the node
            DictionaryModelTypeTest.this.nodeService.deleteNode(node1);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            // Delete the model
            DictionaryModelTypeTest.this.nodeService.deleteNode(modelNode);
            return null;
        }
    });
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ContentReader(org.alfresco.service.cmr.repository.ContentReader) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) ExpectedException(org.junit.rules.ExpectedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap) PropertyMap(org.alfresco.util.PropertyMap) Validator(javax.xml.validation.Validator) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 93 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class DictionaryModelTypeTest method testImportingSameNamespaceFailsWithinSingleModel.

/* MNT-15345 test */
@Test(expected = AlfrescoRuntimeException.class)
public void testImportingSameNamespaceFailsWithinSingleModel() throws Exception {
    // Create model
    PropertyMap properties = new PropertyMap(1);
    properties.put(ContentModel.PROP_MODEL_ACTIVE, true);
    final NodeRef modelNode = this.nodeService.createNode(this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"), ContentModel.TYPE_DICTIONARY_MODEL, properties).getChildRef();
    assertNotNull(modelNode);
    TestTransaction.flagForCommit();
    TestTransaction.end();
    // update model to introduce self referencing dependency
    try {
        RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
        txnHelper.setMaxRetries(1);
        txnHelper.doInTransaction(new RetryingTransactionCallback<Void>() {

            public Void execute() throws Exception {
                ContentWriter contentWriter = contentService.getWriter(modelNode, ContentModel.PROP_CONTENT, true);
                contentWriter.setEncoding("UTF-8");
                contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
                contentWriter.putContent(Thread.currentThread().getContextClassLoader().getResourceAsStream("dictionary/modelWithCurrentNamespaceImported.xml"));
                return null;
            }
        }, false, true);
        fail("Validation should fail as a circular dependency was introduced");
    } catch (AlfrescoRuntimeException e) {
        assertTrue(e.getCause().getMessage().contains("URI http://www.alfresco.org/model/dictionary/1.0 cannot be imported as it is already contained in the model's namespaces"));
        throw e;
    } finally // delete model
    {
        transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

            public Object execute() throws Exception {
                // Delete the model
                DictionaryModelTypeTest.this.nodeService.deleteNode(modelNode);
                return null;
            }
        }, false, true);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException) ExpectedException(org.junit.rules.ExpectedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 94 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class CustomModelServiceImplTest method testModelAdmin.

@Test
public void testModelAdmin() throws Exception {
    assertFalse(customModelService.isModelAdmin(null));
    final String userName = "testUser" + System.currentTimeMillis();
    final PropertyMap testUser = new PropertyMap();
    testUser.put(ContentModel.PROP_USERNAME, userName);
    testUser.put(ContentModel.PROP_FIRSTNAME, "John");
    testUser.put(ContentModel.PROP_LASTNAME, "Doe");
    testUser.put(ContentModel.PROP_PASSWORD, "password");
    final NodeRef personNodeRef = transactionHelper.doInTransaction(new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Exception {
            NodeRef nodeRef = personService.createPerson(testUser);
            assertNotNull(nodeRef);
            assertFalse(customModelService.isModelAdmin(userName));
            // Add the user to the group
            authorityService.addAuthority(CustomModelServiceImpl.GROUP_ALFRESCO_MODEL_ADMINISTRATORS_AUTHORITY, userName);
            assertTrue(customModelService.isModelAdmin(userName));
            return nodeRef;
        }
    });
    // Cleanup
    transactionHelper.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Exception {
            personService.deletePerson(personNodeRef);
            return null;
        }
    });
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) PropertyMap(org.alfresco.util.PropertyMap) NamespaceConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException) InvalidNamespaceException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidNamespaceException) ActiveModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException) ModelDoesNotExistException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException) Test(org.junit.Test)

Example 95 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class HomeFolderProviderSynchronizerTest method createContent.

private NodeRef createContent(String parentPath, String name) throws Exception {
    NodeRef parent = createFolder(parentPath);
    PropertyMap propertyMap = new PropertyMap();
    propertyMap.put(ContentModel.PROP_CONTENT, new ContentData(null, "text/plain", 0L, "UTF-16", Locale.ENGLISH));
    propertyMap.put(ContentModel.PROP_NAME, name);
    NodeRef content = nodeService.createNode(parent, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name), ContentModel.TYPE_CONTENT, propertyMap).getChildRef();
    ContentWriter writer = contentService.getWriter(content, ContentModel.TYPE_CONTENT, true);
    writer.putContent("The cat sat on the mat.");
    // System.out.println(NodeStoreInspector.dumpNode(nodeService, rootNodeRef));
    return content;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap) ContentData(org.alfresco.service.cmr.repository.ContentData)

Aggregations

PropertyMap (org.alfresco.util.PropertyMap)111 NodeRef (org.alfresco.service.cmr.repository.NodeRef)43 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)15 Test (org.junit.Test)14 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)13 QName (org.alfresco.service.namespace.QName)11 Serializable (java.io.Serializable)9 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)8 ContentReader (org.alfresco.service.cmr.repository.ContentReader)8 BaseSpringTest (org.alfresco.util.BaseSpringTest)8 FileContentReader (org.alfresco.repo.content.filestore.FileContentReader)7 DictionaryException (org.alfresco.service.cmr.dictionary.DictionaryException)7 ExpectedException (org.junit.rules.ExpectedException)7 ApplicationContext (org.springframework.context.ApplicationContext)7 StoreRef (org.alfresco.service.cmr.repository.StoreRef)6 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)5 NodeService (org.alfresco.service.cmr.repository.NodeService)5 Date (java.util.Date)4 HashMap (java.util.HashMap)4 ServiceRegistry (org.alfresco.service.ServiceRegistry)4