Skip to content

Commit

Permalink
Merge 13b1561 into 9c86d2d
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Nov 21, 2018
2 parents 9c86d2d + 13b1561 commit b16258a
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 223 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Expand Up @@ -17,7 +17,7 @@ plugins {
}

group = 'com.stehno.ersatz'
version = '1.8.1'
version = '1.9.0'

sourceCompatibility = 8
targetCompatibility = 8
Expand All @@ -40,6 +40,9 @@ dependencies {
implementation 'commons-fileupload:commons-fileupload:1.3.3'
implementation 'javax.servlet:javax.servlet-api:3.1.0'

// closure handling
implementation 'space.jasan:groovy-closure-support:0.4.0'

// these are used to provide junit helpers in production code
compile 'org.hamcrest:hamcrest-library:1.3'
implementation 'junit:junit:4.12'
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/com/stehno/ersatz/Cookie.groovy
Expand Up @@ -18,10 +18,10 @@ package com.stehno.ersatz
import groovy.transform.CompileStatic
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import java.util.function.Consumer

import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST

/**
Expand All @@ -47,7 +47,7 @@ class Cookie {
* @return the configured cookie
*/
static Cookie cookie(@DelegatesTo(value = Cookie, strategy = DELEGATE_FIRST) final Closure closure) {
delegateTo(new Cookie(), closure)
cookie(ConsumerWithDelegate.create(closure))
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/com/stehno/ersatz/CookieMatcher.groovy
Expand Up @@ -19,10 +19,10 @@ import groovy.transform.CompileStatic
import org.hamcrest.BaseMatcher
import org.hamcrest.Description
import org.hamcrest.Matcher
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import java.util.function.Consumer

import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST
import static org.hamcrest.Matchers.equalTo

Expand All @@ -41,7 +41,7 @@ class CookieMatcher extends BaseMatcher<Cookie> {
* @return the configured matcher
*/
static CookieMatcher cookieMatcher(@DelegatesTo(value = CookieMatcher, strategy = DELEGATE_FIRST) final Closure closure) {
delegateTo(new CookieMatcher(), closure)
cookieMatcher(ConsumerWithDelegate.create(closure))
}

/**
Expand Down
15 changes: 4 additions & 11 deletions src/main/groovy/com/stehno/ersatz/ErsatzServer.groovy
Expand Up @@ -37,6 +37,7 @@ import io.undertow.server.handlers.encoding.DeflateEncodingProvider
import io.undertow.server.handlers.encoding.EncodingHandler
import io.undertow.server.handlers.encoding.GzipEncodingProvider
import org.xnio.Options
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import javax.net.ssl.KeyManagerFactory
import javax.net.ssl.SSLContext
Expand All @@ -46,7 +47,6 @@ import java.util.function.BiFunction
import java.util.function.Consumer
import java.util.function.Function

import static com.stehno.ersatz.impl.Delegator.delegateTo
import static com.stehno.ersatz.impl.ResponseChunker.prepareChunks
import static groovy.lang.Closure.DELEGATE_FIRST
import static groovy.transform.TypeCheckingMode.SKIP
Expand Down Expand Up @@ -114,7 +114,7 @@ class ErsatzServer implements ServerConfig, Closeable {
@SuppressWarnings('ThisReferenceEscapesConstructor')
ErsatzServer(@DelegatesTo(value = ServerConfig, strategy = DELEGATE_FIRST) final Closure closure = null) {
if (closure) {
delegateTo(this, closure)
ConsumerWithDelegate.create(closure).accept(this)
}
}

Expand Down Expand Up @@ -306,13 +306,7 @@ class ErsatzServer implements ServerConfig, Closeable {
*/
@SuppressWarnings('ConfusingMethodName')
ErsatzServer expectations(@DelegatesTo(value = Expectations, strategy = DELEGATE_FIRST) final Closure closure) {
delegateTo(expectations, closure)

if (autoStartEnabled) {
start()
}

this
expectations(ConsumerWithDelegate.create(closure))
}

/**
Expand Down Expand Up @@ -389,8 +383,7 @@ class ErsatzServer implements ServerConfig, Closeable {
*/
@Override
ServerConfig authentication(@DelegatesTo(value = AuthenticationConfig, strategy = DELEGATE_FIRST) final Closure closure) {
authenticationConfig = delegateTo(new AuthenticationConfig(), closure)
this
authentication(ConsumerWithDelegate.create(closure))
}

/**
Expand Down
Expand Up @@ -21,8 +21,9 @@ import groovy.transform.EqualsAndHashCode

import java.util.function.Consumer

import space.jasan.support.groovy.closure.ConsumerWithDelegate

import static com.stehno.ersatz.ContentType.TEXT_PLAIN
import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST

/**
Expand All @@ -44,7 +45,7 @@ class MultipartRequestContent {
* @return a configured instance of MultipartRequestContent
*/
static MultipartRequestContent multipart(@DelegatesTo(value = MultipartRequestContent, strategy = DELEGATE_FIRST) final Closure closure) {
delegateTo(new MultipartRequestContent(), closure)
multipart(ConsumerWithDelegate.create(closure))
}

/**
Expand Down
Expand Up @@ -19,10 +19,10 @@ import groovy.transform.CompileStatic
import org.hamcrest.BaseMatcher
import org.hamcrest.Description
import org.hamcrest.Matcher
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import java.util.function.Consumer

import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST
import static org.hamcrest.Matchers.equalTo
import static org.hamcrest.Matchers.notNullValue
Expand All @@ -44,7 +44,7 @@ class MultipartRequestMatcher extends BaseMatcher<MultipartRequestContent> {
* @return a configured matcher instance
*/
static MultipartRequestMatcher multipartMatcher(@DelegatesTo(value = MultipartRequestMatcher, strategy = DELEGATE_FIRST) final Closure closure) {
delegateTo(new MultipartRequestMatcher(), closure)
multipartMatcher(ConsumerWithDelegate.create(closure))
}

/**
Expand Down
Expand Up @@ -17,11 +17,11 @@ package com.stehno.ersatz

import com.stehno.ersatz.impl.ErsatzMultipartResponseContent
import groovy.transform.CompileStatic
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import java.util.function.Consumer
import java.util.function.Function

import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST
import static java.util.Collections.shuffle

Expand All @@ -47,7 +47,7 @@ abstract class MultipartResponseContent {
* @return a reference to this MultipartResponseContent instance
*/
static MultipartResponseContent multipart(final @DelegatesTo(value = MultipartResponseContent, strategy = DELEGATE_FIRST) Closure closure) {
delegateTo(new ErsatzMultipartResponseContent(), closure)
multipart(ConsumerWithDelegate.create(closure))
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/groovy/com/stehno/ersatz/ReceivedMessage.groovy
Expand Up @@ -15,6 +15,8 @@
*/
package com.stehno.ersatz

import java.util.function.Consumer

import static groovy.lang.Closure.DELEGATE_FIRST

/**
Expand Down Expand Up @@ -64,4 +66,12 @@ interface ReceivedMessage {
* @return a reference to this ReceivedMessage
*/
MessageReaction reaction(@DelegatesTo(value = MessageReaction, strategy = DELEGATE_FIRST) Closure closure)

/**
* Used to specify a reaction message to be sent after receiving this message.
*
* @param config the reaction configuration consumer
* @return a reference to this ReceivedMessage
*/
MessageReaction reaction(Consumer<MessageReaction> config)
}
4 changes: 2 additions & 2 deletions src/main/groovy/com/stehno/ersatz/RequestDecoders.groovy
Expand Up @@ -16,11 +16,11 @@
package com.stehno.ersatz

import groovy.transform.Immutable
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import javax.activation.MimeType
import java.util.function.BiFunction

import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST

/**
Expand All @@ -38,7 +38,7 @@ class RequestDecoders {
@SuppressWarnings('ThisReferenceEscapesConstructor')
RequestDecoders(@DelegatesTo(value = RequestDecoders, strategy = DELEGATE_FIRST) Closure closure = null) {
if (closure) {
delegateTo(this, closure)
ConsumerWithDelegate.create(closure).accept(this)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/com/stehno/ersatz/ResponseEncoders.groovy
Expand Up @@ -18,11 +18,11 @@ package com.stehno.ersatz
import groovy.transform.CompileStatic
import groovy.transform.TupleConstructor
import groovy.transform.TypeChecked
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import javax.activation.MimeType
import java.util.function.Function

import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST

/**
Expand All @@ -42,7 +42,7 @@ class ResponseEncoders {
@SuppressWarnings('ThisReferenceEscapesConstructor')
ResponseEncoders(@DelegatesTo(value = ResponseEncoders, strategy = DELEGATE_FIRST) Closure closure = null) {
if (closure) {
delegateTo(this, closure)
ConsumerWithDelegate.create(closure).accept(this)
}
}

Expand Down
29 changes: 0 additions & 29 deletions src/main/groovy/com/stehno/ersatz/impl/Delegator.groovy

This file was deleted.

6 changes: 2 additions & 4 deletions src/main/groovy/com/stehno/ersatz/impl/ErsatzRequest.groovy
Expand Up @@ -24,6 +24,7 @@ import com.stehno.ersatz.Response
import com.stehno.ersatz.ResponseEncoders
import org.hamcrest.Matcher
import org.hamcrest.StringDescription
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import java.util.function.Consumer

Expand All @@ -36,7 +37,6 @@ import static com.stehno.ersatz.HttpMethod.PATCH
import static com.stehno.ersatz.HttpMethod.POST
import static com.stehno.ersatz.HttpMethod.PUT
import static com.stehno.ersatz.HttpMethod.TRACE
import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST
import static org.hamcrest.Matchers.anything
import static org.hamcrest.Matchers.contains
Expand Down Expand Up @@ -173,9 +173,7 @@ class ErsatzRequest implements Request {

@Override
Request responder(@DelegatesTo(value = Response, strategy = DELEGATE_FIRST) final Closure closure) {
Response response = delegateTo(newResponse(), closure)
responses.add(response)
this
responder(ConsumerWithDelegate.create(closure))
}

@Override @SuppressWarnings('ConfusingMethodName')
Expand Down
6 changes: 3 additions & 3 deletions src/main/groovy/com/stehno/ersatz/impl/ErsatzResponse.groovy
Expand Up @@ -29,9 +29,10 @@ import java.util.concurrent.TimeUnit
import java.util.function.Consumer
import java.util.function.Function

import space.jasan.support.groovy.closure.ConsumerWithDelegate

import static com.stehno.ersatz.ContentType.CONTENT_TYPE_HEADER
import static com.stehno.ersatz.ContentType.TEXT_PLAIN
import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST
import static java.util.concurrent.TimeUnit.MILLISECONDS

Expand Down Expand Up @@ -201,8 +202,7 @@ class ErsatzResponse implements Response {

@Override
Response chunked(@DelegatesTo(value = ChunkingConfig, strategy = DELEGATE_FIRST) Closure closure) {
chunkingConfig = delegateTo(new ChunkingConfig(), closure)
this
chunked(ConsumerWithDelegate.create(closure))
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions src/main/groovy/com/stehno/ersatz/impl/ExpectationsImpl.groovy
Expand Up @@ -25,6 +25,7 @@ import com.stehno.ersatz.WebSocketExpectations
import groovy.transform.CompileStatic
import org.hamcrest.Matcher
import org.hamcrest.Matchers
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import java.util.concurrent.TimeUnit
import java.util.function.Consumer
Expand All @@ -37,7 +38,6 @@ import static com.stehno.ersatz.HttpMethod.OPTIONS
import static com.stehno.ersatz.HttpMethod.PATCH
import static com.stehno.ersatz.HttpMethod.POST
import static com.stehno.ersatz.HttpMethod.PUT
import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST
import static java.util.concurrent.TimeUnit.SECONDS
import static org.hamcrest.Matchers.equalTo
Expand Down Expand Up @@ -315,9 +315,7 @@ class ExpectationsImpl implements Expectations {

@Override
WebSocketExpectations ws(final String path, @DelegatesTo(value = WebSocketExpectations, strategy = DELEGATE_FIRST) Closure closure) {
WebSocketExpectationsImpl wse = delegateTo(new WebSocketExpectationsImpl(path), closure)
webSockets[path] = wse
wse
ws(path, ConsumerWithDelegate.create(closure))
}

@Override
Expand Down Expand Up @@ -378,9 +376,7 @@ class ExpectationsImpl implements Expectations {
}

private Request expect(final Request request, final Closure closure) {
delegateTo(request, closure)
requests.add(request)
request
expect(request, ConsumerWithDelegate.create(closure))
}

private Request expect(final Request request, final Consumer<? extends Request> consumer) {
Expand Down
Expand Up @@ -21,14 +21,15 @@ import com.stehno.ersatz.WsMessageType
import groovy.util.logging.Slf4j
import io.undertow.websockets.core.BufferedBinaryMessage
import io.undertow.websockets.core.BufferedTextMessage
import space.jasan.support.groovy.closure.ConsumerWithDelegate

import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.function.Consumer

import static com.stehno.ersatz.WsMessageType.BINARY
import static com.stehno.ersatz.WsMessageType.TEXT
import static com.stehno.ersatz.WsMessageType.resolve
import static com.stehno.ersatz.impl.Delegator.delegateTo
import static groovy.lang.Closure.DELEGATE_FIRST

@Slf4j @SuppressWarnings('ConfusingMethodName')
Expand Down Expand Up @@ -71,7 +72,13 @@ class ReceivedMessageImpl implements ReceivedMessage {

@Override
MessageReaction reaction(@DelegatesTo(value = MessageReaction, strategy = DELEGATE_FIRST) Closure closure) {
MessageReactionImpl reaction = delegateTo(new MessageReactionImpl(), closure)
reaction(ConsumerWithDelegate.create(closure))
}

@Override
MessageReaction reaction(Consumer<MessageReaction> config) {
MessageReactionImpl reaction = new MessageReactionImpl()
config.accept(reaction)
reactions << reaction
reaction
}
Expand Down

0 comments on commit b16258a

Please sign in to comment.