Skip to content

Commit

Permalink
CAMEL-21452: decouple the infrastructure from the testing API
Browse files Browse the repository at this point in the history
  • Loading branch information
Croway committed Nov 19, 2024
1 parent 96a0981 commit 006e276
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 42 deletions.
6 changes: 6 additions & 0 deletions components/camel-arangodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-infra-arangodb</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
5 changes: 5 additions & 0 deletions test-infra/camel-test-infra-arangodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<name>Camel :: Test Infra :: ArangoDB</name>

<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-infra-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-infra-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
*/
package org.apache.camel.test.infra.arangodb.services;

import org.apache.camel.test.infra.common.services.TestService;
import org.apache.camel.test.infra.common.services.InfrastructureService;

/**
* Test infra service for ArangoDB
*/
public interface ArangoDBService extends TestService {
public interface ArangoDBService extends InfrastructureService {

int getPort();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.apache.camel.test.infra.common.services.SingletonService;

public final class ArangoDBServiceFactory {
private static class SingletonArangoDBService extends SingletonService<ArangoDBService> implements ArangoDBService {
public SingletonArangoDBService(ArangoDBService service, String name) {
private static class SingletonArangoDBService extends SingletonService<ArangoDBTestService> implements ArangoDBTestService {
public SingletonArangoDBService(ArangoDBTestService service, String name) {
super(service, name);
}

Expand All @@ -40,30 +40,36 @@ private ArangoDBServiceFactory() {

}

public static SimpleTestServiceBuilder<ArangoDBService> builder() {
public static SimpleTestServiceBuilder<ArangoDBTestService> builder() {
return new SimpleTestServiceBuilder<>("arangodb");
}

public static ArangoDBService createService() {
public static ArangoDBTestService createService() {
return builder()
.addLocalMapping(ArangoDBLocalContainerService::new)
.addRemoteMapping(ArangoDBRemoteService::new)
.addLocalMapping(ArangoDBLocalContainerTestService::new)
.addRemoteMapping(ArangoDBRemoteTestService::new)
.build();
}

public static ArangoDBService createSingletonService() {
public static ArangoDBTestService createSingletonService() {
return SingletonServiceHolder.INSTANCE;
}

private static class SingletonServiceHolder {
static final ArangoDBService INSTANCE;
static final ArangoDBTestService INSTANCE;
static {
SimpleTestServiceBuilder<ArangoDBService> instance = builder();
instance.addLocalMapping(() -> new SingletonArangoDBService(new ArangoDBLocalContainerService(), "arangoDB"))
.addRemoteMapping(ArangoDBRemoteService::new)
SimpleTestServiceBuilder<ArangoDBTestService> instance = builder();
instance.addLocalMapping(() -> new SingletonArangoDBService(new ArangoDBLocalContainerTestService(), "arangoDB"))
.addRemoteMapping(ArangoDBRemoteTestService::new)
.build();

INSTANCE = instance.build();
}
}
}

class ArangoDBLocalContainerTestService extends ArangoDBLocalContainerService implements ArangoDBTestService {
}

class ArangoDBRemoteTestService extends ArangoDBRemoteService implements ArangoDBTestService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.test.infra.arangodb.services;

import org.apache.camel.test.infra.common.services.TestService;

/**
* Test infra service for ArangoDB
*/
public interface ArangoDBTestService extends TestService, ArangoDBService {

int getPort();

String getHost();

default String getServiceAddress() {
return String.format("%s:%d", getHost(), getPort());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.camel.test.infra.common.services;

//import org.junit.jupiter.api.extension.ConditionEvaluationResult;
//import org.junit.jupiter.api.extension.ExecutionCondition;
//import org.junit.jupiter.api.extension.ExtensionContext;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;

public interface ContainerService<T extends GenericContainer> {

// @Override
// default ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
//
// if (ContainerEnvironmentUtil.isDockerAvailable()) {
// return ConditionEvaluationResult.enabled("Docker is available");
// }
//
// Logger logger = LoggerFactory.getLogger(ContainerService.class);
// logger.warn("Test {} is disabled because docker is not available", extensionContext.getElement().orElse(null));
//
// System.err.println(
// "Container-based tests were disabled because Docker is NOT available. Check the log files on target/failsafe-reports");
// return ConditionEvaluationResult.disabled("Docker is NOT available");
// }

T getContainer();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.test.infra.common.services;

public interface InfrastructureService extends AutoCloseable {

/**
* Register service properties (such as using System.setProperties) so that they can be resolved at distance (ie.:
* when using Spring's PropertySourcesPlaceholderConfigurer or simply when trying to collect test infra information
* outside of the test class itself).
*/
void registerProperties();

/**
* Perform any initialization necessary
*/
void initialize();

/**
* Shuts down the service after the test has completed
*/
void shutdown();

@Override
default void close() {
shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleTestServiceBuilder<T extends TestService> implements TestServiceBuilder<T> {
public class SimpleTestServiceBuilder<T extends InfrastructureService> implements TestServiceBuilder<T> {
private static final Logger LOG = LoggerFactory.getLogger(SimpleTestServiceBuilder.class);

private final Map<String, Supplier<T>> mappings = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
*
* @param <T> The type of the service to be wrapped
*/
public class SingletonService<T extends TestService> implements ExtensionContext.Store.CloseableResource, TestService {
public class SingletonService<T extends InfrastructureService>
implements ExtensionContext.Store.CloseableResource, TestService {
private static final Logger LOG = LoggerFactory.getLogger(SingletonService.class);

private final T service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,7 @@
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public interface TestService extends AutoCloseable, BeforeAllCallback, AfterAllCallback {

/**
* Register service properties (such as using System.setProperties) so that they can be resolved at distance (ie.:
* when using Spring's PropertySourcesPlaceholderConfigurer or simply when trying to collect test infra information
* outside of the test class itself).
*/
void registerProperties();

/**
* Perform any initialization necessary
*/
void initialize();

/**
* Shuts down the service after the test has completed
*/
void shutdown();

@Override
default void close() {
shutdown();
}
public interface TestService extends AutoCloseable, BeforeAllCallback, AfterAllCallback, InfrastructureService {

@Override
default void beforeAll(ExtensionContext extensionContext) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.camel.test.infra.common.services;

public interface TestServiceBuilder<T extends TestService> {
public interface TestServiceBuilder<T extends InfrastructureService> {

T build();
}

0 comments on commit 006e276

Please sign in to comment.