package com.opentable.extension;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

public class BodyTransformerTest {

	@Rule
	public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8080).extensions(new BodyTransformer()));

	@Test
	public void replaceVariableHolder() throws Exception {
		wireMockRule.stubFor(post(urlEqualTo("/get/this"))
			.willReturn(aResponse()
				.withStatus(200)
				.withHeader("content-type", "application/json")
				.withBody("{\"var\":$(var), \"got\":\"it\"}")
				.withTransform("body-transformer")));


		given()
			.contentType("application/json")
			.body("{\"var\":1111}")
		.when()
			.post("/get/this")
		.then()
			.statusCode(200)
			.body("var", equalTo(1111))
			.body("got", equalTo("it"));

		wireMockRule.verify(postRequestedFor(urlEqualTo("/get/this")));
	}

	@Test
	public void replaceNestedVariables() throws Exception {
		wireMockRule.stubFor(post(urlEqualTo("/get/this"))
			.willReturn(aResponse()
				.withStatus(200)
				.withHeader("content-type", "application/json")
				.withBody("{\"var\":$(var), \"got\":\"it\", \"nested_attr\": \"$(nested.attr)\"}")
				.withTransform("body-transformer")));


		given()
			.contentType("application/json")
			.body("{\"var\":1111, \"nested\": {\"attr\": \"found\"}}}")
		.when()
			.post("/get/this")
		.then()
			.statusCode(200)
			.body("var", equalTo(1111))
			.body("got", equalTo("it"))
			.body("nested_attr", equalTo("found"));

		wireMockRule.verify(postRequestedFor(urlEqualTo("/get/this")));
	}

	@Test
	public void nullVariableNotFound() throws Exception {
		wireMockRule.stubFor(post(urlEqualTo("/get/this"))
			.willReturn(aResponse()
				.withStatus(200)
				.withHeader("content-type", "application/json")
				.withBody("{\"var\":$(var)}")
				.withTransform("body-transformer")));

		given()
			.contentType("application/json")
			.body("{\"something\":\"different\"}")
		.when()
			.post("/get/this")
		.then()
			.statusCode(200)
			.body("var", equalTo(null));

		wireMockRule.verify(postRequestedFor(urlEqualTo("/get/this")));
	}
}

@@ -27,16 +27,72 @@
 nsion;%0A%0A
+import com.github.tomakehurst.wiremock.client.WireMock;%0A
 import c
@@ -2423,11 +2423,445 @@
 %22)));%0A%09%7D
+%0A%0A%09@Test%0A%09public void doesNotApplyGlobally() throws Exception %7B%0A%09%09wireMockRule.stubFor(post(urlEqualTo(%22/get/this%22))%0A%09%09%09.willReturn(aResponse()%0A%09%09%09%09.withStatus(200)%0A%09%09%09%09.withHeader(%22content-type%22, %22application/json%22)%0A%09%09%09%09.withBody(%22%7B%5C%22var%5C%22:$(var)%7D%22)));%0A%0A%09%09given()%0A%09%09%09.contentType(%22application/json%22)%0A%09%09%09.body(%22%7B%5C%22var%5C%22:%5C%22foo%5C%22%7D%22)%0A%09%09.when()%0A%09%09%09.post(%22/get/this%22)%0A%09%09.then()%0A%09%09%09.statusCode(200)%0A%09%09%09.body(equalTo(%22%7B%5C%22var%5C%22:$(var)%7D%22));%0A%09%7D
 %0A%7D%0A

test for when body transformer is not applied