Skip to content
This repository was archived by the owner on Nov 10, 2017. It is now read-only.

#14-#16 Backend:add post mongo repository service controller #21

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
590631a
#14 add PostRepository class
olegbal Jun 17, 2017
b276c4d
#15 - Add PostService interface and implementation. Add tests for Pos…
olegbal Jun 17, 2017
6e7f3a9
#16 - Rename package controllers to controller. Add PostController. A…
olegbal Jun 17, 2017
1d390c8
Add Post model. Add mongodb connection url to application.yml config
olegbal Jun 17, 2017
10b5fc6
Update spring-boot version to 1.5.4. Add mongodb dependency
olegbal Jun 17, 2017
0e38a1e
#14 - Change Id type from Long to String
olegbal Jul 1, 2017
40ba8f5
#15 - Additional methods. Rename some methods.
olegbal Jul 1, 2017
b2a500f
#16 - Add consuming media types.
olegbal Jul 1, 2017
85692eb
Add lombock dependency.
olegbal Jul 1, 2017
6e7d512
#15 - Add RequiredArgsConstructor lombock annotation. Fix method name…
olegbal Jul 1, 2017
1137322
#16 - Add RequiredArgsConstructor lombock annotation. Add new methods…
olegbal Jul 1, 2017
10b22c7
Remove getters and setters. Add @Data lombock annotation instead of t…
olegbal Jul 1, 2017
4d7be50
#16 - Rename PostController test to PostControllerTest. Add tests for…
olegbal Jul 2, 2017
8f7d249
Remove System.out.printl.
olegbal Jul 2, 2017
3132d31
Add @Autowired annotation for constructors.
olegbal Jul 5, 2017
1b40298
Move postService.update(post) method out of ResponseEntity
olegbal Jul 5, 2017
e1823c1
Delete no-args constructor.
olegbal Jul 5, 2017
a0e9523
Optimize imports. Remove org.springframework.test.web.servlet.request…
olegbal Jul 5, 2017
845cefa
Remove empty lines between class declaration and it's implementation.…
olegbal Jul 5, 2017
cdcd52f
Remove empty lines between class declaration and it's implementation.…
olegbal Jul 5, 2017
ff94988
Remove empty lines between class declaration and it's implementation.…
olegbal Jul 5, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ buildscript {

classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.standardout:gradle-versioneye-plugin:${versioneyeVersion}")

}
}

Expand Down Expand Up @@ -42,8 +41,10 @@ dependencies {
compile("org.springframework.boot:spring-boot-starter-jetty")
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
testCompile('org.springframework.boot:spring-boot-starter-test')
compile("org.codehaus.groovy:groovy-all:${groovyVersion}")
compileOnly("org.projectlombok:lombok")
}

findbugs {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ checkstyleVersion=7.8.1
jacocoVersion=0.7.9
groovyVersion=2.4.10
#ext versions
springBootVersion=1.5.3.RELEASE
springBootVersion=1.5.4.RELEASE
versioneyeVersion=1.4.0
1 change: 0 additions & 1 deletion src/main/java/com/github/dregheap/DregHeap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@SpringBootApplication
public class DregHeap {

public static void main(String[] args) {
SpringApplication.run(DregHeap.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.dregheap.controllers;
package com.github.dregheap.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -9,9 +9,8 @@
@RestController
@RequestMapping(path = "/api/v1/hello")
public class HelloController {

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity getHelloMessage() {
return new ResponseEntity("Hello world!", HttpStatus.OK);
return new ResponseEntity<>("Hello world!", HttpStatus.OK);
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/github/dregheap/controller/PostController.java
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why OK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

@RequestMapping(method = RequestMethod.DELETE, consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity deletePost(@RequestBody Post post) {
postService.delete(post);
return new ResponseEntity(HttpStatus.OK);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why OK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
15 changes: 15 additions & 0 deletions src/main/java/com/github/dregheap/model/Post.java
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> {
}
53 changes: 53 additions & 0 deletions src/main/java/com/github/dregheap/service/DefaultPostService.java
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if post is null?

post.setId(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null?? For what?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/github/dregheap/service/PostService.java
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);
}
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

spring.data:
mongodb:
uri: mongodb://admin:[email protected]:21212/robanukah
103 changes: 103 additions & 0 deletions src/test/java/com/github/dregheap/controller/PostControllerTest.java
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();
Copy link
Member

Choose a reason for hiding this comment

The 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());


}
}
Loading