Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-10982
This commit is contained in:
		
						commit
						8f7bc84798
					
				| @ -9,7 +9,7 @@ public interface IOperations<T extends Serializable> { | ||||
| 
 | ||||
|     // read - one | ||||
| 
 | ||||
|     T findOne(final long id); | ||||
|     T findById(final long id); | ||||
| 
 | ||||
|     // read - all | ||||
| 
 | ||||
|  | ||||
| @ -18,9 +18,8 @@ public abstract class AbstractService<T extends Serializable> implements IOperat | ||||
| 
 | ||||
|     @Override | ||||
|     @Transactional(readOnly = true) | ||||
|     public T findOne(final long id) { | ||||
|         return getDao().findById(id) | ||||
|             .get(); | ||||
|     public T findById(final long id) { | ||||
|         return getDao().findById(id).orElse(null); | ||||
|     } | ||||
| 
 | ||||
|     // read - all | ||||
|  | ||||
| @ -55,7 +55,7 @@ public class WebConfig implements WebMvcConfigurer { | ||||
|     @Bean | ||||
|     public FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter() { | ||||
|         FilterRegistrationBean<ShallowEtagHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>( new ShallowEtagHeaderFilter()); | ||||
|         filterRegistrationBean.addUrlPatterns("/auth/foos/*"); | ||||
|         filterRegistrationBean.addUrlPatterns("/foos/*"); | ||||
|         filterRegistrationBean.setName("etagFilter"); | ||||
|         return filterRegistrationBean; | ||||
|     } | ||||
|  | ||||
| @ -20,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RequestParam; | ||||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| import org.springframework.web.server.ResponseStatusException; | ||||
| import org.springframework.web.util.UriComponentsBuilder; | ||||
| 
 | ||||
| import com.baeldung.persistence.model.Foo; | ||||
| @ -32,7 +33,7 @@ import com.baeldung.web.util.RestPreconditions; | ||||
| import com.google.common.base.Preconditions; | ||||
| 
 | ||||
| @RestController | ||||
| @RequestMapping(value = "/auth/foos") | ||||
| @RequestMapping(value = "/foos") | ||||
| public class FooController { | ||||
| 
 | ||||
|     @Autowired | ||||
| @ -51,23 +52,30 @@ public class FooController { | ||||
|     @GetMapping(value = "/{id}/custom-etag") | ||||
|     public ResponseEntity<Foo> findByIdWithCustomEtag(@PathVariable("id") final Long id, | ||||
|         final HttpServletResponse response) { | ||||
|         final Foo resourceById = RestPreconditions.checkFound(service.findOne(id)); | ||||
|         final Foo foo = RestPreconditions.checkFound(service.findById(id)); | ||||
| 
 | ||||
|         eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response)); | ||||
|         return ResponseEntity.ok() | ||||
|             .eTag(Long.toString(resourceById.getVersion())) | ||||
|             .body(resourceById); | ||||
|             .eTag(Long.toString(foo.getVersion())) | ||||
|             .body(foo); | ||||
|     } | ||||
| 
 | ||||
|     // read - one | ||||
| 
 | ||||
|     @GetMapping(value = "/{id}") | ||||
|     public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) { | ||||
|         final Foo resourceById = RestPreconditions.checkFound(service.findOne(id)); | ||||
|         try { | ||||
|             final Foo resourceById = RestPreconditions.checkFound(service.findById(id)); | ||||
| 
 | ||||
|             eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response)); | ||||
|             return resourceById; | ||||
|         } | ||||
|         catch (MyResourceNotFoundException exc) { | ||||
|             throw new ResponseStatusException( | ||||
|                 HttpStatus.NOT_FOUND, "Foo Not Found", exc); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     // read - all | ||||
| 
 | ||||
| @ -120,7 +128,7 @@ public class FooController { | ||||
|     @ResponseStatus(HttpStatus.OK) | ||||
|     public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) { | ||||
|         Preconditions.checkNotNull(resource); | ||||
|         RestPreconditions.checkFound(service.findOne(resource.getId())); | ||||
|         RestPreconditions.checkFound(service.findById(resource.getId())); | ||||
|         service.update(resource); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -7,34 +7,28 @@ import javax.servlet.http.HttpServletResponse; | ||||
| 
 | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.stereotype.Controller; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMethod; | ||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||||
| import org.springframework.web.util.UriTemplate; | ||||
| 
 | ||||
| import com.baeldung.web.util.LinkUtil; | ||||
| 
 | ||||
| @Controller | ||||
| @RequestMapping(value = "/auth/") | ||||
| public class RootController { | ||||
| 
 | ||||
|     public RootController() { | ||||
|         super(); | ||||
|     } | ||||
| 
 | ||||
|     // API | ||||
| 
 | ||||
|     // discover | ||||
| 
 | ||||
|     @RequestMapping(value = "admin", method = RequestMethod.GET) | ||||
|     @GetMapping("/") | ||||
|     @ResponseStatus(value = HttpStatus.NO_CONTENT) | ||||
|     public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) { | ||||
|         final String rootUri = request.getRequestURL() | ||||
|             .toString(); | ||||
| 
 | ||||
|         final URI fooUri = new UriTemplate("{rootUri}/{resource}").expand(rootUri, "foo"); | ||||
|         final String linkToFoo = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection"); | ||||
|         response.addHeader("Link", linkToFoo); | ||||
|         final URI fooUri = new UriTemplate("{rootUri}{resource}").expand(rootUri, "foos"); | ||||
|         final String linkToFoos = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection"); | ||||
|         response.addHeader("Link", linkToFoos); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -115,7 +115,7 @@ class PaginatedResultsRetrievedDiscoverabilityListener implements ApplicationLis | ||||
|     protected void plural(final UriComponentsBuilder uriBuilder, final Class clazz) { | ||||
|         final String resourceName = clazz.getSimpleName() | ||||
|             .toLowerCase() + "s"; | ||||
|         uriBuilder.path("/auth/" + resourceName); | ||||
|         uriBuilder.path("/" + resourceName); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -1,4 +1,3 @@ | ||||
| server.port=8082 | ||||
| server.servlet.context-path=/spring-boot-rest | ||||
| 
 | ||||
| ### Spring Boot default error handling configurations | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| package com.baeldung; | ||||
| 
 | ||||
| public interface Consts { | ||||
|     int APPLICATION_PORT = 8082; | ||||
|     int APPLICATION_PORT = 8080; | ||||
| } | ||||
|  | ||||
| @ -59,7 +59,7 @@ public abstract class AbstractLiveTest<T extends Serializable> { | ||||
|     // | ||||
| 
 | ||||
|     protected String getURL() { | ||||
|         return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/auth/foos"; | ||||
|         return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos"; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -27,7 +27,7 @@ public class FooControllerAppIntegrationTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenFindPaginatedRequest_thenEmptyResponse() throws Exception { | ||||
|         this.mockMvc.perform(get("/auth/foos").param("page", "0") | ||||
|         this.mockMvc.perform(get("/foos").param("page", "0") | ||||
|             .param("size", "2")) | ||||
|             .andExpect(status().isOk()) | ||||
|             .andExpect(content().json("[]")); | ||||
|  | ||||
| @ -29,7 +29,7 @@ public class FooControllerCustomEtagIntegrationTest { | ||||
|     @Autowired | ||||
|     private MockMvc mvc; | ||||
| 
 | ||||
|     private String FOOS_ENDPOINT = "/auth/foos/"; | ||||
|     private String FOOS_ENDPOINT = "/foos/"; | ||||
|     private String CUSTOM_ETAG_ENDPOINT_SUFFIX = "/custom-etag"; | ||||
| 
 | ||||
|     private static String serializeFoo(Foo foo) throws Exception { | ||||
|  | ||||
| @ -51,7 +51,7 @@ public class FooControllerWebLayerIntegrationTest { | ||||
|         doNothing().when(publisher) | ||||
|             .publishEvent(any(PaginatedResultsRetrievedEvent.class)); | ||||
| 
 | ||||
|         this.mockMvc.perform(get("/auth/foos").param("page", "0") | ||||
|         this.mockMvc.perform(get("/foos").param("page", "0") | ||||
|             .param("size", "2")) | ||||
|             .andExpect(status().isOk()) | ||||
|             .andExpect(jsonPath("$",Matchers.hasSize(1))); | ||||
|  | ||||
| @ -74,7 +74,7 @@ public class FooPageableLiveTest extends AbstractBasicLiveTest<Foo> { | ||||
|     } | ||||
| 
 | ||||
|     protected String getPageableURL() { | ||||
|         return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/auth/foos/pageable"; | ||||
|         return "http://localhost:" + APPLICATION_PORT + "/spring-boot-rest/foos/pageable"; | ||||
|     } | ||||
|      | ||||
| } | ||||
|  | ||||
| @ -42,15 +42,14 @@ | ||||
|                     "raw": "{\n  \"name\": \"Transformers\"\n}" | ||||
|                 }, | ||||
|                 "url": { | ||||
|                     "raw": "http://localhost:8082/spring-boot-rest/auth/foos", | ||||
|                     "raw": "http://localhost:8080/spring-boot-rest/foos", | ||||
|                     "protocol": "http", | ||||
|                     "host": [ | ||||
|                         "localhost" | ||||
|                     ], | ||||
|                     "port": "8082", | ||||
|                     "port": "8080", | ||||
|                     "path": [ | ||||
|                         "spring-boot-rest", | ||||
|                         "auth", | ||||
|                         "foos" | ||||
|                     ] | ||||
|                 } | ||||
| @ -85,15 +84,14 @@ | ||||
|                     "raw": "" | ||||
|                 }, | ||||
|                 "url": { | ||||
|                     "raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}", | ||||
|                     "raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}", | ||||
|                     "protocol": "http", | ||||
|                     "host": [ | ||||
|                         "localhost" | ||||
|                     ], | ||||
|                     "port": "8082", | ||||
|                     "port": "8080", | ||||
|                     "path": [ | ||||
|                         "spring-boot-rest", | ||||
|                         "auth", | ||||
|                         "foos", | ||||
|                         "{{id}}" | ||||
|                     ] | ||||
| @ -123,15 +121,14 @@ | ||||
|                     "raw": "" | ||||
|                 }, | ||||
|                 "url": { | ||||
|                     "raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}", | ||||
|                     "raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}", | ||||
|                     "protocol": "http", | ||||
|                     "host": [ | ||||
|                         "localhost" | ||||
|                     ], | ||||
|                     "port": "8082", | ||||
|                     "port": "8080", | ||||
|                     "path": [ | ||||
|                         "spring-boot-rest", | ||||
|                         "auth", | ||||
|                         "foos", | ||||
|                         "{{id}}" | ||||
|                     ] | ||||
| @ -164,15 +161,14 @@ | ||||
|                     "raw": "" | ||||
|                 }, | ||||
|                 "url": { | ||||
|                     "raw": "http://localhost:8082/spring-boot-rest/auth/foos/{{id}}", | ||||
|                     "raw": "http://localhost:8080/spring-boot-rest/foos/{{id}}", | ||||
|                     "protocol": "http", | ||||
|                     "host": [ | ||||
|                         "localhost" | ||||
|                     ], | ||||
|                     "port": "8082", | ||||
|                     "port": "8080", | ||||
|                     "path": [ | ||||
|                         "spring-boot-rest", | ||||
|                         "auth", | ||||
|                         "foos", | ||||
|                         "{{id}}" | ||||
|                     ] | ||||
|  | ||||
| @ -8,16 +8,30 @@ | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>com.baeldung</groupId> | ||||
|         <artifactId>parent-java</artifactId> | ||||
|         <artifactId>parent-boot-2</artifactId> | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|         <relativePath>../../parent-java</relativePath> | ||||
|         <relativePath>../../parent-boot-2</relativePath> | ||||
|         <!-- lookup parent from repository --> | ||||
|     </parent> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-web</artifactId> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-test</artifactId> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.google.guava</groupId> | ||||
|             <artifactId>guava</artifactId> | ||||
|             <version>${guava.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>javax.servlet</groupId> | ||||
|             <artifactId>javax.servlet-api</artifactId> | ||||
|             <version>${javax.servlet-api.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>javax.servlet</groupId> | ||||
| @ -27,49 +41,40 @@ | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jetty</groupId> | ||||
|             <artifactId>jetty-security</artifactId> | ||||
|             <version>${jetty.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jetty</groupId> | ||||
|             <artifactId>jetty-servlet</artifactId> | ||||
|             <version>${jetty.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jetty</groupId> | ||||
|             <artifactId>jetty-servlets</artifactId> | ||||
|             <version>${jetty.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jetty</groupId> | ||||
|             <artifactId>jetty-io</artifactId> | ||||
|             <version>${jetty.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jetty</groupId> | ||||
|             <artifactId>jetty-http</artifactId> | ||||
|             <version>${jetty.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jetty</groupId> | ||||
|             <artifactId>jetty-server</artifactId> | ||||
|             <version>${jetty.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse.jetty</groupId> | ||||
|             <artifactId>jetty-util</artifactId> | ||||
|             <version>${jetty.version}</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>org.apache.httpcomponents</groupId> | ||||
|             <artifactId>httpcore</artifactId> | ||||
|             <version>${httpcore.version}</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>org.apache.commons</groupId> | ||||
|             <artifactId>commons-lang3</artifactId> | ||||
|             <version>${commons-lang3.version}</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
| @ -93,18 +98,15 @@ | ||||
|         <dependency> | ||||
|             <groupId>joda-time</groupId> | ||||
|             <artifactId>joda-time</artifactId>             | ||||
|             <version>${joda-time.version}</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>com.fasterxml.jackson.core</groupId> | ||||
|             <artifactId>jackson-annotations</artifactId> | ||||
|             <version>${jackson.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.fasterxml.jackson.core</groupId> | ||||
|             <artifactId>jackson-databind</artifactId> | ||||
|             <version>${jackson.version}</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
| @ -128,7 +130,6 @@ | ||||
|         <dependency> | ||||
|             <groupId>org.apache.httpcomponents</groupId> | ||||
|             <artifactId>httpclient</artifactId> | ||||
|             <version>${httpclient.version}</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <dependency> | ||||
| @ -145,13 +146,11 @@ | ||||
|         <dependency> | ||||
|             <groupId>io.rest-assured</groupId> | ||||
|             <artifactId>rest-assured</artifactId> | ||||
|             <version>${rest-assured.version}</version> | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>io.rest-assured</groupId> | ||||
|             <artifactId>json-schema-validator</artifactId> | ||||
|             <version>${rest-assured-json-schema-validator.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.github.fge</groupId> | ||||
| @ -171,6 +170,7 @@ | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <properties> | ||||
|         <guava.version>18.0</guava.version> | ||||
|         <jackson.version>2.9.7</jackson.version> | ||||
|         <jackson-coreutils.version>1.8</jackson-coreutils.version> | ||||
|         <guava.version>19.0</guava.version> | ||||
|  | ||||
| @ -0,0 +1,13 @@ | ||||
| package com.baeldung.restassured; | ||||
| 
 | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| 
 | ||||
| @SpringBootApplication | ||||
| public class Application { | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         SpringApplication.run(Application.class, args); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,100 @@ | ||||
| package com.baeldung.restassured.controller; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.FileInputStream; | ||||
| import java.io.FileNotFoundException; | ||||
| import java.util.Set; | ||||
| import java.util.UUID; | ||||
| 
 | ||||
| import javax.servlet.http.Cookie; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| 
 | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.core.io.InputStreamResource; | ||||
| import org.springframework.core.io.Resource; | ||||
| import org.springframework.http.HttpHeaders; | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.http.MediaType; | ||||
| import org.springframework.http.ResponseEntity; | ||||
| 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 org.springframework.web.bind.annotation.ResponseStatus; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| 
 | ||||
| import com.baeldung.restassured.model.Movie; | ||||
| import com.baeldung.restassured.service.AppService; | ||||
| 
 | ||||
| @RestController | ||||
| public class AppController { | ||||
| 
 | ||||
|     @Autowired | ||||
|     AppService appService; | ||||
| 
 | ||||
|     @GetMapping("/movies") | ||||
|     public ResponseEntity<?> getMovies() { | ||||
| 
 | ||||
|         Set<Movie> result = appService.getAll(); | ||||
| 
 | ||||
|         return ResponseEntity.ok() | ||||
|             .body(result); | ||||
|     } | ||||
| 
 | ||||
|     @PostMapping("/movie") | ||||
|     @ResponseStatus(HttpStatus.CREATED) | ||||
|     public Movie addMovie(@RequestBody Movie movie) { | ||||
| 
 | ||||
|         appService.add(movie); | ||||
|         return movie; | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/movie/{id}") | ||||
|     public ResponseEntity<?> getMovie(@PathVariable int id) { | ||||
| 
 | ||||
|         Movie movie = appService.findMovie(id); | ||||
|         if (movie == null) { | ||||
|             return ResponseEntity.badRequest() | ||||
|                 .body("Invalid movie id"); | ||||
|         } | ||||
| 
 | ||||
|         return ResponseEntity.ok(movie); | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/welcome") | ||||
|     public ResponseEntity<?> welcome(HttpServletResponse response) { | ||||
| 
 | ||||
|         HttpHeaders headers = new HttpHeaders(); | ||||
|         headers.add(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8"); | ||||
|         headers.add("sessionId", UUID.randomUUID() | ||||
|             .toString()); | ||||
| 
 | ||||
|         Cookie cookie = new Cookie("token", "some-token"); | ||||
|         cookie.setDomain("localhost"); | ||||
| 
 | ||||
|         response.addCookie(cookie); | ||||
| 
 | ||||
|         return ResponseEntity.noContent() | ||||
|             .headers(headers) | ||||
|             .build(); | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/download/{id}") | ||||
|     public ResponseEntity<Resource> getFile(@PathVariable int id) throws FileNotFoundException { | ||||
| 
 | ||||
|         File file = appService.getFile(id); | ||||
| 
 | ||||
|         if (file == null) { | ||||
|             return ResponseEntity.notFound() | ||||
|                 .build(); | ||||
|         } | ||||
| 
 | ||||
|         InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); | ||||
| 
 | ||||
|         return ResponseEntity.ok() | ||||
|             .contentLength(file.length()) | ||||
|             .contentType(MediaType.parseMediaType("application/octet-stream")) | ||||
|             .body(resource); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,58 @@ | ||||
| package com.baeldung.restassured.model; | ||||
| 
 | ||||
| public class Movie { | ||||
| 
 | ||||
|     private Integer id; | ||||
| 
 | ||||
|     private String name; | ||||
| 
 | ||||
|     private String synopsis; | ||||
| 
 | ||||
|     public Movie() { | ||||
|     } | ||||
| 
 | ||||
|     public Movie(Integer id, String name, String synopsis) { | ||||
|         super(); | ||||
|         this.id = id; | ||||
|         this.name = name; | ||||
|         this.synopsis = synopsis; | ||||
|     } | ||||
| 
 | ||||
|     public Integer getId() { | ||||
|         return id; | ||||
|     } | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public String getSynopsis() { | ||||
|         return synopsis; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public int hashCode() { | ||||
|         final int prime = 31; | ||||
|         int result = 1; | ||||
|         result = prime * result + ((id == null) ? 0 : id.hashCode()); | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public boolean equals(Object obj) { | ||||
|         if (this == obj) | ||||
|             return true; | ||||
|         if (obj == null) | ||||
|             return false; | ||||
|         if (getClass() != obj.getClass()) | ||||
|             return false; | ||||
|         Movie other = (Movie) obj; | ||||
|         if (id == null) { | ||||
|             if (other.id != null) | ||||
|                 return false; | ||||
|         } else if (!id.equals(other.id)) | ||||
|             return false; | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,45 @@ | ||||
| package com.baeldung.restassured.service; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.util.HashSet; | ||||
| import java.util.Set; | ||||
| 
 | ||||
| import org.springframework.core.io.ClassPathResource; | ||||
| import org.springframework.stereotype.Service; | ||||
| 
 | ||||
| import com.baeldung.restassured.model.Movie; | ||||
| 
 | ||||
| @Service | ||||
| public class AppService { | ||||
| 
 | ||||
|     private Set<Movie> movieSet = new HashSet<>(); | ||||
| 
 | ||||
|     public Set<Movie> getAll() { | ||||
|         return movieSet; | ||||
|     } | ||||
| 
 | ||||
|     public void add(Movie movie) { | ||||
|         movieSet.add(movie); | ||||
|     } | ||||
| 
 | ||||
|     public Movie findMovie(int id) { | ||||
|         return movieSet.stream() | ||||
|             .filter(movie -> movie.getId() | ||||
|                 .equals(id)) | ||||
|             .findFirst() | ||||
|             .orElse(null); | ||||
|     } | ||||
| 
 | ||||
|     public File getFile(int id) { | ||||
|         File file = null; | ||||
|         try { | ||||
|             file = new ClassPathResource(String.valueOf(id)).getFile(); | ||||
|         } catch (IOException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
| 
 | ||||
|         return file; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										1
									
								
								testing-modules/rest-assured/src/main/resources/1
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								testing-modules/rest-assured/src/main/resources/1
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| File 1 | ||||
							
								
								
									
										1
									
								
								testing-modules/rest-assured/src/main/resources/2
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								testing-modules/rest-assured/src/main/resources/2
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| File 2 | ||||
| @ -0,0 +1,149 @@ | ||||
| package com.baeldung.restassured.controller; | ||||
| 
 | ||||
| import static io.restassured.RestAssured.get; | ||||
| import static io.restassured.RestAssured.given; | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| import static org.hamcrest.CoreMatchers.equalTo; | ||||
| import static org.hamcrest.CoreMatchers.is; | ||||
| import static org.hamcrest.CoreMatchers.notNullValue; | ||||
| import static org.mockito.Mockito.when; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.util.HashMap; | ||||
| import java.util.HashSet; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
| 
 | ||||
| import javax.annotation.PostConstruct; | ||||
| 
 | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.springframework.boot.test.context.SpringBootTest; | ||||
| import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| import org.springframework.boot.web.server.LocalServerPort; | ||||
| import org.springframework.core.io.ClassPathResource; | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.test.context.junit4.SpringRunner; | ||||
| 
 | ||||
| import com.baeldung.restassured.model.Movie; | ||||
| import com.baeldung.restassured.service.AppService; | ||||
| 
 | ||||
| import io.restassured.response.Response; | ||||
| 
 | ||||
| @RunWith(SpringRunner.class) | ||||
| @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||||
| public class AppControllerIntegrationTest { | ||||
| 
 | ||||
|     @LocalServerPort | ||||
|     private int port; | ||||
| 
 | ||||
|     private String uri; | ||||
| 
 | ||||
|     @PostConstruct | ||||
|     public void init() { | ||||
|         uri = "http://localhost:" + port; | ||||
|     } | ||||
| 
 | ||||
|     @MockBean | ||||
|     AppService appService; | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenMovieId_whenMakingGetRequestToMovieEndpoint_thenReturnMovie() { | ||||
| 
 | ||||
|         Movie testMovie = new Movie(1, "movie1", "summary1"); | ||||
|         when(appService.findMovie(1)).thenReturn(testMovie); | ||||
| 
 | ||||
|         get(uri + "/movie/" + testMovie.getId()).then() | ||||
|             .assertThat() | ||||
|             .statusCode(HttpStatus.OK.value()) | ||||
|             .body("id", equalTo(testMovie.getId())) | ||||
|             .body("name", equalTo(testMovie.getName())) | ||||
|             .body("synopsis", notNullValue()); | ||||
| 
 | ||||
|         Movie result = get(uri + "/movie/" + testMovie.getId()).then() | ||||
|             .assertThat() | ||||
|             .statusCode(HttpStatus.OK.value()) | ||||
|             .extract() | ||||
|             .as(Movie.class); | ||||
|         assertThat(result).isEqualTo(testMovie); | ||||
| 
 | ||||
|         String responseString = get(uri + "/movie/" + testMovie.getId()).then() | ||||
|             .assertThat() | ||||
|             .statusCode(HttpStatus.OK.value()) | ||||
|             .extract() | ||||
|             .asString(); | ||||
|         assertThat(responseString).isNotEmpty(); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenCallingMoviesEndpoint_thenReturnAllMovies() { | ||||
| 
 | ||||
|         Set<Movie> movieSet = new HashSet<>(); | ||||
|         movieSet.add(new Movie(1, "movie1", "summary1")); | ||||
|         movieSet.add(new Movie(2, "movie2", "summary2")); | ||||
|         when(appService.getAll()).thenReturn(movieSet); | ||||
| 
 | ||||
|         get(uri + "/movies").then() | ||||
|             .statusCode(HttpStatus.OK.value()) | ||||
|             .assertThat() | ||||
|             .body("size()", is(2)); | ||||
| 
 | ||||
|         Movie[] movies = get(uri + "/movies").then() | ||||
|             .statusCode(200) | ||||
|             .extract() | ||||
|             .as(Movie[].class); | ||||
|         assertThat(movies.length).isEqualTo(2); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenMovie_whenMakingPostRequestToMovieEndpoint_thenCorrect() { | ||||
| 
 | ||||
|         Map<String, String> request = new HashMap<>(); | ||||
|         request.put("id", "11"); | ||||
|         request.put("name", "movie1"); | ||||
|         request.put("synopsis", "summary1"); | ||||
| 
 | ||||
|         int movieId = given().contentType("application/json") | ||||
|             .body(request) | ||||
|             .when() | ||||
|             .post(uri + "/movie") | ||||
|             .then() | ||||
|             .assertThat() | ||||
|             .statusCode(HttpStatus.CREATED.value()) | ||||
|             .extract() | ||||
|             .path("id"); | ||||
|         assertThat(movieId).isEqualTo(11); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenCallingWelcomeEndpoint_thenCorrect() { | ||||
| 
 | ||||
|         get(uri + "/welcome").then() | ||||
|             .assertThat() | ||||
|             .header("sessionId", notNullValue()) | ||||
|             .cookie("token", notNullValue()); | ||||
| 
 | ||||
|         Response response = get(uri + "/welcome"); | ||||
| 
 | ||||
|         String headerName = response.getHeader("sessionId"); | ||||
|         String cookieValue = response.getCookie("token"); | ||||
|         assertThat(headerName).isNotBlank(); | ||||
|         assertThat(cookieValue).isNotBlank(); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenId_whenCallingDowloadEndpoint_thenCorrect() throws IOException { | ||||
| 
 | ||||
|         File file = new ClassPathResource("test.txt").getFile(); | ||||
|         long fileSize = file.length(); | ||||
|         when(appService.getFile(1)).thenReturn(file); | ||||
| 
 | ||||
|         byte[] result = get(uri + "/download/1").asByteArray(); | ||||
| 
 | ||||
|         assertThat(result.length).isEqualTo(fileSize); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										1
									
								
								testing-modules/rest-assured/src/test/resources/test.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								testing-modules/rest-assured/src/test/resources/test.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| Test file | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user