Skip to content

Commit 615982f

Browse files
committed
Day 19: Loosing up dead weight.
1 parent 89fd340 commit 615982f

File tree

10 files changed

+215
-0
lines changed

10 files changed

+215
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Here are the different challenges :
6464
- [Day 16: Make this code immutable.](exercise/day16/docs/challenge.md)
6565
- [Day 17: Design one test that has the impact of thousands.](exercise/day17/docs/challenge.md)
6666
- [Day 18: Automatically detect Linguistic Anti-Patterns (LAP).](exercise/day18/docs/challenge.md)
67+
- [Day 19: Loosing up dead weight.](exercise/day19/docs/challenge.md)
6768

6869
### Solutions
6970
A solution proposal will be published here every day during the `Advent Of Craft` containing `the code` and a `step by step` guide.

exercise/day19/docs/challenge.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Day 19: Loosing up dead weight.
2+
3+
Today, you embark on the last few days of your journey.
4+
You have been warned a big storm is brewing your way but home you go.
5+
6+
You will need to leave up extra weight and tighten up the ship
7+
for the days to come.
8+
9+
The exercise of today deals with exception, again.
10+
11+
You are starting to know the formula. No more exceptions.
12+
13+
This time, the approach might be a bit different.
14+
15+
> **Challenge of day 19: No more exception authorized - use a custom Error.**
16+
17+
May your crafting journey continue!
18+
19+
- <u>💡HINT:</u> What object can you return to get the error state?

exercise/day19/docs/snippet.png

477 KB
Loading

exercise/day19/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.advent-of-craft</groupId>
9+
<artifactId>advent-of-craft2023</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>blog-part4</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
<properties>
16+
<instancio-junit.version>3.0.0</instancio-junit.version>
17+
<vavr.version>0.10.4</vavr.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.instancio</groupId>
23+
<artifactId>instancio-junit</artifactId>
24+
<version>${instancio-junit.version}</version>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>io.vavr</groupId>
29+
<artifactId>vavr</artifactId>
30+
<version>${vavr.version}</version>
31+
</dependency>
32+
</dependencies>
33+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package blog;
2+
3+
import io.vavr.collection.Seq;
4+
5+
import java.time.LocalDate;
6+
7+
import static io.vavr.collection.List.of;
8+
9+
public class Article {
10+
private final String name;
11+
private final String content;
12+
private final Seq<Comment> comments;
13+
14+
public Article(String name, String content) {
15+
this(name, content, of());
16+
}
17+
18+
private Article(String name, String content, Seq<Comment> comments) {
19+
this.name = name;
20+
this.content = content;
21+
this.comments = comments;
22+
}
23+
24+
private Article addComment(
25+
String text,
26+
String author,
27+
LocalDate creationDate) throws CommentAlreadyExistException {
28+
var comment = new Comment(text, author, creationDate);
29+
30+
if (comments.contains(comment)) {
31+
throw new CommentAlreadyExistException();
32+
}
33+
return new Article(name, content, comments.append(comment));
34+
}
35+
36+
public Article addComment(String text, String author) {
37+
return addComment(text, author, LocalDate.now());
38+
}
39+
40+
public Seq<Comment> getComments() {
41+
return comments;
42+
}
43+
}
44+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package blog;
2+
3+
import java.time.LocalDate;
4+
5+
public record Comment(String text, String author, LocalDate creationDate) {
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package blog;
2+
3+
public class CommentAlreadyExistException extends RuntimeException {
4+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package blog;
2+
3+
import java.util.HashMap;
4+
5+
import static org.instancio.Instancio.create;
6+
7+
public class ArticleBuilder {
8+
public static final String AUTHOR = "Pablo Escobar";
9+
public static final String COMMENT_TEXT = "Amazing article !!!";
10+
private final HashMap<String, String> comments;
11+
12+
public ArticleBuilder() {
13+
comments = new HashMap<>();
14+
}
15+
16+
public static ArticleBuilder anArticle() {
17+
return new ArticleBuilder();
18+
}
19+
20+
public ArticleBuilder commented() {
21+
this.comments.put(COMMENT_TEXT, AUTHOR);
22+
return this;
23+
}
24+
25+
public Article build() {
26+
return comments.entrySet()
27+
.stream()
28+
.reduce(new Article(
29+
create(String.class),
30+
create(String.class)
31+
), (a, e) -> a.addComment(e.getKey(), e.getValue()), (p, n) -> p);
32+
}
33+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package blog;
2+
3+
import org.assertj.core.api.ThrowingConsumer;
4+
import org.junit.jupiter.api.Nested;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.function.Function;
8+
9+
import static blog.ArticleBuilder.*;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
12+
import static org.instancio.Instancio.create;
13+
14+
class ArticleTests {
15+
private Article article;
16+
17+
@Test
18+
void should_add_comment_in_an_article() {
19+
when(article -> article.addComment(COMMENT_TEXT, AUTHOR));
20+
then(article -> {
21+
assertThat(article.getComments()).hasSize(1);
22+
assertComment(article.getComments().get(0), COMMENT_TEXT, AUTHOR);
23+
});
24+
}
25+
26+
@Test
27+
void should_add_comment_in_an_article_containing_already_a_comment() {
28+
final var newComment = create(String.class);
29+
final var newAuthor = create(String.class);
30+
31+
when(ArticleBuilder::commented, article -> article.addComment(newComment, newAuthor));
32+
then(article -> {
33+
assertThat(article.getComments()).hasSize(2);
34+
assertComment(article.getComments().last(), newComment, newAuthor);
35+
});
36+
}
37+
38+
private static void assertComment(Comment comment, String commentText, String author) {
39+
assertThat(comment.text()).isEqualTo(commentText);
40+
assertThat(comment.author()).isEqualTo(author);
41+
}
42+
43+
private void when(ArticleBuilder articleBuilder, Function<Article, Article> act) throws CommentAlreadyExistException {
44+
article = act.apply(
45+
articleBuilder.build()
46+
);
47+
}
48+
49+
private void when(Function<Article, Article> act) {
50+
when(anArticle(), act);
51+
}
52+
53+
private void when(Function<ArticleBuilder, ArticleBuilder> options, Function<Article, Article> act) {
54+
when(options.apply(anArticle()), act);
55+
}
56+
57+
private void then(ThrowingConsumer<Article> act) {
58+
act.accept(article);
59+
}
60+
61+
@Nested
62+
class Fail {
63+
@Test
64+
void when__adding_an_existing_comment() {
65+
var article = anArticle()
66+
.commented()
67+
.build();
68+
69+
assertThatThrownBy(() -> {
70+
article.addComment(article.getComments().get(0).text(), article.getComments().get(0).author());
71+
}).isInstanceOf(CommentAlreadyExistException.class);
72+
}
73+
}
74+
}

exercise/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<module>day16</module>
2828
<module>day17</module>
2929
<module>day18</module>
30+
<module>day19</module>
3031
</modules>
3132

3233
<properties>

0 commit comments

Comments
 (0)