Skip to content

Commit

Permalink
Add Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raynigon committed Nov 28, 2024
1 parent c0bb1f0 commit 615fd3c
Showing 1 changed file with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.raynigon.ecs.logging.access.client

import org.slf4j.MDC
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

Check notice on line 6 in ecs-logging-access/src/test/groovy/com/raynigon/ecs/logging/access/client/RestClientConfigurationSpec.groovy

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ecs-logging-access/src/test/groovy/com/raynigon/ecs/logging/access/client/RestClientConfigurationSpec.groovy#L6

The [org.springframework.http.HttpStatusCode] import is never referenced
import org.springframework.http.client.ClientHttpRequest
import org.springframework.http.client.ClientHttpRequestFactory
import org.springframework.http.client.ClientHttpResponse
import org.springframework.web.client.DefaultRestClient
import org.springframework.web.client.RestClient
import spock.lang.Specification

import static com.raynigon.ecs.logging.LoggingConstants.TRANSACTION_ID_HEADER

Check notice on line 14 in ecs-logging-access/src/test/groovy/com/raynigon/ecs/logging/access/client/RestClientConfigurationSpec.groovy

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ecs-logging-access/src/test/groovy/com/raynigon/ecs/logging/access/client/RestClientConfigurationSpec.groovy#L14

Static imports should appear before normal imports
import static com.raynigon.ecs.logging.LoggingConstants.TRANSACTION_ID_PROPERTY

Check notice on line 15 in ecs-logging-access/src/test/groovy/com/raynigon/ecs/logging/access/client/RestClientConfigurationSpec.groovy

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ecs-logging-access/src/test/groovy/com/raynigon/ecs/logging/access/client/RestClientConfigurationSpec.groovy#L15

Static imports should appear before normal imports

class RestClientConfigurationSpec extends Specification {

def 'interceptor is added to rest client builder'() {
given:
RestClient.Builder builder = Mock()

and:
RestClientConfiguration configuration = new RestClientConfiguration()

when:
configuration.customize(builder)

then:
1 * builder.requestInterceptor(_)
}


@SuppressWarnings('GroovyAccessibility')
def 'interceptor is added to rest client derived from builder'() {
given:
RestClient.Builder builder = RestClient.builder()

and:
RestClientConfiguration configuration = new RestClientConfiguration()

when:
configuration.customize(builder)

and: "A RestClient is created from the builder"
RestClient client = builder.build()

and: "Check the field in the DefaultRestClient"
def field = DefaultRestClient.class.getDeclaredField("interceptors")

Check notice on line 49 in ecs-logging-access/src/test/groovy/com/raynigon/ecs/logging/access/client/RestClientConfigurationSpec.groovy

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ecs-logging-access/src/test/groovy/com/raynigon/ecs/logging/access/client/RestClientConfigurationSpec.groovy#L49

DefaultRestClient.class can be rewritten as DefaultRestClient
field.setAccessible(true)

then:
field.get(client).size() == 1
}

def 'interceptor adds transaction id'() {
given: "A ClientHttpRequestFactory"
ClientHttpRequestFactory requestFactory = Mock()
ClientHttpRequest request = Mock()
requestFactory.createRequest(_, _) >> request

and: "A HttpRequest with headers"
HttpHeaders headers = new HttpHeaders()
request.getHeaders() >> headers
request.getAttributes() >> [:]

and: "A HttpResponse"
ClientHttpResponse response = Mock()
request.execute() >> response
response.statusCode >> HttpStatus.OK

and: "A RestClient Builder"
RestClient.Builder builder = RestClient.builder()
.requestFactory(requestFactory)

and: "A RestClientConfiguration"
RestClientConfiguration configuration = new RestClientConfiguration()

and: "A transaction id in the MDC"
MDC.clear()
MDC.put(TRANSACTION_ID_PROPERTY, "test-123")

when: "The configuration is applied to the builder"
configuration.customize(builder)

and: "A RestClient is created from the builder"
RestClient client = builder.build()

and: "A request is executed"
client.get().uri("https://google.com").retrieve().toBodilessEntity()

then: "The transaction id is added to the request"
headers.containsKey(TRANSACTION_ID_HEADER)
headers.get(TRANSACTION_ID_HEADER).first() == "test-123"
}
}

0 comments on commit 615fd3c

Please sign in to comment.