@@ -26,17 +26,19 @@ import java.util.UUID;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Reference;
|
||||
import org.springframework.data.rest.core.config.Projection;
|
||||
import org.springframework.data.rest.tests.shop.Product.ProductNameOnlyProjection;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Craig Andrews
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(of = "id")
|
||||
public class LineItem {
|
||||
|
||||
@Projection(name = "productsOnly", types = { LineItem.class })
|
||||
@Projection(name = "productsOnly", types = LineItem.class)
|
||||
public interface LineItemProductsOnlyProjection {
|
||||
List<Product.ProductNameOnlyProjection> getProducts();
|
||||
List<ProductNameOnlyProjection> getProducts();
|
||||
}
|
||||
|
||||
private final @Id UUID id = UUID.randomUUID();
|
||||
|
||||
@@ -24,16 +24,18 @@ import java.util.UUID;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Reference;
|
||||
import org.springframework.data.rest.core.config.Projection;
|
||||
import org.springframework.data.rest.tests.shop.LineItem.LineItemProductsOnlyProjection;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Craig Andrews
|
||||
*/
|
||||
@Value
|
||||
public class Order {
|
||||
|
||||
@Projection(name = "itemsOnly", types = { Order.class })
|
||||
|
||||
@Projection(name = "itemsOnly", types = Order.class)
|
||||
public interface OrderItemsOnlyProjection {
|
||||
List<LineItem.LineItemProductsOnlyProjection> getItems();
|
||||
List<LineItemProductsOnlyProjection> getItems();
|
||||
}
|
||||
|
||||
private final @Id UUID id = UUID.randomUUID();
|
||||
|
||||
@@ -26,13 +26,13 @@ import org.springframework.data.rest.core.config.Projection;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Craig Andrews
|
||||
*/
|
||||
|
||||
@Value
|
||||
@RequiredArgsConstructor
|
||||
public class Product {
|
||||
|
||||
@Projection(name = "nameOnly", types = { Product.class })
|
||||
@Projection(name = "nameOnly", types = Product.class)
|
||||
public interface ProductNameOnlyProjection {
|
||||
String getName();
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ 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.tests.shop.Product.ProductNameOnlyProjection;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.Resource;
|
||||
@@ -59,15 +60,16 @@ public class ShopConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResourceProcessor<Resource<Product.ProductNameOnlyProjection>> productNameOnlyProjectionResourceProcessor() {
|
||||
return new ResourceProcessor<Resource<Product.ProductNameOnlyProjection>>() {
|
||||
public ResourceProcessor<Resource<ProductNameOnlyProjection>> productNameOnlyProjectionResourceProcessor() {
|
||||
|
||||
return new ResourceProcessor<Resource<ProductNameOnlyProjection>>() {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.ResourceProcessor#process(org.springframework.hateoas.ResourceSupport)
|
||||
*/
|
||||
@Override
|
||||
public Resource<Product.ProductNameOnlyProjection> process(Resource<Product.ProductNameOnlyProjection> resource) {
|
||||
public Resource<ProductNameOnlyProjection> process(Resource<Product.ProductNameOnlyProjection> resource) {
|
||||
resource.add(new Link("alpha", "beta"));
|
||||
return resource;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.data.rest.tests.shop;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -32,7 +32,10 @@ import org.springframework.test.web.servlet.ResultActions;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
/**
|
||||
* Integration tests for projections.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Craig Andrews
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = ShopConfiguration.class)
|
||||
@@ -59,6 +62,47 @@ public class ShopIntegrationTests extends AbstractWebIntegrationTests {
|
||||
expectRelatedResource("customer", actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-221
|
||||
*/
|
||||
@Test
|
||||
public void renderProductNameOnlyProjection() throws Exception {
|
||||
|
||||
Map<String, Object> arguments = Collections.singletonMap("projection", "nameOnly");
|
||||
|
||||
client.follow(client.discoverUnique("products").expand(arguments))//
|
||||
.andExpect(status().isOk())//
|
||||
.andExpect(jsonPath("$._embedded.products[0].name", notNullValue()))//
|
||||
.andExpect(jsonPath("$._embedded.products[0].price").doesNotExist());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-221
|
||||
*/
|
||||
@Test
|
||||
public void renderProductNameOnlyProjectionResourceProcessor() throws Exception {
|
||||
|
||||
Map<String, Object> arguments = Collections.singletonMap("projection", "nameOnly");
|
||||
|
||||
client.follow(client.discoverUnique("products").expand(arguments))//
|
||||
.andExpect(status().isOk())//
|
||||
.andExpect(jsonPath("$._embedded.products[0]._links.beta").exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-221
|
||||
*/
|
||||
@Test
|
||||
public void renderOrderItemsOnlyProjectionResourceProcessor() throws Exception {
|
||||
|
||||
Map<String, Object> arguments = Collections.singletonMap("projection", "itemsOnly");
|
||||
|
||||
client.follow(client.discoverUnique("orders").expand(arguments))//
|
||||
.andExpect(status().isOk())//
|
||||
.andExpect(jsonPath("$._embedded.orders[0].items[0].products[0].name").exists())//
|
||||
.andExpect(jsonPath("$._embedded.orders[0].items[0].products[0]._links.beta").exists());
|
||||
}
|
||||
|
||||
private static void expectRelatedResource(String name, ResultActions actions) throws Exception {
|
||||
|
||||
int dotIndex = name.lastIndexOf('.');
|
||||
@@ -70,36 +114,4 @@ public class ShopIntegrationTests extends AbstractWebIntegrationTests {
|
||||
actions.andExpect(jsonPath(prefix.concat("_links.").concat(suffix)).exists());
|
||||
actions.andExpect(jsonPath(prefix.concat("_embedded.").concat(suffix)).exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderProductNameOnlyProjection() throws Exception {
|
||||
Map<String, Object> arguments = new HashMap<>();
|
||||
arguments.put("projection", "nameOnly");
|
||||
client.follow(client.discoverUnique("products").expand(arguments))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.products[0].name", notNullValue()))
|
||||
.andExpect(jsonPath("$._embedded.products[0].price").doesNotExist());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderProductNameOnlyProjectionResourceProcessor() throws Exception {
|
||||
Map<String, Object> arguments = new HashMap<>();
|
||||
arguments.put("projection", "nameOnly");
|
||||
client.follow(client.discoverUnique("products").expand(arguments))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.products[0]._links.beta").exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-221
|
||||
*/
|
||||
@Test
|
||||
public void renderOrderItemsOnlyProjectionResourceProcessor() throws Exception {
|
||||
Map<String, Object> arguments = new HashMap<>();
|
||||
arguments.put("projection", "itemsOnly");
|
||||
client.follow(client.discoverUnique("orders").expand(arguments))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.orders[0].items[0].products[0].name").exists())
|
||||
.andExpect(jsonPath("$._embedded.orders[0].items[0].products[0]._links.beta").exists());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user