Search in sources :

Example 1 with InvalidNamespaceException

use of org.alfresco.service.cmr.dictionary.CustomModelException.InvalidNamespaceException in project alfresco-repository by Alfresco.

the class CustomModelServiceImplTest method testUpdateModel.

@Test
public void testUpdateModel() throws Exception {
    final String modelName = makeUniqueName("testUpdateCustomModel");
    final String desc = "This is test custom model desc";
    Pair<String, String> namespacePair = getTestNamespacePrefixPair();
    M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    model.setDescription(desc);
    model.setAuthor("John Doe");
    // Add aspect
    String aspectName = "testMarkerAspect";
    model.createAspect(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + aspectName);
    // Create the model
    CustomModelDefinition modelDefinition = createModel(model, false);
    assertNotNull(modelDefinition);
    assertEquals(modelName, modelDefinition.getName().getLocalName());
    assertFalse(modelDefinition.isActive());
    NamespaceDefinition namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    assertEquals(namespacePair.getFirst(), namespaceDefinition.getUri());
    assertEquals(namespacePair.getSecond(), namespaceDefinition.getPrefix());
    assertEquals(desc, modelDefinition.getDescription());
    assertEquals("John Doe", modelDefinition.getAuthor());
    assertEquals(1, modelDefinition.getAspectDefinitions().size());
    // Update the model by removing the namespace
    model.removeNamespace(namespacePair.getFirst());
    try {
        updateModel(modelName, model, false);
        fail("Shouldn't be able to update a custom model with an empty namespace.");
    } catch (InvalidNamespaceException ex) {
    // Expected
    }
    // Update the model by removing the namespace prefix
    model.createNamespace(namespacePair.getFirst(), null);
    try {
        updateModel(modelName, model, false);
        fail("Model validation should have failed, as the namespace prefix is null.");
    } catch (IllegalArgumentException ex) {
    // Expected
    }
    // Update the model by adding more namespace URIs
    model.createNamespace("http://www.alfresco.org/model/contenttest/1.0", namespacePair.getSecond());
    try {
        updateModel(modelName, model, false);
        fail("Shouldn't be able to add more than one namespace URI into a custom model.");
    } catch (InvalidNamespaceException ex) {
    // Expected
    }
    // Update the namespace with a URI that has already been used
    model.removeNamespace(namespacePair.getFirst());
    model.removeNamespace("http://www.alfresco.org/model/contenttest/1.0");
    model.createNamespace("http://www.alfresco.org/model/content/1.0", namespacePair.getSecond());
    try {
        updateModel(modelName, model, false);
        fail("Shouldn't be able to update a model with an already in-use namespace URI.");
    } catch (NamespaceConstraintException ex) {
    // Expected
    }
    // Update the namespace with a Prefix that has already been used
    model.removeNamespace("http://www.alfresco.org/model/content/1.0");
    model.createNamespace(namespacePair.getFirst(), "cm");
    try {
        updateModel(modelName, model, false);
        fail("Shouldn't be able to update a model with an already in-use namespace Prefix.");
    } catch (NamespaceConstraintException ex) {
    // Expected
    }
    // New namespace
    Pair<String, String> newNamespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(newNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(newNamespacePair.getFirst(), newNamespacePair.getSecond());
    model.setDescription(desc);
    // Update non-existing model
    try {
        updateModel(modelName + "non-existing model", model, false);
        fail("Should have thrown ModelDoesNotExistException.");
    } catch (ModelDoesNotExistException ex) {
    // Expected
    }
    modelDefinition = updateModel(modelName, model, false);
    namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    assertEquals(newNamespacePair.getFirst(), namespaceDefinition.getUri());
    assertEquals(newNamespacePair.getSecond(), namespaceDefinition.getPrefix());
    assertEquals(desc, modelDefinition.getDescription());
    assertNull(modelDefinition.getAuthor());
    assertEquals(0, modelDefinition.getAspectDefinitions().size());
    // Test that the cache is updated correctly. This means the cache should have removed the old namespace URI.
    QName aspectQName = QName.createQName("{" + namespacePair.getFirst() + "}" + aspectName);
    AspectDefinition aspectDefinition = getAspect(aspectQName);
    assertNull(aspectDefinition);
    transactionHelper.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Exception {
            // Activate the model
            customModelService.activateCustomModel(modelName);
            return null;
        }
    });
    // Retrieve the model
    modelDefinition = getModel(modelName);
    assertNotNull(modelDefinition);
    assertTrue(modelDefinition.isActive());
    // Try to update only the namespace URI of an active model
    Pair<String, String> activeModelNamespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(newNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(activeModelNamespacePair.getFirst(), newNamespacePair.getSecond());
    try {
        // true => as we activated the model
        updateModel(modelName, model, true);
        fail("Shouldn't be able to update the namespace URI of an active model.");
    } catch (ActiveModelConstraintException ax) {
    // Expected
    }
    // Try to update only the namespace prefix of an active model
    activeModelNamespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(activeModelNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(newNamespacePair.getFirst(), activeModelNamespacePair.getSecond());
    try {
        // true => as we activated the model
        updateModel(modelName, model, true);
        fail("Shouldn't be able to update the namespace prefix of an active model.");
    } catch (ActiveModelConstraintException ax) {
    // Expected
    }
    // Try to update both the namespace URI and prefix of an active model
    activeModelNamespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(activeModelNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(activeModelNamespacePair.getFirst(), activeModelNamespacePair.getSecond());
    try {
        // true => as we activated the model
        updateModel(modelName, model, true);
        fail("Shouldn't be able to update the namespace URI and namespace prefix of an active model.");
    } catch (ActiveModelConstraintException ax) {
    // Expected
    }
    // Update active model's desc and author
    modelDefinition = getModel(modelName);
    namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    model = M2Model.createModel(namespaceDefinition.getPrefix() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(namespaceDefinition.getUri(), namespaceDefinition.getPrefix());
    model.setDescription(desc);
    model.setAuthor("Admin Admin");
    modelDefinition = updateModel(modelName, model, true);
    assertEquals(modelName, modelDefinition.getName().getLocalName());
    assertTrue(modelDefinition.isActive());
    assertEquals(desc, modelDefinition.getDescription());
    assertEquals("Admin Admin", modelDefinition.getAuthor());
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) QName(org.alfresco.service.namespace.QName) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) 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) ActiveModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException) InvalidNamespaceException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidNamespaceException) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) NamespaceConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException) ModelDoesNotExistException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException) Test(org.junit.Test)

Aggregations

AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)1 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)1 ActiveModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException)1 InvalidNamespaceException (org.alfresco.service.cmr.dictionary.CustomModelException.InvalidNamespaceException)1 ModelDoesNotExistException (org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException)1 NamespaceConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException)1 NamespaceDefinition (org.alfresco.service.cmr.dictionary.NamespaceDefinition)1 QName (org.alfresco.service.namespace.QName)1 Test (org.junit.Test)1