Skip to content

Commit

Permalink
impl get post by id & get all post with a basic int. test
Browse files Browse the repository at this point in the history
  • Loading branch information
singals committed Aug 22, 2017
1 parent f549503 commit 768ccbd
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 53 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
<groupId>com.thoughtworks</groupId>
<artifactId>spring-boot-poc</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>spring-boot-poc</name>
Expand Down Expand Up @@ -44,6 +56,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
46 changes: 31 additions & 15 deletions src/main/java/com/thoughtworks/post/PostController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thoughtworks.post;

import lombok.Getter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.hateoas.EntityLinks;
Expand All @@ -8,45 +9,60 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/post")
@ExposesResourceFor(Post.class)
public class PostController {

@Getter
Map<UUID, Post> posts = new HashMap<>();

private static Log logger = LogFactory.getLog(PostController.class);
private Map<UUID, Post> posts = new HashMap();
private final EntityLinks entityLinks;

public PostController(EntityLinks entityLinks) {
this.entityLinks = entityLinks;
}

@PostMapping
public ResponseEntity<Resource<Post>> createPost(@RequestBody @Valid Post post,
HttpServletResponse response) {
public ResponseEntity<Resource<Post>> createPost(@RequestBody @Valid Post post) {

UUID uuid = UUID.randomUUID();
posts.put(uuid, post);

Resource<Post> postResource = new Resource(post);
postResource.add(entityLinks.linkToSingleResource(Post.class, uuid.toString()));

return new ResponseEntity<Resource<Post>>(postResource, HttpStatus.OK);
return new ResponseEntity<>(postResource, HttpStatus.CREATED);
}

@GetMapping(path = "/{id}")
public ResponseEntity<Resource<Post>> getAllPosts(@PathVariable UUID id) {
Post post = posts.get(id);
Resource<Post> postResource = new Resource(post);
HttpStatus status = HttpStatus.NOT_FOUND;
if (post != null) {
postResource.add(entityLinks.linkToSingleResource(Post.class, id.toString()));
status = HttpStatus.OK;
}
return new ResponseEntity<>(postResource, status);
}

@GetMapping()
public Post getPostById() {
return null;
@GetMapping
public ResponseEntity<List<Resource<Post>>> getAllPosts() {
Set<Map.Entry<UUID, Post>> entries = posts.entrySet();
List<Resource<Post>> posts = entries.stream().map(entry -> {
Post post = entry.getValue();
Resource<Post> postResource = new Resource(post);
postResource.add(entityLinks.linkToSingleResource(Post.class, entry.getKey().toString()));
return postResource;
}).collect(Collectors.toList());
return new ResponseEntity<>(posts, HttpStatus.OK);
}
}
38 changes: 0 additions & 38 deletions src/test/java/com/thoughtworks/SpringBootPocApplicationTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.thoughtworks.post;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class PostControllerIntegrationTest {

@Autowired
private TestRestTemplate restTemplate;

@Autowired
private PostController postController;

@Test
public void testThatAPostIsCreated() throws Exception {
Post post = new Post("Test Title", "Test Content");
ResponseEntity<String> entity = this.restTemplate.postForEntity("/post", post, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.CREATED);
String entityBody = entity.getBody();
assertThat(entityBody).startsWith(
"{\"title\":\"Test Title\",\"content\":\"Test Content\"");
assertThat(entityBody).contains("_links\":{\"self\":{\"href\"");

int idStartIndex = entityBody.lastIndexOf("/") + 1;
String id = entityBody.substring(idStartIndex, entityBody.length() - 4);

assertNotNull(postController.getPosts().get(UUID.fromString(id)));
}

}

0 comments on commit 768ccbd

Please sign in to comment.