Skip to content

Commit

Permalink
test: Use ApplicationContext.run(EmbeddedServer (#10664)
Browse files Browse the repository at this point in the history
This PR also adds some missing @requires spec.name to avoid test pollution.
  • Loading branch information
sdelamo authored Mar 28, 2024
1 parent 036f48b commit b932cdf
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@ class SslSelfSignedSpec extends Specification {
@Shared
String host = Optional.ofNullable(System.getenv(Environment.HOSTNAME)).orElse(SocketUtils.LOCALHOST)

ApplicationContext context
EmbeddedServer embeddedServer
HttpClient client

void setup() {
context = ApplicationContext.run([
embeddedServer = ApplicationContext.run(EmbeddedServer, [
'spec.name': 'SslSelfSignedSpec',
'micronaut.ssl.enabled': true,
'micronaut.server.ssl.buildSelfSigned': true,
'micronaut.server.ssl.port': -1,
'micronaut.http.client.ssl.insecure-trust-all-certificates': true,
])
embeddedServer = context.getBean(EmbeddedServer).start()
client = context.createBean(HttpClient, embeddedServer.getURL())
client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())
}

void cleanup() {
client.close()
context.close()
embeddedServer.close()
}

void "expect the url to be https"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ class ProxyInterceptFullResponseSpec extends Specification {

@Shared
@AutoCleanup
ApplicationContext context = ApplicationContext.run(
'spec.name': 'ProxyInterceptFullResponseSpec'
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer,
['spec.name': 'ProxyInterceptFullResponseSpec']
)

@Shared
EmbeddedServer embeddedServer = context.getBean(EmbeddedServer).start()

@Shared
@AutoCleanup
HttpClient client = context.createBean(HttpClient, embeddedServer.getURL())
HttpClient client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())

void "test a request expecting a full response intercepted by a proxy providing a streaming response"() {
expect:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,14 @@ class ReadTimeoutSpec extends Specification {

@Shared
@AutoCleanup
ApplicationContext context = ApplicationContext.run(
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [
"micronaut.http.client.readTimeout": '3s',
'spec.name': 'ReadTimeoutSpec'
)

@Shared
EmbeddedServer embeddedServer = context.getBean(EmbeddedServer).start()
])

@Shared
@AutoCleanup
HttpClient client = context.createBean(HttpClient, embeddedServer.getURL())
HttpClient client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())


void "test read timeout setting"() {
Expand Down Expand Up @@ -321,12 +318,10 @@ class ReadTimeoutSpec extends Specification {

void "test disable read timeout"() {
given:

ApplicationContext clientContext = ApplicationContext.run([
EmbeddedServer server = ApplicationContext.run(EmbeddedServer, [
'spec.name' : 'ReadTimeoutSpec',
'micronaut.http.client.read-timeout': '-1s'])
def server = clientContext.getBean(EmbeddedServer).start()
HttpClient client = clientContext.createBean(HttpClient, server.getURL())
HttpClient client = server.applicationContext.createBean(HttpClient, server.getURL())
when:
def result = client.toBlocking().retrieve(HttpRequest.GET('/timeout/client'), String)

Expand All @@ -335,7 +330,7 @@ class ReadTimeoutSpec extends Specification {

cleanup:
client.close()
clientContext.close()
server.close()
}

@Requires(property = 'spec.name', value = 'ReadTimeoutSpec')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,23 @@ class SslSelfSignedSpec extends Specification {
@Shared
String host = Optional.ofNullable(System.getenv(Environment.HOSTNAME)).orElse(SocketUtils.LOCALHOST)

ApplicationContext context
EmbeddedServer embeddedServer
HttpClient client

void setup() {
context = ApplicationContext.run([
embeddedServer = ApplicationContext.run(EmbeddedServer, [
'spec.name': 'SslSelfSignedSpec',
'micronaut.ssl.enabled': true,
'micronaut.server.ssl.buildSelfSigned': true,
'micronaut.server.ssl.port': -1,
'micronaut.http.client.ssl.insecure-trust-all-certificates': true,
])
embeddedServer = context.getBean(EmbeddedServer).start()
client = context.createBean(HttpClient, embeddedServer.getURL())
client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())
}

void cleanup() {
client.close()
context.close()
embeddedServer.close()
}

void "expect the url to be https"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ class SslStaticCertSpec extends Specification {
@Shared
String host = Optional.ofNullable(System.getenv(Environment.HOSTNAME)).orElse(SocketUtils.LOCALHOST)

ApplicationContext context
EmbeddedServer embeddedServer
HttpClient client

void setup() {
context = ApplicationContext.run([
embeddedServer = ApplicationContext.run(EmbeddedServer, [
'spec.name': 'SslStaticCertSpec',
'micronaut.ssl.enabled': true,
'micronaut.ssl.keyStore.path': 'classpath:keystore.p12',
Expand All @@ -61,13 +60,12 @@ class SslStaticCertSpec extends Specification {
'TLS_DHE_DSS_WITH_AES_256_GCM_SHA384'],
'micronaut.http.client.ssl.insecure-trust-all-certificates': true
])
embeddedServer = context.getBean(EmbeddedServer).start()
client = context.createBean(HttpClient, embeddedServer.getURL())
client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())
}

void cleanup() {
client.close()
context.close()
embeddedServer.close()
}

void "expect the url to be https"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ class BlockingCrudSpec extends Specification {

@Shared
@AutoCleanup
ApplicationContext context = ApplicationContext.run([
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [
'spec.name': 'BlockingCrudSpec',
])

@Shared
EmbeddedServer embeddedServer = context.getBean(EmbeddedServer).start()

void "test configured client"() {
given:
ApplicationContext anotherContext = ApplicationContext.run([
Expand All @@ -65,7 +62,7 @@ class BlockingCrudSpec extends Specification {

void "test CRUD operations on generated client that returns blocking responses"() {
given:
BookClient client = context.getBean(BookClient)
BookClient client = embeddedServer.applicationContext.getBean(BookClient)

when:
Book book = client.get(99)
Expand Down Expand Up @@ -130,7 +127,7 @@ class BlockingCrudSpec extends Specification {

void "test DELETE operation with null values"() {
given:
BookClient client = context.getBean(BookClient)
BookClient client = embeddedServer.applicationContext.getBean(BookClient)

when:
client.delete(null)
Expand All @@ -141,7 +138,7 @@ class BlockingCrudSpec extends Specification {

void "test POST operation with null values"() {
given:
BookClient client = context.getBean(BookClient)
BookClient client = embeddedServer.applicationContext.getBean(BookClient)

when:
Book book = client.save(null)
Expand All @@ -152,7 +149,7 @@ class BlockingCrudSpec extends Specification {

void "test PUT operation with null values"() {
given:
BookClient client = context.getBean(BookClient)
BookClient client = embeddedServer.applicationContext.getBean(BookClient)

when:
Book book = client.update(5, null)
Expand All @@ -163,7 +160,7 @@ class BlockingCrudSpec extends Specification {

void "test GET operation with null values"() {
given:
BookClient client = context.getBean(BookClient)
BookClient client = embeddedServer.applicationContext.getBean(BookClient)

when:
Book book = client.get(null)
Expand All @@ -174,7 +171,7 @@ class BlockingCrudSpec extends Specification {

void "test a declarative client void method and 404 response"() {
given:
VoidNotFoundClient client = context.getBean(VoidNotFoundClient)
VoidNotFoundClient client = embeddedServer.applicationContext.getBean(VoidNotFoundClient)

when:
client.call()
Expand All @@ -186,7 +183,7 @@ class BlockingCrudSpec extends Specification {
@Issue('https://github.com/micronaut-projects/micronaut-core/issues/2959')
void "test annotation stereotype"() {
given:
def client = context.getBean(StereotypeClient)
def client = embeddedServer.applicationContext.getBean(StereotypeClient)

expect:
client.list().size() == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ class BlockingFallbackSpec extends Specification {

@Shared
@AutoCleanup
ApplicationContext context = ApplicationContext.run([
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [
'spec.name':'BlockingFallbackSpec',
])

@Shared
EmbeddedServer embeddedServer = context.getBean(EmbeddedServer).start()

void "test that fallback is called when an exception happens invoking the server"() {
given:
BookClient client = context.getBean(BookClient)
BookClient client = embeddedServer.applicationContext.getBean(BookClient)

when:
Book book = client.get(99)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,13 @@ class BlockingPojoCrudSpec extends Specification {

@Shared
@AutoCleanup
ApplicationContext context = ApplicationContext.run([
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [
'spec.name':'BlockingPojoCrudSpec',
])

@Shared
EmbeddedServer embeddedServer = context.getBean(EmbeddedServer).start()

void "test CRUD operations on generated client that returns blocking responses"() {
given:
BookClient client = context.getBean(BookClient)
BookClient client = embeddedServer.applicationContext.getBean(BookClient)

when:
Book book = client.get(99)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

/**
Expand All @@ -46,32 +47,31 @@ import spock.lang.Specification
*/
class ClientFilterSpec extends Specification{

@Shared
@AutoCleanup
ApplicationContext context = ApplicationContext.run([
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [
'spec.name': 'ClientFilterSpec',
])

EmbeddedServer embeddedServer = context.getBean(EmbeddedServer).start()

void "test client filter includes header"() {
given:
MyApi myApi = context.getBean(MyApi)
MyApi myApi = embeddedServer.applicationContext.getBean(MyApi)

expect:
myApi.name() == 'Fred'
}

void "test method-based client filter includes header"() {
given:
MyMethodApi myApi = context.getBean(MyMethodApi)
MyMethodApi myApi = embeddedServer.applicationContext.getBean(MyMethodApi)

expect:
myApi.name() == 'Fred'
}

void "test a client with no service ids doesn't match a filter with a service id"() {
given:
HttpClient client = context.createBean(HttpClient, embeddedServer.getURL())
HttpClient client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())

when:
HttpResponse<String> response = client.toBlocking().exchange("/filters/name", String.class)
Expand Down Expand Up @@ -103,7 +103,7 @@ class ClientFilterSpec extends Specification{

void "test a client filter that throws an exception"() {
given:
HttpClient client = context.createBean(HttpClient, embeddedServer.getURL())
HttpClient client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())

when:
HttpResponse<String> response = client.toBlocking().exchange("/filter-exception/name", String.class)
Expand All @@ -118,7 +118,7 @@ class ClientFilterSpec extends Specification{

void "test a client filter matching to the root"() {
given:
RootApi rootApi = context.getBean(RootApi)
RootApi rootApi = embeddedServer.applicationContext.getBean(RootApi)

expect:
rootApi.name() == 'processed'
Expand Down Expand Up @@ -304,8 +304,8 @@ class ClientFilterSpec extends Specification{

void "filter always observes a response"() {
given:
ObservesResponseClient client = context.getBean(ObservesResponseClient)
ObservesResponseFilter filter = context.getBean(ObservesResponseFilter)
ObservesResponseClient client = embeddedServer.applicationContext.getBean(ObservesResponseClient)
ObservesResponseFilter filter = embeddedServer.applicationContext.getBean(ObservesResponseFilter)

when:
Mono.from(client.monoVoid()).block() == null
Expand Down
Loading

0 comments on commit b932cdf

Please sign in to comment.