-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1119 from DependencyTrack/feature/removeMockServer
removed usage of mockserver
- Loading branch information
Showing
15 changed files
with
619 additions
and
888 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
commons/src/test/java/org/dependencytrack/util/WireMockTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.dependencytrack.util; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.Map; | ||
|
||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; | ||
|
||
/** | ||
* A Quarkus test resource that provisions a WireMock server on a random open port. | ||
* <p> | ||
* Note that the same {@link WireMockServer} instance will be used for all tests in the | ||
* annotated test class. Stubs will need to manually be reset after each test using {@link WireMockServer#resetAll()}. | ||
* | ||
* @see <a href="https://quarkus.io/guides/getting-started-testing#altering-the-test-class">Quarkus Documentation</a> | ||
*/ | ||
public class WireMockTestResource implements QuarkusTestResourceLifecycleManager { | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface InjectWireMock { | ||
} | ||
|
||
private WireMockServer wireMockServer; | ||
private String serverUrlProperty; | ||
|
||
@Override | ||
public void init(final Map<String, String> initArgs) { | ||
serverUrlProperty = initArgs.get("serverUrlProperty"); | ||
} | ||
|
||
@Override | ||
public Map<String, String> start() { | ||
wireMockServer = new WireMockServer(options().dynamicPort()); | ||
wireMockServer.start(); | ||
|
||
if (serverUrlProperty == null) { | ||
return null; | ||
} | ||
|
||
return Map.of(serverUrlProperty, wireMockServer.baseUrl()); | ||
} | ||
|
||
@Override | ||
public synchronized void stop() { | ||
if (wireMockServer != null) { | ||
wireMockServer.stop(); | ||
wireMockServer = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void inject(final TestInjector testInjector) { | ||
testInjector.injectIntoFields(wireMockServer, | ||
new TestInjector.AnnotatedAndMatchesType(InjectWireMock.class, WireMockServer.class)); | ||
} | ||
|
||
} |
Oops, something went wrong.