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

Add config option that enables Service Loading #111

Merged
merged 1 commit into from
Apr 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ public interface WireMockServerBuildTimeConfig {
*/
@WithDefault("false")
boolean globalResponseTemplating();

/**
* Control whether WireMock Extension <a href=
* "https://wiremock.org/docs/extending-wiremock/#extension-registration-via-service-loading">service
* loading</a>,
* is enabled
*/
@WithDefault("false")
boolean extensionScanningEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ void watchWireMockConfigFiles(WireMockServerBuildTimeConfig config,
private static RunningDevService startWireMockDevService(WireMockServerBuildTimeConfig config) {

final WireMockConfiguration configuration = options().usingFilesUnderDirectory(config.filesMapping())
.globalTemplating(config.globalResponseTemplating());
.globalTemplating(config.globalResponseTemplating())
.extensionScanningEnabled(config.extensionScanningEnabled());
config.port().ifPresentOrElse(configuration::port, configuration::dynamicPort);

final WireMockServer server = new WireMockServer(configuration);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkiverse.wiremock.devservice;

import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformerV2;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;

public class CustomTransformer implements ResponseDefinitionTransformerV2 {
@Override
public ResponseDefinition transform(ServeEvent serveEvent) {
ResponseDefinition responseDefinition = serveEvent.getResponseDefinition();
responseDefinition.getTransformerParameters().put("custom", "good");
return responseDefinition;
}

@Override
public String getName() {
return "custom-transformer";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

package io.quarkiverse.wiremock.devservice;

import static io.quarkiverse.wiremock.devservice.WireMockConfigKey.PORT;
import static org.hamcrest.Matchers.is;
import static org.jboss.resteasy.reactive.RestResponse.StatusCode.OK;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class WiremockExtensionServiceLoadingDisabled {

private static final String APP_PROPERTIES = "application-extension-loading-disabled.properties";
chberger marked this conversation as resolved.
Show resolved Hide resolved

@RegisterExtension
static final QuarkusUnitTest UNIT_TEST = new QuarkusUnitTest().withConfigurationResource(APP_PROPERTIES)
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(CustomTransformer.class)
.addAsResource("META-INF/services/com.github.tomakehurst.wiremock.extension.Extension"));

@Test
void testExtensionScanningEnabled() {
final int port = ConfigProvider.getConfig().getValue(PORT, Integer.class);
RestAssured.when().get(String.format("http://localhost:%d/custom-variable", port)).then().statusCode(OK)
.body(is("Response is test!"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkiverse.wiremock.devservice;

import static io.quarkiverse.wiremock.devservice.WireMockConfigKey.PORT;
import static org.hamcrest.Matchers.is;
import static org.jboss.resteasy.reactive.RestResponse.StatusCode.OK;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class WiremockExtensionServiceLoadingEnabled {

private static final String APP_PROPERTIES = "application-extension-loading-enabled.properties";

@RegisterExtension
static final QuarkusUnitTest UNIT_TEST = new QuarkusUnitTest().withConfigurationResource(APP_PROPERTIES)
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(CustomTransformer.class)
.addAsResource("META-INF/services/com.github.tomakehurst.wiremock.extension.Extension"));

@Test
void testExtensionScanningEnabled() {
final int port = ConfigProvider.getConfig().getValue(PORT, Integer.class);
RestAssured.when().get(String.format("http://localhost:%d/custom-variable", port)).then().statusCode(OK)
.body(is("Response is good!"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkiverse.wiremock.devservice.CustomTransformer
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.wiremock.devservices.global-response-templating=true
quarkus.wiremock.devservices.extension-scanning-disabled=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.wiremock.devservices.global-response-templating=true
quarkus.wiremock.devservices.extension-scanning-enabled=true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be so kind to include the new config parameter in the documentation as well - thank you very much
docs/modules/ROOT/pages/includes/quarkus-wiremock.adoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this meant to be updated automatically when building the project? Any idea why it wasn't updated?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'm aware that this works when contributing to the platform. That's why the config comments are mandatory, right? However, I haven't thought about how this should work for extensions. I've just maintained them manually. Let me check ... 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be mistaken, but I am pretty sure it work automatically. @gastaldi can you confirm?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this needs to be changed to match the config document: https://github.com/quarkiverse/quarkus-wiremock/blob/main/docs/pom.xml#L68

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gastaldi Ah damn, thanks! I'll change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gastaldi!

14 changes: 14 additions & 0 deletions deployment/src/test/resources/mappings/custom-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"request": {
"method": "GET",
"url": "/custom-variable"
},
"response": {
"status": 200,
"body": "Response is {{ parameters.custom }}!",
"transformers": ["custom-transformer"],
"transformerParameters": {
"custom": "test"
}
}
}