|
| 1 | +/* |
| 2 | + * Copyright 2017-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 | + * https://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 | +package org.springframework.hateoas.mediatype.hal; |
| 17 | + |
| 18 | +import static org.hamcrest.CoreMatchers.*; |
| 19 | +import static org.hamcrest.collection.IsCollectionWithSize.*; |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 22 | +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.core.io.ClassPathResource; |
| 31 | +import org.springframework.hateoas.config.EnableHypermediaSupport; |
| 32 | +import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; |
| 33 | +import org.springframework.hateoas.support.MappingUtils; |
| 34 | +import org.springframework.hateoas.support.WebMvcEmployeeController; |
| 35 | +import org.springframework.http.HttpHeaders; |
| 36 | +import org.springframework.http.MediaType; |
| 37 | +import org.springframework.test.context.ContextConfiguration; |
| 38 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 39 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 40 | +import org.springframework.test.web.servlet.MockMvc; |
| 41 | +import org.springframework.web.context.WebApplicationContext; |
| 42 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author Greg Turnquist |
| 46 | + */ |
| 47 | +@ExtendWith(SpringExtension.class) |
| 48 | +@WebAppConfiguration |
| 49 | +@ContextConfiguration |
| 50 | +class HalHandleApplicationJsonWebMvcIntegrationTest { |
| 51 | + |
| 52 | + @Autowired WebApplicationContext context; |
| 53 | + |
| 54 | + MockMvc mockMvc; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + void setUp() { |
| 58 | + |
| 59 | + this.mockMvc = webAppContextSetup(this.context).build(); |
| 60 | + WebMvcEmployeeController.reset(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void singleEmployee() throws Exception { |
| 65 | + |
| 66 | + this.mockMvc.perform(get("/employees/0").accept(MediaType.APPLICATION_JSON)) // |
| 67 | + |
| 68 | + .andExpect(status().isOk()) // |
| 69 | + .andExpect(jsonPath("$.name", is("Frodo Baggins"))) // |
| 70 | + .andExpect(jsonPath("$.role", is("ring bearer"))) |
| 71 | + |
| 72 | + .andExpect(jsonPath("$._links.*", hasSize(2))) |
| 73 | + .andExpect(jsonPath("$._links['self'].href", is("http://localhost/employees/0"))) |
| 74 | + .andExpect(jsonPath("$._links['employees'].href", is("http://localhost/employees"))); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void collectionOfEmployees() throws Exception { |
| 79 | + |
| 80 | + this.mockMvc.perform(get("/employees").accept(MediaType.APPLICATION_JSON)) // |
| 81 | + .andExpect(status().isOk()) // |
| 82 | + .andExpect(jsonPath("$._embedded.employees[0].name", is("Frodo Baggins"))) |
| 83 | + .andExpect(jsonPath("$._embedded.employees[0].role", is("ring bearer"))) |
| 84 | + .andExpect(jsonPath("$._embedded.employees[0]._links['self'].href", is("http://localhost/employees/0"))) |
| 85 | + .andExpect(jsonPath("$._embedded.employees[1].name", is("Bilbo Baggins"))) |
| 86 | + .andExpect(jsonPath("$._embedded.employees[1].role", is("burglar"))) |
| 87 | + .andExpect(jsonPath("$._embedded.employees[1]._links['self'].href", is("http://localhost/employees/1"))) |
| 88 | + |
| 89 | + .andExpect(jsonPath("$._links.*", hasSize(1))) |
| 90 | + .andExpect(jsonPath("$._links['self'].href", is("http://localhost/employees"))); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void createNewEmployee() throws Exception { |
| 95 | + |
| 96 | + String specBasedJson = MappingUtils.read(new ClassPathResource("new-employee.json", getClass())); |
| 97 | + |
| 98 | + this.mockMvc.perform(post("/employees") // |
| 99 | + .content(specBasedJson) // |
| 100 | + .contentType(MediaType.APPLICATION_JSON_VALUE)) // |
| 101 | + .andExpect(status().isCreated()) |
| 102 | + .andExpect(header().stringValues(HttpHeaders.LOCATION, "http://localhost/employees/2")); |
| 103 | + } |
| 104 | + |
| 105 | + @Configuration |
| 106 | + @EnableWebMvc |
| 107 | + @EnableHypermediaSupport(type = HypermediaType.HAL) |
| 108 | + static class TestConfig { |
| 109 | + |
| 110 | + @Bean |
| 111 | + WebMvcEmployeeController employeeController() { |
| 112 | + return new WebMvcEmployeeController(); |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + HalConfiguration halFormsConfiguration() { |
| 117 | + return new HalConfiguration().withAdditionalMediatype(MediaType.APPLICATION_JSON); |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + @Configuration |
| 123 | + @EnableHypermediaSupport(type = HypermediaType.HAL) |
| 124 | + static class WithHalConfiguration { |
| 125 | + |
| 126 | + static final HalConfiguration CONFIG = new HalConfiguration().withAdditionalMediatype(MediaType.APPLICATION_JSON); |
| 127 | + |
| 128 | + @Bean |
| 129 | + public HalConfiguration halConfiguration() { |
| 130 | + return CONFIG; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments