Last active
December 5, 2024 20:44
-
-
Save dirkbolte/358f9b2dd9444d6d6651c66fdf732c33 to your computer and use it in GitHub Desktop.
wiremock example
This file contains hidden or 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
/* | |
* Copyright (C) 2023 Dirk Bolte | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.wiremock.extensions.state.examples; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; | |
import com.github.tomakehurst.wiremock.client.WireMock; | |
import com.github.tomakehurst.wiremock.common.ConsoleNotifier; | |
import com.github.tomakehurst.wiremock.extension.Parameters; | |
import com.github.tomakehurst.wiremock.junit5.WireMockExtension; | |
import com.github.tomakehurst.wiremock.store.Store; | |
import io.restassured.RestAssured; | |
import io.restassured.http.ContentType; | |
import org.apache.http.HttpStatus; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.TestInstance; | |
import org.junit.jupiter.api.extension.RegisterExtension; | |
import org.junit.jupiter.api.parallel.Execution; | |
import org.wiremock.extensions.state.CaffeineStore; | |
import org.wiremock.extensions.state.StateExtension; | |
import java.net.URI; | |
import java.util.Map; | |
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | |
import static com.github.tomakehurst.wiremock.client.WireMock.get; | |
import static com.github.tomakehurst.wiremock.client.WireMock.put; | |
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; | |
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathTemplate; | |
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | |
import static io.restassured.RestAssured.given; | |
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | |
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD; | |
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
@Execution(SAME_THREAD) | |
class ReproTest { | |
private static final Store<String, Object> store = new CaffeineStore(); | |
@RegisterExtension | |
public static WireMockExtension wm = WireMockExtension.newInstance() | |
.options( | |
wireMockConfig().dynamicPort().dynamicHttpsPort().templatingEnabled(true).globalTemplating(true) | |
.extensions(new StateExtension(store)) | |
.notifier(new ConsoleNotifier(true)) | |
) | |
.build(); | |
@BeforeEach | |
public void setup() throws JsonProcessingException { | |
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | |
createGetStub(); | |
createPutStub(); | |
} | |
@Test | |
public void testCrud() { | |
var request = Map.of("a", "b", "c", "d"); | |
var id = given() | |
.accept(ContentType.JSON) | |
.body(request) | |
.put(assertDoesNotThrow(() -> new URI(wm.getRuntimeInfo().getHttpBaseUrl() + "/v1/defs/aaa"))) | |
.then() | |
.statusCode(HttpStatus.SC_NO_CONTENT); | |
given() | |
.accept(ContentType.JSON) | |
.get(assertDoesNotThrow(() -> new URI(wm.getRuntimeInfo().getHttpBaseUrl() + "/v1/defs/aaa"))) | |
.then() | |
.statusCode(HttpStatus.SC_ACCEPTED); | |
} | |
private void createPutStub() throws JsonProcessingException { | |
ResponseDefinitionBuilder putResp = aResponse().withStatus(HttpStatus.SC_NO_CONTENT); | |
wm.stubFor( | |
put(urlPathTemplate("/v1/defs/{name}")) | |
.willReturn(putResp) | |
.withServeEventListener( | |
"recordState", | |
Parameters.from( | |
Map.of( | |
"context", | |
"{{request.pathSegments.[name]}}", | |
"state", | |
Map.of( | |
"statePayload", | |
"{{{jsonPath request.body '$'}}}"))))); | |
} | |
private void createGetStub() throws JsonProcessingException { | |
wm.stubFor( | |
get(urlPathTemplate("/v1/defs/{name}")) | |
.andMatching( | |
"state-matcher", | |
Parameters.from(Map.of("hasContext", "{{ request.pathSegments.[name] }}"))) | |
.willReturn( | |
WireMock.ok() | |
.withHeader("content-type", "application/json") | |
.withBody("{{{state context=request.pathSegments.[name] property='statePayload' }}}"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment