|  | 
|  | 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 | +} | 
0 commit comments