Skip to content
This repository was archived by the owner on Jul 7, 2021. It is now read-only.

Commit 0a7a324

Browse files
committed
#21: Improve test coverage >80%
1 parent 0e02bd8 commit 0a7a324

File tree

7 files changed

+139
-22
lines changed

7 files changed

+139
-22
lines changed

src/main/java/org/proshin/blog/configuration/ApplicationContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
66
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
77
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
8+
import lombok.extern.log4j.Log4j;
89
import org.springframework.context.annotation.Bean;
910
import org.springframework.context.annotation.Configuration;
1011
import org.springframework.web.client.RestTemplate;
1112

13+
@Log4j
1214
@Configuration
1315
public class ApplicationContext {
1416

17+
public ApplicationContext() {
18+
log.info("A new instance of application context has been instantiated");
19+
}
20+
1521
@Bean
1622
public RestTemplate getRestTemplate() {
1723
return new RestTemplate();

src/main/java/org/proshin/blog/dynamodb/DynamoPost.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import static java.time.LocalDateTime.now;
99
import java.util.UUID;
1010
import lombok.NonNull;
11+
import org.apache.commons.lang3.builder.EqualsBuilder;
12+
import org.apache.commons.lang3.builder.HashCodeBuilder;
1113
import org.proshin.blog.DateToString;
1214
import org.proshin.blog.StringToDate;
1315
import org.proshin.blog.exception.PostNotFoundException;
@@ -117,4 +119,36 @@ public PersistentPost save() {
117119
return new DynamoPost(posts, posts.getItem(new PrimaryKey("id", generatedId)));
118120
}
119121
}
122+
123+
@Override
124+
public boolean equals(Object o) {
125+
if (this == o)
126+
return true;
127+
128+
if (o == null || getClass() != o.getClass())
129+
return false;
130+
131+
DynamoPost that = (DynamoPost) o;
132+
133+
return new EqualsBuilder()
134+
.append(published, that.published)
135+
.append(id, that.id)
136+
.append(title, that.title)
137+
.append(creationDate, that.creationDate)
138+
.append(publicationDate, that.publicationDate)
139+
.append(content, that.content)
140+
.isEquals();
141+
}
142+
143+
@Override
144+
public int hashCode() {
145+
return new HashCodeBuilder(17, 37)
146+
.append(id)
147+
.append(title)
148+
.append(creationDate)
149+
.append(publicationDate)
150+
.append(published)
151+
.append(content)
152+
.toHashCode();
153+
}
120154
}

src/main/java/org/proshin/blog/dynamodb/DynamoPosts.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
44
import com.amazonaws.services.dynamodbv2.document.Item;
5-
import com.amazonaws.services.dynamodbv2.document.Page;
65
import com.amazonaws.services.dynamodbv2.document.PrimaryKey;
76
import com.amazonaws.services.dynamodbv2.document.ScanOutcome;
87
import com.amazonaws.services.dynamodbv2.document.Table;
9-
import com.google.common.collect.Iterables;
8+
import com.amazonaws.services.dynamodbv2.document.internal.PageIterable;
109
import static com.google.common.collect.Lists.newArrayList;
1110
import static java.time.LocalDateTime.now;
12-
import static java.util.Collections.emptyList;
1311
import static java.util.Collections.singletonMap;
1412
import java.util.List;
1513
import lombok.NonNull;
@@ -44,31 +42,30 @@ public PersistentPost selectOne(@NonNull String id) throws PostNotFoundException
4442
@NonNull
4543
@Override
4644
public List<PersistentPost> selectPage(int offset, int count, boolean publishedOnly) {
47-
Page<Item, ScanOutcome> firstPage =
45+
PageIterable<Item, ScanOutcome> pages =
4846
publishedOnly
49-
? Iterables.getFirst(
50-
posts.scan("published = :published",
47+
? posts
48+
.scan("published = :published",
5149
null,
52-
singletonMap(":published", true)).pages(),
53-
null)
54-
: Iterables.getFirst(
55-
posts.scan().pages(),
56-
null);
57-
if (firstPage == null) {
58-
return emptyList();
59-
}
50+
singletonMap(":published", true))
51+
.pages()
52+
: posts
53+
.scan()
54+
.pages();
6055

6156
List<PersistentPost> dynamoPosts = newArrayList();
62-
for (Item item : firstPage) {
63-
dynamoPosts.add(new DynamoPost(posts, item));
64-
}
57+
pages.forEach(page -> {
58+
page.forEach(item -> {
59+
dynamoPosts.add(new DynamoPost(posts, item));
60+
});
61+
});
6562
return dynamoPosts;
6663
}
6764

