From d2f78ece7c2e77b3ac234a0088188115a708b70a Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Fri, 16 Aug 2024 10:29:29 +0200 Subject: [PATCH] Move Spring Data REST Starbucks example to Testcontainers. --- rest/starbucks/pom.xml | 13 ++++++ .../rest/stores/StarbucksClient.java | 41 ++++++++---------- .../StoreRepositoryIntegrationTests.java | 9 ++-- .../rest/stores/TestApplication.java | 43 +++++++++++++++++++ 4 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 rest/starbucks/src/test/java/example/springdata/rest/stores/TestApplication.java diff --git a/rest/starbucks/pom.xml b/rest/starbucks/pom.xml index 9eabe98eb..7dce29b98 100644 --- a/rest/starbucks/pom.xml +++ b/rest/starbucks/pom.xml @@ -93,6 +93,19 @@ webjars-locator-core runtime + + + + org.springframework.boot + spring-boot-testcontainers + test + + + + org.testcontainers + mongodb + test + diff --git a/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java b/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java index f03b63663..a314c91a4 100644 --- a/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java +++ b/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2021 the original author or authors. + * Copyright 2014-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,20 +26,17 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.context.annotation.Bean; import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.MediaTypes; import org.springframework.hateoas.client.Traverson; import org.springframework.hateoas.server.core.TypeReferences.CollectionModelType; import org.springframework.hateoas.server.core.TypeReferences.EntityModelType; import org.springframework.hateoas.server.core.TypeReferences.PagedModelType; -import org.springframework.http.RequestEntity; -import org.springframework.web.client.RestOperations; -import org.springframework.web.client.RestTemplate; +import org.springframework.web.client.RestClient; /** * A test case to discover the search resource and execute a predefined search with it. @@ -47,22 +44,12 @@ * @author Oliver Gierke * @author Divya Srivastava */ -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @Slf4j +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) class StarbucksClient { - @SpringBootApplication - static class Config { - - @Bean - public RestTemplate restTemplate() { - return new RestTemplate(); - } - } - @LocalServerPort int port; - - @Autowired RestOperations restOperations; + @Autowired TestRestTemplate template; private static final String SERVICE_URI = "http://localhost:%s/api"; @@ -102,17 +89,23 @@ void accessServiceUsingRestTemplate() { // Access root resource - var uri = URI.create(String.format(SERVICE_URI, port)); - var request = RequestEntity.get(uri).accept(HAL_JSON).build(); - var rootLinks = restOperations.exchange(request, new EntityModelType<>() {}).getBody(); - var links = rootLinks.getLinks(); + var client = RestClient.create(template.getRestTemplate()); + + var links = client.get() + .uri(URI.create(String.format(SERVICE_URI, port))) + .accept(HAL_JSON) + .retrieve() + .body(new EntityModelType<>() {}) + .getLinks(); // Follow stores link var storesLink = links.getRequiredLink("stores").expand(); - request = RequestEntity.get(URI.create(storesLink.getHref())).accept(HAL_JSON).build(); - var stores = restOperations.exchange(request, new CollectionModelType() {}).getBody(); + var stores = client.get().uri(storesLink.toUri()) + .accept(HAL_JSON) + .retrieve() + .body(new CollectionModelType() {}); stores.getContent().forEach(store -> log.info("{} - {}", store.name, store.address)); } diff --git a/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java b/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java index f5de082ee..e3ab2fc4d 100644 --- a/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java +++ b/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2021 the original author or authors. + * Copyright 2014-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.PageRequest; @@ -38,8 +37,8 @@ * @author Oliver Gierke * @author Mark Paluch */ -@SpringBootTest(classes = Application.class) -public class StoreRepositoryIntegrationTests { +@SpringBootTest(classes = { Application.class, TestApplication.class }) +class StoreRepositoryIntegrationTests { @Autowired StoreRepository repository; @@ -50,7 +49,7 @@ public void clearDb() { } @Test - public void findsStoresByLocation() { + void findsStoresByLocation() { var location = new Point(-73.995146, 40.740337); var store = new Store(UUID.randomUUID(), "Foo", new Address("street", "city", "zip", location)); diff --git a/rest/starbucks/src/test/java/example/springdata/rest/stores/TestApplication.java b/rest/starbucks/src/test/java/example/springdata/rest/stores/TestApplication.java new file mode 100644 index 000000000..cb1bfcf18 --- /dev/null +++ b/rest/starbucks/src/test/java/example/springdata/rest/stores/TestApplication.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.rest.stores; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.utility.DockerImageName; + +/** + * @author Oliver Drotbohm + */ +@Configuration +public class TestApplication { + + @Bean + @ServiceConnection + MongoDBContainer mongoDBContainer() { + return new MongoDBContainer(DockerImageName.parse("mongodb/mongodb-community-server")); + } + + public static void main(String[] args) { + + SpringApplication.from(Application::main) + .with(TestApplication.class) + .run(args); + } +}