|
| 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 static org.junit.Assert.*; |
| 8 | + |
| 9 | +/** |
| 10 | + * Comprehensive unit tests for ResponseType enum. |
| 11 | + */ |
| 12 | +@RunWith(RobolectricTestRunner.class) |
| 13 | +public class TestResponseType { |
| 14 | + |
| 15 | + // ========== ENUM VALUES TESTS ========== |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testEnumValues() { |
| 19 | + ResponseType[] types = ResponseType.values(); |
| 20 | + assertNotNull(types); |
| 21 | + assertEquals(3, types.length); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testNetworkTypeExists() { |
| 26 | + ResponseType type = ResponseType.NETWORK; |
| 27 | + assertNotNull(type); |
| 28 | + assertEquals("NETWORK", type.name()); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testCacheTypeExists() { |
| 33 | + ResponseType type = ResponseType.CACHE; |
| 34 | + assertNotNull(type); |
| 35 | + assertEquals("CACHE", type.name()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testUnknownTypeExists() { |
| 40 | + ResponseType type = ResponseType.UNKNOWN; |
| 41 | + assertNotNull(type); |
| 42 | + assertEquals("UNKNOWN", type.name()); |
| 43 | + } |
| 44 | + |
| 45 | + // ========== VALUE OF TESTS ========== |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testValueOfNetwork() { |
| 49 | + ResponseType type = ResponseType.valueOf("NETWORK"); |
| 50 | + assertEquals(ResponseType.NETWORK, type); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testValueOfCache() { |
| 55 | + ResponseType type = ResponseType.valueOf("CACHE"); |
| 56 | + assertEquals(ResponseType.CACHE, type); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testValueOfUnknown() { |
| 61 | + ResponseType type = ResponseType.valueOf("UNKNOWN"); |
| 62 | + assertEquals(ResponseType.UNKNOWN, type); |
| 63 | + } |
| 64 | + |
| 65 | + @Test(expected = IllegalArgumentException.class) |
| 66 | + public void testValueOfInvalid() { |
| 67 | + ResponseType.valueOf("INVALID"); |
| 68 | + } |
| 69 | + |
| 70 | + // ========== ORDINAL TESTS ========== |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testNetworkOrdinal() { |
| 74 | + assertEquals(0, ResponseType.NETWORK.ordinal()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testCacheOrdinal() { |
| 79 | + assertEquals(1, ResponseType.CACHE.ordinal()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testUnknownOrdinal() { |
| 84 | + assertEquals(2, ResponseType.UNKNOWN.ordinal()); |
| 85 | + } |
| 86 | + |
| 87 | + // ========== EQUALITY TESTS ========== |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testEnumEquality() { |
| 91 | + ResponseType type1 = ResponseType.NETWORK; |
| 92 | + ResponseType type2 = ResponseType.NETWORK; |
| 93 | + |
| 94 | + assertEquals(type1, type2); |
| 95 | + assertSame(type1, type2); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testEnumInequality() { |
| 100 | + ResponseType network = ResponseType.NETWORK; |
| 101 | + ResponseType cache = ResponseType.CACHE; |
| 102 | + |
| 103 | + assertNotEquals(network, cache); |
| 104 | + assertNotSame(network, cache); |
| 105 | + } |
| 106 | + |
| 107 | + // ========== SWITCH STATEMENT TESTS ========== |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testSwitchStatement() { |
| 111 | + String result = getTypeDescription(ResponseType.NETWORK); |
| 112 | + assertEquals("Response from network", result); |
| 113 | + |
| 114 | + result = getTypeDescription(ResponseType.CACHE); |
| 115 | + assertEquals("Response from cache", result); |
| 116 | + |
| 117 | + result = getTypeDescription(ResponseType.UNKNOWN); |
| 118 | + assertEquals("Unknown response", result); |
| 119 | + } |
| 120 | + |
| 121 | + private String getTypeDescription(ResponseType type) { |
| 122 | + switch (type) { |
| 123 | + case NETWORK: |
| 124 | + return "Response from network"; |
| 125 | + case CACHE: |
| 126 | + return "Response from cache"; |
| 127 | + case UNKNOWN: |
| 128 | + return "Unknown response"; |
| 129 | + default: |
| 130 | + return "Unexpected type"; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // ========== ITERATION TESTS ========== |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testIterateAllTypes() { |
| 138 | + int count = 0; |
| 139 | + for (ResponseType type : ResponseType.values()) { |
| 140 | + assertNotNull(type); |
| 141 | + assertNotNull(type.name()); |
| 142 | + count++; |
| 143 | + } |
| 144 | + assertEquals(3, count); |
| 145 | + } |
| 146 | + |
| 147 | + // ========== TO STRING TESTS ========== |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testToString() { |
| 151 | + assertEquals("NETWORK", ResponseType.NETWORK.toString()); |
| 152 | + assertEquals("CACHE", ResponseType.CACHE.toString()); |
| 153 | + assertEquals("UNKNOWN", ResponseType.UNKNOWN.toString()); |
| 154 | + } |
| 155 | + |
| 156 | + // ========== NAME TESTS ========== |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testName() { |
| 160 | + assertEquals("NETWORK", ResponseType.NETWORK.name()); |
| 161 | + assertEquals("CACHE", ResponseType.CACHE.name()); |
| 162 | + assertEquals("UNKNOWN", ResponseType.UNKNOWN.name()); |
| 163 | + } |
| 164 | + |
| 165 | + // ========== ARRAY USAGE TESTS ========== |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testCanBeUsedInArray() { |
| 169 | + ResponseType[] supportedTypes = { |
| 170 | + ResponseType.NETWORK, |
| 171 | + ResponseType.CACHE |
| 172 | + }; |
| 173 | + |
| 174 | + assertEquals(2, supportedTypes.length); |
| 175 | + assertEquals(ResponseType.NETWORK, supportedTypes[0]); |
| 176 | + assertEquals(ResponseType.CACHE, supportedTypes[1]); |
| 177 | + } |
| 178 | + |
| 179 | + // ========== COMPARISON TESTS ========== |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testCompareTo() { |
| 183 | + assertTrue(ResponseType.NETWORK.compareTo(ResponseType.CACHE) < 0); |
| 184 | + assertTrue(ResponseType.CACHE.compareTo(ResponseType.UNKNOWN) < 0); |
| 185 | + assertTrue(ResponseType.UNKNOWN.compareTo(ResponseType.NETWORK) > 0); |
| 186 | + assertEquals(0, ResponseType.NETWORK.compareTo(ResponseType.NETWORK)); |
| 187 | + } |
| 188 | + |
| 189 | + // ========== ALL TYPES IN ORDER TESTS ========== |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testAllTypesInOrder() { |
| 193 | + ResponseType[] types = ResponseType.values(); |
| 194 | + assertEquals(ResponseType.NETWORK, types[0]); |
| 195 | + assertEquals(ResponseType.CACHE, types[1]); |
| 196 | + assertEquals(ResponseType.UNKNOWN, types[2]); |
| 197 | + } |
| 198 | + |
| 199 | + // ========== USAGE PATTERN TESTS ========== |
| 200 | + |
| 201 | + @Test |
| 202 | + public void testUsageInConditional() { |
| 203 | + ResponseType type = ResponseType.NETWORK; |
| 204 | + |
| 205 | + if (type == ResponseType.NETWORK) { |
| 206 | + assertTrue(true); // Expected path |
| 207 | + } else { |
| 208 | + fail("Should be NETWORK"); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void testUsageInMultipleConditionals() { |
| 214 | + ResponseType type = ResponseType.CACHE; |
| 215 | + |
| 216 | + boolean isCache = type == ResponseType.CACHE; |
| 217 | + boolean isNetwork = type == ResponseType.NETWORK; |
| 218 | + boolean isUnknown = type == ResponseType.UNKNOWN; |
| 219 | + |
| 220 | + assertTrue(isCache); |
| 221 | + assertFalse(isNetwork); |
| 222 | + assertFalse(isUnknown); |
| 223 | + } |
| 224 | +} |
0 commit comments