Search in sources :

Example 1 with NamespaceConstraintException

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

the class CustomModelServiceImplTest method testCreateBasicInactiveModel.

@Test
public void testCreateBasicInactiveModel() throws Exception {
    final String modelName1 = makeUniqueName("testCustomModel1");
    final String desc = "This is test custom model desc";
    Pair<String, String> namespacePair = getTestNamespacePrefixPair();
    M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName1);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    model.setDescription(desc);
    model.setAuthor("John Doe");
    // Create the 1st model
    CustomModelDefinition modelDefinition = createModel(model, false);
    assertNotNull(modelDefinition);
    assertEquals(modelName1, modelDefinition.getName().getLocalName());
    assertTrue("There is no imported namespace.", modelDefinition.getImportedNamespaces().isEmpty());
    NamespaceDefinition namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    assertNotNull(namespaceDefinition);
    assertEquals(namespacePair.getFirst(), namespaceDefinition.getUri());
    assertEquals(namespacePair.getSecond(), namespaceDefinition.getPrefix());
    assertEquals(desc, modelDefinition.getDescription());
    assertEquals("John Doe", modelDefinition.getAuthor());
    final String modelName2 = makeUniqueName("testCustomModel2");
    model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName2);
    model.createNamespace(namespacePair.getFirst(), "newTestPrefix");
    // Create the 2nd model - duplicate namespace URI
    try {
        createModel(model, false);
        fail("Shouldn't be able to create a model with an already in-use namespace uri.");
    } catch (NamespaceConstraintException ex) {
    // Expected
    }
    final String modelName3 = makeUniqueName("testCustomModel3");
    model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName3);
    model.createNamespace(getTestNamespacePrefixPair().getFirst(), namespacePair.getSecond());
    // Create the 2nd model - duplicate namespace Prefix
    try {
        createModel(model, false);
        fail("Shouldn't be able to create a model with an already in-use namespace prefix.");
    } catch (NamespaceConstraintException ex) {
    // Expected
    }
    namespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName2);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    modelDefinition = createModel(model, false);
    assertNotNull(modelDefinition);
    assertEquals(modelName2, modelDefinition.getName().getLocalName());
    namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    assertNotNull(namespaceDefinition);
    assertEquals(namespacePair.getFirst(), namespaceDefinition.getUri());
    assertEquals(namespacePair.getSecond(), namespaceDefinition.getPrefix());
    try {
        // Test duplicate model
        createModel(model, false);
        fail("Shouldn't be able to create a duplicate model.");
    } catch (Exception e) {
    // Expected
    }
    try {
        // Test creating a model with the same name as the bootstrapped model
        model.setName("contentmodel");
        createModel(model, false);
        fail("Shouldn't be able to create a model with the same name as the bootstrapped model.");
    } catch (Exception e) {
    // Expected
    }
    // Test list all models
    try {
        customModelService.getCustomModels(null);
        fail("Should have thrown IllegalArgumentException as PagingRequest was null.");
    } catch (IllegalArgumentException e) {
    // Expected
    }
    final PagingRequest pagingRequest = new PagingRequest(0, Integer.MAX_VALUE);
    PagingResults<CustomModelDefinition> result = transactionHelper.doInTransaction(new RetryingTransactionCallback<PagingResults<CustomModelDefinition>>() {

        public PagingResults<CustomModelDefinition> execute() throws Exception {
            return customModelService.getCustomModels(pagingRequest);
        }
    });
    assertTrue(result.getTotalResultCount().getFirst() >= 2);
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) PagingResults(org.alfresco.query.PagingResults) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) NamespaceConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException) 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) PagingRequest(org.alfresco.query.PagingRequest) Test(org.junit.Test)

Example 2 with NamespaceConstraintException

use of org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException 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

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