REST-assured web

This commit is contained in:
Dhananjay Singh
2019-03-11 00:30:33 +01:00
parent 3a57c59960
commit 1131705393
9 changed files with 379 additions and 19 deletions
@@ -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;
}
}
@@ -0,0 +1 @@
File 1
@@ -0,0 +1 @@
File 2