6865
@NonNull
6966
@Override
70-
public PersistentPost create() {
71-
return new DynamoPost(posts, "New article", now(), now(), false, "It's a draft")
67+
public PersistentPost create(@NonNull String title, @NonNull String content) {
68+
return new DynamoPost(posts, title, now(), now(), false, content)
7269
.save();
7370
}
7471

src/main/java/org/proshin/blog/model/Posts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface Posts {
1212
List<PersistentPost> selectPage(int offset, int count, boolean publishedOnly);
1313

1414
@NonNull
15-
PersistentPost create();
15+
PersistentPost create(@NonNull String title, @NonNull String content);
1616

1717
void delete(@NonNull String id);
1818
}

src/main/java/org/proshin/blog/page/admin/AdminPostsPagesController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ModelAndView getPosts() {
4141
public ModelAndView create() {
4242
return new SmartModelAndView(
4343
String.format("redirect:/admin/posts/%s/edit",
44-
posts.create().getId()));
44+
posts.create("New post", "It's a draft").getId()));
4545
}
4646

4747
@GetMapping("/{id}/edit")
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.proshin.blog.dynamodb;
2+
3+
import static java.time.LocalDateTime.now;
4+
import static java.util.Collections.singletonList;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import static org.hamcrest.CoreMatchers.hasItems;
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.junit.Assert.assertThat;
10+
import org.junit.Test;
11+
import org.proshin.blog.AbstractIntegrationTest;
12+
import org.proshin.blog.exception.PostNotFoundException;
13+
import org.proshin.blog.model.PersistentPost;
14+
import org.proshin.blog.model.Post;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
17+
public class DynamoPostsTest extends AbstractIntegrationTest {
18+
19+
@Autowired
20+
private DynamoPosts posts;
21+
22+
@Test
23+
public void testSelectOne_findExistingPost() {
24+
PersistentPost post =
25+
new DynamoPost(posts.getTable(), "Published post", now(), now(), true, "Some content")
26+
.save();
27+
assertThat(posts.selectOne(post.getId()), is(post));
28+
}
29+
30+
@Test(expected = PostNotFoundException.class)
31+
public void testSelectOne_findNonExistingPost() {
32+
posts.selectOne("ID of non-existing post");
33+
}
34+
35+
@Test
36+
public void testThatPublishedAreFilteredOut() {
37+
String publishedId =
38+
new DynamoPost(posts.getTable(), "Published post", now(), now(), true, "Some content")
39+
.save()
40+
.getId();
41+
String nonPublishedId =
42+
new DynamoPost(posts.getTable(), "NOT published post", now(), now(), false, "Some content")
43+
.save()
44+
.getId();
45+
46+
List<String> allPostsPage =
47+
posts.selectPage(0, 1, false)
48+
.stream()
49+
.map(Post::getId)
50+
.collect(Collectors.toList());
51+
assertThat(allPostsPage, hasItems(publishedId, nonPublishedId));
52+
53+
List<String> publishedPage =
54+
posts.selectPage(0, 1, true)
55+
.stream()
56+
.map(Post::getId)
57+
.collect(Collectors.toList());
58+
assertThat(publishedPage, is(singletonList(publishedId)));
59+
}
60+
61+
@Test
62+
public void testCreate() {
63+
String createdPostId = posts.create("New post", "It's a draft").getId();
64+
PersistentPost post = posts.selectOne(createdPostId);
65+
assertThat(post.getTitle(), is("New post"));
66+
assertThat(post.getContent(), is("It's a draft"));
67+
}
68+
69+
@Test(expected = PostNotFoundException.class)
70+
public void testDelete() {
71+
PersistentPost post =
72+
new DynamoPost(posts.getTable(), "Published post", now(), now(), true, "Some content")
73+
.save();
74+
75+
posts.delete(post.getId());
76+
77+
posts.selectOne(post.getId());
78+
}
79+
}

src/test/resources/application.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ blog:
1313
secretKey: dummy
1414
reCaptcha:
1515
key: dummy
16-
secret: dummy
16+
secret: dummy
17+
logging.level.org.springframework.test.context.cache: debug

0 commit comments

Comments
 (0)