Skip to content

Commit 906e780

Browse files
author
Anand Rajneesh
committed
[#2 #3] Added basic Spring MVC and Test examples
1 parent 84c108c commit 906e780

File tree

7 files changed

+390
-22
lines changed

7 files changed

+390
-22
lines changed

src/main/java/org/gluecoders/library/Application.java

-9
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,15 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.web.bind.annotation.GetMapping;
6-
import org.springframework.web.bind.annotation.RequestMapping;
7-
import org.springframework.web.bind.annotation.RestController;
85

96
/**
107
* Created by Anand_Rajneesh on 6/10/2017.
118
*/
129
@SpringBootApplication
13-
@RestController
1410
public class Application {
1511

1612
public static void main(String[] args) {
1713
SpringApplication.run(Application.class, args);
1814
}
1915

20-
@RequestMapping("/")
21-
@GetMapping
22-
public String hello(){
23-
return "Hi ! Welcome to Spring Boot Guide";
24-
}
2516
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.gluecoders.library.models;
2+
3+
import java.io.Serializable;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* Created by Anand_Rajneesh on 6/10/2017.
9+
*/
10+
public class Book implements Serializable {
11+
12+
private String title;
13+
private String author;
14+
private int publishedYear;
15+
private List<String> categories;
16+
private String publisher;
17+
private long isbnCode;
18+
19+
public Book() {
20+
}
21+
22+
public String getTitle() {
23+
return title;
24+
}
25+
26+
public void setTitle(String title) {
27+
this.title = title;
28+
}
29+
30+
public String getAuthor() {
31+
return author;
32+
}
33+
34+
public void setAuthor(String author) {
35+
this.author = author;
36+
}
37+
38+
public List<String> getCategories() {
39+
return categories;
40+
}
41+
42+
public void setCategories(List<String> categories) {
43+
this.categories = categories;
44+
}
45+
46+
public String getPublisher() {
47+
return publisher;
48+
}
49+
50+
public void setPublisher(String publisher) {
51+
this.publisher = publisher;
52+
}
53+
54+
public long getIsbnCode() {
55+
return isbnCode;
56+
}
57+
58+
public void setIsbnCode(long isbnCode) {
59+
this.isbnCode = isbnCode;
60+
}
61+
62+
public int getPublishedYear() {
63+
return publishedYear;
64+
}
65+
66+
public void setPublishedYear(int publishedYear) {
67+
this.publishedYear = publishedYear;
68+
}
69+
70+
@Override
71+
public boolean equals(Object obj) {
72+
return obj != null && obj instanceof Book && Long.compare(isbnCode, ((Book) obj).getIsbnCode()) == 0;
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return Long.hashCode(isbnCode);
78+
}
79+
80+
public static Builder builder(){
81+
return new Builder();
82+
}
83+
84+
public static class Builder{
85+
86+
private Book book = new Book();
87+
88+
public Builder title(String title){
89+
book.setTitle(title);
90+
return this;
91+
}
92+
93+
public Builder author(String author){
94+
book.setAuthor(author);
95+
return this;
96+
}
97+
98+
public Builder isbn(long isbnCode){
99+
book.setIsbnCode(isbnCode);
100+
return this;
101+
}
102+
103+
public Builder yearOfPublishing(int year){
104+
book.setPublishedYear(year);
105+
return this;
106+
}
107+
108+
public Builder categories(String ... category){
109+
book.setCategories(Arrays.asList(category));
110+
return this;
111+
}
112+
113+
public Builder publisher(String publisher){
114+
book.setPublisher(publisher);
115+
return this;
116+
}
117+
118+
public Book build(){
119+
return this.book;
120+
}
121+
}
122+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.gluecoders.library.rest;
2+
3+
import org.gluecoders.library.models.Book;
4+
import org.gluecoders.library.services.BookService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import java.util.List;
12+
13+
/**
14+
* Created by Anand_Rajneesh on 6/10/2017.
15+
*/
16+
@RestController()
17+
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, path = "/books")
18+
public class Books {
19+
20+
@Autowired
21+
private BookService bookService;
22+
23+
@GetMapping
24+
public ResponseEntity<List<Book>> getAllBooks() {
25+
List<Book> list = bookService.getAllBooks();
26+
return new ResponseEntity<>(list, HttpStatus.OK);
27+
}
28+
29+
@GetMapping("/{isbn}")
30+
public ResponseEntity<Book> getBook(@PathVariable("isbn") long isbnCode) {
31+
Book book = bookService.getBookByISBN(isbnCode);
32+
if(book == null) {
33+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
34+
}else{
35+
return new ResponseEntity<>(book, HttpStatus.OK);
36+
}
37+
}
38+
39+
@PostMapping
40+
public ResponseEntity<Book> addBook(@RequestBody Book book) {
41+
book = bookService.addBook(book);
42+
return new ResponseEntity<>(book, HttpStatus.CREATED);
43+
}
44+
45+
@DeleteMapping("/{isbn}")
46+
public ResponseEntity deleteBook(@PathVariable("isbn") long isbnCode) {
47+
bookService.deleteBook(isbnCode);
48+
return new ResponseEntity(HttpStatus.OK);
49+
}
50+
51+
}
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.gluecoders.library.services;
2+
3+
import org.gluecoders.library.models.Book;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Created by Anand_Rajneesh on 6/11/2017.
9+
*/
10+
public interface BookService {
11+
12+
List<Book> getAllBooks();
13+
14+
Book addBook(Book book);
15+
16+
Book getBookByISBN(long isbn);
17+
18+
void deleteBook(long isbn);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.gluecoders.library.services;
2+
3+
import org.gluecoders.library.models.Book;
4+
import org.springframework.stereotype.Component;
5+
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
/**
10+
* Created by Anand_Rajneesh on 6/11/2017.
11+
*/
12+
@Component
13+
public class DefaultBookService implements BookService {
14+
15+
16+
@Override
17+
public List<Book> getAllBooks() {
18+
return Collections.emptyList();
19+
}
20+
21+
@Override
22+
public Book addBook(Book book) {
23+
return book;
24+
}
25+
26+
@Override
27+
public Book getBookByISBN(long isbn) {
28+
return null;
29+
}
30+
31+
@Override
32+
public void deleteBook(long isbn) {
33+
34+
}
35+
}

src/test/java/org/gluecoders/library/ApplicationTest.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,15 @@
22

33
import org.junit.Test;
44
import org.junit.runner.RunWith;
5-
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
75
import org.springframework.test.context.junit4.SpringRunner;
8-
import org.springframework.test.web.servlet.MockMvc;
9-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
10-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
116
/**
127
* Created by Anand_Rajneesh on 6/10/2017.
138
*/
149
@RunWith(SpringRunner.class)
15-
@WebMvcTest(Application.class)
1610
public class ApplicationTest {
1711

18-
@Autowired
19-
private MockMvc mvc;
20-
2112
@Test
22-
public void testHello() throws Exception {
23-
mvc.perform(get("/"))
24-
.andExpect(status().isOk())
25-
.andExpect(content().string("Hi ! Welcome to Spring Boot Guide"));
13+
public void testStartup() throws Exception {
14+
Application.main(new String[]{});
2615
}
2716
}

0 commit comments

Comments
 (0)