-
Notifications
You must be signed in to change notification settings - Fork 0
#14-#16 Backend:add post mongo repository service controller #21
base: master
Are you sure you want to change the base?
Changes from all commits
590631a
b276c4d
6e7f3a9
1d390c8
10b5fc6
0e38a1e
40ba8f5
b2a500f
85692eb
6e7d512
1137322
10b22c7
4d7be50
8f7d249
3132d31
1b40298
e1823c1
a0e9523
845cefa
cdcd52f
ff94988
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.github.dregheap.controller; | ||
|
||
import com.github.dregheap.model.Post; | ||
import com.github.dregheap.service.PostService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
@RequestMapping(path = "/api/v1/posts") | ||
public class PostController { | ||
private final PostService postService; | ||
|
||
@RequestMapping(method = RequestMethod.GET) | ||
public ResponseEntity getAllPosts() { | ||
return new ResponseEntity<>(postService.findAll(), HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.GET, params = {"page", "size"}) | ||
public ResponseEntity getAllPagedPosts(Pageable pageable) { | ||
return new ResponseEntity<>(postService.findAll(pageable), HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping(path = "/{id}", method = RequestMethod.GET) | ||
public ResponseEntity getPostById(@PathVariable String id) { | ||
return new ResponseEntity<>(postService.findById(id), HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE}) | ||
public ResponseEntity createPost(@RequestBody Post post) { | ||
return new ResponseEntity<>(postService.create(post), HttpStatus.CREATED); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.PUT, consumes = {MediaType.APPLICATION_JSON_VALUE}) | ||
public ResponseEntity updatePost(@RequestBody Post post) { | ||
postService.update(post); | ||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
@RequestMapping(path = "/{id}", method = RequestMethod.DELETE) | ||
public ResponseEntity deletePostById(@PathVariable String id) { | ||
postService.deleteById(id); | ||
return new ResponseEntity(HttpStatus.OK); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.DELETE, consumes = {MediaType.APPLICATION_JSON_VALUE}) | ||
public ResponseEntity deletePost(@RequestBody Post post) { | ||
postService.delete(post); | ||
return new ResponseEntity(HttpStatus.OK); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why OK? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.dregheap.model; | ||
|
||
import lombok.Data; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@Data | ||
@Document(collection = "posts") | ||
public class Post { | ||
@Id | ||
private String id; | ||
private String userId; | ||
private String topicId; | ||
private String postMessage; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.dregheap.repository; | ||
|
||
import com.github.dregheap.model.Post; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface PostRepository extends MongoRepository<Post, String> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.github.dregheap.service; | ||
|
||
import com.github.dregheap.model.Post; | ||
import com.github.dregheap.repository.PostRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor(onConstructor=@__(@Autowired)) | ||
public class DefaultPostService implements PostService { | ||
private final PostRepository postRepository; | ||
|
||
@Override | ||
public List<Post> findAll() { | ||
return postRepository.findAll(); | ||
} | ||
|
||
@Override | ||
public Page<Post> findAll(Pageable pageable) { | ||
return postRepository.findAll(pageable); | ||
} | ||
|
||
@Override | ||
public Post findById(String id) { | ||
return postRepository.findOne(id); | ||
} | ||
|
||
@Override | ||
public Post create(Post post) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if post is null? |
||
post.setId(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Null?? For what? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For prevent updating of object with the same id as in request body. This method is only for creating new objects in db. |
||
return postRepository.save(post); | ||
} | ||
|
||
@Override | ||
public Post update(Post post) { | ||
return postRepository.save(post); | ||
} | ||
|
||
@Override | ||
public void delete(Post post) { | ||
postRepository.delete(post); | ||
} | ||
|
||
@Override | ||
public void deleteById(String id) { | ||
postRepository.delete(id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.github.dregheap.service; | ||
|
||
import com.github.dregheap.model.Post; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
|
||
public interface PostService { | ||
List<Post> findAll(); | ||
|
||
Page<Post> findAll(Pageable pageable); | ||
|
||
Post findById(String id); | ||
|
||
Post create(Post post); | ||
|
||
Post update(Post post); | ||
|
||
void delete(Post post); | ||
|
||
void deleteById(String id); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
spring.data: | ||
mongodb: | ||
uri: mongodb://admin:[email protected]:21212/robanukah |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.github.dregheap.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.dregheap.model.Post; | ||
import com.github.dregheap.service.PostService; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@WebMvcTest(controllers = {PostController.class}) | ||
public class PostControllerTest { | ||
@Autowired | ||
private MockMvc mvc; | ||
|
||
@MockBean | ||
private PostService postService; | ||
|
||
private ObjectMapper objectWriter; | ||
|
||
@Before | ||
public void setUp() { | ||
objectWriter = new ObjectMapper(); | ||
} | ||
|
||
@Test | ||
public void getAllPostsShouldReturnPostsListAndStatusOK() throws Exception { | ||
List<Post> postList = new ArrayList<>(Arrays.asList(new Post(), new Post(), new Post())); | ||
PageRequest pageRequest = new PageRequest(0, 2); | ||
PageImpl<Post> pageList = new PageImpl<>(postList.subList(0, 2)); | ||
|
||
when(postService.findAll()).thenReturn(postList); | ||
when(postService.findAll(pageRequest)).thenReturn(pageList); | ||
|
||
mvc.perform(get("/api/v1/posts")) | ||
.andExpect(status().isOk()).andExpect(content().json(objectWriter.writeValueAsString(postList))); | ||
} | ||
|
||
@Test | ||
public void createPostShouldReturnStatusCreated() throws Exception { | ||
Post creatingPost = new Post(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separate that |
||
creatingPost.setId("1"); | ||
creatingPost.setPostMessage("testMessage"); | ||
creatingPost.setTopicId("1"); | ||
creatingPost.setUserId("1"); | ||
|
||
when(postService.create(creatingPost)).thenReturn(creatingPost); | ||
|
||
mvc.perform(post("/api/v1/posts").contentType("application/json").content(objectWriter.writeValueAsString(creatingPost))) | ||
.andExpect(status().isCreated()).andExpect(content().json(objectWriter.writeValueAsString(creatingPost))); | ||
|
||
} | ||
|
||
@Test | ||
public void updatePostShouldReturnStatusNoContent() throws Exception { | ||
Post creatingPost = new Post(); | ||
creatingPost.setId("1"); | ||
creatingPost.setPostMessage("testMessage"); | ||
creatingPost.setTopicId("1"); | ||
creatingPost.setUserId("1"); | ||
|
||
when(postService.update(creatingPost)).thenReturn(creatingPost); | ||
|
||
mvc.perform(put("/api/v1/posts").contentType("application/json").content(objectWriter.writeValueAsString(creatingPost))) | ||
.andExpect(status().isNoContent()).andExpect(content().json(objectWriter.writeValueAsString(creatingPost))); | ||
|
||
} | ||
|
||
@Test | ||
public void detelePostShouldReturnStatusOK() throws Exception { | ||
Post post = new Post(); | ||
|
||
doNothing().when(postService).delete(post); | ||
doNothing().when(postService).deleteById("1"); | ||
|
||
mvc.perform(delete("/api/v1/posts").contentType("application/json").content(objectWriter.writeValueAsString(post))) | ||
.andExpect(status().isOk()); | ||
mvc.perform(delete("/api/v1/posts/1")) | ||
.andExpect(status().isOk()); | ||
|
||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html