Skip to content

Commit

Permalink
Move Spring Data REST Starbucks example to Testcontainers.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Aug 16, 2024
1 parent 0d3df30 commit d2f78ec
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 29 deletions.
13 changes: 13 additions & 0 deletions rest/starbucks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@
<artifactId>webjars-locator-core</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Testcontainers -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,43 +26,30 @@

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.
*
* @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";

Expand Down Expand Up @@ -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<Store>() {}).getBody();
var stores = client.get().uri(storesLink.toUri())
.accept(HAL_JSON)
.retrieve()
.body(new CollectionModelType<Store>() {});

stores.getContent().forEach(store -> log.info("{} - {}", store.name, store.address));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit d2f78ec

Please sign in to comment.