Skip to content

Commit c38995c

Browse files
committed
adding dlqObject and dlqWriter interface to
Signed-off-by: Christopher Manning <cmanning09@users.noreply.github.com>
1 parent b3142e0 commit c38995c

File tree

5 files changed

+443
-0
lines changed

5 files changed

+443
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.dataprepper.model.failures;
7+
8+
import java.time.Instant;
9+
import java.time.ZoneId;
10+
import java.time.format.DateTimeFormatter;
11+
import java.util.Objects;
12+
13+
import static com.google.common.base.Preconditions.checkArgument;
14+
import static com.google.common.base.Preconditions.checkNotNull;
15+
16+
/**
17+
* A model representing DLQ objects in Data Prepper
18+
*
19+
* @since 2.2
20+
*/
21+
public class DlqObject {
22+
23+
private static final String ISO8601_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
24+
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(ISO8601_FORMAT_STRING)
25+
.withZone(ZoneId.systemDefault());;
26+
27+
private static final String VERSION = "1";
28+
29+
private final String pluginId;
30+
31+
private final String pluginName;
32+
33+
private final String pipelineName;
34+
35+
private final String version;
36+
37+
private final Object failedData;
38+
39+
private final String timestamp;
40+
41+
private DlqObject(final String pluginId, final String pluginName, final String pipelineName, final Object failedData) {
42+
43+
checkNotNull(pluginId, "pluginId cannot be null");
44+
checkArgument(!pluginId.isEmpty(), "pluginId cannot be an empty string");
45+
checkNotNull(pluginName, "pluginName cannot be null");
46+
checkArgument(!pluginName.isEmpty(), "pluginName cannot be an empty string");
47+
checkNotNull(pipelineName, "pipelineName cannot be null");
48+
checkArgument(!pipelineName.isEmpty(), "pipelineName cannot be an empty string");
49+
checkNotNull(failedData, "failedData cannot be null");
50+
51+
this.pluginId = pluginId;
52+
this.pluginName = pluginName;
53+
this.pipelineName = pipelineName;
54+
this.version = VERSION;
55+
this.failedData = failedData;
56+
57+
this.timestamp = FORMATTER.format(Instant.now());
58+
}
59+
60+
public String getPluginId() {
61+
return pluginId;
62+
}
63+
64+
public String getPluginName() {
65+
return pluginName;
66+
}
67+
68+
public String getPipelineName() {
69+
return pipelineName;
70+
}
71+
72+
public String getVersion() {
73+
return version;
74+
}
75+
76+
public Object getFailedData() {
77+
return failedData;
78+
}
79+
80+
public String getTimestamp() {
81+
return timestamp;
82+
}
83+
84+
@Override
85+
public boolean equals(final Object o) {
86+
if (this == o) return true;
87+
if (o == null || getClass() != o.getClass()) return false;
88+
final DlqObject that = (DlqObject) o;
89+
return Objects.equals(timestamp, that.getTimestamp())
90+
&& Objects.equals(failedData, that.getFailedData())
91+
&& Objects.equals(pluginId, that.pluginId)
92+
&& Objects.equals(pluginName, that.pluginName)
93+
&& Objects.equals(pipelineName, that.pipelineName)
94+
&& Objects.equals(version, that.version);
95+
}
96+
97+
@Override
98+
public int hashCode() {
99+
return Objects.hash(pluginId, pluginName, pipelineName, version, timestamp, failedData);
100+
}
101+
102+
@Override
103+
public String toString() {
104+
return "DlqObject{" +
105+
"pluginId='" + pluginId + '\'' +
106+
", pluginName='" + pluginName + '\'' +
107+
", pipelineName='" + pipelineName + '\'' +
108+
", version='" + version + '\'' +
109+
", timestamp='" + timestamp + '\'' +
110+
", failedData=" + failedData +
111+
'}';
112+
}
113+
114+
public static Builder builder() {
115+
return new Builder();
116+
}
117+
118+
public static class Builder {
119+
120+
private String pluginId;
121+
private String pluginName;
122+
private String pipelineName;
123+
private Object failedData;
124+
125+
public Builder withPluginId(final String pluginId) {
126+
this.pluginId = pluginId;
127+
return this;
128+
}
129+
130+
public Builder withPluginName(final String pluginName) {
131+
this.pluginName = pluginName;
132+
return this;
133+
}
134+
135+
public Builder withPipelineName(final String pipelineName) {
136+
this.pipelineName = pipelineName;
137+
return this;
138+
}
139+
140+
public Builder withFailedData(final Object failedData) {
141+
this.failedData = failedData;
142+
return this;
143+
}
144+
145+
public DlqObject build() {
146+
return new DlqObject(this.pluginId, this.pluginName, this.pipelineName, this.failedData);
147+
}
148+
149+
}
150+
}
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.dataprepper.model.failures;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Nested;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.time.Instant;
13+
import java.time.LocalDateTime;
14+
import java.time.ZoneId;
15+
import java.time.format.DateTimeFormatter;
16+
import java.util.concurrent.TimeUnit;
17+
18+
import static java.util.UUID.randomUUID;
19+
import static org.hamcrest.CoreMatchers.allOf;
20+
import static org.hamcrest.CoreMatchers.containsString;
21+
import static org.hamcrest.CoreMatchers.equalTo;
22+
import static org.hamcrest.CoreMatchers.is;
23+
import static org.hamcrest.CoreMatchers.not;
24+
import static org.hamcrest.CoreMatchers.notNullValue;
25+
import static org.hamcrest.MatcherAssert.assertThat;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
import static org.hamcrest.Matchers.lessThanOrEqualTo;
28+
29+
public class DlqObjectTest {
30+
31+
private static final String ISO8601_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
32+
private String pluginId;
33+
private String pluginName;
34+
private String pipelineName;
35+
private Object failedData;
36+
37+
@BeforeEach
38+
public void setUp() {
39+
pluginId = randomUUID().toString();
40+
pluginName = randomUUID().toString();
41+
pipelineName = randomUUID().toString();
42+
failedData = randomUUID();
43+
}
44+
45+
@Test
46+
public void testBuild() {
47+
48+
final DlqObject testObject = DlqObject.builder()
49+
.withPluginId(pluginId)
50+
.withPluginName(pluginName)
51+
.withPipelineName(pipelineName)
52+
.withFailedData(failedData)
53+
.build();
54+
55+
assertThat(testObject, is(notNullValue()));
56+
}
57+
58+
@Nested
59+
public class InvalidBuildParameters {
60+
61+
private void createTestObject() {
62+
DlqObject.builder()
63+
.withPluginId(pluginId)
64+
.withPluginName(pluginName)
65+
.withPipelineName(pipelineName)
66+
.withFailedData(failedData)
67+
.build();
68+
}
69+
@Test
70+
public void testInvalidPluginId() {
71+
pluginId = null;
72+
assertThrows(NullPointerException.class, this::createTestObject);
73+
pluginId = "";
74+
assertThrows(IllegalArgumentException.class, this::createTestObject);
75+
}
76+
77+
@Test
78+
public void testInvalidPluginName() {
79+
pluginName = null;
80+
assertThrows(NullPointerException.class, this::createTestObject);
81+
pluginName = "";
82+
assertThrows(IllegalArgumentException.class, this::createTestObject);
83+
}
84+
85+
@Test
86+
public void testInvalidPipelineName() {
87+
pipelineName = null;
88+
assertThrows(NullPointerException.class, this::createTestObject);
89+
pipelineName = "";
90+
assertThrows(IllegalArgumentException.class, this::createTestObject);
91+
}
92+
93+
@Test
94+
public void testInvalidFailedData() {
95+
failedData = null;
96+
assertThrows(NullPointerException.class, this::createTestObject);
97+
}
98+
}
99+
100+
@Nested
101+
class Getters {
102+
103+
private DlqObject testObject;
104+
105+
@BeforeEach
106+
public void setup() {
107+
108+
testObject = DlqObject.builder()
109+
.withPluginId(pluginId)
110+
.withPluginName(pluginName)
111+
.withPipelineName(pipelineName)
112+
.withFailedData(failedData)
113+
.build();
114+
}
115+
116+
@Test
117+
public void testGetPluginId() {
118+
final String actualPluginId = testObject.getPluginId();
119+
assertThat(actualPluginId, is(notNullValue()));
120+
assertThat(actualPluginId, is(pluginId));
121+
}
122+
123+
@Test
124+
public void testGetPluginName() {
125+
final String actualPluginName = testObject.getPluginName();
126+
assertThat(actualPluginName, is(notNullValue()));
127+
assertThat(actualPluginName, is(pluginName));
128+
}
129+
130+
@Test
131+
public void testGetPipelineName() {
132+
final String actualPipelineName = testObject.getPipelineName();
133+
assertThat(actualPipelineName, is(notNullValue()));
134+
assertThat(actualPipelineName, is(pipelineName));
135+
}
136+
137+
@Test
138+
public void testGetFailedData() {
139+
final Object actualFailedData = testObject.getFailedData();
140+
assertThat(actualFailedData, is(notNullValue()));
141+
assertThat(actualFailedData, is(failedData));
142+
}
143+
144+
@Test
145+
public void testGetTimestamp() {
146+
final String string = testObject.getTimestamp();
147+
assertThat(string, is(notNullValue()));
148+
149+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(ISO8601_FORMAT_STRING); // Specify locale to determine human language and cultural norms used in translating that input string.
150+
Instant actualTimestamp = LocalDateTime.parse(testObject.getTimestamp(), formatter)
151+
.atZone(ZoneId.systemDefault().normalized())
152+
.toInstant();
153+
154+
assertThat(actualTimestamp, is(lessThanOrEqualTo(Instant.now())));
155+
}
156+
157+
@Test
158+
public void testGetVersion() {
159+
final String string = testObject.getVersion();
160+
assertThat(string, is(notNullValue()));
161+
assertThat(string, is(equalTo("1")));
162+
}
163+
}
164+
165+
166+
@Nested
167+
class EqualsAndToString {
168+
169+
private DlqObject testObject;
170+
171+
@BeforeEach
172+
public void setup() {
173+
testObject = DlqObject.builder()
174+
.withPluginId(pluginId)
175+
.withPluginName(pluginName)
176+
.withPipelineName(pipelineName)
177+
.withFailedData(failedData)
178+
.build();
179+
}
180+
181+
@Test
182+
void equals_returns_false_for_null() {
183+
assertThat(testObject.equals(null), equalTo(false));
184+
}
185+
186+
@Test
187+
void equals_on_same_instance_returns_true() {
188+
assertThat(testObject, equalTo(testObject));
189+
}
190+
191+
@Test
192+
void equals_returns_false_for_two_instances_with_different_values() throws InterruptedException {
193+
TimeUnit.SECONDS.sleep(1); // To ensure that the timestamp is different
194+
195+
final DlqObject otherTestObject = DlqObject.builder()
196+
.withPluginId(pluginId)
197+
.withPluginName(pluginName)
198+
.withPipelineName(pipelineName)
199+
.withFailedData(failedData)
200+
.build();
201+
202+
assertThat(testObject, is(not(equalTo(otherTestObject))
203+
));
204+
}
205+
206+
@Test
207+
void toString_has_all_values() {
208+
final String string = testObject.toString();
209+
210+
assertThat(string, notNullValue());
211+
assertThat(string, allOf(
212+
containsString("DlqObject"),
213+
containsString(pluginId),
214+
containsString(pluginName),
215+
containsString(pipelineName),
216+
containsString(failedData.toString())
217+
));
218+
}
219+
}
220+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
/*
3+
* Copyright OpenSearch Contributors
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
dependencies {
8+
implementation project(':data-prepper-api')
9+
}

0 commit comments

Comments
 (0)