Skip to content

Commit

Permalink
feat: add support for NotEmpty matcher in V4 DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Feb 8, 2023
1 parent 4ac9dcd commit 1bee97d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import au.com.dius.pact.core.model.V4Interaction;
import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.core.model.annotations.Pact;
import au.com.dius.pact.core.model.messaging.Message;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.hamcrest.core.Is;
Expand All @@ -16,13 +15,11 @@

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

import static au.com.dius.pact.consumer.dsl.Matchers.notEmpty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "v4_test_provider")
Expand All @@ -41,7 +38,9 @@ public V4Pact httpInteraction(PactBuilder builder) {
.method("GET"))
.willRespondWith(responseBuilder -> responseBuilder
.status(200)
.body("{\"responsetest\": true, \"version\": \"v3\"}"))
.body("{\"responsetest\": true, \"version\": \"v3\"}")
.header("test", notEmpty("Example"))
)
.comment("This is also a comment");
})
.comment("This is also a comment")
Expand All @@ -55,6 +54,7 @@ void runHttpTest(MockServer mockServer) throws IOException {
assertThat(httpResponse.getCode(), is(200));
assertThat(new String(httpResponse.getEntity().getContent().readAllBytes()),
is(equalTo("{\"responsetest\": true, \"version\": \"v3\"}")));
assertThat(httpResponse.containsHeader("test"), is(true));
}

@Pact(consumer = "v4_test_consumer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ data class TimestampMatcher(
val pattern: String = DateFormatUtils.ISO_DATETIME_FORMAT.pattern,
val dateTimeValue: String?
) : Matcher(
dateTimeValue ?: DateTimeFormatter.ofPattern(pattern).format(Instant.ofEpochMilli(DATE_2000).atOffset(ZoneOffset.UTC)),
dateTimeValue ?: DateTimeFormatter.ofPattern(pattern)
.format(Instant.ofEpochMilli(DATE_2000).atOffset(ZoneOffset.UTC)),
au.com.dius.pact.core.model.matchingrules.TimestampMatcher(pattern),
if (dateTimeValue == null) DateTimeGenerator(pattern) else null
)
Expand Down Expand Up @@ -89,6 +90,9 @@ object NullMatcher : Matcher(null, au.com.dius.pact.core.model.matchingrules.Nul
data class ProviderStateMatcher(val expression: String, override val value: String) : Matcher(value,
null, ProviderStateGenerator(expression))

data class NotEmptyMatcher(override val value: Any?) : Matcher(value,
au.com.dius.pact.core.model.matchingrules.NotEmptyMatcher)

/**
* Exception for handling invalid matchers
*/
Expand Down Expand Up @@ -317,4 +321,13 @@ object Matchers {
fun fromProviderState(expression: String, value: String): Matcher {
return ProviderStateMatcher(expression, value)
}

/**
* Matches if the value is not empty (empty string, null, or missing)
* @param value Example value
*/
@JvmStatic
fun notEmpty(example: String): Matcher {
return NotEmptyMatcher(example)
}
}

0 comments on commit 1bee97d

Please sign in to comment.