Skip to content

Commit

Permalink
Day 19: Loosing up dead weight.
Browse files Browse the repository at this point in the history
yanncourtel committed Dec 19, 2023
1 parent 89fd340 commit 615982f
Showing 10 changed files with 215 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ Here are the different challenges :
- [Day 16: Make this code immutable.](exercise/day16/docs/challenge.md)
- [Day 17: Design one test that has the impact of thousands.](exercise/day17/docs/challenge.md)
- [Day 18: Automatically detect Linguistic Anti-Patterns (LAP).](exercise/day18/docs/challenge.md)
- [Day 19: Loosing up dead weight.](exercise/day19/docs/challenge.md)

### Solutions
A solution proposal will be published here every day during the `Advent Of Craft` containing `the code` and a `step by step` guide.
19 changes: 19 additions & 0 deletions exercise/day19/docs/challenge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Day 19: Loosing up dead weight.

Today, you embark on the last few days of your journey.
You have been warned a big storm is brewing your way but home you go.

You will need to leave up extra weight and tighten up the ship
for the days to come.

The exercise of today deals with exception, again.

You are starting to know the formula. No more exceptions.

This time, the approach might be a bit different.

> **Challenge of day 19: No more exception authorized - use a custom Error.**
May your crafting journey continue!

- <u>💡HINT:</u> What object can you return to get the error state?
Binary file added exercise/day19/docs/snippet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions exercise/day19/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.advent-of-craft</groupId>
<artifactId>advent-of-craft2023</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>blog-part4</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<instancio-junit.version>3.0.0</instancio-junit.version>
<vavr.version>0.10.4</vavr.version>
</properties>

<dependencies>
<dependency>
<groupId>org.instancio</groupId>
<artifactId>instancio-junit</artifactId>
<version>${instancio-junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>
</dependencies>
</project>
44 changes: 44 additions & 0 deletions exercise/day19/src/main/java/blog/Article.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package blog;

import io.vavr.collection.Seq;

import java.time.LocalDate;

import static io.vavr.collection.List.of;

public class Article {
private final String name;
private final String content;
private final Seq<Comment> comments;

public Article(String name, String content) {
this(name, content, of());
}

private Article(String name, String content, Seq<Comment> comments) {
this.name = name;
this.content = content;
this.comments = comments;
}

private Article addComment(
String text,
String author,
LocalDate creationDate) throws CommentAlreadyExistException {
var comment = new Comment(text, author, creationDate);

if (comments.contains(comment)) {
throw new CommentAlreadyExistException();
}
return new Article(name, content, comments.append(comment));
}

public Article addComment(String text, String author) {
return addComment(text, author, LocalDate.now());
}

public Seq<Comment> getComments() {
return comments;
}
}

6 changes: 6 additions & 0 deletions exercise/day19/src/main/java/blog/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package blog;

import java.time.LocalDate;

public record Comment(String text, String author, LocalDate creationDate) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package blog;

public class CommentAlreadyExistException extends RuntimeException {
}
33 changes: 33 additions & 0 deletions exercise/day19/src/test/java/blog/ArticleBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package blog;

import java.util.HashMap;

import static org.instancio.Instancio.create;

public class ArticleBuilder {
public static final String AUTHOR = "Pablo Escobar";
public static final String COMMENT_TEXT = "Amazing article !!!";
private final HashMap<String, String> comments;

public ArticleBuilder() {
comments = new HashMap<>();
}

public static ArticleBuilder anArticle() {
return new ArticleBuilder();
}

public ArticleBuilder commented() {
this.comments.put(COMMENT_TEXT, AUTHOR);
return this;
}

public Article build() {
return comments.entrySet()
.stream()
.reduce(new Article(
create(String.class),
create(String.class)
), (a, e) -> a.addComment(e.getKey(), e.getValue()), (p, n) -> p);
}
}
74 changes: 74 additions & 0 deletions exercise/day19/src/test/java/blog/ArticleTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package blog;

import org.assertj.core.api.ThrowingConsumer;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.function.Function;

import static blog.ArticleBuilder.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.instancio.Instancio.create;

class ArticleTests {
private Article article;

@Test
void should_add_comment_in_an_article() {
when(article -> article.addComment(COMMENT_TEXT, AUTHOR));
then(article -> {
assertThat(article.getComments()).hasSize(1);
assertComment(article.getComments().get(0), COMMENT_TEXT, AUTHOR);
});
}

@Test
void should_add_comment_in_an_article_containing_already_a_comment() {
final var newComment = create(String.class);
final var newAuthor = create(String.class);

when(ArticleBuilder::commented, article -> article.addComment(newComment, newAuthor));
then(article -> {
assertThat(article.getComments()).hasSize(2);
assertComment(article.getComments().last(), newComment, newAuthor);
});
}

private static void assertComment(Comment comment, String commentText, String author) {
assertThat(comment.text()).isEqualTo(commentText);
assertThat(comment.author()).isEqualTo(author);
}

private void when(ArticleBuilder articleBuilder, Function<Article, Article> act) throws CommentAlreadyExistException {
article = act.apply(
articleBuilder.build()
);
}

private void when(Function<Article, Article> act) {
when(anArticle(), act);
}

private void when(Function<ArticleBuilder, ArticleBuilder> options, Function<Article, Article> act) {
when(options.apply(anArticle()), act);
}

private void then(ThrowingConsumer<Article> act) {
act.accept(article);
}

@Nested
class Fail {
@Test
void when__adding_an_existing_comment() {
var article = anArticle()
.commented()
.build();

assertThatThrownBy(() -> {
article.addComment(article.getComments().get(0).text(), article.getComments().get(0).author());
}).isInstanceOf(CommentAlreadyExistException.class);
}
}
}
1 change: 1 addition & 0 deletions exercise/pom.xml
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
<module>day16</module>
<module>day17</module>
<module>day18</module>
<module>day19</module>
</modules>

<properties>

0 comments on commit 615982f

Please sign in to comment.