|
| 1 | +package org.fisco.bcos.sdk.v3.test.client.protocol.request; |
| 2 | + |
| 3 | +import org.fisco.bcos.sdk.v3.client.protocol.request.JsonRpcRequest; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.atomic.AtomicLong; |
| 12 | + |
| 13 | +public class JsonRpcRequestTest { |
| 14 | + |
| 15 | + private JsonRpcRequest<String> request; |
| 16 | + private List<String> testParams; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setUp() { |
| 20 | + // Reset the ID getter to ensure consistent test behavior |
| 21 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 22 | + |
| 23 | + testParams = Arrays.asList("param1", "param2"); |
| 24 | + request = new JsonRpcRequest<>("testMethod", testParams); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testConstructor() { |
| 29 | + JsonRpcRequest<String> newRequest = new JsonRpcRequest<>("method1", testParams); |
| 30 | + |
| 31 | + Assert.assertNotNull(newRequest); |
| 32 | + Assert.assertEquals("method1", newRequest.getMethod()); |
| 33 | + Assert.assertEquals(testParams, newRequest.getParams()); |
| 34 | + Assert.assertEquals("2.0", newRequest.getJsonrpc()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testConstructorWithNullParams() { |
| 39 | + JsonRpcRequest<String> newRequest = new JsonRpcRequest<>("method1", null); |
| 40 | + |
| 41 | + Assert.assertNotNull(newRequest); |
| 42 | + Assert.assertEquals("method1", newRequest.getMethod()); |
| 43 | + Assert.assertNull(newRequest.getParams()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testConstructorWithEmptyParams() { |
| 48 | + JsonRpcRequest<String> newRequest = new JsonRpcRequest<>("method1", Collections.emptyList()); |
| 49 | + |
| 50 | + Assert.assertNotNull(newRequest); |
| 51 | + Assert.assertEquals("method1", newRequest.getMethod()); |
| 52 | + Assert.assertEquals(0, newRequest.getParams().size()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testIdIncrement() { |
| 57 | + // Reset the ID getter |
| 58 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(100)); |
| 59 | + |
| 60 | + JsonRpcRequest<String> request1 = new JsonRpcRequest<>("method1", testParams); |
| 61 | + JsonRpcRequest<String> request2 = new JsonRpcRequest<>("method2", testParams); |
| 62 | + JsonRpcRequest<String> request3 = new JsonRpcRequest<>("method3", testParams); |
| 63 | + |
| 64 | + Assert.assertEquals(100L, request1.getId()); |
| 65 | + Assert.assertEquals(101L, request2.getId()); |
| 66 | + Assert.assertEquals(102L, request3.getId()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testGetJsonrpc() { |
| 71 | + Assert.assertEquals("2.0", request.getJsonrpc()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testSetJsonrpc() { |
| 76 | + request.setJsonrpc("3.0"); |
| 77 | + Assert.assertEquals("3.0", request.getJsonrpc()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testGetMethod() { |
| 82 | + Assert.assertEquals("testMethod", request.getMethod()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testSetMethod() { |
| 87 | + request.setMethod("newMethod"); |
| 88 | + Assert.assertEquals("newMethod", request.getMethod()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testGetParams() { |
| 93 | + Assert.assertEquals(testParams, request.getParams()); |
| 94 | + Assert.assertEquals(2, request.getParams().size()); |
| 95 | + Assert.assertEquals("param1", request.getParams().get(0)); |
| 96 | + Assert.assertEquals("param2", request.getParams().get(1)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testSetParams() { |
| 101 | + List<String> newParams = Arrays.asList("new1", "new2", "new3"); |
| 102 | + request.setParams(newParams); |
| 103 | + |
| 104 | + Assert.assertEquals(newParams, request.getParams()); |
| 105 | + Assert.assertEquals(3, request.getParams().size()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testGetId() { |
| 110 | + long id = request.getId(); |
| 111 | + Assert.assertTrue(id >= 0); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testSetId() { |
| 116 | + request.setId(999L); |
| 117 | + Assert.assertEquals(999L, request.getId()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testGetNextIdGetter() { |
| 122 | + AtomicLong getter = JsonRpcRequest.getNextIdGetter(); |
| 123 | + Assert.assertNotNull(getter); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testSetNextIdGetter() { |
| 128 | + AtomicLong newGetter = new AtomicLong(500); |
| 129 | + JsonRpcRequest.setNextIdGetter(newGetter); |
| 130 | + |
| 131 | + JsonRpcRequest<String> newRequest = new JsonRpcRequest<>("method", testParams); |
| 132 | + Assert.assertEquals(500L, newRequest.getId()); |
| 133 | + |
| 134 | + JsonRpcRequest<String> anotherRequest = new JsonRpcRequest<>("method", testParams); |
| 135 | + Assert.assertEquals(501L, anotherRequest.getId()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testEquals() { |
| 140 | + // Reset ID getter for consistent IDs |
| 141 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 142 | + |
| 143 | + JsonRpcRequest<String> request1 = new JsonRpcRequest<>("method1", testParams); |
| 144 | + JsonRpcRequest<String> request2 = new JsonRpcRequest<>("method1", testParams); |
| 145 | + |
| 146 | + // Set same ID for comparison |
| 147 | + request2.setId(request1.getId()); |
| 148 | + |
| 149 | + Assert.assertEquals(request1, request2); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testEqualsWithSameObject() { |
| 154 | + Assert.assertEquals(request, request); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testEqualsWithNull() { |
| 159 | + Assert.assertNotEquals(request, null); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testEqualsWithDifferentClass() { |
| 164 | + Assert.assertNotEquals(request, "string"); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testEqualsWithDifferentMethod() { |
| 169 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 170 | + |
| 171 | + JsonRpcRequest<String> request1 = new JsonRpcRequest<>("method1", testParams); |
| 172 | + JsonRpcRequest<String> request2 = new JsonRpcRequest<>("method2", testParams); |
| 173 | + |
| 174 | + request2.setId(request1.getId()); |
| 175 | + |
| 176 | + Assert.assertNotEquals(request1, request2); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testEqualsWithDifferentParams() { |
| 181 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 182 | + |
| 183 | + JsonRpcRequest<String> request1 = new JsonRpcRequest<>("method1", testParams); |
| 184 | + JsonRpcRequest<String> request2 = new JsonRpcRequest<>("method1", Arrays.asList("different")); |
| 185 | + |
| 186 | + request2.setId(request1.getId()); |
| 187 | + |
| 188 | + Assert.assertNotEquals(request1, request2); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testEqualsWithDifferentId() { |
| 193 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 194 | + |
| 195 | + JsonRpcRequest<String> request1 = new JsonRpcRequest<>("method1", testParams); |
| 196 | + JsonRpcRequest<String> request2 = new JsonRpcRequest<>("method1", testParams); |
| 197 | + |
| 198 | + Assert.assertNotEquals(request1, request2); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void testEqualsWithDifferentJsonrpc() { |
| 203 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 204 | + |
| 205 | + JsonRpcRequest<String> request1 = new JsonRpcRequest<>("method1", testParams); |
| 206 | + JsonRpcRequest<String> request2 = new JsonRpcRequest<>("method1", testParams); |
| 207 | + |
| 208 | + request2.setId(request1.getId()); |
| 209 | + request2.setJsonrpc("3.0"); |
| 210 | + |
| 211 | + Assert.assertNotEquals(request1, request2); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void testHashCode() { |
| 216 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 217 | + |
| 218 | + JsonRpcRequest<String> request1 = new JsonRpcRequest<>("method1", testParams); |
| 219 | + JsonRpcRequest<String> request2 = new JsonRpcRequest<>("method1", testParams); |
| 220 | + |
| 221 | + request2.setId(request1.getId()); |
| 222 | + |
| 223 | + Assert.assertEquals(request1.hashCode(), request2.hashCode()); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + public void testHashCodeConsistency() { |
| 228 | + int hashCode1 = request.hashCode(); |
| 229 | + int hashCode2 = request.hashCode(); |
| 230 | + |
| 231 | + Assert.assertEquals(hashCode1, hashCode2); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + public void testToString() { |
| 236 | + String str = request.toString(); |
| 237 | + |
| 238 | + Assert.assertNotNull(str); |
| 239 | + Assert.assertTrue(str.contains("JsonRpcRequest")); |
| 240 | + Assert.assertTrue(str.contains("jsonrpc")); |
| 241 | + Assert.assertTrue(str.contains("method")); |
| 242 | + Assert.assertTrue(str.contains("params")); |
| 243 | + Assert.assertTrue(str.contains("id")); |
| 244 | + Assert.assertTrue(str.contains("testMethod")); |
| 245 | + Assert.assertTrue(str.contains("2.0")); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void testToStringWithNullParams() { |
| 250 | + JsonRpcRequest<String> nullParamsRequest = new JsonRpcRequest<>("method", null); |
| 251 | + String str = nullParamsRequest.toString(); |
| 252 | + |
| 253 | + Assert.assertNotNull(str); |
| 254 | + Assert.assertTrue(str.contains("null")); |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + public void testGenericTypeWithInteger() { |
| 259 | + List<Integer> intParams = Arrays.asList(1, 2, 3); |
| 260 | + JsonRpcRequest<Integer> intRequest = new JsonRpcRequest<>("intMethod", intParams); |
| 261 | + |
| 262 | + Assert.assertEquals("intMethod", intRequest.getMethod()); |
| 263 | + Assert.assertEquals(intParams, intRequest.getParams()); |
| 264 | + Assert.assertEquals(Integer.valueOf(1), intRequest.getParams().get(0)); |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + public void testGenericTypeWithObject() { |
| 269 | + List<Object> objParams = Arrays.asList("string", 123, true); |
| 270 | + JsonRpcRequest<Object> objRequest = new JsonRpcRequest<>("objMethod", objParams); |
| 271 | + |
| 272 | + Assert.assertEquals("objMethod", objRequest.getMethod()); |
| 273 | + Assert.assertEquals(objParams, objRequest.getParams()); |
| 274 | + Assert.assertEquals(3, objRequest.getParams().size()); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + public void testConcurrentIdGeneration() throws InterruptedException { |
| 279 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 280 | + |
| 281 | + int threadCount = 10; |
| 282 | + int requestsPerThread = 10; |
| 283 | + Thread[] threads = new Thread[threadCount]; |
| 284 | + |
| 285 | + for (int i = 0; i < threadCount; i++) { |
| 286 | + threads[i] = new Thread(() -> { |
| 287 | + for (int j = 0; j < requestsPerThread; j++) { |
| 288 | + new JsonRpcRequest<>("method", testParams); |
| 289 | + } |
| 290 | + }); |
| 291 | + threads[i].start(); |
| 292 | + } |
| 293 | + |
| 294 | + for (Thread thread : threads) { |
| 295 | + thread.join(); |
| 296 | + } |
| 297 | + |
| 298 | + // After all threads complete, the next ID should be threadCount * requestsPerThread |
| 299 | + JsonRpcRequest<String> finalRequest = new JsonRpcRequest<>("method", testParams); |
| 300 | + Assert.assertEquals(threadCount * requestsPerThread, finalRequest.getId()); |
| 301 | + } |
| 302 | + |
| 303 | + @Test |
| 304 | + public void testDefaultJsonrpcVersion() { |
| 305 | + JsonRpcRequest<String> newRequest = new JsonRpcRequest<>("method", testParams); |
| 306 | + Assert.assertEquals("2.0", newRequest.getJsonrpc()); |
| 307 | + } |
| 308 | + |
| 309 | + @Test |
| 310 | + public void testMultipleRequestsWithDifferentIds() { |
| 311 | + JsonRpcRequest.setNextIdGetter(new AtomicLong(0)); |
| 312 | + |
| 313 | + JsonRpcRequest<String> req1 = new JsonRpcRequest<>("method1", testParams); |
| 314 | + JsonRpcRequest<String> req2 = new JsonRpcRequest<>("method2", testParams); |
| 315 | + JsonRpcRequest<String> req3 = new JsonRpcRequest<>("method3", testParams); |
| 316 | + |
| 317 | + Assert.assertNotEquals(req1.getId(), req2.getId()); |
| 318 | + Assert.assertNotEquals(req2.getId(), req3.getId()); |
| 319 | + Assert.assertNotEquals(req1.getId(), req3.getId()); |
| 320 | + } |
| 321 | +} |
0 commit comments