|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.robolectric.RobolectricTestRunner; |
| 6 | + |
| 7 | +import java.util.jar.Attributes; |
| 8 | + |
| 9 | +import static org.junit.Assert.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * Comprehensive unit tests for Metadata class. |
| 13 | + */ |
| 14 | +@RunWith(RobolectricTestRunner.class) |
| 15 | +public class TestMetadata { |
| 16 | + |
| 17 | + // ========== CONSTRUCTOR TESTS ========== |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testConstructorWithAllParameters() { |
| 21 | + Attributes attrs = new Attributes(); |
| 22 | + Metadata metadata = new Metadata("Sample text", "entry", "uid123", |
| 23 | + "content_type_uid", "block", "<div>HTML</div>", attrs); |
| 24 | + |
| 25 | + assertNotNull(metadata); |
| 26 | + assertEquals("Sample text", metadata.getText()); |
| 27 | + assertEquals("entry", metadata.getItemType()); |
| 28 | + assertEquals("uid123", metadata.getItemUid()); |
| 29 | + assertEquals("content_type_uid", metadata.getContentTypeUid()); |
| 30 | + assertEquals(StyleType.BLOCK, metadata.getStyleType()); |
| 31 | + assertEquals("<div>HTML</div>", metadata.getOuterHTML()); |
| 32 | + assertNotNull(metadata.getAttributes()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testConstructorWithBlockStyleType() { |
| 37 | + Attributes attrs = new Attributes(); |
| 38 | + Metadata metadata = new Metadata("text", "asset", "asset_uid", |
| 39 | + "asset", "block", "<p>content</p>", attrs); |
| 40 | + |
| 41 | + assertEquals(StyleType.BLOCK, metadata.getStyleType()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testConstructorWithInlineStyleType() { |
| 46 | + Attributes attrs = new Attributes(); |
| 47 | + Metadata metadata = new Metadata("text", "entry", "entry_uid", |
| 48 | + "blog", "inline", "<span>text</span>", attrs); |
| 49 | + |
| 50 | + assertEquals(StyleType.INLINE, metadata.getStyleType()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testConstructorWithLinkStyleType() { |
| 55 | + Attributes attrs = new Attributes(); |
| 56 | + Metadata metadata = new Metadata("Link text", "entry", "entry_123", |
| 57 | + "page", "link", "<a href='#'>Link</a>", attrs); |
| 58 | + |
| 59 | + assertEquals(StyleType.LINK, metadata.getStyleType()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testConstructorWithDisplayStyleType() { |
| 64 | + Attributes attrs = new Attributes(); |
| 65 | + Metadata metadata = new Metadata("Display", "asset", "img_uid", |
| 66 | + "asset", "display", "<img src='#'/>", attrs); |
| 67 | + |
| 68 | + assertEquals(StyleType.DISPLAY, metadata.getStyleType()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testConstructorWithDownloadStyleType() { |
| 73 | + Attributes attrs = new Attributes(); |
| 74 | + Metadata metadata = new Metadata("Download", "asset", "file_uid", |
| 75 | + "asset", "download", "<a download>File</a>", attrs); |
| 76 | + |
| 77 | + assertEquals(StyleType.DOWNLOAD, metadata.getStyleType()); |
| 78 | + } |
| 79 | + |
| 80 | + // ========== GETTER TESTS ========== |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGetText() { |
| 84 | + Metadata metadata = new Metadata("Test text content", "entry", "uid", |
| 85 | + "ct_uid", "block", "<div/>", new Attributes()); |
| 86 | + |
| 87 | + assertEquals("Test text content", metadata.getText()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testGetItemType() { |
| 92 | + Metadata metadata = new Metadata("text", "asset", "uid", |
| 93 | + "ct_uid", "block", "<div/>", new Attributes()); |
| 94 | + |
| 95 | + assertEquals("asset", metadata.getItemType()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testGetItemUid() { |
| 100 | + Metadata metadata = new Metadata("text", "entry", "unique_id_123", |
| 101 | + "ct_uid", "block", "<div/>", new Attributes()); |
| 102 | + |
| 103 | + assertEquals("unique_id_123", metadata.getItemUid()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testGetContentTypeUid() { |
| 108 | + Metadata metadata = new Metadata("text", "entry", "uid", |
| 109 | + "blog_post", "block", "<div/>", new Attributes()); |
| 110 | + |
| 111 | + assertEquals("blog_post", metadata.getContentTypeUid()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testGetStyleType() { |
| 116 | + Metadata metadata = new Metadata("text", "entry", "uid", |
| 117 | + "ct_uid", "inline", "<span/>", new Attributes()); |
| 118 | + |
| 119 | + assertEquals(StyleType.INLINE, metadata.getStyleType()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testGetOuterHTML() { |
| 124 | + String html = "<div class='content'>Hello World</div>"; |
| 125 | + Metadata metadata = new Metadata("text", "entry", "uid", |
| 126 | + "ct_uid", "block", html, new Attributes()); |
| 127 | + |
| 128 | + assertEquals(html, metadata.getOuterHTML()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testGetAttributes() { |
| 133 | + Attributes attrs = new Attributes(); |
| 134 | + attrs.putValue("key", "value"); |
| 135 | + Metadata metadata = new Metadata("text", "entry", "uid", |
| 136 | + "ct_uid", "block", "<div/>", attrs); |
| 137 | + |
| 138 | + assertNotNull(metadata.getAttributes()); |
| 139 | + assertEquals(attrs, metadata.getAttributes()); |
| 140 | + } |
| 141 | + |
| 142 | + // ========== TO STRING TESTS ========== |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testToString() { |
| 146 | + Metadata metadata = new Metadata("Sample", "entry", "uid123", |
| 147 | + "blog", "block", "<div/>", new Attributes()); |
| 148 | + |
| 149 | + String toString = metadata.toString(); |
| 150 | + assertNotNull(toString); |
| 151 | + assertTrue(toString.contains("Sample")); |
| 152 | + assertTrue(toString.contains("entry")); |
| 153 | + assertTrue(toString.contains("uid123")); |
| 154 | + assertTrue(toString.contains("blog")); |
| 155 | + assertTrue(toString.contains("BLOCK")); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testToStringContainsAllFields() { |
| 160 | + Metadata metadata = new Metadata("Text", "asset", "asset_uid", |
| 161 | + "asset", "inline", "<span>HTML</span>", new Attributes()); |
| 162 | + |
| 163 | + String toString = metadata.toString(); |
| 164 | + assertTrue(toString.contains("text='Text'")); |
| 165 | + assertTrue(toString.contains("type='asset'")); |
| 166 | + assertTrue(toString.contains("uid='asset_uid'")); |
| 167 | + assertTrue(toString.contains("contentTypeUid='asset'")); |
| 168 | + assertTrue(toString.contains("sysStyleType=INLINE")); |
| 169 | + } |
| 170 | + |
| 171 | + // ========== STYLE TYPE CONVERSION TESTS ========== |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testStyleTypeConversionWithLowerCase() { |
| 175 | + Metadata metadata = new Metadata("text", "entry", "uid", |
| 176 | + "ct_uid", "block", "<div/>", new Attributes()); |
| 177 | + |
| 178 | + assertEquals(StyleType.BLOCK, metadata.getStyleType()); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testStyleTypeConversionWithUpperCase() { |
| 183 | + Metadata metadata = new Metadata("text", "entry", "uid", |
| 184 | + "ct_uid", "INLINE", "<span/>", new Attributes()); |
| 185 | + |
| 186 | + assertEquals(StyleType.INLINE, metadata.getStyleType()); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void testStyleTypeConversionWithMixedCase() { |
| 191 | + Metadata metadata = new Metadata("text", "entry", "uid", |
| 192 | + "ct_uid", "LiNk", "<a/>", new Attributes()); |
| 193 | + |
| 194 | + assertEquals(StyleType.LINK, metadata.getStyleType()); |
| 195 | + } |
| 196 | + |
| 197 | + // ========== EDGE CASE TESTS ========== |
| 198 | + |
| 199 | + @Test |
| 200 | + public void testWithEmptyText() { |
| 201 | + Metadata metadata = new Metadata("", "entry", "uid", |
| 202 | + "ct_uid", "block", "<div/>", new Attributes()); |
| 203 | + |
| 204 | + assertEquals("", metadata.getText()); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void testWithEmptyHtml() { |
| 209 | + Metadata metadata = new Metadata("text", "entry", "uid", |
| 210 | + "ct_uid", "block", "", new Attributes()); |
| 211 | + |
| 212 | + assertEquals("", metadata.getOuterHTML()); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void testWithComplexHtml() { |
| 217 | + String complexHtml = "<div class='wrapper'><p>Text</p><img src='url'/></div>"; |
| 218 | + Metadata metadata = new Metadata("text", "asset", "uid", |
| 219 | + "asset", "display", complexHtml, new Attributes()); |
| 220 | + |
| 221 | + assertEquals(complexHtml, metadata.getOuterHTML()); |
| 222 | + } |
| 223 | + |
| 224 | + // ========== MULTIPLE INSTANCE TESTS ========== |
| 225 | + |
| 226 | + @Test |
| 227 | + public void testMultipleInstancesAreIndependent() { |
| 228 | + Metadata m1 = new Metadata("Text 1", "entry", "uid1", |
| 229 | + "ct1", "block", "<div/>", new Attributes()); |
| 230 | + Metadata m2 = new Metadata("Text 2", "asset", "uid2", |
| 231 | + "ct2", "inline", "<span/>", new Attributes()); |
| 232 | + |
| 233 | + assertNotEquals(m1.getText(), m2.getText()); |
| 234 | + assertNotEquals(m1.getItemType(), m2.getItemType()); |
| 235 | + assertNotEquals(m1.getItemUid(), m2.getItemUid()); |
| 236 | + assertNotEquals(m1.getContentTypeUid(), m2.getContentTypeUid()); |
| 237 | + assertNotEquals(m1.getStyleType(), m2.getStyleType()); |
| 238 | + } |
| 239 | +} |
| 240 | + |
0 commit comments