Skip to content

Commit

Permalink
Update gradle-plugin version (#269) (#270)
Browse files Browse the repository at this point in the history
* Update gradle-plugin version (#269)

* Update spring poot version to 3.0.0 in the sample project

* Use jre guava version

* Removed line with System.out.println

* Remove unused h2 database config

---------

Co-authored-by: Wojciech Odrobina <wodrobina@epages.com>
  • Loading branch information
markruler and wodrobina authored Sep 27, 2024
1 parent 88d78d5 commit 9023099
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 88 deletions.
11 changes: 6 additions & 5 deletions samples/restdocs-api-spec-sample-web-test-client/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
buildscript {
ext {
springBootVersion = '2.1.9.RELEASE'
springBootVersion = '3.0.0'
}
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("com.epages:restdocs-api-spec-gradle-plugin:0.9.5")
classpath("com.epages:restdocs-api-spec-gradle-plugin:0.19.2")
}
}

Expand All @@ -21,8 +21,8 @@ repositories {
mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceCompatibility = 17
targetCompatibility = 17

ext {
snippetsDir = file('build/generated-snippets')
Expand All @@ -35,7 +35,8 @@ dependencies {
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.restdocs:spring-restdocs-webtestclient')
testImplementation('io.projectreactor:reactor-test')
testImplementation('com.epages:restdocs-api-spec:0.11.2')

testImplementation project(':restdocs-api-spec-mockmvc')
}

openapi {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
14 changes: 8 additions & 6 deletions samples/restdocs-api-spec-sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
buildscript {
ext {
springBootVersion = '2.1.9.RELEASE'
springBootVersion = '3.0.0'
}
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("com.epages:restdocs-api-spec-gradle-plugin:0.9.5")
classpath("com.epages:restdocs-api-spec-gradle-plugin:0.19.2")
}
}

Expand All @@ -17,7 +17,8 @@ apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.epages.restdocs-api-spec'

sourceCompatibility = 1.8
sourceCompatibility = 17
targetCompatibility = 17

repositories {
mavenCentral()
Expand All @@ -30,15 +31,16 @@ ext {
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-data-rest')
implementation('org.springframework.boot:spring-boot-starter-validation')
runtimeOnly('com.h2database:h2')

testImplementation('org.junit.jupiter:junit-jupiter-engine')

testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.restdocs:spring-restdocs-mockmvc')

testImplementation('com.epages:restdocs-api-spec-mockmvc:0.11.2')
// testImplementation project(':restdocs-api-spec-mockmvc') //enable for depending on the submodule directly
testImplementation('com.google.guava:guava:23.0')
testImplementation project(':restdocs-api-spec-mockmvc') // enable for depending on the submodule directly
testImplementation('com.google.guava:guava:33.3.1-jre')
}

configurations.all {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
package com.epages.restdocs.apispec.sample;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.hateoas.Identifiable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.hateoas.RepresentationModel;

import static jakarta.persistence.GenerationType.IDENTITY;
import static java.math.BigDecimal.ZERO;
import static javax.persistence.GenerationType.IDENTITY;

@Entity
public class Cart implements Identifiable<Long> {
public class Cart extends RepresentationModel<Cart> {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@ManyToMany
@ManyToMany(fetch = FetchType.EAGER)
private List<Product> products = new ArrayList<>();

@JsonIgnore
private boolean ordered;

@Override
protected Cart() {
}

public Cart(List<Product> products) {
this.products = products;
}

public Long getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.epages.restdocs.apispec.sample;

import java.util.Optional;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.Resources;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import static org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE;

@RepositoryRestController
@RequestMapping("/carts")
@BasePathAwareController("/carts")
public class CartController {

private final CartRepository cartRepository;
Expand All @@ -32,14 +36,14 @@ public CartController(CartRepository cartRepository, ProductRepository productRe
@PostMapping
public ResponseEntity<CartResourceResourceAssembler.CartResource> create() {
Cart cart = cartRepository.save(new Cart());
return ResponseEntity.created(entityLinks.linkForSingleResource(cart).toUri())
.body(cartResourceResourceAssembler.toResource(cart));
return ResponseEntity.created(entityLinks.linkForItemResource(Cart.class, cart.getId()).toUri())
.body(cartResourceResourceAssembler.toModel(cart));
}

@GetMapping("/{cartId}")
public ResponseEntity<CartResourceResourceAssembler.CartResource> get(@PathVariable Long cartId) {
return cartRepository.findById(cartId)
.map(cartResourceResourceAssembler::toResource)
.map(cartResourceResourceAssembler::toModel)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
Expand All @@ -51,13 +55,13 @@ public ResponseEntity<CartResourceResourceAssembler.CartResource> order(@PathVar
cart.setOrdered(true);
return cartRepository.save(cart);
})
.map(cartResourceResourceAssembler::toResource)
.map(cartResourceResourceAssembler::toModel)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

@PostMapping(value = "/{cartId}/products", consumes = TEXT_URI_LIST_VALUE)
public ResponseEntity<CartResourceResourceAssembler.CartResource> addProducts(@PathVariable Long cartId, @RequestBody Resources<Object> resource) {
public ResponseEntity<CartResourceResourceAssembler.CartResource> addProducts(@PathVariable Long cartId, @RequestBody CollectionModel<Object> resource) {
return cartRepository.findById(cartId)
.map(cart -> {
resource.getLinks().stream()
Expand All @@ -69,7 +73,7 @@ public ResponseEntity<CartResourceResourceAssembler.CartResource> addProducts(@P
.forEach(product -> cart.getProducts().add(product));
return cartRepository.save(cart);
})
.map(cartResourceResourceAssembler::toResource)
.map(cartResourceResourceAssembler::toModel)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.epages.restdocs.apispec.sample;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(exported = false)
public interface CartRepository extends PagingAndSortingRepository<Cart, Long> {
@RepositoryRestResource
public interface CartRepository extends CrudRepository<Cart, Long>, PagingAndSortingRepository<Cart, Long> {

}
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package com.epages.restdocs.apispec.sample;

import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import jakarta.validation.constraints.NotNull;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport;
import org.springframework.stereotype.Component;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@Component
public class CartResourceResourceAssembler extends ResourceAssemblerSupport<Cart, CartResourceResourceAssembler.CartResource> {
public class CartResourceResourceAssembler extends RepresentationModelAssemblerSupport<Cart, CartResourceResourceAssembler.CartResource> {

private EntityLinks entityLinks;
private final EntityLinks entityLinks;

public CartResourceResourceAssembler(EntityLinks entityLinks) {
super(CartController.class, CartResource.class);
this.entityLinks = entityLinks;
}

@Override
protected CartResource instantiateResource(Cart cart) {
protected CartResource instantiateModel(Cart cart) {
return new CartResource(
cart.getProducts().stream()
.collect(Collectors.groupingBy(Product::getId)).values().stream()
Expand All @@ -35,52 +34,53 @@ protected CartResource instantiateResource(Cart cart) {
,cart.getTotal());
}

private Resource<ProductLineItem> toProductLineItemResource(ProductLineItem pli) {
Resource<ProductLineItem> resource = new Resource<>(pli);
resource.add(entityLinks.linkForSingleResource(pli.getProduct()).withRel("product"));
private EntityModel<ProductLineItem> toProductLineItemResource(ProductLineItem pli) {
EntityModel<ProductLineItem> resource = EntityModel.of(pli);
resource.add(entityLinks.linkForItemResource(Product.class, pli.getProduct().getId()).withRel("product"));
return resource;
}

@Override
public CartResource toResource(Cart cart) {
public CartResource toModel(Cart cart) {

CartResource resource = super.createResourceWithId(cart.getId(), cart);
CartResource resource = super.createModelWithId(cart.getId(), cart);

if (!cart.isOrdered()) {
resource.add(linkTo(methodOn(CartController.class).order(cart.getId())).withRel("order"));
}
return resource;
}

static class CartResource extends ResourceSupport {
static class CartResource extends RepresentationModel<CartResource> {

private final BigDecimal total;
private final List<EntityModel<ProductLineItem>> products;

public CartResource(List<Resource<ProductLineItem>> products, BigDecimal total) {
public CartResource(List<EntityModel<ProductLineItem>> products, BigDecimal total) {
this.total = total;
this.products = products;
}

private final BigDecimal total;
private final List<Resource<ProductLineItem>> products;

public BigDecimal getTotal() {
return this.total;
}

public List<Resource<ProductLineItem>> getProducts() {
public List<EntityModel<ProductLineItem>> getProducts() {
return this.products;
}
}

static class ProductLineItem extends ResourceSupport {
static class ProductLineItem extends RepresentationModel<ProductLineItem> {
private final int quantity;

@NotNull
private final Product product;

public ProductLineItem(int quantity, @NotNull Product product) {
this.quantity = quantity;
this.product = product;
}

private final int quantity;
@NotNull
private final Product product;

public int getQuantity() {
return quantity;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.epages.restdocs.apispec.sample;

import org.springframework.hateoas.Identifiable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import java.math.BigDecimal;
import org.springframework.hateoas.RepresentationModel;

import static javax.persistence.GenerationType.IDENTITY;
import static jakarta.persistence.GenerationType.IDENTITY;

@Entity
public class Product implements Identifiable<Long> {
public class Product extends RepresentationModel<Product> {

protected Product() {
}

public Product(String name, BigDecimal price) {
this.name = name;
this.price = price;
}

private Product() {
}

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.epages.restdocs.apispec.sample;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {
public interface ProductRepository extends CrudRepository<Product, Long>, PagingAndSortingRepository<Product, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring:
output:
ansi:
enabled: 'ALWAYS'
spring:
output:
ansi:
enabled: 'ALWAYS'
Loading

0 comments on commit 9023099

Please sign in to comment.