|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Worldline, Inc. |
| 3 | + * |
| 4 | + * MultiChainJavaAPI code distributed under the GPLv3 license, see COPYING file. |
| 5 | + * https://github.com/SimplyUb/MultiChainJavaAPI/blob/master/LICENSE |
| 6 | + * |
| 7 | + */ |
| 8 | +package multichain.object.formatters; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import org.junit.Assert; |
| 13 | +import org.junit.Test; |
| 14 | +import com.google.gson.Gson; |
| 15 | +import com.google.gson.internal.LinkedTreeMap; |
| 16 | +import multichain.command.CommandElt; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Ub - H. MARTEAU |
| 20 | + * @version 2.0.1 |
| 21 | + */ |
| 22 | +public class GenericOutputFormatterTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testFormatBlock() { |
| 26 | + String blockJsonValue = |
| 27 | + "{\"hash\": \"00cd11ab4a7146236595c29f52a874521845cb2a417da4e938266e1a3a1f3bb2\"," |
| 28 | + + "\"miner\": \"13SKg7Qw7UxPWvjDMkskyvKU7HfM9mz1KHi43E\"," + "\"confirmations\": 235," |
| 29 | + + "\"size\": 241," + "\"height\": 0," + "\"version\": 1," |
| 30 | + + "\"merkleroot\": \"428c3d6b97720175dba80c893ec97407522f564af5665ecff0684cd8d74eae6e\"," |
| 31 | + + "\"tx\": [" + " \"428c3d6b97720175dba80c893ec97407522f564af5665ecff0684cd8d74eae6e\"" |
| 32 | + + "]," + "\"time\": 1543504235," + "\"nonce\": 97," + "\"bits\": \"2000ffff\"," |
| 33 | + + "\"difficulty\": 5.96046447753906E-8," |
| 34 | + + "\"chainwork\": \"0000000000000000000000000000000000000000000000000000000000000100\"," |
| 35 | + + "\"nextblockhash\": \"0074f303c0b50dcf4a0f8e48840c14af9dd5666f5f953b154b9d7d2549ea02f9\"" |
| 36 | + + "}"; |
| 37 | + |
| 38 | + Class<?>[] returnedTypes = CommandElt.GETBLOCK.getReturnedClass(); |
| 39 | + LinkedTreeMap<String, Object> linkedTreeMapValue = |
| 40 | + new Gson().fromJson(blockJsonValue, LinkedTreeMap.class); |
| 41 | + |
| 42 | + Object returnedValue = GenericOutputFormatter.format(linkedTreeMapValue, returnedTypes); |
| 43 | + |
| 44 | + System.out.println(returnedValue); |
| 45 | + System.out.println(returnedValue.getClass()); |
| 46 | + |
| 47 | + Assert.assertNotNull(returnedValue); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testFormatAddressesSimple() { |
| 53 | + String addresseJsonValue01 = "\"11ADDRESSE01ADDRESSE01ADDRESSE01111111\""; |
| 54 | + String addresseJsonValue02 = "\"22ADDRESSE02ADDRESSE02ADDRESSE02222222\""; |
| 55 | + String addresseJsonValue03 = "\"33ADDRESSE03ADDRESSE03ADDRESSE03333333\""; |
| 56 | + String addresseJsonValue04 = "\"44ADDRESSE04ADDRESSE04ADDRESSE04444444\""; |
| 57 | + |
| 58 | + Class<?>[] returnedTypes = CommandElt.GETADDRESSES.getReturnedClass(); |
| 59 | + String linkedTreeMapValue1 = new Gson().fromJson(addresseJsonValue01, String.class); |
| 60 | + String linkedTreeMapValue2 = new Gson().fromJson(addresseJsonValue02, String.class); |
| 61 | + String linkedTreeMapValue3 = new Gson().fromJson(addresseJsonValue03, String.class); |
| 62 | + String linkedTreeMapValue4 = new Gson().fromJson(addresseJsonValue04, String.class); |
| 63 | + |
| 64 | + List<String> listLinkedTreeMap = new ArrayList<>(); |
| 65 | + listLinkedTreeMap.add(linkedTreeMapValue1); |
| 66 | + listLinkedTreeMap.add(linkedTreeMapValue2); |
| 67 | + listLinkedTreeMap.add(linkedTreeMapValue3); |
| 68 | + listLinkedTreeMap.add(linkedTreeMapValue4); |
| 69 | + |
| 70 | + Object returnedValue = GenericOutputFormatter.formatList(listLinkedTreeMap, returnedTypes); |
| 71 | + |
| 72 | + System.out.println(returnedValue); |
| 73 | + System.out.println(returnedValue.getClass()); |
| 74 | + if (ArrayList.class.isInstance(returnedValue) && returnedValue != null |
| 75 | + && ((ArrayList) returnedValue).size() > 0) { |
| 76 | + System.out.println(((ArrayList) returnedValue).get(0).getClass()); |
| 77 | + } |
| 78 | + |
| 79 | + Assert.assertNotNull(returnedValue); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testFormatAddressesVerbose() { |
| 87 | + String addresseJsonValue01 = |
| 88 | + "{\"address\": \"11ADDRESSE01ADDRESSE01ADDRESSE01111111\",\"ismine\": true," |
| 89 | + + "\"iswatchonly\": false,\"isscript\": false," |
| 90 | + + "\"pubkey\": \"000011ADDRESSE01ADDRESSE01ADDRESSE01111111ADRESSE01ADDRESSE0111111\"," |
| 91 | + + "\"iscompressed\": true,\"account\": \"adresse01\"}"; |
| 92 | + String addresseJsonValue02 = |
| 93 | + "{\"address\": \"22ADDRESSE02ADDRESSE02ADDRESSE02222222\",\"ismine\": true," |
| 94 | + + "\"iswatchonly\": false,\"isscript\": false," |
| 95 | + + "\"pubkey\": \"000022ADDRESSE02ADDRESSE02ADDRESSE02222222ADRESSE02ADDRESSE0222222\"," |
| 96 | + + "\"iscompressed\": true,\"account\": \"adresse02\"}"; |
| 97 | + String addresseJsonValue03 = |
| 98 | + "{\"address\": \"33ADDRESSE03ADDRESSE03ADDRESSE03333333\",\"ismine\": true," |
| 99 | + + "\"iswatchonly\": false,\"isscript\": false," |
| 100 | + + "\"pubkey\": \"000033ADDRESSE03ADDRESSE03ADDRESSE03333333ADRESSE03ADDRESSE0333333\"," |
| 101 | + + "\"iscompressed\": true,\"account\": \"adresse03\"}"; |
| 102 | + String addresseJsonValue04 = |
| 103 | + "{\"address\": \"44ADDRESSE04ADDRESSE04ADDRESSE04444444\",\"ismine\": true," |
| 104 | + + "\"iswatchonly\": false,\"isscript\": false," |
| 105 | + + "\"pubkey\": \"000044ADDRESSE04ADDRESSE04ADDRESSE04444444ADRESSE04ADDRESSE0444444\"," |
| 106 | + + "\"iscompressed\": true,\"account\": \"adresse04\"}"; |
| 107 | + |
| 108 | + Class<?>[] returnedTypes = CommandElt.GETADDRESSES.getReturnedClass(); |
| 109 | + LinkedTreeMap<String, Object> linkedTreeMapValue1 = |
| 110 | + new Gson().fromJson(addresseJsonValue01, LinkedTreeMap.class); |
| 111 | + LinkedTreeMap<String, Object> linkedTreeMapValue2 = |
| 112 | + new Gson().fromJson(addresseJsonValue02, LinkedTreeMap.class); |
| 113 | + LinkedTreeMap<String, Object> linkedTreeMapValue3 = |
| 114 | + new Gson().fromJson(addresseJsonValue03, LinkedTreeMap.class); |
| 115 | + LinkedTreeMap<String, Object> linkedTreeMapValue4 = |
| 116 | + new Gson().fromJson(addresseJsonValue04, LinkedTreeMap.class); |
| 117 | + |
| 118 | + List<LinkedTreeMap<String, Object>> listLinkedTreeMap = new ArrayList<>(); |
| 119 | + listLinkedTreeMap.add(linkedTreeMapValue1); |
| 120 | + listLinkedTreeMap.add(linkedTreeMapValue2); |
| 121 | + listLinkedTreeMap.add(linkedTreeMapValue3); |
| 122 | + listLinkedTreeMap.add(linkedTreeMapValue4); |
| 123 | + |
| 124 | + Object returnedValue = GenericOutputFormatter.formatList(listLinkedTreeMap, returnedTypes); |
| 125 | + |
| 126 | + System.out.println(returnedValue); |
| 127 | + System.out.println(returnedValue.getClass()); |
| 128 | + if (ArrayList.class.isInstance(returnedValue) && returnedValue != null |
| 129 | + && ((ArrayList) returnedValue).size() > 0) { |
| 130 | + System.out.println(((ArrayList) returnedValue).get(0).getClass()); |
| 131 | + } |
| 132 | + |
| 133 | + Assert.assertNotNull(returnedValue); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +} |
0 commit comments