Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use a custom application.properties per test scenario #281

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ public class PingPongResourceIT {

As seen in the above example, everything is bounded to a Service object that will contain everything needed to interact with our resources.

### Application properties

By default, the test framework will use the `application.properties` file at `src/main/resources` folder. The service interface provides multiple methods to add properties at test scope only:
- `service.withProperties(path)`
- `service.withProperty(key, value)`

If you want to use a different application properties file for all the tests, you can add the `application.properties` file at `src/test/resources` and the test framework will use this instead.

Moreover, if you want to select a concrete application properties file for a single test scenario, then you can configure your Quarkus application using:

```java
@QuarkusScenario
public class PingPongResourceIT {

// Now, the application will use the file `my-custom-properties.properties` instead of the `application.properties`
@QuarkusApplication(properties = "my-custom-properties.properties")
static final RestService pingpong = new RestService();
}
```

This option is available also for Dev Mode, Remote Dev mode and remote git applications, and works for JVM, Native, OpenShift and Kubernetes.

| Note that the test framework does not support the usage of YAML files yet [#240](https://github.com/quarkus-qe/quarkus-test-framework/issues/240)

### Forced Dependencies

We can also specify dependencies that are not part of the pom.xml by doing:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.qe;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;

@QuarkusScenario
public class ComputedValuesUsingCustomPropertiesPingPongResourceIT {

@QuarkusApplication(properties = "custom.properties")
static final RestService pingpong = new RestService();

@Test
public void shouldGetComputedValuesFromCustomPropertiesFile() {
assertEquals("C", pingpong.getProperty("property.exists.only.in.custom.properties").get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.qe;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.DevModeQuarkusApplication;

@QuarkusScenario
public class DevModeComputedValuesUsingCustomPropertiesPingPongResourceIT {

@DevModeQuarkusApplication(properties = "custom.properties")
static final RestService pingpong = new RestService();

@Test
public void shouldGetComputedValuesFromCustomPropertiesFile() {
assertEquals("C", pingpong.getProperty("property.exists.only.in.custom.properties").get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.qe;

import io.quarkus.test.scenarios.OpenShiftDeploymentStrategy;
import io.quarkus.test.scenarios.OpenShiftScenario;

@OpenShiftScenario(deployment = OpenShiftDeploymentStrategy.UsingOpenShiftExtension)
public class OpenShiftUsingExtensionComputedValuesUsingCustomPropertiesPingPongResourceIT
extends ComputedValuesUsingCustomPropertiesPingPongResourceIT {
}
1 change: 1 addition & 0 deletions examples/pingpong/src/test/resources/custom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
property.exists.only.in.custom.properties=C
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
// By default, it will load all the classes in the classpath.
Class<?>[] classes() default {};

/**
* @return the properties file to use to configure the Quarkus application.
*/
String properties() default "application.properties";

/**
* Enable GRPC configuration. This property will map the gPRC service to a random port.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@
String mavenArgs() default "-DskipTests=true -DskipITs=true -Dquarkus.platform.version=${QUARKUS_VERSION}";

boolean devMode() default false;

/**
* @return the properties file to use to configure the Quarkus application.
*/
String properties() default "application.properties";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

Class<? extends ManagedResourceBuilder> builder() default ProdQuarkusApplicationManagedResourceBuilder.class;

/**
* @return the properties file to use to configure the Quarkus application.
*/
String properties() default "application.properties";

/**
* Add forced dependencies.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface RemoteDevModeQuarkusApplication {
String password() default "qe";

/**
* @return the properties file to use to configure the Quarkus application.
*/
String properties() default "application.properties";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DevModeQuarkusApplicationManagedResourceBuilder extends QuarkusAppl
public void init(Annotation annotation) {
DevModeQuarkusApplication metadata = (DevModeQuarkusApplication) annotation;
initAppClasses(metadata.classes());
setPropertiesFile(metadata.properties());
setGrpcEnabled(metadata.grpc());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void init(Annotation annotation) {
mavenArgs = metadata.mavenArgs();
devMode = metadata.devMode();
initAppClasses(new Class[0]);
setPropertiesFile(metadata.properties());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected Path getArtifact() {
@Override
public void init(Annotation annotation) {
QuarkusApplication metadata = (QuarkusApplication) annotation;
setPropertiesFile(metadata.properties());
setSslEnabled(metadata.ssl());
setGrpcEnabled(metadata.grpc());
initAppClasses(metadata.classes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public abstract class QuarkusApplicationManagedResourceBuilder implements Manage
private List<AppDependency> forcedDependencies = Collections.emptyList();
private boolean requiresCustomBuild = false;
private ServiceContext context;
private String propertiesFile = APPLICATION_PROPERTIES;
private boolean sslEnabled = false;
private boolean grpcEnabled = false;
private Map<String, String> propertiesSnapshot;
Expand All @@ -67,6 +68,10 @@ protected void setContext(ServiceContext context) {
this.context = context;
}

protected void setPropertiesFile(String propertiesFile) {
this.propertiesFile = propertiesFile;
}

protected boolean isSslEnabled() {
return sslEnabled;
}
Expand Down Expand Up @@ -100,7 +105,7 @@ public String getComputedProperty(String name) {
Path applicationProperties = getComputedApplicationProperties();
if (!Files.exists(applicationProperties)) {
// computed properties have not been propagated yet, we use the one from src/main/resources
applicationProperties = RESOURCES_FOLDER.resolve(APPLICATION_PROPERTIES);
applicationProperties = RESOURCES_FOLDER.resolve(propertiesFile);
}

if (!Files.exists(applicationProperties)) {
Expand Down Expand Up @@ -179,22 +184,23 @@ protected Path getResourcesApplicationFolder() {
return getApplicationFolder();
}

private Path getComputedApplicationProperties() {
protected Path getComputedApplicationProperties() {
return getResourcesApplicationFolder().resolve(APPLICATION_PROPERTIES);
}

private void createComputedApplicationProperties() {
Path applicationProperties = getComputedApplicationProperties();
Path sourceApplicationProperties = getResourcesApplicationFolder().resolve(propertiesFile);
Path generatedApplicationProperties = getResourcesApplicationFolder().resolve(APPLICATION_PROPERTIES);
Map<String, String> map = new HashMap<>();
// Put the original application properties
if (Files.exists(applicationProperties)) {
map.putAll(PropertiesUtils.toMap(applicationProperties));
// Add the content of the source application properties into the auto-generated application.properties
if (Files.exists(sourceApplicationProperties)) {
map.putAll(PropertiesUtils.toMap(sourceApplicationProperties));
}

// Then put the build properties
// Then add the service properties
map.putAll(context.getOwner().getProperties());
// Then replace the application properties
PropertiesUtils.fromMap(map, applicationProperties);
// Then overwrite the application properties with the generated application.properties
PropertiesUtils.fromMap(map, generatedApplicationProperties);
}

private boolean isBuildProperty(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class RemoteDevModeQuarkusApplicationManagedResourceBuilder extends Artif
public void init(Annotation annotation) {
RemoteDevModeQuarkusApplication metadata = (RemoteDevModeQuarkusApplication) annotation;
liveReloadPassword = metadata.password();
setPropertiesFile(metadata.properties());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.test.utils;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static String loadFile(String file) {
fail("Could not load file " + file + " . Caused by " + e.getMessage());
}

return null;
return EMPTY;
}

public static void recreateDirectory(Path folder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

Expand Down Expand Up @@ -53,7 +54,7 @@ public static Map<String, String> toMap(String propertiesFile) {
fail("Could not load map from system resource. Caused by " + e);
}

return null;
return Collections.emptyMap();
}

public static Map<String, String> toMap(Path path) {
Expand All @@ -63,7 +64,7 @@ public static Map<String, String> toMap(Path path) {
fail("Could not load map from path. Caused by " + e);
}

return null;
return Collections.emptyMap();
}

public static Map<String, String> toMap(InputStream is) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public class ExtensionOpenShiftQuarkusApplicationManagedResource
private static final String QUARKUS_KUBERNETES_DEPLOYMENT_TARGET = "quarkus.kubernetes.deployment-target";
private static final String KNATIVE = "knative";

private static final String APPLICATION_PROPERTIES_PATH = "src/main/resources/application.properties";

public ExtensionOpenShiftQuarkusApplicationManagedResource(ProdQuarkusApplicationManagedResourceBuilder model) {
super(model);
}
Expand Down Expand Up @@ -84,7 +82,7 @@ private void copyBuildPropertiesIntoAppFolder() {
return;
}

Path applicationPropertiesPath = model.getContext().getServiceFolder().resolve(APPLICATION_PROPERTIES_PATH);
Path applicationPropertiesPath = model.getComputedApplicationProperties();
if (Files.exists(applicationPropertiesPath)) {
buildProperties.putAll(PropertiesUtils.toMap(applicationPropertiesPath));
}
Expand Down