Skip to content

Commit

Permalink
Allow to set the port via an expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
holomekc committed May 12, 2023
1 parent 2a8b602 commit fa65ca2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ data class ProviderInfo @JvmOverloads constructor(
ProviderType.UNSPECIFIED -> null
else -> annotation.providerType
}
return ProviderInfo(providerName, annotation.hostInterface, annotation.port, pactVersion, providerType,
val port = ExpressionParser().parseExpression(annotation.port, DataType.STRING)?.toString() ?: annotation.port
return ProviderInfo(providerName, annotation.hostInterface, port, pactVersion, providerType,
annotation.https, annotation.mockServerImplementation, annotation.keyStorePath, annotation.keyStoreAlias,
annotation.keyStorePassword, annotation.privateKeyPassword)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package au.com.dius.pact.consumer.junit5;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit.MockServerConfig;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.annotations.Pact;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

/**
* @author Christopher Holomek ([email protected])
* @since 12.05.23
*/
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "ArticlesProvider", pactVersion = PactSpecVersion.V3)
public class PactTestForPortTest {

private String response;
final private String EXPECTED_RESPONSE = "expected";

private Map<String, String> headers = MapUtils.putAll(new HashMap<>(), new String[] {
"Content-Type", "text/plain"
});

@BeforeEach
void beforeEach() {
System.setProperty("pact.test.port", "1234");
response = EXPECTED_RESPONSE;
}

@AfterEach
void afterEach() {
System.clearProperty("pact.test.port");
}

@Pact(consumer = "Consumer")
public RequestResponsePact pactExecutedAfterBeforeEach(PactDslWithProvider builder) {
return builder
.given("provider state")
.uponReceiving("request")
.path("/")
.method("GET")
.willRespondWith()
.headers(headers)
.status(200)
.body(response)
.toPact();
}

@Test
@PactTestFor(pactMethod = "pactExecutedAfterBeforeEach")
@MockServerConfig(port = "${pact.test.port}")
void testPactExecutedAfterBeforeEach(MockServer mockServer) throws IOException {
ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.get(mockServer.getUrl() + "/").execute().returnResponse();
assertThat(IOUtils.toString(httpResponse.getEntity().getContent()),
is(equalTo(EXPECTED_RESPONSE)));
}

@Test
@PactTestFor(pactMethod = "pactExecutedAfterBeforeEach", port = "${pact.test.port}")
void testPactExecutedAfterBeforeEachClassic(MockServer mockServer) throws IOException {
ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.get(mockServer.getUrl() + "/").execute().returnResponse();
assertThat(IOUtils.toString(httpResponse.getEntity().getContent()),
is(equalTo(EXPECTED_RESPONSE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package au.com.dius.pact.consumer.model

import au.com.dius.pact.consumer.junit.MockServerConfig
import au.com.dius.pact.core.model.PactSpecVersion
import au.com.dius.pact.core.support.expressions.DataType
import au.com.dius.pact.core.support.expressions.ExpressionParser
import io.ktor.util.network.hostname
import java.net.InetSocketAddress
import java.util.Optional
Expand Down Expand Up @@ -122,9 +124,10 @@ open class MockProviderConfig @JvmOverloads constructor (
fun fromMockServerAnnotation(config: Optional<MockServerConfig>): MockProviderConfig? {
return if (config.isPresent) {
val annotation = config.get()
val port = ExpressionParser().parseExpression(annotation.port, DataType.STRING)?.toString() ?: annotation.port
MockProviderConfig(
annotation.hostInterface.ifEmpty { LOCALHOST },
if (annotation.port.isEmpty()) 0 else annotation.port.toInt(),
if (port.isEmpty()) 0 else port.toInt(),
PactSpecVersion.UNSPECIFIED,
if (annotation.tls) "https" else HTTP,
annotation.implementation,
Expand Down

0 comments on commit fa65ca2

Please sign in to comment.