diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/HotelRepository.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/HotelRepository.java deleted file mode 100644 index 988105f33d..0000000000 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/HotelRepository.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2012-2013 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 - * - * http://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 org.springframework.boot.sample.data.jpa.domain.repository; - -import org.springframework.boot.sample.data.jpa.domain.City; -import org.springframework.boot.sample.data.jpa.domain.Hotel; -import org.springframework.data.repository.Repository; - -public interface HotelRepository extends Repository { - - Hotel findByCityAndName(City city, String name); - -} diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/HotelSummaryRepository.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/HotelSummaryRepository.java deleted file mode 100644 index 01c1a0c170..0000000000 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/HotelSummaryRepository.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2012-2013 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 - * - * http://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 org.springframework.boot.sample.data.jpa.domain.repository; - -import java.util.List; -import java.util.Locale; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; - -import org.springframework.boot.sample.data.jpa.domain.City; -import org.springframework.boot.sample.data.jpa.domain.Hotel; -import org.springframework.boot.sample.data.jpa.domain.HotelSummary; -import org.springframework.boot.sample.data.jpa.domain.RatingCount; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; -import org.springframework.data.domain.Sort.Order; -import org.springframework.stereotype.Repository; - -@Repository -public class HotelSummaryRepository { - - private static final String AVERAGE_REVIEW_FUNCTION = "avg(r.rating)"; - - private static final String FIND_BY_CITY_QUERY = "select new " - + HotelSummary.class.getName() + "(h.city, h.name, " - + AVERAGE_REVIEW_FUNCTION - + ") from Hotel h left outer join h.reviews r where h.city = ?1 group by h"; - - private static final String FIND_BY_CITY_COUNT_QUERY = "select count(h) from Hotel h where h.city = ?1"; - - private static final String FIND_RATING_COUNTS_QUERY = "select new " - + RatingCount.class.getName() + "(r.rating, count(r)) " - + "from Review r where r.hotel = ?1 group by r.rating order by r.rating DESC"; - - private EntityManager entityManager; - - public Page findByCity(City city, Pageable pageable) { - StringBuilder queryString = new StringBuilder(FIND_BY_CITY_QUERY); - applySorting(queryString, pageable == null ? null : pageable.getSort()); - - Query query = this.entityManager.createQuery(queryString.toString()); - query.setParameter(1, city); - query.setFirstResult(pageable.getOffset()); - query.setMaxResults(pageable.getPageSize()); - - Query countQuery = this.entityManager.createQuery(FIND_BY_CITY_COUNT_QUERY); - countQuery.setParameter(1, city); - - @SuppressWarnings("unchecked") - List content = query.getResultList(); - - Long total = (Long) countQuery.getSingleResult(); - - return new PageImpl(content, pageable, total); - } - - @SuppressWarnings("unchecked") - public List findRatingCounts(Hotel hotel) { - Query query = this.entityManager.createQuery(FIND_RATING_COUNTS_QUERY); - query.setParameter(1, hotel); - return query.getResultList(); - } - - private void applySorting(StringBuilder query, Sort sort) { - if (sort != null) { - query.append(" order by"); - for (Order order : sort) { - String aliasedProperty = getAliasedProperty(order.getProperty()); - query.append(String.format(" %s %s,", aliasedProperty, order - .getDirection().name().toLowerCase(Locale.US))); - } - query.deleteCharAt(query.length() - 1); - } - } - - private String getAliasedProperty(String property) { - if (property.equals("averageRating")) { - return AVERAGE_REVIEW_FUNCTION; - } - return "h." + property; - } - - @PersistenceContext - public void setEntityManager(EntityManager entityManager) { - this.entityManager = entityManager; - } - -} diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/CityRepository.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/CityRepository.java similarity index 79% rename from spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/CityRepository.java rename to spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/CityRepository.java index 24468039f1..1215913794 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/CityRepository.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/CityRepository.java @@ -14,19 +14,19 @@ * limitations under the License. */ -package org.springframework.boot.sample.data.jpa.domain.repository; +package org.springframework.boot.sample.data.jpa.service; import org.springframework.boot.sample.data.jpa.domain.City; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.Repository; -public interface CityRepository extends Repository { +interface CityRepository extends Repository { Page findAll(Pageable pageable); - Page findByNameLikeAndCountryLikeAllIgnoringCase(String name, String country, - Pageable pageable); + Page findByNameContainingAndCountryContainingAllIgnoringCase(String name, + String country, Pageable pageable); City findByNameAndCountryAllIgnoringCase(String name, String country); diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/impl/CityServiceImpl.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/CityServiceImpl.java similarity index 67% rename from spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/impl/CityServiceImpl.java rename to spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/CityServiceImpl.java index add3b2afbb..e1f7edaf35 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/impl/CityServiceImpl.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/CityServiceImpl.java @@ -14,15 +14,11 @@ * limitations under the License. */ -package org.springframework.boot.sample.data.jpa.service.impl; +package org.springframework.boot.sample.data.jpa.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.sample.data.jpa.domain.City; import org.springframework.boot.sample.data.jpa.domain.HotelSummary; -import org.springframework.boot.sample.data.jpa.domain.repository.CityRepository; -import org.springframework.boot.sample.data.jpa.domain.repository.HotelSummaryRepository; -import org.springframework.boot.sample.data.jpa.service.CitySearchCriteria; -import org.springframework.boot.sample.data.jpa.service.CityService; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; @@ -32,31 +28,41 @@ import org.springframework.util.StringUtils; @Component("cityService") @Transactional -public class CityServiceImpl implements CityService { +class CityServiceImpl implements CityService { // FIXME deal with null repository return values - private CityRepository cityRepository; + private final CityRepository cityRepository; - private HotelSummaryRepository hotelSummaryRepository; + private final HotelRepository hotelRepository; + + @Autowired + public CityServiceImpl(CityRepository cityRepository, HotelRepository hotelRepository) { + this.cityRepository = cityRepository; + this.hotelRepository = hotelRepository; + } @Override public Page findCities(CitySearchCriteria criteria, Pageable pageable) { + Assert.notNull(criteria, "Criteria must not be null"); String name = criteria.getName(); + if (!StringUtils.hasLength(name)) { return this.cityRepository.findAll(null); } + String country = ""; int splitPos = name.lastIndexOf(","); + if (splitPos >= 0) { country = name.substring(splitPos + 1); name = name.substring(0, splitPos); } - name = "%" + name.trim() + "%"; - country = "%" + country.trim() + "%"; - return this.cityRepository.findByNameLikeAndCountryLikeAllIgnoringCase(name, - country, pageable); + + return this.cityRepository + .findByNameContainingAndCountryContainingAllIgnoringCase(name.trim(), + country.trim(), pageable); } @Override @@ -69,16 +75,6 @@ public class CityServiceImpl implements CityService { @Override public Page getHotels(City city, Pageable pageable) { Assert.notNull(city, "City must not be null"); - return this.hotelSummaryRepository.findByCity(city, pageable); - } - - @Autowired - public void setCityRepository(CityRepository cityRepository) { - this.cityRepository = cityRepository; - } - - @Autowired - public void setHotelSummaryRepository(HotelSummaryRepository hotelSummaryRepository) { - this.hotelSummaryRepository = hotelSummaryRepository; + return this.hotelRepository.findByCity(city, pageable); } } diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelRepository.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelRepository.java new file mode 100644 index 0000000000..1ddd4f9ec9 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelRepository.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2013 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 + * + * http://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 org.springframework.boot.sample.data.jpa.service; + +import java.util.List; + +import org.springframework.boot.sample.data.jpa.domain.City; +import org.springframework.boot.sample.data.jpa.domain.Hotel; +import org.springframework.boot.sample.data.jpa.domain.HotelSummary; +import org.springframework.boot.sample.data.jpa.domain.RatingCount; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; + +interface HotelRepository extends Repository { + + Hotel findByCityAndName(City city, String name); + + @Query("select new org.springframework.boot.sample.data.jpa.domain.HotelSummary(h.city, h.name, avg(r.rating)) " + + "from Hotel h left outer join h.reviews r where h.city = ?1 group by h") + Page findByCity(City city, Pageable pageable); + + @Query("select new org.springframework.boot.sample.data.jpa.domain.RatingCount(r.rating, count(r)) " + + "from Review r where r.hotel = ?1 group by r.rating order by r.rating DESC") + List findRatingCounts(Hotel hotel); +} diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/impl/HotelServiceImpl.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelServiceImpl.java similarity index 73% rename from spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/impl/HotelServiceImpl.java rename to spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelServiceImpl.java index d7a73c2370..14918cc7f6 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/impl/HotelServiceImpl.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelServiceImpl.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.sample.data.jpa.service.impl; +package org.springframework.boot.sample.data.jpa.service; import java.util.HashMap; import java.util.List; @@ -27,11 +27,6 @@ import org.springframework.boot.sample.data.jpa.domain.Rating; import org.springframework.boot.sample.data.jpa.domain.RatingCount; import org.springframework.boot.sample.data.jpa.domain.Review; import org.springframework.boot.sample.data.jpa.domain.ReviewDetails; -import org.springframework.boot.sample.data.jpa.domain.repository.HotelRepository; -import org.springframework.boot.sample.data.jpa.domain.repository.HotelSummaryRepository; -import org.springframework.boot.sample.data.jpa.domain.repository.ReviewRepository; -import org.springframework.boot.sample.data.jpa.service.HotelService; -import org.springframework.boot.sample.data.jpa.service.ReviewsSummary; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; @@ -40,15 +35,20 @@ import org.springframework.util.Assert; @Component("hotelService") @Transactional -public class HotelServiceImpl implements HotelService { +class HotelServiceImpl implements HotelService { // FIXME deal with null repository return values - private HotelRepository hotelRepository; + private final HotelRepository hotelRepository; - private HotelSummaryRepository hotelSummaryRepository; + private final ReviewRepository reviewRepository; - private ReviewRepository reviewRepository; + @Autowired + public HotelServiceImpl(HotelRepository hotelRepository, + ReviewRepository reviewRepository) { + this.hotelRepository = hotelRepository; + this.reviewRepository = reviewRepository; + } @Override public Hotel getHotel(City city, String name) { @@ -78,26 +78,10 @@ public class HotelServiceImpl implements HotelService { @Override public ReviewsSummary getReviewSummary(Hotel hotel) { - List ratingCounts = this.hotelSummaryRepository - .findRatingCounts(hotel); + List ratingCounts = this.hotelRepository.findRatingCounts(hotel); return new ReviewsSummaryImpl(ratingCounts); } - @Autowired - public void setHotelRepository(HotelRepository hotelRepository) { - this.hotelRepository = hotelRepository; - } - - @Autowired - public void setHotelSummaryRepository(HotelSummaryRepository hotelSummaryRepository) { - this.hotelSummaryRepository = hotelSummaryRepository; - } - - @Autowired - public void setReviewRepository(ReviewRepository reviewRepository) { - this.reviewRepository = reviewRepository; - } - private static class ReviewsSummaryImpl implements ReviewsSummary { private Map ratingCount; diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/ReviewRepository.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/ReviewRepository.java similarity index 88% rename from spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/ReviewRepository.java rename to spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/ReviewRepository.java index 508b7fb6ac..37a9858c40 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/domain/repository/ReviewRepository.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/ReviewRepository.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.sample.data.jpa.domain.repository; +package org.springframework.boot.sample.data.jpa.service; import org.springframework.boot.sample.data.jpa.domain.Hotel; import org.springframework.boot.sample.data.jpa.domain.Review; @@ -22,7 +22,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.Repository; -public interface ReviewRepository extends Repository { +interface ReviewRepository extends Repository { Page findByHotel(Hotel hotel, Pageable pageable); diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/ReviewsSummary.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/ReviewsSummary.java index dc56bfa58d..80df2b5ea7 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/ReviewsSummary.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/ReviewsSummary.java @@ -20,6 +20,6 @@ import org.springframework.boot.sample.data.jpa.domain.Rating; public interface ReviewsSummary { - public long getNumberOfReviewsWithRating(Rating rating); + long getNumberOfReviewsWithRating(Rating rating); } diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/AbstractIntegrationTests.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/AbstractIntegrationTests.java new file mode 100644 index 0000000000..4d574ea569 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/AbstractIntegrationTests.java @@ -0,0 +1,36 @@ +/* + * Copyright 2013 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 + * + * http://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 org.springframework.boot.sample.data.jpa; + +import org.junit.runner.RunWith; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +/** + * Base class for integration tests. Mimics the behavior of + * {@link SpringApplication#run(String...)}. + * + * @author Oliver Gierke + */ +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = SampleDataJpaApplication.class, initializers = ConfigFileApplicationContextInitializer.class) +public abstract class AbstractIntegrationTests { + +} diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java index d5cc937ab2..fbc1a23e80 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java @@ -1,87 +1,37 @@ -/* - * Copyright 2012-2013 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 - * - * http://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 org.springframework.boot.sample.data.jpa; -import java.io.IOException; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; - -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; -import org.springframework.boot.SpringApplication; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.web.client.DefaultResponseErrorHandler; -import org.springframework.web.client.RestTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; -import static org.junit.Assert.assertEquals; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** - * Basic integration tests for service demo application. + * Integration test to run the application. * - * @author Dave Syer + * @author Oliver Gierke */ -public class SampleDataJpaApplicationTests { +public class SampleDataJpaApplicationTests extends AbstractIntegrationTests { - private static ConfigurableApplicationContext context; + @Autowired + private WebApplicationContext context; - @BeforeClass - public static void start() throws Exception { - Future future = Executors - .newSingleThreadExecutor().submit( - new Callable() { - @Override - public ConfigurableApplicationContext call() throws Exception { - return (ConfigurableApplicationContext) SpringApplication - .run(SampleDataJpaApplication.class); - } - }); - context = future.get(30, TimeUnit.SECONDS); - } + private MockMvc mvc; - @AfterClass - public static void stop() { - if (context != null) { - context.close(); - } + @Before + public void setUp() { + this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); } @Test public void testHome() throws Exception { - ResponseEntity entity = getRestTemplate().getForEntity( - "http://localhost:8080", String.class); - assertEquals(HttpStatus.OK, entity.getStatusCode()); - assertEquals("Bath", entity.getBody()); - } - - private RestTemplate getRestTemplate() { - RestTemplate restTemplate = new RestTemplate(); - restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { - @Override - public void handleError(ClientHttpResponse response) throws IOException { - } - }); - return restTemplate; + this.mvc.perform(get("/")).andExpect(status().isOk()) + .andExpect(content().string("Bath")); } - } diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/CityRepositoryIntegrationTests.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/CityRepositoryIntegrationTests.java new file mode 100644 index 0000000000..f40848f955 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/CityRepositoryIntegrationTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2013 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 + * + * http://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 org.springframework.boot.sample.data.jpa.service; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.sample.data.jpa.AbstractIntegrationTests; +import org.springframework.boot.sample.data.jpa.domain.City; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * Integration tests for {@link CityRepository}. + * + * @author Oliver Gierke + */ +public class CityRepositoryIntegrationTests extends AbstractIntegrationTests { + + @Autowired + CityRepository repository; + + @Test + public void findsFirstPageOfCities() { + + Page cities = this.repository.findAll(new PageRequest(0, 10)); + assertThat(cities.getTotalElements(), is(21L)); + } +} diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/HotelRepositoryIntegrationTests.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/HotelRepositoryIntegrationTests.java new file mode 100644 index 0000000000..295670c172 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/HotelRepositoryIntegrationTests.java @@ -0,0 +1,66 @@ +/* + * Copyright 2013 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 + * + * http://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 org.springframework.boot.sample.data.jpa.service; + +import java.util.List; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.sample.data.jpa.AbstractIntegrationTests; +import org.springframework.boot.sample.data.jpa.domain.City; +import org.springframework.boot.sample.data.jpa.domain.Hotel; +import org.springframework.boot.sample.data.jpa.domain.HotelSummary; +import org.springframework.boot.sample.data.jpa.domain.Rating; +import org.springframework.boot.sample.data.jpa.domain.RatingCount; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort.Direction; + +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +/** + * Integration tests for {@link HotelRepository}. + * + * @author Oliver Gierke + */ +public class HotelRepositoryIntegrationTests extends AbstractIntegrationTests { + + @Autowired + CityRepository cityRepository; + @Autowired + HotelRepository repository; + + @Test + public void executesQueryMethodsCorrectly() { + City city = this.cityRepository + .findAll(new PageRequest(0, 1, Direction.ASC, "name")).getContent() + .get(0); + assertThat(city.getName(), is("Atlanta")); + + Page hotels = this.repository.findByCity(city, new PageRequest(0, + 10, Direction.ASC, "name")); + Hotel hotel = this.repository.findByCityAndName(city, hotels.getContent().get(0) + .getName()); + assertThat(hotel.getName(), is("Doubletree")); + + List counts = this.repository.findRatingCounts(hotel); + assertThat(counts, hasSize(1)); + assertThat(counts.get(0).getRating(), is(Rating.AVERAGE)); + assertThat(counts.get(0).getCount(), is(2L)); + } +}