Skip to content

Commit

Permalink
refactor: converted PactDslJsonArray to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen authored and rholshausen committed Jul 28, 2023
1 parent 367d332 commit b3fc7a5
Show file tree
Hide file tree
Showing 9 changed files with 1,389 additions and 1,448 deletions.
1,418 changes: 0 additions & 1,418 deletions consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonArray.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import java.time.Instant;
Expand All @@ -49,7 +50,7 @@
public class PactDslJsonBody extends DslPart {

private static final String EXAMPLE = "Example \"";
private final JsonValue.Object body;
private JsonValue.Object body;

/**
* Constructs a new body as a root
Expand Down Expand Up @@ -120,12 +121,17 @@ public void putArrayPrivate(DslPart object) {
}
}

@Override
public JsonValue getBody() {
return body;
}
@Override
public JsonValue getBody() {
return body;
}

/**
@Override
public void setBody(JsonValue body) {
this.body = (JsonValue.Object) body;
}

/**
* Attribute that must be the specified value
* @param name attribute name
* @param value string value
Expand Down Expand Up @@ -1032,7 +1038,7 @@ public PactDslJsonBody hexValue(String name) {
* @param hexValue example value to use for generated bodies
*/
public PactDslJsonBody hexValue(String name, String hexValue) {
if (!hexValue.matches(HEXADECIMAL)) {
if (!hexValue.matches(DslPart.Companion.getHEXADECIMAL().getPattern())) {
throw new InvalidMatcherException(EXAMPLE + hexValue + "\" is not a hexadecimal value");
}
body.add(name, new JsonValue.StringValue(hexValue.toCharArray()));
Expand Down Expand Up @@ -1064,11 +1070,11 @@ public PactDslJsonBody uuid(String name, UUID uuid) {
* @param uuid example UUID to use for generated bodies
*/
public PactDslJsonBody uuid(String name, String uuid) {
if (!uuid.matches(UUID_REGEX)) {
if (!uuid.matches(DslPart.Companion.getUUID_REGEX().getPattern())) {
throw new InvalidMatcherException(EXAMPLE + uuid + "\" is not an UUID");
}
body.add(name, new JsonValue.StringValue(uuid.toCharArray()));
getMatchers().addRule(matcherKey(name, getRootPath()), regexp(UUID_REGEX));
getMatchers().addRule(matcherKey(name, getRootPath()), regexp(DslPart.Companion.getUUID_REGEX().getPattern()));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public JsonValue getBody() {
return Json.toJson(value);
}

@Override
public void setBody(JsonValue body) {
value = body;
}

/**
* @deprecated Use PactDslJsonArray for arrays
*/
Expand Down Expand Up @@ -612,7 +617,7 @@ public static PactDslJsonRootValue hexValue() {
* @param hexValue example value to use for generated bodies
*/
public static PactDslJsonRootValue hexValue(String hexValue) {
if (!hexValue.matches(HEXADECIMAL)) {
if (!hexValue.matches(DslPart.Companion.getHEXADECIMAL().getPattern())) {
throw new InvalidMatcherException(EXAMPLE + hexValue + "\" is not a hexadecimal value");
}
PactDslJsonRootValue value = new PactDslJsonRootValue();
Expand All @@ -628,7 +633,7 @@ public static PactDslJsonRootValue uuid() {
PactDslJsonRootValue value = new PactDslJsonRootValue();
value.getGenerators().addGenerator(Category.BODY, "", UuidGenerator.INSTANCE);
value.setValue("e2490de5-5bd3-43d5-b7c4-526e33f71304");
value.setMatcher(value.regexp(UUID_REGEX));
value.setMatcher(value.regexp(DslPart.Companion.getUUID_REGEX().getPattern()));
return value;
}

Expand All @@ -645,13 +650,13 @@ public static PactDslJsonRootValue uuid(UUID uuid) {
* @param uuid example UUID to use for generated bodies
*/
public static PactDslJsonRootValue uuid(String uuid) {
if (!uuid.matches(UUID_REGEX)) {
if (!uuid.matches(DslPart.Companion.getUUID_REGEX().getPattern())) {
throw new InvalidMatcherException(EXAMPLE + uuid + "\" is not an UUID");
}

PactDslJsonRootValue value = new PactDslJsonRootValue();
value.setValue(uuid);
value.setMatcher(value.regexp(UUID_REGEX));
value.setMatcher(value.regexp(DslPart.Companion.getUUID_REGEX().getPattern()));
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public JsonValue getBody() {
return value;
}

@Override
public void setBody(JsonValue body) {
value = body;
}

/**
* @deprecated Use PactDslJsonArray for arrays
*/
Expand Down Expand Up @@ -567,7 +572,7 @@ public static PactDslRootValue hexValue() {
* @param hexValue example value to use for generated bodies
*/
public static PactDslRootValue hexValue(String hexValue) {
if (!hexValue.matches(HEXADECIMAL)) {
if (!hexValue.matches(DslPart.Companion.getHEXADECIMAL().getPattern())) {
throw new InvalidMatcherException(EXAMPLE + hexValue + "\" is not a hexadecimal value");
}
PactDslRootValue value = new PactDslRootValue();
Expand All @@ -583,7 +588,7 @@ public static PactDslRootValue uuid() {
PactDslRootValue value = new PactDslRootValue();
value.getGenerators().addGenerator(Category.BODY, "", UuidGenerator.INSTANCE);
value.setValue("e2490de5-5bd3-43d5-b7c4-526e33f71304");
value.setMatcher(value.regexp(UUID_REGEX));
value.setMatcher(value.regexp(DslPart.Companion.getUUID_REGEX().getPattern()));
return value;
}

Expand All @@ -600,13 +605,13 @@ public static PactDslRootValue uuid(UUID uuid) {
* @param uuid example UUID to use for generated bodies
*/
public static PactDslRootValue uuid(String uuid) {
if (!uuid.matches(UUID_REGEX)) {
if (!uuid.matches(DslPart.Companion.getUUID_REGEX().getPattern())) {
throw new InvalidMatcherException(EXAMPLE + uuid + "\" is not an UUID");
}

PactDslRootValue value = new PactDslRootValue();
value.setValue(uuid);
value.setMatcher(value.regexp(UUID_REGEX));
value.setMatcher(value.regexp(DslPart.Companion.getUUID_REGEX().getPattern()));
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class DslPart {

abstract fun putObjectPrivate(obj: DslPart)
abstract fun putArrayPrivate(obj: DslPart)
abstract val body: JsonValue
abstract var body: JsonValue

/**
* Field which is an array
Expand All @@ -58,7 +58,7 @@ abstract class DslPart {
/**
* Close of the previous array element
*/
abstract fun closeArray(): DslPart
abstract fun closeArray(): DslPart?

/**
* Array field where each element must match the following object
Expand Down Expand Up @@ -431,9 +431,9 @@ abstract class DslPart {
abstract fun matchUrl2(vararg pathFragments: Any): DslPart

companion object {
const val HEXADECIMAL = "[0-9a-fA-F]+"
const val IP_ADDRESS = "(\\d{1,3}\\.)+\\d{1,3}"
const val UUID_REGEX = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
val HEXADECIMAL = Regex("[0-9a-fA-F]+")
val IP_ADDRESS = Regex("(\\d{1,3}\\.)+\\d{1,3}")
val UUID_REGEX = Regex("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
const val DATE_2000 = 949323600000L

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.com.dius.pact.consumer.dsl

import au.com.dius.pact.consumer.InvalidMatcherException
import au.com.dius.pact.consumer.dsl.DslPart.Companion.HEXADECIMAL
import au.com.dius.pact.core.model.ContentType
import au.com.dius.pact.core.model.generators.DateGenerator
import au.com.dius.pact.core.model.generators.DateTimeGenerator
Expand Down Expand Up @@ -286,7 +287,7 @@ class FormPostBuilder(
* @param hexValue example value to use for generated bodies
*/
fun hexValue(name: String, hexValue: String): FormPostBuilder {
if (!hexValue.matches(HEXADECIMAL_REGEX)) {
if (!hexValue.matches(HEXADECIMAL)) {
throw InvalidMatcherException("Example \"$hexValue\" is not a hexadecimal value")
}
body[name] = listOf(hexValue)
Expand Down Expand Up @@ -318,11 +319,11 @@ class FormPostBuilder(
* @param uuid example UUID to use for generated bodies
*/
fun uuid(name: String, uuid: String): FormPostBuilder {
if (!uuid.matches(UUID_REGEX)) {
if (!uuid.matches(DslPart.UUID_REGEX)) {
throw InvalidMatcherException("Example \"$uuid\" is not an UUID")
}
body[name] = listOf(uuid)
matchers.addRule(ROOT + name, PM.stringMatcher(DslPart.UUID_REGEX))
matchers.addRule(ROOT + name, PM.stringMatcher(DslPart.UUID_REGEX.pattern))
return this
}

Expand Down Expand Up @@ -434,8 +435,6 @@ class FormPostBuilder(

companion object {
val BODY = au.com.dius.pact.core.model.generators.Category.BODY
val HEXADECIMAL_REGEX = Regex(DslPart.HEXADECIMAL)
val UUID_REGEX = Regex(DslPart.UUID_REGEX)
val APPLICATION_FORM_URLENCODED = org.apache.http.entity.ContentType.APPLICATION_FORM_URLENCODED.toString()
const val ROOT = "$."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ open class MetadataBuilder(
* @param hexValue example value to use for generated bodies
*/
fun hexValue(name: String, hexValue: String): MetadataBuilder {
if (!hexValue.matches(Regex(DslPart.HEXADECIMAL))) {
if (!hexValue.matches(DslPart.HEXADECIMAL)) {
throw InvalidMatcherException("Example \"$hexValue\" is not a valid hexadecimal value")
}
values[name] = hexValue
Expand Down Expand Up @@ -360,11 +360,11 @@ open class MetadataBuilder(
* @param uuid example UUID to use for generated bodies
*/
fun uuid(name: String, uuid: String): MetadataBuilder {
if (!uuid.matches(Regex(DslPart.UUID_REGEX))) {
if (!uuid.matches(DslPart.UUID_REGEX)) {
throw InvalidMatcherException("Example \"$uuid\" is not a valid UUID")
}
values[name] = uuid
matchers.addRule(name, RegexMatcher(DslPart.UUID_REGEX))
matchers.addRule(name, RegexMatcher(DslPart.UUID_REGEX.pattern))
return this
}

Expand Down
Loading

0 comments on commit b3fc7a5

Please sign in to comment.