|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import org.json.JSONObject; |
| 6 | +import org.junit.After; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.robolectric.RobolectricTestRunner; |
| 11 | +import org.robolectric.annotation.Config; |
| 12 | + |
| 13 | +import static org.junit.Assert.*; |
| 14 | + |
| 15 | +@RunWith(RobolectricTestRunner.class) |
| 16 | +@Config(sdk = 28, manifest = Config.NONE) |
| 17 | +public class TestContentType { |
| 18 | + |
| 19 | + private Context mockContext; |
| 20 | + private Stack stack; |
| 21 | + private ContentType contentType; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setUp() throws Exception { |
| 25 | + mockContext = TestUtils.createMockContext(); |
| 26 | + stack = Contentstack.stack(mockContext, |
| 27 | + TestUtils.getTestApiKey(), |
| 28 | + TestUtils.getTestDeliveryToken(), |
| 29 | + TestUtils.getTestEnvironment()); |
| 30 | + contentType = stack.contentType(TestUtils.getTestContentType()); |
| 31 | + TestUtils.cleanupTestCache(); |
| 32 | + } |
| 33 | + |
| 34 | + @After |
| 35 | + public void tearDown() { |
| 36 | + TestUtils.cleanupTestCache(); |
| 37 | + contentType = null; |
| 38 | + stack = null; |
| 39 | + mockContext = null; |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testContentTypeCreation() { |
| 44 | + assertNotNull("ContentType should not be null", contentType); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testEntryCreation() { |
| 49 | + Entry entry = contentType.entry(); |
| 50 | + assertNotNull("Entry should not be null", entry); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testEntryWithUid() { |
| 55 | + Entry entry = contentType.entry("test_entry_uid"); |
| 56 | + assertNotNull("Entry with uid should not be null", entry); |
| 57 | + assertEquals("Entry UID should match", "test_entry_uid", entry.getUid()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testEntryWithEmptyUid() { |
| 62 | + Entry entry = contentType.entry(""); |
| 63 | + assertNotNull("Entry should not be null with empty uid", entry); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testEntryWithNullUid() { |
| 68 | + Entry entry = contentType.entry(null); |
| 69 | + assertNotNull("Entry should not be null with null uid", entry); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testQueryCreation() { |
| 74 | + Query query = contentType.query(); |
| 75 | + assertNotNull("Query should not be null", query); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testSetHeader() { |
| 80 | + contentType.setHeader("custom-header", "custom-value"); |
| 81 | + assertNotNull("ContentType should not be null after setHeader", contentType); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testSetHeaderWithEmptyKey() { |
| 86 | + contentType.setHeader("", "value"); |
| 87 | + assertNotNull("ContentType should not be null", contentType); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testSetHeaderWithEmptyValue() { |
| 92 | + contentType.setHeader("key", ""); |
| 93 | + assertNotNull("ContentType should not be null", contentType); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testSetHeaderWithNullKey() { |
| 98 | + contentType.setHeader(null, "value"); |
| 99 | + assertNotNull("ContentType should not be null", contentType); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testSetHeaderWithNullValue() { |
| 104 | + contentType.setHeader("key", null); |
| 105 | + assertNotNull("ContentType should not be null", contentType); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testRemoveHeader() { |
| 110 | + contentType.setHeader("custom-header", "custom-value"); |
| 111 | + contentType.removeHeader("custom-header"); |
| 112 | + assertNotNull("ContentType should not be null after removeHeader", contentType); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testRemoveHeaderWithEmptyKey() { |
| 117 | + contentType.removeHeader(""); |
| 118 | + assertNotNull("ContentType should not be null", contentType); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testRemoveHeaderWithNullKey() { |
| 123 | + contentType.removeHeader(null); |
| 124 | + assertNotNull("ContentType should not be null", contentType); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testRemoveNonExistentHeader() { |
| 129 | + contentType.removeHeader("non-existent-header"); |
| 130 | + assertNotNull("ContentType should not be null", contentType); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testMultipleEntries() { |
| 135 | + Entry entry1 = contentType.entry("uid1"); |
| 136 | + Entry entry2 = contentType.entry("uid2"); |
| 137 | + Entry entry3 = contentType.entry("uid3"); |
| 138 | + |
| 139 | + assertNotNull("Entry 1 should not be null", entry1); |
| 140 | + assertNotNull("Entry 2 should not be null", entry2); |
| 141 | + assertNotNull("Entry 3 should not be null", entry3); |
| 142 | + assertNotEquals("Entries should be different instances", entry1, entry2); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testMultipleQueries() { |
| 147 | + Query query1 = contentType.query(); |
| 148 | + Query query2 = contentType.query(); |
| 149 | + |
| 150 | + assertNotNull("Query 1 should not be null", query1); |
| 151 | + assertNotNull("Query 2 should not be null", query2); |
| 152 | + assertNotEquals("Queries should be different instances", query1, query2); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testEntryAfterSettingHeaders() { |
| 157 | + contentType.setHeader("header1", "value1"); |
| 158 | + contentType.setHeader("header2", "value2"); |
| 159 | + |
| 160 | + Entry entry = contentType.entry("test_uid"); |
| 161 | + assertNotNull("Entry should not be null after setting headers", entry); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testQueryAfterSettingHeaders() { |
| 166 | + contentType.setHeader("header1", "value1"); |
| 167 | + contentType.setHeader("header2", "value2"); |
| 168 | + |
| 169 | + Query query = contentType.query(); |
| 170 | + assertNotNull("Query should not be null after setting headers", query); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testMultipleHeaderOperations() { |
| 175 | + contentType.setHeader("header1", "value1"); |
| 176 | + contentType.setHeader("header2", "value2"); |
| 177 | + contentType.removeHeader("header1"); |
| 178 | + contentType.setHeader("header3", "value3"); |
| 179 | + |
| 180 | + assertNotNull("ContentType should not be null after multiple operations", contentType); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testSetSameHeaderMultipleTimes() { |
| 185 | + contentType.setHeader("header", "value1"); |
| 186 | + contentType.setHeader("header", "value2"); |
| 187 | + contentType.setHeader("header", "value3"); |
| 188 | + |
| 189 | + assertNotNull("ContentType should not be null", contentType); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void testEntryWithLongUid() { |
| 194 | + String longUid = "a".repeat(100); |
| 195 | + Entry entry = contentType.entry(longUid); |
| 196 | + assertNotNull("Entry should not be null with long UID", entry); |
| 197 | + assertEquals("Entry UID should match", longUid, entry.getUid()); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void testEntryWithSpecialCharactersInUid() { |
| 202 | + String[] specialUids = {"uid-with-dashes", "uid_with_underscores", "uid.with.dots"}; |
| 203 | + |
| 204 | + for (String uid : specialUids) { |
| 205 | + Entry entry = contentType.entry(uid); |
| 206 | + assertNotNull("Entry should not be null for UID: " + uid, entry); |
| 207 | + assertEquals("Entry UID should match", uid, entry.getUid()); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void testHeaderWithSpecialCharacters() { |
| 213 | + contentType.setHeader("x-custom-header", "value"); |
| 214 | + contentType.setHeader("header_with_underscore", "value"); |
| 215 | + contentType.setHeader("header.with.dots", "value"); |
| 216 | + |
| 217 | + assertNotNull("ContentType should not be null with special character headers", contentType); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void testHeaderWithLongValue() { |
| 222 | + String longValue = "v".repeat(1000); |
| 223 | + contentType.setHeader("long-header", longValue); |
| 224 | + assertNotNull("ContentType should not be null with long header value", contentType); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + public void testQueryChaining() { |
| 229 | + Query query = contentType.query(); |
| 230 | + query.where("field", "value") |
| 231 | + .limit(10) |
| 232 | + .skip(5) |
| 233 | + .includeCount(); |
| 234 | + |
| 235 | + assertNotNull("Query should support chaining", query); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + public void testEntryChaining() { |
| 240 | + Entry entry = contentType.entry("test_uid"); |
| 241 | + entry.only(new String[]{"title"}) |
| 242 | + .setLocale("en-us") |
| 243 | + .includeReference("category"); |
| 244 | + |
| 245 | + assertNotNull("Entry should support chaining", entry); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void testConcurrentOperations() { |
| 250 | + contentType.setHeader("header1", "value1"); |
| 251 | + Entry entry = contentType.entry("uid1"); |
| 252 | + contentType.setHeader("header2", "value2"); |
| 253 | + Query query = contentType.query(); |
| 254 | + contentType.removeHeader("header1"); |
| 255 | + |
| 256 | + assertNotNull("Entry should not be null", entry); |
| 257 | + assertNotNull("Query should not be null", query); |
| 258 | + assertNotNull("ContentType should not be null", contentType); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + public void testMultipleContentTypesFromSameStack() { |
| 263 | + ContentType ct1 = stack.contentType("type1"); |
| 264 | + ContentType ct2 = stack.contentType("type2"); |
| 265 | + |
| 266 | + ct1.setHeader("header1", "value1"); |
| 267 | + ct2.setHeader("header2", "value2"); |
| 268 | + |
| 269 | + assertNotNull("ContentType 1 should not be null", ct1); |
| 270 | + assertNotNull("ContentType 2 should not be null", ct2); |
| 271 | + assertNotEquals("ContentTypes should be different instances", ct1, ct2); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void testHeaderPersistenceAcrossEntries() { |
| 276 | + contentType.setHeader("persistent-header", "persistent-value"); |
| 277 | + |
| 278 | + Entry entry1 = contentType.entry("uid1"); |
| 279 | + Entry entry2 = contentType.entry("uid2"); |
| 280 | + |
| 281 | + assertNotNull("Entry 1 should not be null", entry1); |
| 282 | + assertNotNull("Entry 2 should not be null", entry2); |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + public void testHeaderPersistenceAcrossQueries() { |
| 287 | + contentType.setHeader("persistent-header", "persistent-value"); |
| 288 | + |
| 289 | + Query query1 = contentType.query(); |
| 290 | + Query query2 = contentType.query(); |
| 291 | + |
| 292 | + assertNotNull("Query 1 should not be null", query1); |
| 293 | + assertNotNull("Query 2 should not be null", query2); |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + public void testEmptyContentTypeName() { |
| 298 | + ContentType emptyContentType = stack.contentType(""); |
| 299 | + assertNotNull("ContentType with empty name should not be null", emptyContentType); |
| 300 | + } |
| 301 | + |
| 302 | + @Test |
| 303 | + public void testContentTypeNameWithSpaces() { |
| 304 | + ContentType spacedContentType = stack.contentType("content type with spaces"); |
| 305 | + assertNotNull("ContentType with spaces should not be null", spacedContentType); |
| 306 | + } |
| 307 | + |
| 308 | + @Test |
| 309 | + public void testContentTypeNameWithNumbers() { |
| 310 | + ContentType numberedContentType = stack.contentType("content_type_123"); |
| 311 | + assertNotNull("ContentType with numbers should not be null", numberedContentType); |
| 312 | + } |
| 313 | + |
| 314 | + @Test |
| 315 | + public void testContentTypeIntegrity() { |
| 316 | + // Perform various operations |
| 317 | + contentType.setHeader("test", "value"); |
| 318 | + contentType.entry("test_uid"); |
| 319 | + contentType.query(); |
| 320 | + contentType.removeHeader("test"); |
| 321 | + |
| 322 | + // ContentType should still be valid |
| 323 | + assertNotNull("ContentType should maintain integrity", contentType); |
| 324 | + Entry newEntry = contentType.entry("another_uid"); |
| 325 | + assertNotNull("Should still be able to create entries", newEntry); |
| 326 | + } |
| 327 | +} |
| 328 | + |
0 commit comments