Skip to content

Commit 5373888

Browse files
committed
add images as not required to pending tweet domain
1 parent cbadb6a commit 5373888

6 files changed

Lines changed: 552 additions & 81 deletions

File tree

src/main/java/com/mastercloudapps/twitterscheduler/domain/pending/PendingTweet.java

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
import static java.util.Objects.requireNonNull;
44

55
import java.time.Instant;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.Collection;
9+
import java.util.Collections;
10+
import java.util.List;
11+
import java.util.Objects;
12+
import java.util.stream.Collectors;
613

7-
import com.mastercloudapps.twitterscheduler.controller.exception.ExpiredPublicationDateException;
814
import com.mastercloudapps.twitterscheduler.domain.shared.AggregateRoot;
915
import com.mastercloudapps.twitterscheduler.domain.shared.Message;
1016
import com.mastercloudapps.twitterscheduler.domain.shared.NullableInstant;
@@ -16,14 +22,17 @@ public class PendingTweet extends AggregateRoot<PendingTweetId> {
1622
private Message message;
1723

1824
private NullableInstant publicationDate;
19-
25+
2026
private NullableInstant createdAt;
2127

28+
private final List<PendingTweetImage> images;
29+
2230
private PendingTweet(final Builder builder) {
2331
super(builder.pendingTweetId);
2432
this.message = builder.message;
2533
this.publicationDate = builder.publicationDate;
2634
this.createdAt = builder.createdAt;
35+
this.images = builder.images;
2736
}
2837

2938
public Message message() {
@@ -35,9 +44,9 @@ public NullableInstant publicationDate() {
3544

3645
return publicationDate;
3746
}
38-
47+
3948
public NullableInstant createdAt() {
40-
49+
4150
return createdAt;
4251
}
4352

@@ -60,17 +69,43 @@ public interface PublicationDateStep {
6069

6170
CreatedAtStep publicationDate(final Instant instant);
6271
}
63-
72+
6473
public interface CreatedAtStep {
65-
74+
6675
Build createdAt(final Instant instant);
6776
}
6877

6978
public interface Build {
79+
80+
Build images(List<PendingTweetImage> images);
7081

7182
PendingTweet build();
7283
}
7384

85+
public void addImages(PendingTweetImage... images) {
86+
this.images.addAll(Arrays.stream(images).filter(Objects::nonNull).collect(Collectors.toSet()));
87+
}
88+
89+
public void addImages(Collection<PendingTweetImage> images) {
90+
this.images.addAll(images.stream().filter(Objects::nonNull).collect(Collectors.toSet()));
91+
}
92+
93+
public void deleteImages(PendingTweetImage... images) {
94+
this.images.removeAll(Arrays.stream(images).filter(Objects::nonNull).collect(Collectors.toSet()));
95+
}
96+
97+
public void deleteImages(Collection<PendingTweetImage> images) {
98+
this.images.removeAll(images.stream().filter(Objects::nonNull).collect(Collectors.toSet()));
99+
}
100+
101+
public void deleteImages() {
102+
this.images.clear();
103+
}
104+
105+
public List<PendingTweetImage> getImages() {
106+
return Collections.unmodifiableList(images);
107+
}
108+
74109
public static class Builder implements IdStep, MessageStep, PublicationDateStep,
75110
CreatedAtStep, Build {
76111

@@ -79,8 +114,10 @@ public static class Builder implements IdStep, MessageStep, PublicationDateStep,
79114
private Message message;
80115

81116
private NullableInstant publicationDate;
82-
117+
83118
private NullableInstant createdAt;
119+
120+
private List<PendingTweetImage> images = new ArrayList<>();
84121

85122
@Override
86123
public MessageStep id(Long pendingTweetId) {
@@ -109,11 +146,17 @@ public Build createdAt(Instant instant) {
109146
this.createdAt = niCreatedAt;
110147
return this;
111148
}
112-
149+
113150
@Override
114151
public PendingTweet build() {
115152
return new PendingTweet(this);
116153
}
154+
155+
@Override
156+
public Build images(List<PendingTweetImage> images) {
157+
this.images = Objects.requireNonNull(images);
158+
return this;
159+
}
117160
}
118-
161+
119162
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.mastercloudapps.twitterscheduler.domain.pending;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import java.util.Objects;
6+
7+
import com.mastercloudapps.twitterscheduler.domain.shared.Entity;
8+
import com.mastercloudapps.twitterscheduler.domain.shared.Url;
9+
10+
public class PendingTweetImage extends Entity<PendingTweetImageId> {
11+
12+
private static final long serialVersionUID = 5298131475420038098L;
13+
14+
private Url url;
15+
16+
private PendingTweetImage(Builder builder) {
17+
super(builder.pendingTweetImageId);
18+
this.url = builder.url;
19+
}
20+
21+
public Url url() {
22+
return url;
23+
}
24+
25+
public static IdStep builder() {
26+
return new Builder();
27+
}
28+
29+
public interface IdStep {
30+
31+
UrlStep id(Long pendingTweetImageId);
32+
}
33+
34+
public interface UrlStep {
35+
36+
Build url(String url);
37+
}
38+
39+
public interface Build {
40+
41+
PendingTweetImage build();
42+
}
43+
44+
public static class Builder implements IdStep, UrlStep, Build {
45+
46+
private PendingTweetImageId pendingTweetImageId;
47+
48+
private Url url;
49+
50+
@Override
51+
public UrlStep id(Long pendingTweetImageId) {
52+
this.pendingTweetImageId = PendingTweetImageId.valueOf(requireNonNull(pendingTweetImageId, "Pending Tweet Image Id cannot be null."));
53+
return this;
54+
}
55+
56+
@Override
57+
public Build url(String url) {
58+
this.url = Url.valueOf(requireNonNull(url, "Url cannot be null."));
59+
return this;
60+
}
61+
62+
@Override
63+
public PendingTweetImage build() {
64+
return new PendingTweetImage(this);
65+
}
66+
67+
}
68+
69+
@Override
70+
public boolean equals(Object o) {
71+
if (this == o) {
72+
return true;
73+
}
74+
if (o == null || getClass() != o.getClass()) {
75+
return false;
76+
}
77+
if (!super.equals(o)) {
78+
return false;
79+
}
80+
PendingTweetImage that = (PendingTweetImage) o;
81+
return Objects.equals(url, that.url);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return Objects.hash(super.hashCode(), url);
87+
}
88+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.mastercloudapps.twitterscheduler.domain.pending;
2+
3+
import java.util.Objects;
4+
5+
import com.mastercloudapps.twitterscheduler.domain.shared.id.DomainObjectId;
6+
7+
public class PendingTweetImageId extends DomainObjectId<Long> {
8+
9+
private static final long serialVersionUID = -3692626899655506192L;
10+
11+
private static final Long DEFAULT_VALUE = Long.MAX_VALUE;
12+
13+
public PendingTweetImageId(Long id) {
14+
super(id);
15+
}
16+
17+
public static PendingTweetImageId valueOf(final Long id) {
18+
return new PendingTweetImageId(id);
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "PendingTweetImageId{" + this.id + "}";
24+
}
25+
26+
@Override
27+
public boolean equals(final Object o) {
28+
if (this == o) {
29+
return true;
30+
}
31+
if (o == null || getClass() != o.getClass()) {
32+
return false;
33+
}
34+
PendingTweetImageId that = (PendingTweetImageId) o;
35+
return Objects.equals(id, that.id);
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return Objects.hash(id);
41+
}
42+
43+
public static Long defaultValue() {
44+
return DEFAULT_VALUE;
45+
}
46+
47+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.mastercloudapps.twitterscheduler.domain.pending;
2+
3+
import static org.hamcrest.CoreMatchers.containsString;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
import static org.hamcrest.Matchers.is;
6+
import static org.hamcrest.Matchers.not;
7+
import static org.hamcrest.Matchers.notNullValue;
8+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.DisplayName;
13+
import org.junit.jupiter.api.Nested;
14+
import org.junit.jupiter.api.Test;
15+
16+
class PendingTweetImageIdTest {
17+
18+
private PendingTweetImageId createPendingTweetImageId(MockData mockData) {
19+
return PendingTweetImageId.valueOf(mockData.id);
20+
}
21+
22+
enum MockData {
23+
INVALID_ID(null),
24+
VALID_ID(1L),
25+
VALID_ID_OTHER(2L);
26+
27+
private final Long id;
28+
29+
MockData(Long id) {
30+
this.id = id;
31+
}
32+
}
33+
34+
@Nested
35+
@DisplayName("Test plan creation with invalid params")
36+
class TestPlanCreationWithInvalidParams {
37+
38+
@Test
39+
@DisplayName("Test creation with null id, expected NullPointerException")
40+
void testNullId() {
41+
42+
assertThrows(NullPointerException.class, () -> createPendingTweetImageId(MockData.INVALID_ID));
43+
}
44+
}
45+
46+
@Nested
47+
@DisplayName("Test plan creation")
48+
class TestPlanCreation {
49+
50+
private PendingTweetImageId pendingTweetImageId;
51+
52+
@BeforeEach
53+
void setUp() {
54+
55+
pendingTweetImageId = createPendingTweetImageId(MockData.VALID_ID);
56+
}
57+
58+
@Test
59+
@DisplayName("Test creation, expected not null")
60+
void testNotNull() {
61+
62+
assertThat(pendingTweetImageId, is(notNullValue()));
63+
}
64+
65+
@Test
66+
@DisplayName("Test creation, expected id")
67+
void testEqualsId() {
68+
69+
assertThat(pendingTweetImageId.id(), is(MockData.VALID_ID.id));
70+
}
71+
72+
@Test
73+
@DisplayName("Test toString, expected id contained")
74+
void testToString() {
75+
76+
assertThat(pendingTweetImageId.toString(), containsString(MockData.VALID_ID.id.toString()));
77+
}
78+
}
79+
80+
@Nested
81+
@DisplayName("Test plan for equals and hashcode")
82+
class TestPlanEqualsHashCode {
83+
84+
private PendingTweetImageId pendingTweetImageId;
85+
86+
@BeforeEach
87+
void setUp() {
88+
89+
pendingTweetImageId = createPendingTweetImageId(MockData.VALID_ID);
90+
}
91+
92+
@Test
93+
@DisplayName("Test with itself, expected equals")
94+
void testEqualsItself() {
95+
96+
assertThat(pendingTweetImageId, is(pendingTweetImageId));
97+
}
98+
99+
@Test
100+
@DisplayName("Test with same id, expected equals and same hashcode")
101+
void testSameId() {
102+
103+
var same = createPendingTweetImageId(MockData.VALID_ID);
104+
assertThat(pendingTweetImageId, is(same));
105+
assertThat(pendingTweetImageId.hashCode(), is(same.hashCode()));
106+
}
107+
108+
@Test
109+
@DisplayName("Test with other null, expected not equals")
110+
void testNotEqualsNull() {
111+
112+
assertNotEquals(null, pendingTweetImageId);
113+
}
114+
115+
@Test
116+
@DisplayName("Test with different id, expected not equals and different hashCode")
117+
void testDifferentId() {
118+
119+
var other = createPendingTweetImageId(MockData.VALID_ID_OTHER);
120+
assertThat(pendingTweetImageId, is(not(other)));
121+
assertThat(pendingTweetImageId.hashCode(), is(not(other.hashCode())));
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)