How to use Testcontainers with Alfresco Out-of-Process Extensions

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use Testcontainers with Alfresco Out-of-Process Extensions

angelborroy
Alfresco Employee
1 0 1,718

Testcontainers is a powerful tool that can significantly enhance your testing workflow. In this blog post, we'll explore how the testcontainers-alfresco-oop project leverages Testcontainers to streamline the development and testing of Alfresco Out-of-Process (OOP) extensions.

Alfresco Containers in Testcontainers

With the introduction of the org.alfresco.alfresco-testcontainers artifact in Maven Central, using Testcontainers for unit testing has become significantly easier.

Import the dependency in your pom.xml file.

<dependency>
    <groupId>org.alfresco</groupId>
    <artifactId>alfresco-testcontainers</artifactId>
    <version>0.8.1</version>
    <scope>test</scope>
</dependency>

Start the container to make Alfresco available for your testing methods.

AlfrescoContainer<?> alfrescoContainer = 
  new AlfrescoContainer<>("23.2.1").withMessagingEnabled();

And that’s it—you’re all set to start developing your test code!

The source code for this artifact is available at https://github.com/AlfrescoLabs/alfresco-testcontainers. We welcome issues and pull requests!

What is testcontainers-alfresco-oop?

The testcontainers-alfresco-oop project is a Spring Boot application designed to simplify the development and testing of Alfresco OoP extensions. It leverages the Testcontainers framework to provide a robust testing environment for Alfresco Community, ensuring that your extensions perform reliably in real-world scenarios.

Why Use Testcontainers for Alfresco?

1. Isolated and Reliable Testing

Testcontainers provides isolated, disposable containers for running tests. This isolation ensures that each test runs in a clean environment, free from interference by other tests or external factors. For Alfresco integrations, this means you can test your OoP extensions against a fresh instance of Alfresco, simulating real-world conditions without worrying about lingering side effects from previous tests.

2. Easy Setup and Teardown

With Testcontainers, setting up and tearing down test environments becomes a breeze. You can spin up a new Alfresco instance with ActiveMQ enabled, run your tests, and then clean up the environment—all with minimal configuration. This automation eliminates the need for manual setup and teardown, reducing the risk of human error and improving test reliability.

3. Consistent Test Environments

Testcontainers ensures that your tests run against a consistent environment every time. This consistency is vital for reliable integration testing, especially when dealing with complex systems like Alfresco. By using a Docker container for Alfresco, you ensure that every test runs against the same version and configuration of Alfresco, minimizing discrepancies and unexpected issues.

Key Features of testcontainers-alfresco-oop

1. Integration with Alfresco Community ActiveMQ

The project includes a Spring Boot application that listens to Alfresco Community ActiveMQ to detect the creation of HTML files. It uses this integration to validate the functionality of your OoP extensions in a realistic setting. By connecting to ActiveMQ, the application ensures that events are captured and processed correctly.

2. Custom Testing Strategy

The project comes with a well-defined testing strategy using Testcontainers. Here’s how you can use it:

a. Start the Alfresco Container: Initialize and start the Alfresco container with ActiveMQ enabled before running your tests.

@BeforeAll
static void setUp() {
    alfrescoContainer = 
        new AlfrescoContainer<>("23.2.1").withMessagingEnabled();
    alfrescoContainer.start();
    activemqContainer = alfrescoContainer.getActivemqContainer();
}

b. Configure ActiveMQ Properties: Dynamically set the properties for connecting to ActiveMQ within your test configuration.

@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.activemq.brokerUrl", () ->
        "tcp://" + activemqContainer.getHost() + ":" + activemqContainer.getMappedPort(61616));
}

c. Set Alfresco Container URL: Configure your REST client to connect to the Alfresco container’s URL.

@BeforeEach
void setUpEach() {
    restClient.setAlfrescoUrl("http", alfrescoContainer.getHost(), alfrescoContainer.getMappedPort(8080), "alfresco");
}

d. Clean Up After Tests: Stop the Alfresco container after all tests have completed to ensure no resources are left hanging.

@AfterAll
static void tearDownAll() {
    alfrescoContainer.stop();
}

3. Documentation and Examples

The project provides detailed documentation and example configurations, making it easy to get started. The README file includes instructions on setting up the environment, running tests, and understanding the various components of the project.

Getting Started

To get started with testcontainers-alfresco-oop, follow these steps:

1. Clone the Repository

git clone https://github.com/aborroy/testcontainers-alfresco-oop.git
cd testcontainers-alfresco-oop

2. Build the Project

mvn clean install

3. Run the Tests

Execute your tests using Maven:

mvn test

Conclusion

The testcontainers-alfresco-oop project demonstrates the power of combining Testcontainers with Alfresco for effective out-of-process extension development and testing. By providing isolated, consistent, and automated testing environments, this project helps ensure that your Alfresco integrations perform as expected, even in complex scenarios.

Embrace the power of Testcontainers and elevate your Alfresco development and testing workflow today!

About the Author
Angel Borroy is Hyland Developer Evangelist. Over the last 15 years, he has been working as a software architect on Java, BPM, document management and electronic signatures. He has been working with Alfresco during the last years to customize several implementations in large organizations and to provide add-ons to the Community based on Record Management and Electronic Signature. He writes (sometimes) on his personal blog http://angelborroy.wordpress.com. He is (proud) member of the Order of the Bee.