From 2be6b2b6a9c6521e7f01277c338e4294f6ba616f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 29 Feb 2016 19:01:52 +0100 Subject: [PATCH] DATAREST-776, DATAREST-775 - Integration tests for lookup types and nested associations. Added shop integration test example that shows and tests nested associations and both unique and collection lookup type references. --- spring-data-rest-tests/pom.xml | 2 + .../spring-data-rest-tests-shop/pom.xml | 31 ++++++ .../data/rest/tests/shop/Address.java | 42 ++++++++ .../data/rest/tests/shop/Customer.java | 40 ++++++++ .../data/rest/tests/shop/CustomerExcerpt.java | 33 +++++++ .../rest/tests/shop/CustomerRepository.java | 29 ++++++ .../data/rest/tests/shop/LineItem.java | 57 +++++++++++ .../data/rest/tests/shop/LineItemType.java | 38 ++++++++ .../tests/shop/LineItemTypeRepository.java | 29 ++++++ .../data/rest/tests/shop/Order.java | 42 ++++++++ .../data/rest/tests/shop/OrderRepository.java | 27 ++++++ .../data/rest/tests/shop/Product.java | 37 ++++++++ .../data/rest/tests/shop/ProductExcerpt.java | 24 +++++ .../rest/tests/shop/ProductRepository.java | 30 ++++++ .../rest/tests/shop/ShopConfiguration.java | 95 +++++++++++++++++++ .../rest/tests/shop/ShopIntegrationTests.java | 70 ++++++++++++++ 16 files changed, 626 insertions(+) create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/pom.xml create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Address.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Customer.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/CustomerExcerpt.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/CustomerRepository.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItem.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItemType.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItemTypeRepository.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Order.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/OrderRepository.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Product.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/ProductExcerpt.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/ProductRepository.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/test/java/org/springframework/data/rest/tests/shop/ShopConfiguration.java create mode 100644 spring-data-rest-tests/spring-data-rest-tests-shop/src/test/java/org/springframework/data/rest/tests/shop/ShopIntegrationTests.java diff --git a/spring-data-rest-tests/pom.xml b/spring-data-rest-tests/pom.xml index 7e2c1c835..859eba82d 100644 --- a/spring-data-rest-tests/pom.xml +++ b/spring-data-rest-tests/pom.xml @@ -19,11 +19,13 @@ spring-data-rest-tests-jpa spring-data-rest-tests-mongodb spring-data-rest-tests-security + spring-data-rest-tests-shop spring-data-rest-tests-solr 2.4.4 + 1.8 diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/pom.xml b/spring-data-rest-tests/spring-data-rest-tests-shop/pom.xml new file mode 100644 index 000000000..eea0d8253 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + + + org.springframework.data + spring-data-rest-tests + 2.5.0.BUILD-SNAPSHOT + + + Spring Data REST Tests - Shop + spring-data-rest-tests-shop + + + + + ${project.groupId} + spring-data-rest-tests-core + ${project.version} + test-jar + + + + org.springframework.data + spring-data-keyvalue + ${springdata.keyvalue} + + + + + diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Address.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Address.java new file mode 100644 index 000000000..1abfc2a1f --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Address.java @@ -0,0 +1,42 @@ +/* + * Copyright 2014-2016 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.data.rest.tests.shop; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +import java.util.UUID; + +import org.springframework.data.annotation.Id; + +/** + * @author Oliver Gierke + */ +@Data +@RequiredArgsConstructor +public class Address { + + private @Id UUID id = UUID.randomUUID(); + private final String street, zipCode, city, state; + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() { + return String.format("%s, %s %s, %s", street, zipCode, city, state); + } +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Customer.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Customer.java new file mode 100644 index 000000000..bae23d2ab --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Customer.java @@ -0,0 +1,40 @@ +/* + * Copyright 2014-2016 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.data.rest.tests.shop; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +import java.util.UUID; + +import org.springframework.data.annotation.Id; + +/** + * @author Oliver Gierke + */ +@Data +@RequiredArgsConstructor +public class Customer { + + private final @Id UUID id = UUID.randomUUID(); + private final String firstname, lastname; + private final Gender gender; + private final Address address; + + static enum Gender { + MALE, FEMALE; + } +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/CustomerExcerpt.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/CustomerExcerpt.java new file mode 100644 index 000000000..72120b6f3 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/CustomerExcerpt.java @@ -0,0 +1,33 @@ +/* + * Copyright 2014-2016 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.data.rest.tests.shop; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.rest.core.config.Projection; + +/** + * @author Oliver Gierke + */ +@Projection(name = "excerpt", types = Customer.class) +public interface CustomerExcerpt { + + String getFirstname(); + + String getLastname(); + + @Value("#{target.address.toString()}") + String getAddress(); +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/CustomerRepository.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/CustomerRepository.java new file mode 100644 index 000000000..6c2c3bdd1 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/CustomerRepository.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014-2016 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.data.rest.tests.shop; + +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +/** + * @author Oliver Gierke + */ +@RepositoryRestResource(excerptProjection = CustomerExcerpt.class) +public interface CustomerRepository extends CrudRepository { + +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItem.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItem.java new file mode 100644 index 000000000..007d7a7ba --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItem.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014-2016 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.data.rest.tests.shop; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Reference; + +/** + * @author Oliver Gierke + */ +@Data +@EqualsAndHashCode(of = "id") +public class LineItem { + + private final @Id UUID id = UUID.randomUUID(); + private final String description; + private final BigDecimal price; + + private final @Reference Product product; + private final @Reference List products; + + private final @Reference LineItemType type; + private final @Reference List types; + + public LineItem(Product product, LineItemType type) { + + this.price = product.getPrice(); + this.description = product.getName(); + + this.type = type; + this.types = Arrays.asList(type, type); + + this.product = product; + this.products = Arrays.asList(product, product); + } +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItemType.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItemType.java new file mode 100644 index 000000000..8a358d9d5 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItemType.java @@ -0,0 +1,38 @@ +/* + * Copyright 2016 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.data.rest.tests.shop; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.ToString; + +import java.util.UUID; + +import org.springframework.data.annotation.Id; + +/** + * @author Oliver Gierke + */ +@Getter +@EqualsAndHashCode(of = "id") +@ToString +@RequiredArgsConstructor +public class LineItemType { + + private final @Id UUID id = UUID.randomUUID(); + private final String name; +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItemTypeRepository.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItemTypeRepository.java new file mode 100644 index 000000000..a2b196a4e --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/LineItemTypeRepository.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016 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.data.rest.tests.shop; + +import java.util.Optional; +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; + +/** + * @author Oliver Gierke + */ +public interface LineItemTypeRepository extends CrudRepository { + + Optional findByName(String name); +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Order.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Order.java new file mode 100644 index 000000000..09c4d390c --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Order.java @@ -0,0 +1,42 @@ +/* + * Copyright 2014-2016 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.data.rest.tests.shop; + +import lombok.Value; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Reference; + +/** + * @author Oliver Gierke + */ +@Value +public class Order { + + private final @Id UUID id = UUID.randomUUID(); + private final List items = new ArrayList<>(); + private final @Reference Customer customer; + + public Order add(LineItem item) { + + this.items.add(item); + return this; + } +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/OrderRepository.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/OrderRepository.java new file mode 100644 index 000000000..c1c96d143 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/OrderRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright 2014-2016 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.data.rest.tests.shop; + +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; + +/** + * @author Oliver Gierke + */ +public interface OrderRepository extends CrudRepository { + +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Product.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Product.java new file mode 100644 index 000000000..a2c2bb0f9 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/Product.java @@ -0,0 +1,37 @@ +/* + * Copyright 2016 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.data.rest.tests.shop; + +import lombok.RequiredArgsConstructor; +import lombok.Value; + +import java.math.BigDecimal; +import java.util.UUID; + +import org.springframework.data.annotation.Id; + +/** + * @author Oliver Gierke + */ + +@Value +@RequiredArgsConstructor +public class Product { + + private final @Id UUID id = UUID.randomUUID(); + private final String name; + private final BigDecimal price; +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/ProductExcerpt.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/ProductExcerpt.java new file mode 100644 index 000000000..28aa5fef6 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/ProductExcerpt.java @@ -0,0 +1,24 @@ +/* + * Copyright 2016 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.data.rest.tests.shop; + +/** + * @author Oliver Gierke + */ +interface ProductExcerpt { + + String getName(); +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/ProductRepository.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/ProductRepository.java new file mode 100644 index 000000000..b320f0093 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/main/java/org/springframework/data/rest/tests/shop/ProductRepository.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016 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.data.rest.tests.shop; + +import java.util.Optional; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +/** + * @author Oliver Gierke + */ +@RepositoryRestResource(excerptProjection = Product.class) +public interface ProductRepository extends CrudRepository { + + Optional findByName(String name); +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/test/java/org/springframework/data/rest/tests/shop/ShopConfiguration.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/test/java/org/springframework/data/rest/tests/shop/ShopConfiguration.java new file mode 100644 index 000000000..239ff9bdb --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/test/java/org/springframework/data/rest/tests/shop/ShopConfiguration.java @@ -0,0 +1,95 @@ +/* + * Copyright 2014-2016 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.data.rest.tests.shop; + +import java.math.BigDecimal; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.map.repository.config.EnableMapRepositories; +import org.springframework.data.rest.core.config.RepositoryRestConfiguration; +import org.springframework.data.rest.tests.shop.Customer.Gender; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.Resource; +import org.springframework.hateoas.ResourceProcessor; + +/** + * @author Oliver Gierke + */ +@Configuration +@EnableMapRepositories +public class ShopConfiguration { + + @Autowired CustomerRepository customers; + @Autowired OrderRepository orders; + @Autowired ProductRepository products; + @Autowired LineItemTypeRepository lineItemTypes; + + @Bean + public ResourceProcessor> lineItemResourceProcessor() { + return new ResourceProcessor>() { + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.ResourceProcessor#process(org.springframework.hateoas.ResourceSupport) + */ + @Override + public Resource process(Resource resource) { + resource.add(new Link("foo", "bar")); + return resource; + } + }; + } + + @PostConstruct + public void init() { + + LineItemType lineItemType = lineItemTypes.save(new LineItemType("good")); + + Product guitar = products.save(new Product("Lakewood guitar", new BigDecimal(1299.0))); + Product drums = products.save(new Product("Yamaha Drums", new BigDecimal(2999.0))); + + Customer dave = customers.save(new Customer("Dave", "Matthews", Gender.MALE, // + new Address("4711 Some Place", "54321", "Charlottesville", "VA"))); + + Order order = new Order(dave); + + order.add(new LineItem(guitar, lineItemType)); + order.add(new LineItem(drums, lineItemType)); + + orders.save(order); + } + + @Configuration + static class SpringDataRestConfiguration extends RepositoryRestConfigurerAdapter { + + /* + * (non-Javadoc) + * @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration) + */ + @Override + public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { + + config.withEntityLookup().forRepository(ProductRepository.class, Product::getName, ProductRepository::findByName); + config.withEntityLookup().forValueRepository(LineItemTypeRepository.class, LineItemType::getName, + LineItemTypeRepository::findByName); + } + } +} diff --git a/spring-data-rest-tests/spring-data-rest-tests-shop/src/test/java/org/springframework/data/rest/tests/shop/ShopIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-shop/src/test/java/org/springframework/data/rest/tests/shop/ShopIntegrationTests.java new file mode 100644 index 000000000..b91082360 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-shop/src/test/java/org/springframework/data/rest/tests/shop/ShopIntegrationTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2016 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.data.rest.tests.shop; + +import static org.hamcrest.CoreMatchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.data.rest.tests.AbstractWebIntegrationTests; +import org.springframework.hateoas.Link; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.web.servlet.ResultActions; + +import com.jayway.jsonpath.JsonPath; + +/** + * @author Oliver Gierke + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = ShopConfiguration.class) +public class ShopIntegrationTests extends AbstractWebIntegrationTests { + + @Test + public void rendersRepresentationCorrectly() throws Exception { + + Link ordersLink = client.discoverUnique("orders"); + String selfLink = JsonPath.parse(client.request(ordersLink).getContentAsString()) + .read("$._embedded.orders[0]._links.self.href"); + + ResultActions actions = client.follow(selfLink) + // Lookup type is rendered as String + .andExpect(jsonPath("items[0].type", is("good")))// + // Invokes ResourceProcessor for nested Resource + .andExpect(jsonPath("items[0]._links.bar").exists());// + + // Adds excerpt projection for related product + expectRelatedResource("items[0].product", actions); + expectRelatedResource("items[0].products", actions); + + // Adds excerpt projection for root level reference + expectRelatedResource("customer", actions); + } + + private static void expectRelatedResource(String name, ResultActions actions) throws Exception { + + int dotIndex = name.lastIndexOf('.'); + + String prefix = dotIndex == -1 ? "" : name.substring(0, dotIndex); + String suffix = dotIndex == -1 ? name : name.substring(dotIndex); + + actions.andExpect(jsonPath(prefix.concat(suffix)).doesNotExist()); + actions.andExpect(jsonPath(prefix.concat("_links.").concat(suffix)).exists()); + actions.andExpect(jsonPath(prefix.concat("_embedded.").concat(suffix)).exists()); + } +}