JAVA-8363: Fix FooLiveTest - separate mime from etag code

This commit is contained in:
Krzysiek 2021-11-25 13:25:40 +01:00
parent 8d8ae5daa9
commit 49ffde8b48
4 changed files with 47 additions and 31 deletions

View File

@ -0,0 +1,36 @@
package com.baeldung.mime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping(value = "/foos")
public class FooController {
@Autowired
private FooDao fooDao;
@GetMapping(value = "/{id}")
public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) {
return fooDao.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Foo create(@RequestBody final Foo resource, final HttpServletResponse response) {
return fooDao.save(resource);
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.mime;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface FooDao extends CrudRepository<Foo, Long>{
}

View File

@ -1,28 +0,0 @@
package com.baeldung.mime;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
@Configuration
public class WebConfig {
// Etags
// If we're not using Spring Boot we can make use of
// AbstractAnnotationConfigDispatcherServletInitializer#getServletFilters
@Bean
public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() {
FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>( new ShallowEtagHeaderFilter());
filterRegistrationBean.addUrlPatterns("/foos/*");
filterRegistrationBean.setName("etagFilter");
return filterRegistrationBean;
}
// We can also just declare the filter directly
// @Bean
// public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
// return new ShallowEtagHeaderFilter();
// }
}

View File

@ -18,7 +18,7 @@ import io.restassured.RestAssured;
import io.restassured.response.Response;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WebConfig.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@SpringBootTest(classes = FooController.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@ComponentScan({"com.baeldung.mime"})
@EnableAutoConfiguration
@ActiveProfiles("test")
@ -44,12 +44,12 @@ public class FooLiveTest {
createAsUri(resource);
}
private final String createAsUri(final Foo resource) {
private String createAsUri(final Foo resource) {
final Response response = createAsResponse(resource);
return getURL() + "/" + response.getBody().as(Foo.class).getId();
}
private final Response createAsResponse(final Foo resource) {
private Response createAsResponse(final Foo resource) {
final String resourceAsString = marshaller.encode(resource);
return RestAssured.given()