|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.hateoas |
| 18 | + |
| 19 | +import org.assertj.core.api.Assertions.assertThat |
| 20 | +import org.junit.Test |
| 21 | +import org.springframework.http.HttpMethod |
| 22 | +import org.springframework.http.ResponseEntity |
| 23 | +import org.springframework.web.bind.annotation.DeleteMapping |
| 24 | +import org.springframework.web.bind.annotation.GetMapping |
| 25 | +import org.springframework.web.bind.annotation.PathVariable |
| 26 | +import org.springframework.web.bind.annotation.PutMapping |
| 27 | +import org.springframework.web.bind.annotation.RequestBody |
| 28 | +import org.springframework.web.bind.annotation.RequestMapping |
| 29 | + |
| 30 | +/** |
| 31 | + * Unit tests for [LinkBuilderDsl]. |
| 32 | + * |
| 33 | + * @author Roland Kulcsár |
| 34 | + */ |
| 35 | +class LinkBuilderDslUnitTest : TestUtils() { |
| 36 | + |
| 37 | + private val REL_PRODUCTS = "products" |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `creates link to controller method`() { |
| 41 | + val self = linkTo<CustomerController> { findById("15") } withRel Link.REL_SELF |
| 42 | + |
| 43 | + assertPointsToMockServer(self) |
| 44 | + assertThat(self.rel).isEqualTo(Link.REL_SELF) |
| 45 | + assertThat(self.href).endsWith("/customers/15") |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `creates affordance to controller method`() { |
| 50 | + val delete = afford<CustomerController> { delete("15") } |
| 51 | + |
| 52 | + assertThat(delete.httpMethod).isEqualTo(HttpMethod.DELETE) |
| 53 | + assertThat(delete.name).isEqualTo("delete") |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `creates link to controller method with affordances`() { |
| 58 | + val id = "15" |
| 59 | + val self = linkTo<CustomerController> { findById(id) } withRel Link.REL_SELF |
| 60 | + val selfWithAffordances = self andAffordances { |
| 61 | + afford<CustomerController> { update(id, CustomerDto("John Doe")) } |
| 62 | + afford<CustomerController> { delete(id) } |
| 63 | + } |
| 64 | + |
| 65 | + assertThat(selfWithAffordances.affordances).hasSize(3) |
| 66 | + assertThat(selfWithAffordances.hashCode()).isNotEqualTo(self.hashCode()) |
| 67 | + assertThat(selfWithAffordances).isNotEqualTo(self) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `adds links to wrapped domain object`() { |
| 72 | + val customer = Resource(Customer("15", "John Doe")) |
| 73 | + |
| 74 | + customer.add(CustomerController::class) { |
| 75 | + linkTo { findById(it.content.id) } withRel Link.REL_SELF |
| 76 | + linkTo { findProductsById(it.content.id) } withRel REL_PRODUCTS |
| 77 | + } |
| 78 | + |
| 79 | + customer.links.forEach { assertPointsToMockServer(it) } |
| 80 | + assertThat(customer.hasLink(Link.REL_SELF)).isTrue() |
| 81 | + assertThat(customer.hasLink(REL_PRODUCTS)).isTrue() |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `adds links to resource`() { |
| 86 | + val customer = CustomerResource("15", "John Doe") |
| 87 | + |
| 88 | + customer.add(CustomerController::class) { |
| 89 | + linkTo { findById(it.id) } withRel Link.REL_SELF |
| 90 | + linkTo { findProductsById(it.id) } withRel REL_PRODUCTS |
| 91 | + } |
| 92 | + |
| 93 | + customer.links.forEach { assertPointsToMockServer(it) } |
| 94 | + assertThat(customer.hasLink(Link.REL_SELF)).isTrue() |
| 95 | + assertThat(customer.hasLink(REL_PRODUCTS)).isTrue() |
| 96 | + } |
| 97 | + |
| 98 | + data class Customer(val id: String, val name: String) |
| 99 | + data class CustomerDto(val name: String) |
| 100 | + open class CustomerResource(val id: String, val name: String) : ResourceSupport() |
| 101 | + open class ProductResource(val id: String) : ResourceSupport() |
| 102 | + |
| 103 | + @RequestMapping("/customers") |
| 104 | + interface CustomerController { |
| 105 | + |
| 106 | + @GetMapping("/{id}") |
| 107 | + fun findById(@PathVariable id: String): ResponseEntity<CustomerResource> |
| 108 | + |
| 109 | + @GetMapping("/{id}/products") |
| 110 | + fun findProductsById(@PathVariable id: String): PagedResources<ProductResource> |
| 111 | + |
| 112 | + @PutMapping("/{id}") |
| 113 | + fun update(@PathVariable id: String, @RequestBody customer: CustomerDto): ResponseEntity<CustomerResource> |
| 114 | + |
| 115 | + @DeleteMapping("/{id}") |
| 116 | + fun delete(@PathVariable id: String): ResponseEntity<Unit> |
| 117 | + } |
| 118 | +} |
0 commit comments