Sharing Transactions Between Virtual Services and Wiremock

You can add stubs from external mocking tools to use those definitions as virtual services in BlazeMeter.

The following external tools are supported:

You can integrate Transactions between virtual services and Wiremock in the following ways:

Prerequisites for In-Code Sharing Between Virtual Services and Wiremock

To interact with the Transaction repository directly from your Wiremock code, ensure that you meet the following prerequisites.

These prerequisites are not required if you are simply uploading JSON files exported from Wiremock.

Wiremock Prerequisites

The Wiremock extension library is required to connect with BlazeMeter virtual services:

Maven:

<repositories>
  <!-- your other repositories -->
  <repository>
    <id>blazemeter</id>
    <name>blazemeter</name>
    <url>http://blazemeter.jfrog.io/artifactory/blazemeter-public/</url>
  </repository>
</repositories>
<dependency>
  <groupId>com.perforce.blazemeter-wiremock-extension</groupId>
  <artifactId>blazemeter-wiremock-extension</artifactId>
  <version>1.0.3</version>
</dependency>

Gradle:

repositories {
  maven {
    url "http://blazemeter.jfrog.io/artifactory/blazemeter-public/"
  }
}
dependencies {
  compile 'com.perforce.blazemeter-wiremock-extension:blazemeter-wiremock-extension:1.0.3'
}

Only version 1.0.3 of BlazeMeter-WireMock extension supports ability to upload transactions from code to BlazeMeter Asset Catalog and to download transactions from BlazeMeter Asset Catalog to code.

Exporting Transactions to a JSON file

Export virtual endpoint definitions from your code into a JSON transaction file, which you can then import into the Transaction Repository through the Service Virtualization UI. Using this method, developers using mocking tools can share the artifacts they created in their code with other team members who can load them into the Service Virtualization UI, augment them as needed, and run them as virtual services hosting the content imported from the code.

Wiremock Code Samples:

public class WiremockExportExampleTest {
	@Rule
	public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8081));
	WiremockTxnRepoStore store = new WiremockTxnRepoStore.StoreBuilder()
			.withTestInstance(this).withWireMockServerOrRule(wireMockRule)
			.exportAsDslFile("/tmp/dsl")
			.build();
	@Test
	public void wiremockExportExampleTest() {
		stubFor(get(urlEqualTo("/cities"))
				.willReturn(aResponse()
				.withStatus(200).withBody("...cities...")));
		
		/* rest of the test */			
	}
}

The vanilla Wiremock library lets you export a JSON DSL file using the WireMock.saveAllMappings(); method. Any file exported using this approach could be imported to Service Virtualization.

After you export the definitions into JSON, you can add them as Transactions from a file to the Transaction Repository.

Upload Transactions to Transaction Repository from Code

You can directly upload in-code definitions of virtual endpoints to the Transaction Repository. Once the test with these definitions runs, they are converted from their in-code form into Transaction format and uploaded to the repository.

This is another way that developers using Wiremock can share the artifacts they created in code with other team members, who can use and augment them in the UI as virtual services.

For more information about the apiKey and apiSecret variables, see BlazeMeter API Keys.

Wiremock Code Samples:

@TransactionCloudRepository(
		workspaces = {"Default Workspace"},
		apiKey = "...",
		apiSecret = "...",
		uri = "https://mock.blazemeter.com"
)
public class WiremockUploadExampleTest {
	@Rule
	public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8081));
	private WiremockTxnRepoStore store = new WiremockTxnRepoStore.StoreBuilder()
			.withTestInstance(this).withWireMockServerOrRule(wireMockRule)
			.uploadDslToTransactionRepository("Default Workspace", "Default Service")
			.build();
	@Test
	public void wiremockUploadExampleTest() {
		stubFor(get(urlEqualTo("/cities"))
				.willReturn(aResponse()
				.withStatus(200).withBody("...cities...")));
										
		/* rest of the test */
	}
}

When you use this method, the Transactions included in the test are automatically loaded into the Transaction Repository.

Reference Transaction Repository from Code

For Transactions that already exist in the Repository, you can reference them directly from the code in your mocking tools. This lets developers use Transaction definitions that other team members created in their code.

For more information about the apiKey and apiSecret variables, see BlazeMeter API Keys.

Wiremock Code Samples:

@TransactionCloudRepository(
		workspaces = {"Default Workspace"},
		apiKey = "...",
		apiSecret = "...",
		uri = "https://mock.blazemeter.com"
)
public class WiremockRepoExampleTest {
	@Rule
	public WireMockRule wireMockRule = new WireMockRule(
			wireMockConfig().port(8081));
	private WiremockTxnRepoStore store = new WiremockTxnRepoStore.StoreBuilder()
			.withTestInstance(this).withWireMockServerOrRule(wireMockRule)
			.build();
	@Test
	public void wiremockRepoExampleTest() {
		// virtualization based on Transactions from Repository
		store.useTransaction("Example transaction", "Default Service");
		/* rest of the test */
	}
}