From 1407aaa5426119371210e6ee20fae3aca8915e4d Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sun, 17 Nov 2024 11:41:10 +0100 Subject: [PATCH] Use WireMock in integration test --- pom.xml | 39 +------- src/build/scripts/LittleProxyStart.groovy | 93 ------------------- .../invoker.properties | 3 + .../download-licenses-proxy/postbuild.groovy | 11 ++- .../download-licenses-proxy/prebuild.groovy | 47 ++++++++++ 5 files changed, 63 insertions(+), 130 deletions(-) delete mode 100644 src/build/scripts/LittleProxyStart.groovy create mode 100644 src/it/download-licenses-proxy/prebuild.groovy diff --git a/pom.xml b/pom.xml index 37cbc3b94..1be8439c8 100644 --- a/pom.xml +++ b/pom.xml @@ -435,40 +435,6 @@ - - - org.codehaus.gmaven - groovy-maven-plugin - - - org.littleshoot - littleproxy - 1.1.2 - - - - - LittleProxyStart - - execute - - pre-integration-test - - ${project.basedir}/src/build/scripts/LittleProxyStart.groovy - - - - LittleProxyStop - - execute - - post-integration-test - - System.setProperty('LittleProxy.stop', 'true'); Thread.sleep(250) - - - - maven-invoker-plugin @@ -501,6 +467,11 @@ commons-io 2.17.0 + + org.wiremock + wiremock-standalone + 3.9.2 + diff --git a/src/build/scripts/LittleProxyStart.groovy b/src/build/scripts/LittleProxyStart.groovy deleted file mode 100644 index 233c5ca22..000000000 --- a/src/build/scripts/LittleProxyStart.groovy +++ /dev/null @@ -1,93 +0,0 @@ - -/* - * A script to start LittleProxy on port 18080. - * Do not forget to kill it by System.setProperty('LittleProxy.stop', 'true') in the same JVM - */ - -import org.littleshoot.proxy.HttpFilters; -import org.littleshoot.proxy.HttpFiltersSourceAdapter; -import org.littleshoot.proxy.HttpProxyServer; -import org.littleshoot.proxy.impl.DefaultHttpProxyServer; -import org.littleshoot.proxy.HttpFiltersAdapter; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.http.HttpMessage; -import io.netty.handler.codec.http.HttpObject; -import io.netty.handler.codec.http.HttpRequest; -import io.netty.handler.codec.http.HttpResponse; -import io.netty.buffer.Unpooled; -import io.netty.buffer.ByteBuf; -import io.netty.handler.codec.http.HttpVersion; -import io.netty.handler.codec.http.HttpHeaders; -import io.netty.handler.codec.http.DefaultFullHttpResponse; - - -class KillerThread extends Thread { - private final long timeoutMs = 20 * 1000 * 60 - private final long startTime = System.currentTimeMillis() - private final HttpProxyServer server; - public KillerThread( HttpProxyServer server ) - { - super(); - this.server = server; - } - @Override - public void run() - { - while (true) { - if (System.getProperty("LittleProxy.stop") != null) { - println "LittleProxy stopped via LittleProxy.stop system property" - break - } - if (server.stopped.get()) { - println "LittleProxy stopped by itself" - break - } - if (System.currentTimeMillis() > startTime + timeoutMs) { - println "LittleProxy timeouted" - break - } - try - { - Thread.sleep( 100 ); - //println "LittleProxy is running" - } - catch ( InterruptedException e ) - { - Thread.currentThread().interrupt(); - } - } - server.stop(); - } -} - -final HttpProxyServer server = - DefaultHttpProxyServer.bootstrap() - .withPort(18080) - .withFiltersSource(new HttpFiltersSourceAdapter() { - @Override - public int getMaximumResponseBufferSizeInBytes() { - return 2 * 1024 * 1024; - } - public HttpFilters filterRequest(HttpRequest req, ChannelHandlerContext ctx) { - if (io.netty.handler.codec.http.HttpMethod.GET == req.getMethod() - && 'http://www.apache.org/licenses/LICENSE-2.0.txt'.equals(req.getUri())) { - return new HttpFiltersAdapter(req) { - @Override - public HttpResponse clientToProxyRequest(HttpObject httpObject) { - return super.clientToProxyRequest(httpObject) - } - - @Override - public HttpObject serverToProxyResponse(HttpObject httpObject) { - println "LittleProxy overwrites the response body for URL 'http://www.apache.org/licenses/LICENSE-2.0.txt'" - final ByteBuf buffer = Unpooled.copiedBuffer('Proxied via LittleProxy', java.nio.charset.StandardCharsets.UTF_8); - final HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpResponseStatus.OK, buffer); - response.trailingHeaders().add(httpObject.headers()) - return response; - } - }; - } - } - }) - .start(); -new KillerThread(server).start(); diff --git a/src/it/download-licenses-proxy/invoker.properties b/src/it/download-licenses-proxy/invoker.properties index faaaacd97..15ddce0a4 100644 --- a/src/it/download-licenses-proxy/invoker.properties +++ b/src/it/download-licenses-proxy/invoker.properties @@ -3,3 +3,6 @@ invoker.settingsFile = src/it/settings-proxy.xml # execute as last test, all needed artifact should be resolved before this test invoker.ordinal = -100 + +# wiremock requires 11+ +invoker.java.version = 11+ \ No newline at end of file diff --git a/src/it/download-licenses-proxy/postbuild.groovy b/src/it/download-licenses-proxy/postbuild.groovy index bd84bdfb1..eb4a103ac 100644 --- a/src/it/download-licenses-proxy/postbuild.groovy +++ b/src/it/download-licenses-proxy/postbuild.groovy @@ -20,15 +20,20 @@ * #L% */ -import java.nio.file.Path; -import java.nio.file.Files; +import com.github.tomakehurst.wiremock.WireMockServer + +import java.nio.file.Files +import java.nio.file.Path final Path basePath = basedir.toPath() final Path asl2 = basePath.resolve('target/generated-resources/licenses/apache license 2.0 - license-2.0.txt') assert Files.exists(asl2) -assert asl2.toFile().text.contains('Proxied via LittleProxy') +assert asl2.toFile().text.contains('Proxied via WireMock') final Path expectedLicensesXml = basePath.resolve('licenses.expected.xml') final Path licensesXml = basePath.resolve('target/generated-resources/licenses.xml') assert expectedLicensesXml.toFile().text.equals(licensesXml.toFile().text) + +WireMockServer wireMockServer = context.get("wireMockServer") +wireMockServer.stop() diff --git a/src/it/download-licenses-proxy/prebuild.groovy b/src/it/download-licenses-proxy/prebuild.groovy new file mode 100644 index 000000000..c70bcd4f5 --- /dev/null +++ b/src/it/download-licenses-proxy/prebuild.groovy @@ -0,0 +1,47 @@ +import com.github.tomakehurst.wiremock.client.WireMock + +/* + * #%L + * License Maven Plugin + * %% + * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit, Tony chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +import com.github.tomakehurst.wiremock.WireMockServer +import com.github.tomakehurst.wiremock.core.WireMockConfiguration +import com.github.tomakehurst.wiremock.common.ConsoleNotifier + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo +import static com.github.tomakehurst.wiremock.client.WireMock.get +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor + +WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options() + .port(18080) + .notifier(new ConsoleNotifier(false))) +wireMockServer.start() + +configureFor(wireMockServer.port()) + +stubFor(get('/licenses/LICENSE-2.0.txt') + .withHeader('Host', equalTo('www.apache.org')) + .willReturn(aResponse().withStatus(200).withBody('Proxied via WireMock'))) + +context.put("wireMockServer", wireMockServer) +true