diff --git a/jota/src/main/java/org/iota/jota/IotaAccount.java b/jota/src/main/java/org/iota/jota/IotaAccount.java index 175e3605..a97268bb 100644 --- a/jota/src/main/java/org/iota/jota/IotaAccount.java +++ b/jota/src/main/java/org/iota/jota/IotaAccount.java @@ -474,12 +474,12 @@ public Future newDepositRequest(DepositRequest reques StoredDepositAddress storedRequest = new StoredDepositAddress(request, options.getSecurityLevel()); accountManager.addDepositRequest(address.getIndex(), storedRequest); balanceCache.addBalance( - new Input(address.getAddress().getHashCheckSum(), 0, address.getIndex(), options.getSecurityLevel()), + new Input(address.getAddressHash().getHashCheckSum(), 0, address.getIndex(), options.getSecurityLevel()), request); EventNewInput event = new EventNewInput(address, request); eventManager.emit(event); - return new ConditionalDepositAddress(request, address.getAddress()); + return new ConditionalDepositAddress(request, address.getAddressHash()); }); task.run(); return task; @@ -732,7 +732,7 @@ private List prepareBundle(Bundle bundle, List transfers){ private List sendTrytes(Hash reference, String... trytes) { GetTransactionsToApproveResponse txs = getApi().getTransactionsToApprove(options.getDepth(), - reference == null ? null : reference.getHash()); + reference == null ? null : reference.getHashString()); EventAttachingToTangle attach = new EventAttachingToTangle(trytes); getEventManager().emit(attach); diff --git a/jota/src/main/java/org/iota/jota/account/AccountBalanceCache.java b/jota/src/main/java/org/iota/jota/account/AccountBalanceCache.java index 0c5a8de6..a44e512d 100644 --- a/jota/src/main/java/org/iota/jota/account/AccountBalanceCache.java +++ b/jota/src/main/java/org/iota/jota/account/AccountBalanceCache.java @@ -61,13 +61,13 @@ private void calculateBalance(IotaAPI api, int index, DepositRequest request, in long balance; if (request.hasTimeOut()) { // Not a remainder address, check balance - balance = api.getBalance(address.getAddress().getHashCheckSum()); + balance = api.getBalance(address.getAddressHash().getHashCheckSum()); } else { balance = request.getExpectedAmount(); } Input input = new Input( - address.getAddress().getHashCheckSum(), balance, index, security + address.getAddressHash().getHashCheckSum(), balance, index, security ); addInput(input, request); } @@ -78,7 +78,7 @@ private void addInput(Input input, DepositRequest balance) { } public Entry getByAddress(Address address){ - return getByHash(address.getAddress()); + return getByHash(address.getAddressHash()); } public Entry getByHash(Hash hash){ diff --git a/jota/src/main/java/org/iota/jota/account/AccountState.java b/jota/src/main/java/org/iota/jota/account/AccountState.java index 704c72be..d01e1fd9 100644 --- a/jota/src/main/java/org/iota/jota/account/AccountState.java +++ b/jota/src/main/java/org/iota/jota/account/AccountState.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Set; import org.iota.jota.account.deposits.StoredDepositAddress; @@ -94,15 +95,15 @@ public AccountState clone() throws CloneNotSupportedException { if (null != depositRequests) { newState.depositRequests = new HashMap<>(); - for (int key : depositRequests.keySet()) { - newState.depositRequests.put(key, depositRequests.get(key).clone()); + for (Map.Entry entry : depositRequests.entrySet()) { + newState.depositRequests.put(entry.getKey(), entry.getValue().clone()); } } if (null != pendingTransfers) { newState.pendingTransfers = new HashMap<>(); - for (String key : pendingTransfers.keySet()) { - newState.pendingTransfers.put(key, pendingTransfers.get(key).clone()); + for (Map.Entry entry : pendingTransfers.entrySet()) { + newState.pendingTransfers.put(entry.getKey(), entry.getValue().clone()); } } @@ -134,4 +135,6 @@ public boolean equals(Object obj) { && Objects.equals(depositRequests, as.depositRequests) && Objects.equals(pendingTransfers, as.pendingTransfers); } + + //TODO: SonarLint-Rule: "equals(Object obj)" and "hashCode()" should be overridden in pairs. Therefore also overrie "hashCode()". } diff --git a/jota/src/main/java/org/iota/jota/account/AccountStateManager.java b/jota/src/main/java/org/iota/jota/account/AccountStateManager.java index af0d42db..a688a6b4 100644 --- a/jota/src/main/java/org/iota/jota/account/AccountStateManager.java +++ b/jota/src/main/java/org/iota/jota/account/AccountStateManager.java @@ -95,7 +95,7 @@ public Input createRemainder(long remainder) { Input remainderInput = new Input( - addressService.get(key).getAddress().getHash(), + addressService.get(key).getAddressHash().getHashString(), remainder, key, options.getSecurityLevel() @@ -202,7 +202,7 @@ public Map getPendingTransfers(){ public boolean isOwnAddress(String hash) { for (Entry entry : getDepositRequests().entrySet()) { if (addressService.get(entry.getKey(), entry.getValue().getSecurityLevel()) - .getAddress().getHash().equals(hash)) { + .getAddressHash().getHashString().equals(hash)) { return true; } diff --git a/jota/src/main/java/org/iota/jota/account/deposits/methods/MagnetMethod.java b/jota/src/main/java/org/iota/jota/account/deposits/methods/MagnetMethod.java index 004cee42..4a5fe8a9 100644 --- a/jota/src/main/java/org/iota/jota/account/deposits/methods/MagnetMethod.java +++ b/jota/src/main/java/org/iota/jota/account/deposits/methods/MagnetMethod.java @@ -135,7 +135,7 @@ private String getParam(String condition, Map> paramsMap, S @Override public String build(ConditionalDepositAddress conditions) { - String address = conditions.getDepositAddress().getHash(); + String address = conditions.getDepositAddress().getHashString(); String magnetChecksum = magnetChecksum(address, conditions.getRequest().getTimeOut().getTime(), conditions.getRequest().isMultiUse(), diff --git a/jota/src/main/java/org/iota/jota/account/event/AccountEventType.java b/jota/src/main/java/org/iota/jota/account/event/AccountEventType.java index f7742321..72e870cc 100644 --- a/jota/src/main/java/org/iota/jota/account/event/AccountEventType.java +++ b/jota/src/main/java/org/iota/jota/account/event/AccountEventType.java @@ -1,16 +1,16 @@ package org.iota.jota.account.event; public enum AccountEventType { - Promotion, - Reattachment, - SendingTransfer, - TransferConfirmed, - ReceivedDeposit, - ReceivingDeposit, - ReceivedMessage, - DepositAddress, - Shutdown, - Error, - AttachingToTangle, - DoingProofOfWork + PROMOTION, + REATTACHMENT, + SENDING_TRANSFER, + TRANSFER_CONFIRMED, + RECEIVED_DEPOSIT, + RECEIVING_DEPOSIT, + RECEIVED_MESSAGE, + DEPOSIT_ADDRESS, + SHUTDOWN, + ERROR, + ATTACHING_TO_TANGLE, + DOING_PROOF_OF_WORK } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventAccountError.java b/jota/src/main/java/org/iota/jota/account/event/events/EventAccountError.java index ff0e7d7c..a14e4330 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventAccountError.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventAccountError.java @@ -9,7 +9,7 @@ public class EventAccountError extends EventImpl { private boolean shouldLog; public EventAccountError(Exception exception) { - super(AccountEventType.Error); + super(AccountEventType.ERROR); this.exception = exception; this.shouldLog = true; } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventAttachingToTangle.java b/jota/src/main/java/org/iota/jota/account/event/events/EventAttachingToTangle.java index d5df1e53..2a095805 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventAttachingToTangle.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventAttachingToTangle.java @@ -9,7 +9,7 @@ public class EventAttachingToTangle extends EventImpl { private String[] trytes; public EventAttachingToTangle(String[] trytes) { - super(AccountEventType.AttachingToTangle); + super(AccountEventType.ATTACHING_TO_TANGLE); this.trytes = trytes; } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventDoingProofOfWork.java b/jota/src/main/java/org/iota/jota/account/event/events/EventDoingProofOfWork.java index 7b980e21..08aadb5e 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventDoingProofOfWork.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventDoingProofOfWork.java @@ -9,7 +9,7 @@ public class EventDoingProofOfWork extends EventImpl { private String[] trytes; public EventDoingProofOfWork(String[] trytes) { - super(AccountEventType.DoingProofOfWork); + super(AccountEventType.DOING_PROOF_OF_WORK); this.trytes = trytes; } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventNewInput.java b/jota/src/main/java/org/iota/jota/account/event/events/EventNewInput.java index 12e2e8f8..2524ea16 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventNewInput.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventNewInput.java @@ -11,7 +11,7 @@ public class EventNewInput extends EventImpl { private Address address; public EventNewInput(Address address, DepositRequest request) { - super(AccountEventType.DepositAddress); + super(AccountEventType.DEPOSIT_ADDRESS); this.address = address; this.request = request; } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventPromotion.java b/jota/src/main/java/org/iota/jota/account/event/events/EventPromotion.java index 722bc23b..b5b90077 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventPromotion.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventPromotion.java @@ -9,7 +9,7 @@ public class EventPromotion extends EventImpl { private Bundle promotedBundle; public EventPromotion(Bundle promotedBundle) { - super(AccountEventType.Promotion); + super(AccountEventType.PROMOTION); this.promotedBundle = promotedBundle; } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventReattachment.java b/jota/src/main/java/org/iota/jota/account/event/events/EventReattachment.java index c35a2030..e37b249a 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventReattachment.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventReattachment.java @@ -10,7 +10,7 @@ public class EventReattachment extends EventImpl { private Bundle newBundle; public EventReattachment(Bundle originalBundle, Bundle newBundle) { - super(AccountEventType.Reattachment); + super(AccountEventType.REATTACHMENT); this.originalBundle = originalBundle; this.newBundle = newBundle; } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventReceivedDeposit.java b/jota/src/main/java/org/iota/jota/account/event/events/EventReceivedDeposit.java index 842cf0e9..0d0d2274 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventReceivedDeposit.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventReceivedDeposit.java @@ -14,7 +14,7 @@ public class EventReceivedDeposit extends EventAbstractBundle { private Address receiver; public EventReceivedDeposit(Bundle bundle, Address address) { - super(AccountEventType.ReceivedDeposit, bundle); + super(AccountEventType.RECEIVED_DEPOSIT, bundle); receiver = address; } @@ -28,13 +28,13 @@ public Address getAddress(){ public long getAmount() { for (Transaction t : getBundle().getTransactions()) { - if (t.getAddress().equals(receiver.getAddress().toString())) { + if (t.getAddress().equals(receiver.getAddressHash().toString())) { return t.getValue(); } } // This should NEVER happen - log.error("Deposit received event fired but could not find amount!\\n Please check " + receiver.getAddress()); + log.error("Deposit received event fired but could not find amount!\\n Please check " + receiver.getAddressHash()); return -1; } } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventReceivedMessage.java b/jota/src/main/java/org/iota/jota/account/event/events/EventReceivedMessage.java index 185ae9b6..bb44ca03 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventReceivedMessage.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventReceivedMessage.java @@ -8,7 +8,7 @@ public class EventReceivedMessage extends EventAbstractBundle { private String message = null; public EventReceivedMessage(Bundle bundle) { - super(AccountEventType.ReceivedMessage, bundle); + super(AccountEventType.RECEIVED_MESSAGE, bundle); } public String getMessage() { diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventReceivingDeposit.java b/jota/src/main/java/org/iota/jota/account/event/events/EventReceivingDeposit.java index 4dd07057..39531eb4 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventReceivingDeposit.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventReceivingDeposit.java @@ -14,7 +14,7 @@ public class EventReceivingDeposit extends EventAbstractBundle { private Address receiver; public EventReceivingDeposit(Bundle bundle, Address address) { - super(AccountEventType.ReceivingDeposit, bundle); + super(AccountEventType.RECEIVING_DEPOSIT, bundle); this.receiver = address; } @@ -28,13 +28,13 @@ public Address getAddress(){ public long getAmount() { for (Transaction t : getBundle().getTransactions()) { - if (t.getAddress().equals(receiver.getAddress().toString())) { + if (t.getAddress().equals(receiver.getAddressHash().toString())) { return t.getValue(); } } // This should NEVER happen - log.error("Deposit received event fired but could not find amount!\\n Please check " + receiver.getAddress()); + log.error("Deposit received event fired but could not find amount!\\n Please check " + receiver.getAddressHash()); return -1; } } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventSentTransfer.java b/jota/src/main/java/org/iota/jota/account/event/events/EventSentTransfer.java index 07c655bb..b466e552 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventSentTransfer.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventSentTransfer.java @@ -9,7 +9,7 @@ public class EventSentTransfer extends EventImpl { private Bundle bundle; public EventSentTransfer(Bundle bundle) { - super(AccountEventType.SendingTransfer); + super(AccountEventType.SENDING_TRANSFER); this.bundle = bundle; } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventShutdown.java b/jota/src/main/java/org/iota/jota/account/event/events/EventShutdown.java index 17af60f5..ea742f01 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventShutdown.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventShutdown.java @@ -10,7 +10,7 @@ public class EventShutdown extends EventImpl { private Date time; public EventShutdown(Date now) { - super(AccountEventType.Shutdown); + super(AccountEventType.SHUTDOWN); this.time = now; } diff --git a/jota/src/main/java/org/iota/jota/account/event/events/EventTransferConfirmed.java b/jota/src/main/java/org/iota/jota/account/event/events/EventTransferConfirmed.java index 6f66bac2..663986f2 100644 --- a/jota/src/main/java/org/iota/jota/account/event/events/EventTransferConfirmed.java +++ b/jota/src/main/java/org/iota/jota/account/event/events/EventTransferConfirmed.java @@ -9,7 +9,7 @@ public class EventTransferConfirmed extends EventImpl { private Bundle bundle; public EventTransferConfirmed(Bundle bundle) { - super(AccountEventType.TransferConfirmed); + super(AccountEventType.TRANSFER_CONFIRMED); this.bundle = bundle; } diff --git a/jota/src/main/java/org/iota/jota/account/plugins/promoter/PromoterReattacherImpl.java b/jota/src/main/java/org/iota/jota/account/plugins/promoter/PromoterReattacherImpl.java index 4722a5da..d7ea192f 100644 --- a/jota/src/main/java/org/iota/jota/account/plugins/promoter/PromoterReattacherImpl.java +++ b/jota/src/main/java/org/iota/jota/account/plugins/promoter/PromoterReattacherImpl.java @@ -171,9 +171,9 @@ private PendingTransfer getPendingTransferForBundle(Bundle bundle) { } private String findPromotableTail(PendingTransfer pendingBundle) { - String tailOrig = pendingBundle.getTailHashes().get(0).getHash(); + String tailOrig = pendingBundle.getTailHashes().get(0).getHashString(); for (int i = pendingBundle.getTailHashes().size() - 1; i >= 0; i--) { - String tail = pendingBundle.getTailHashes().get(i).getHash(); + String tail = pendingBundle.getTailHashes().get(i).getHashString(); Transaction tailTransaction = getBundleTail(tailOrig, tail); if (null == tailTransaction) { diff --git a/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/IncomingTransferCheckerImpl.java b/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/IncomingTransferCheckerImpl.java index 81209d16..b258ee1f 100644 --- a/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/IncomingTransferCheckerImpl.java +++ b/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/IncomingTransferCheckerImpl.java @@ -87,7 +87,7 @@ private void addUnconfirmedBundle(Address address) { ScheduledFuture task = service.scheduleAtFixedRate( new IncomingTransferCheckerTask(address, api, eventManager, skipFirst, accountManager), 0, CHECK_INCOMING_DELAY, TimeUnit.MILLISECONDS); - unconfirmedBundles.put(address.getAddress().getHash(), task); + unconfirmedBundles.put(address.getAddressHash().getHashString(), task); } @AccountEvent diff --git a/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/IncomingTransferCheckerTask.java b/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/IncomingTransferCheckerTask.java index 176ec533..d3ef00ef 100644 --- a/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/IncomingTransferCheckerTask.java +++ b/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/IncomingTransferCheckerTask.java @@ -55,7 +55,7 @@ public IncomingTransferCheckerTask(Address address, IotaAPI api, EventManager ev @Override public void run() { - String addrHash = address.getAddress().getHashCheckSum(); + String addrHash = address.getAddressHash().getHashCheckSum(); try { Bundle[] bundles = api.bundlesFromAddresses(true, addrHash); if (bundles == null) { diff --git a/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/OutgoingTransferCheckerImpl.java b/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/OutgoingTransferCheckerImpl.java index f556c1fd..8fbf6071 100644 --- a/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/OutgoingTransferCheckerImpl.java +++ b/jota/src/main/java/org/iota/jota/account/plugins/transferchecker/OutgoingTransferCheckerImpl.java @@ -85,7 +85,7 @@ private void doTask(Bundle bundle) { // Get states of all tails (reattachments incl original) GetInclusionStateResponse check = api.getLatestInclusion( - pending.getTailHashes().stream().map(Hash::getHash).toArray(size -> new String[size]) + pending.getTailHashes().stream().map(Hash::getHashString).toArray(size -> new String[size]) ); if (anyTrue(check.getStates())) { diff --git a/jota/src/main/java/org/iota/jota/account/store/AccountFileStore.java b/jota/src/main/java/org/iota/jota/account/store/AccountFileStore.java index 5f0a12f2..f1871e5a 100644 --- a/jota/src/main/java/org/iota/jota/account/store/AccountFileStore.java +++ b/jota/src/main/java/org/iota/jota/account/store/AccountFileStore.java @@ -13,6 +13,8 @@ import org.iota.jota.store.Store; import org.iota.jota.types.Hash; import org.iota.jota.types.Trytes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -22,6 +24,8 @@ */ public class AccountFileStore extends AccountStoreImpl { + private static final Logger log = LoggerFactory.getLogger(AccountFileStore.class); + private Store store; public AccountFileStore() { @@ -59,8 +63,7 @@ public void shutdown() { try { store.save(true); } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("Exception at shutdown. Message: {}",e.getMessage(), e); } } @@ -69,7 +72,8 @@ public AccountState loadAccount(String id) { AccountState state = store.get(id, null); if (state == null) { - saveAccount(id, state = new AccountState()); + state = new AccountState(); + saveAccount(id, state); } return state; } @@ -119,19 +123,19 @@ public void addPendingTransfer(String id, Hash tailTx, Trytes[] bundleTrytes, in PendingTransfer pendingTransfer = new PendingTransfer(trytesToTrits(bundleTrytes)); pendingTransfer.addTail(tailTx); - loadAccount(id).addPendingTransfers(tailTx.getHash(), pendingTransfer); + loadAccount(id).addPendingTransfers(tailTx.getHashString(), pendingTransfer); save(); } @Override public void removePendingTransfer(String id, Hash tailHash) { - loadAccount(id).removePendingTransfer(tailHash.getHash()); + loadAccount(id).removePendingTransfer(tailHash.getHashString()); save(); } @Override public void addTailHash(String id, Hash tailHash, Hash newTailTxHash) { - loadAccount(id).getPendingTransfer(tailHash.getHash()).addTail(newTailTxHash); + loadAccount(id).getPendingTransfer(tailHash.getHashString()).addTail(newTailTxHash); save(); } @@ -155,8 +159,7 @@ private void save() { try { store.save(false); } catch (Exception e) { - // TODO log account error - e.printStackTrace(); + log.error("Exception at shutdown. Message: {}",e.getMessage(), e); } } diff --git a/jota/src/main/java/org/iota/jota/account/store/MongoStore.java b/jota/src/main/java/org/iota/jota/account/store/MongoStore.java index 10268ded..1338e681 100644 --- a/jota/src/main/java/org/iota/jota/account/store/MongoStore.java +++ b/jota/src/main/java/org/iota/jota/account/store/MongoStore.java @@ -301,7 +301,7 @@ public void addPendingTransfer(String id, Hash tailTx, Trytes[] bundleTrytes, in Filters.eq("_id", id), new Document("$set", new Document(PENDING, - new Document(tailTx.getHash(), transfer))), + new Document(tailTx.getHashString(), transfer))), updateOptions); if (result.isModifiedCountAvailable() && result.getModifiedCount() == 0) { @@ -315,7 +315,7 @@ public void removePendingTransfer(String id, Hash tailHash) { Filters.eq("_id", id), new Document("$unset", new Document(PENDING, - new Document(tailHash.getHash(), ""))), + new Document(tailHash.getHashString(), ""))), updateOptions); if (result == null) { // TODO Log account error @@ -327,7 +327,7 @@ public void addTailHash(String id, Hash tailHash, Hash newTailTxHash) { UpdateResult result = collection.updateOne( Filters.eq("_id", id), new Document("$push", - new Document(PENDING + "." + tailHash.getHash() + "." + TAILS, newTailTxHash)), + new Document(PENDING + "." + tailHash.getHashString() + "." + TAILS, newTailTxHash)), updateOptions); if (result == null) { // TODO Log account error diff --git a/jota/src/main/java/org/iota/jota/builder/AbstractBuilder.java b/jota/src/main/java/org/iota/jota/builder/AbstractBuilder.java index c5315bca..b982a597 100644 --- a/jota/src/main/java/org/iota/jota/builder/AbstractBuilder.java +++ b/jota/src/main/java/org/iota/jota/builder/AbstractBuilder.java @@ -1,5 +1,6 @@ package org.iota.jota.builder; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -8,6 +9,7 @@ import org.iota.jota.config.types.EnvConfig; import org.iota.jota.config.types.IotaDefaultConfig; import org.iota.jota.config.types.PropertiesConfig; +import org.iota.jota.error.InternalException; import org.iota.jota.store.PropertiesStore; import org.slf4j.Logger; @@ -19,10 +21,10 @@ @SuppressWarnings("unchecked") public abstract class AbstractBuilder { - private Logger log; + private final Logger log; private Config config; - public AbstractBuilder(Logger log) { + protected AbstractBuilder(Logger log) { this.log = log; } @@ -40,7 +42,7 @@ public E build() { protected abstract E compile(); - protected abstract T generate() throws Exception; + protected abstract T generate() throws IOException; public T config(F config) { try { @@ -67,7 +69,7 @@ public T config(PropertiesConfig properties) { return (T) this; } - protected List getConfigs() throws Exception{ + protected List getConfigs() throws IOException { EnvConfig env = new EnvConfig(); ArrayList array = new ArrayList<>(); array.add((F)env); diff --git a/jota/src/main/java/org/iota/jota/builder/AccountBuilder.java b/jota/src/main/java/org/iota/jota/builder/AccountBuilder.java index 07c7bc26..2b2c13ea 100644 --- a/jota/src/main/java/org/iota/jota/builder/AccountBuilder.java +++ b/jota/src/main/java/org/iota/jota/builder/AccountBuilder.java @@ -1,5 +1,6 @@ package org.iota.jota.builder; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -17,6 +18,7 @@ import org.iota.jota.config.options.AccountSettings; import org.iota.jota.config.types.IotaDefaultConfig; import org.iota.jota.error.ArgumentException; +import org.iota.jota.error.InternalException; import org.iota.jota.utils.Constants; import org.iota.jota.utils.InputValidator; import org.slf4j.Logger; @@ -42,9 +44,11 @@ public class AccountBuilder extends AbstractBuilder 0) { - this.mwm = mwm; + this.mwm = setIntValueOrLogAWarning(mwm, "mwm"); + return this; + } + + private int setIntValueOrLogAWarning(int intValue, String intTypeName){ + int tempInt = 0; + if (intValue > 0) { + tempInt = intValue; } else { - log.warn(Constants.INVALID_INPUT_ERROR); + log.warn("{} For: {}", Constants.INVALID_INPUT_ERROR, intTypeName); } - return this; + return tempInt; } public AccountBuilder depth(int depth) { - if (depth > 0) { - this.depth = depth; - } else { - log.warn(Constants.INVALID_INPUT_ERROR); - } + this.depth = setIntValueOrLogAWarning(depth, "depth"); return this; } @@ -145,42 +151,62 @@ public AccountBuilder plugin(Plugin plugin){ } @Override - public AccountBuilder generate() throws Exception { + public AccountBuilder generate() throws InternalException, IOException { //If a config is specified through ENV, that one will be in the stream, otherwise default config is used for (AccountConfig config : getConfigs()) { if (config != null) { //calculate Account specific values - - if (0 == getMwm() && config.getMwm() != 0) { - mwm(config.getMwm()); - } - - if (0 == getDepth() && config.getDepth() != 0) { - depth(config.getDepth()); - } - - if (0 == getSecurityLevel() && config.getSecurityLevel() != 0) { - securityLevel(config.getSecurityLevel()); - } - - if (null == store && config.getStore() != null) { - store(config.getStore()); - } - - if (null == api) { - api(new IotaAPI.Builder().build()); - } - - if (null == clock) { - // TODO: Configify - clock(new SystemClock()); - } + checkMwm(config); + checkDepth(config); + checkSecurityLevel(config); + checkStore(config); + checkApi(); + checkClock(); + } else { + log.warn("AccountConfig is null"); } } return this; } - + + private void checkClock() { + if (null == clock) { + // TODO: Configify + clock(new SystemClock()); + } + } + + private void checkApi() { + if (null == api) { + api(new IotaAPI.Builder().build()); + } + } + + private void checkStore(AccountConfig config) { + if (null == store && config.getStore() != null) { + store(config.getStore()); + } + } + + private void checkSecurityLevel(AccountConfig config) { + if (0 == getSecurityLevel() && config.getSecurityLevel() != 0) { + securityLevel(config.getSecurityLevel()); + } + } + + private void checkDepth(AccountConfig config) { + if (0 == getDepth() && config.getDepth() != 0) { + depth(config.getDepth()); + } + } + + private void checkMwm(AccountConfig config) { + if (0 == getMwm() && config.getMwm() != 0) { + mwm(config.getMwm()); + } + } + @Override protected IotaAccount compile(){ return new IotaAccount(new AccountOptions(this)); diff --git a/jota/src/main/java/org/iota/jota/builder/ApiBuilder.java b/jota/src/main/java/org/iota/jota/builder/ApiBuilder.java index 5b7f7de9..7ada82f1 100644 --- a/jota/src/main/java/org/iota/jota/builder/ApiBuilder.java +++ b/jota/src/main/java/org/iota/jota/builder/ApiBuilder.java @@ -12,6 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; @@ -49,7 +50,7 @@ public ApiBuilder() { */ @Override @SuppressWarnings("deprecation") - protected T generate() throws Exception { + protected T generate() throws IOException { for (ApiConfig config : getConfigs()) { if (config != null) { // Defaults have the node in the nodes list diff --git a/jota/src/main/java/org/iota/jota/config/IotaClientConfig.java b/jota/src/main/java/org/iota/jota/config/IotaClientConfig.java index f160e105..2bdfc4c6 100644 --- a/jota/src/main/java/org/iota/jota/config/IotaClientConfig.java +++ b/jota/src/main/java/org/iota/jota/config/IotaClientConfig.java @@ -1,5 +1,6 @@ package org.iota.jota.config; +import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.LinkedList; @@ -12,6 +13,7 @@ import org.iota.jota.config.types.IotaDefaultConfig; import org.iota.jota.connection.Connection; import org.iota.jota.connection.ConnectionFactory; +import org.iota.jota.error.InternalException; import org.iota.jota.store.Store; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,9 +45,9 @@ protected IotaClientConfig() { * The store gets loaded as well. * * @param store the store to use in this config - * @throws Exception if the store failed to load + * @throws InternalException if the store failed to load */ - public IotaClientConfig(Store store) throws Exception { + public IotaClientConfig(Store store) throws InternalException, IOException { this.store = store; this.store.load(); } diff --git a/jota/src/main/java/org/iota/jota/config/types/EnvConfig.java b/jota/src/main/java/org/iota/jota/config/types/EnvConfig.java index 24a7c0f7..3d0a8cc6 100644 --- a/jota/src/main/java/org/iota/jota/config/types/EnvConfig.java +++ b/jota/src/main/java/org/iota/jota/config/types/EnvConfig.java @@ -1,11 +1,13 @@ package org.iota.jota.config.types; +import java.io.IOException; import java.util.List; import org.iota.jota.account.AccountStore; import org.iota.jota.account.store.AccountFileStore; import org.iota.jota.config.IotaClientConfig; import org.iota.jota.connection.Connection; +import org.iota.jota.error.InternalException; import org.iota.jota.store.EnvironmentStore; public class EnvConfig extends IotaClientConfig { @@ -21,7 +23,7 @@ public class EnvConfig extends IotaClientConfig { private static final String ENV_DEPTH = "IOTA_ACCOUNT_DEPTH"; private static final String ENV_SECURITY = "IOTA_ACCOUNT_SECURITY"; - public EnvConfig() throws Exception { + public EnvConfig() throws InternalException, IOException { super(new EnvironmentStore()); } diff --git a/jota/src/main/java/org/iota/jota/store/EnvironmentStore.java b/jota/src/main/java/org/iota/jota/store/EnvironmentStore.java index 388c298f..2c8a98c5 100644 --- a/jota/src/main/java/org/iota/jota/store/EnvironmentStore.java +++ b/jota/src/main/java/org/iota/jota/store/EnvironmentStore.java @@ -1,5 +1,7 @@ package org.iota.jota.store; +import org.iota.jota.error.InternalException; + import java.io.Serializable; import java.util.Map; @@ -11,8 +13,8 @@ public void load() { } @Override - public void save(boolean closeResources) throws Exception { - throw new Exception("Environment store does not allow saving"); + public void save(boolean closeResources) throws InternalException { + throw new InternalException("Environment store does not allow saving"); } @Override diff --git a/jota/src/main/java/org/iota/jota/store/FlatFileStore.java b/jota/src/main/java/org/iota/jota/store/FlatFileStore.java index 93b1d6be..d020094a 100644 --- a/jota/src/main/java/org/iota/jota/store/FlatFileStore.java +++ b/jota/src/main/java/org/iota/jota/store/FlatFileStore.java @@ -1,12 +1,6 @@ package org.iota.jota.store; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Serializable; +import java.io.*; import java.net.URI; import java.util.HashMap; import java.util.Map; @@ -50,7 +44,7 @@ public FlatFileStore(URI location) { } @Override - public void load() throws Exception { + public void load() throws IOException { if (null != file) { if (!file.exists()) { file.createNewFile(); diff --git a/jota/src/main/java/org/iota/jota/store/IotaClientStore.java b/jota/src/main/java/org/iota/jota/store/IotaClientStore.java index 559189ad..d44fd567 100644 --- a/jota/src/main/java/org/iota/jota/store/IotaClientStore.java +++ b/jota/src/main/java/org/iota/jota/store/IotaClientStore.java @@ -1,5 +1,8 @@ package org.iota.jota.store; +import org.iota.jota.error.InternalException; + +import java.io.IOException; import java.io.Serializable; import java.util.Map; @@ -21,12 +24,12 @@ public String toString() { } @Override - public void load() throws Exception { + public void load() throws InternalException, IOException { store.load(); } @Override - public void save(boolean closeResources) throws Exception { + public void save(boolean closeResources) throws InternalException { store.save(closeResources); } diff --git a/jota/src/main/java/org/iota/jota/store/Store.java b/jota/src/main/java/org/iota/jota/store/Store.java index 0a3c326f..73178ac0 100644 --- a/jota/src/main/java/org/iota/jota/store/Store.java +++ b/jota/src/main/java/org/iota/jota/store/Store.java @@ -1,5 +1,8 @@ package org.iota.jota.store; +import org.iota.jota.error.InternalException; + +import java.io.IOException; import java.io.Serializable; import java.util.Map; @@ -13,17 +16,17 @@ public interface Store { /** * Loads this store into cache * - * @throws Exception + * @throws InternalException */ - void load() throws Exception; + void load() throws InternalException, IOException; /** * Saves this store onto its storage method. * * @param closeResources if we want to clean up (final safe before exit) - * @throws Exception + * @throws InternalException */ - void save(boolean closeResources) throws Exception; + void save(boolean closeResources) throws InternalException; /** * diff --git a/jota/src/main/java/org/iota/jota/types/Address.java b/jota/src/main/java/org/iota/jota/types/Address.java index f44c108a..b8343e8c 100644 --- a/jota/src/main/java/org/iota/jota/types/Address.java +++ b/jota/src/main/java/org/iota/jota/types/Address.java @@ -2,26 +2,26 @@ public class Address { - private Hash address; + private final Hash addressHash; - private int index; + private final int index; - private int securityLevel; + private final int securityLevel; /** * - * @param address - * @param index - * @param securityLevel + * @param addressHash Represents the address in the form of a hash. + * @param index The index of an address. + * @param securityLevel The securityLevel of an address. */ - public Address(Hash address, int index, int securityLevel) { - this.address = address; + public Address(Hash addressHash, int index, int securityLevel) { + this.addressHash = addressHash; this.index = index; this.securityLevel = securityLevel; } - public Hash getAddress() { - return address; + public Hash getAddressHash() { + return addressHash; } public int getIndex() { @@ -34,6 +34,6 @@ public int getSecurityLevel() { @Override public String toString() { - return "Address [address=" + address + ", index=" + index + ", securityLevel=" + securityLevel + "]"; + return "Address [address=" + addressHash + ", index=" + index + ", securityLevel=" + securityLevel + "]"; } } diff --git a/jota/src/main/java/org/iota/jota/types/Hash.java b/jota/src/main/java/org/iota/jota/types/Hash.java index cc2879d7..b54dc518 100644 --- a/jota/src/main/java/org/iota/jota/types/Hash.java +++ b/jota/src/main/java/org/iota/jota/types/Hash.java @@ -11,7 +11,7 @@ public class Hash implements Serializable { private static final long serialVersionUID = -5040410304130966841L; - private String hash; + private String hashString; private transient String hashCheckSum; /** @@ -22,35 +22,35 @@ private Hash() { } - public Hash(String hash) throws ArgumentException { - if (InputValidator.isAddress(hash)) { - hashCheckSum = hash; - this.hash = hash.substring(0, 81); - } else if (!InputValidator.isHash(hash)){ + public Hash(String hashString) { + if (InputValidator.isAddress(hashString)) { + hashCheckSum = hashString; + this.hashString = hashString.substring(0, 81); + } else if (!InputValidator.isHash(hashString)){ throw new ArgumentException(Constants.INVALID_HASH_INPUT_ERROR); } else { - this.hash = hash; + this.hashString = hashString; } } - public String getHash() { - return hash; + public String getHashString() { + return hashString; } public String getHashCheckSum() { - if (null != hashCheckSum) { - return hashCheckSum; + if (null == hashCheckSum) { + hashCheckSum = Checksum.addChecksum(hashString); } - return hashCheckSum = Checksum.addChecksum(hash); + return hashCheckSum; } @Override public String toString() { - return hash; + return hashString; } @Override public boolean equals(Object obj) { - return obj != null && obj.getClass().equals(Hash.class) && hash.equals(((Hash)obj).hash); + return obj != null && obj.getClass().equals(Hash.class) && hashString.equals(((Hash)obj).hashString); } } diff --git a/jota/src/main/java/org/iota/jota/types/Recipient.java b/jota/src/main/java/org/iota/jota/types/Recipient.java index 13a5b24a..bce3523c 100644 --- a/jota/src/main/java/org/iota/jota/types/Recipient.java +++ b/jota/src/main/java/org/iota/jota/types/Recipient.java @@ -4,10 +4,10 @@ public class Recipient { - private long value; - private String message; - private String tag; - private String[] addresses; + private final long value; + private final String message; + private final String tag; + private final String[] addresses; public Recipient(long value, String message, String tag, String... addresses) { this.value = value; @@ -34,8 +34,8 @@ public String[] getAddresses() { @Override public String toString() { - return "Recipient [value=" + value + ", message=" + message + ", tag=" + tag + ", address=" - + Arrays.toString(addresses) + "]"; + return "Recipient [value=" + getValue() + ", message=" + getMessage() + ", tag=" + getTag() + ", address=" + + Arrays.toString(getAddresses()) + "]"; } @Override @@ -55,7 +55,11 @@ public boolean equals(Object obj) { return true; } - if (obj == null || getClass() != obj.getClass()) { + if (obj == null) { + return false; + } + + if (getClass() != obj.getClass()) { return false; } @@ -63,17 +67,25 @@ public boolean equals(Object obj) { if (!Arrays.equals(addresses, other.addresses)) { return false; } - - if ((message == null && other.message != null) || !message.equals(other.message)) { + + // check the message + if(!checkGivenValue(message, other.message)) { return false; } - - if ((tag == null && other.tag != null) || !tag.equals(other.tag)) { + + // check the tag + if(!checkGivenValue(tag, other.tag)) { return false; } - + return value == other.value; } - - + + private boolean checkGivenValue (String thisValue, String otherValue) { + if(thisValue == null && otherValue != null) { + return false; + } + + return thisValue == null || thisValue.equals(otherValue); + } } diff --git a/jota/src/test/java/org/iota/jota/IotaAccountIntegrationTest.java b/jota/src/test/java/org/iota/jota/IotaAccountIntegrationTest.java index c8630c83..72eadd29 100644 --- a/jota/src/test/java/org/iota/jota/IotaAccountIntegrationTest.java +++ b/jota/src/test/java/org/iota/jota/IotaAccountIntegrationTest.java @@ -1,5 +1,6 @@ package org.iota.jota; +import static org.apache.commons.io.output.NullOutputStream.NULL_OUTPUT_STREAM; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; @@ -10,7 +11,6 @@ import java.util.concurrent.ExecutionException; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.output.NullOutputStream; import org.iota.jota.account.deposits.ConditionalDepositAddress; import org.iota.jota.account.errors.AccountError; import org.iota.jota.account.errors.SendException; @@ -27,15 +27,15 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -public class IotaAccountIntegrationTest { - - private static String lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc lorem lorem, tristique vel pharetra in, consectetur sed ex. Maecenas sit amet porttitor mauris, in ullamcorper augue. Etiam pellentesque in velit ut pellentesque. Cras dignissim quam ut imperdiet pellentesque. Proin ac ullamcorper mi. Integer suscipit sagittis augue, quis elementum dui venenatis ut. Phasellus id elit malesuada, convallis libero eget, fringilla sem. In tincidunt semper massa, nec dictum velit hendrerit et. Maecenas venenatis, felis ut eleifend elementum, enim mauris pulvinar mi, mattis posuere dolor nunc quis metus.\n" + +class IotaAccountIntegrationTest { + + private static final String lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc lorem lorem, tristique vel pharetra in, consectetur sed ex. Maecenas sit amet porttitor mauris, in ullamcorper augue. Etiam pellentesque in velit ut pellentesque. Cras dignissim quam ut imperdiet pellentesque. Proin ac ullamcorper mi. Integer suscipit sagittis augue, quis elementum dui venenatis ut. Phasellus id elit malesuada, convallis libero eget, fringilla sem. In tincidunt semper massa, nec dictum velit hendrerit et. Maecenas venenatis, felis ut eleifend elementum, enim mauris pulvinar mi, mattis posuere dolor nunc quis metus.\n" + "Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Etiam lacinia malesuada justo, sed gravida risus iaculis quis. Pellentesque lorem libero, feugiat sed nisl id, auctor euismod velit. Nunc sit amet ornare ante, eleifend dapibus ex. Nam imperdiet, arcu in laoreet hendrerit, lorem ex lobortis ex, sollicitudin gravida nisl dolor id enim. In at turpis arcu. Phasellus egestas tortor ipsum. Donec euismod odio ut diam blandit aliquet. Cras at consequat turpis. Etiam ut nibh sit amet nibh venenatis fermentum ac id arcu. Suspendisse mollis ex ac libero finibus, quis scelerisque arcu condimentum.\n" + "Duis leo massa, ullamcorper sed finibus in, ullamcorper ut lacus. Nulla commodo tellus at metus consectetur convallis. Sed finibus, felis vitae pharetra porttitor, nisl lectus tempus justo, quis imperdiet neque ante id turpis. Sed fermentum molestie erat quis luctus. Pellentesque et semper elit. Fusce at lectus cursus, hendrerit elit ac, cursus orci. Nunc vestibulum cursus leo, ac varius urna sagittis in. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris finibus lorem dolor, eu vehicula ante varius ac. Proin ut hendrerit massa. Maecenas pellentesque mattis risus, ac fermentum ex pellentesque in. Donec mauris lectus, vehicula vitae velit in, efficitur consectetur quam. Donec ut ipsum rhoncus, congue justo sit amet, tincidunt est. Duis tempor iaculis massa, sit amet imperdiet arcu scelerisque non. Nullam quis mi tincidunt, fermentum mauris at, rutrum risus. Etiam pharetra leo odio, ut suscipit felis auctor et.\n" + "Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam condimentum sapien ac lacinia varius. Proin sollicitudin sem ligula. Morbi suscipit maximus lorem ut aliquet. Vestibulum at feugiat orci. Maecenas condimentum vitae est vitae pretium. Etiam nec metus ut purus ullamcorper sodales at nec quam. Proin eleifend ante felis, et molestie mauris tincidunt ut. Fusce sit amet est tempus, luctus risus ac, imperdiet mi. Aliquam hendrerit leo orci. Mauris sed nisi ut mauris iaculis condimentum vitae fringilla libero. Praesent egestas ultricies nisl, interdum vehicula ipsum lobortis et. Praesent vitae pulvinar lacus, sed vestibulum dui. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec accumsan fermentum euismod.\n" + "Morbi auctor massa et sem vulputate feugiat. In hac habitasse platea dictumst. Integer ullamcorper ipsum nec orci ultrices consectetur. Nullam id placerat odio, et eleifend lacus. Phasellus id mi ornare, blandit massa sit amet, rhoncus leo. Vestibulum pharetra bibendum lorem, quis efficitur odio consectetur sit amet. Nunc leo diam, interdum ac magna id, ornare porta nunc. Morbi lectus nibh, rutrum in rutrum sit amet, fermentum ut risus. Mauris tempor eget tortor ac iaculis. Suspendisse laoreet ullamcorper turpis, a rutrum diam ornare quis. Proin ac diam sodales risus volutpat pulvinar. Vestibulum scelerisque lorem ac leo auctor, sit amet fringilla dolor viverra. Fusce vel pretium magna. Etiam id dui fermentum, tristique arcu sed, finibus nisi."; - private static int MWM = 9; + private static final int MWM = 9; private static final String TEST_SEED = "IJEEPFTJEFGFRDTSQGLGEAUZPUJFP9LDMDOOYUNOZFJ9JMJFALJATJGHEUPHHFVTFDYSGZNKMRK9EQKWG"; private static final String TEST_SEED_ID = "J9SPZIPMIHEGZEBNDLMBTVVTCGQREQXZFXUYTJTYVQCR9TUZWZDBSJBOZLTTLJYXCGGVAIEQFPWLNUGHD"; @@ -84,14 +84,14 @@ void load() { @Test void sendZeroValueTest() throws AccountError, InterruptedException, ExecutionException { // CDA needed for getting first addr value - JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-test.store"), new NullOutputStream()); + JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-test.store"), NULL_OUTPUT_STREAM); store = new AccountFileStore(json); IotaAccount account = new IotaAccount.Builder(TEST_SEED).mwm(MWM).store(store).api(iotaAPI).build(); Bundle sent = account.sendZeroValue("Another IOTA Accounts test run at " + new Date().toString(), "IOTA9ACCOUNTS", - account.getAccountManager().getNextAddress().getAddress().getHashCheckSum()).get(); + account.getAccountManager().getNextAddress().getAddressHash().getHashCheckSum()).get(); assertTrue(BundleValidator.isBundle(sent), "Should be a valid bundle"); } @@ -99,22 +99,22 @@ void sendZeroValueTest() throws AccountError, InterruptedException, ExecutionExc @Test void sendLongZeroMessage() throws ArgumentException, SendException, InterruptedException, ExecutionException { // CDA needed for getting first addr value - JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-test.store"), new NullOutputStream()); + JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-test.store"), NULL_OUTPUT_STREAM); store = new AccountFileStore(json); IotaAccount account = new IotaAccount.Builder(TEST_SEED).mwm(MWM).store(store).api(iotaAPI).build(); Bundle sent = account.sendZeroValue(lorem, "IOTA9ACCOUNTS", - account.getAccountManager().getNextAddress().getAddress().getHashCheckSum()).get(); + account.getAccountManager().getNextAddress().getAddressHash().getHashCheckSum()).get(); assertTrue(BundleValidator.isBundle(sent), "Should be a valid bundle"); } @Test void sendValueTest() throws AccountError, InterruptedException, ExecutionException { - IotaAPI iotaAPI = fakeBalance(ADDR_1_SEC_3, 5l); + IotaAPI iotaAPI = fakeBalance(ADDR_1_SEC_3, 5L); - JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-test.store"), new NullOutputStream()); + JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-test.store"), NULL_OUTPUT_STREAM); store = new AccountFileStore(json); IotaAccount account = new IotaAccount.Builder(TEST_SEED).mwm(MWM).store(store).api(iotaAPI).build(); @@ -131,9 +131,9 @@ void sendValueTest() throws AccountError, InterruptedException, ExecutionExcepti @Test void sendLongValueTest() throws AccountError, InterruptedException, ExecutionException { - IotaAPI iotaAPI = fakeBalance(ADDR_1_SEC_3, 10l); + IotaAPI iotaAPI = fakeBalance(ADDR_1_SEC_3, 10L); - JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-test.store"), new NullOutputStream()); + JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-test.store"), NULL_OUTPUT_STREAM); store = new AccountFileStore(json); IotaAccount account = new IotaAccount.Builder(TEST_SEED).mwm(MWM).store(store).api(iotaAPI).build(); @@ -149,10 +149,10 @@ void sendLongValueTest() throws AccountError, InterruptedException, ExecutionExc @Test void sendLongMultiValueTest() throws AccountError, InterruptedException, ExecutionException { - IotaAPI iotaAPI = fakeBalance(ADDR_1_SEC_3, 5l); - iotaAPI = fakeBalance(ADDR_2_SEC_3, 5l, iotaAPI); + IotaAPI iotaAPI = fakeBalance(ADDR_1_SEC_3, 5L); + iotaAPI = fakeBalance(ADDR_2_SEC_3, 5L, iotaAPI); - JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-testMulti.store"), new NullOutputStream()); + JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-testMulti.store"), NULL_OUTPUT_STREAM); store = new AccountFileStore(json); IotaAccount account = new IotaAccount.Builder(TEST_SEED).mwm(MWM).store(store).api(iotaAPI).build(); @@ -168,10 +168,10 @@ void sendLongMultiValueTest() throws AccountError, InterruptedException, Executi @Test void sendLongMultiValueRemainderTest() throws AccountError, InterruptedException, ExecutionException { - IotaAPI iotaAPI = fakeBalance(ADDR_1_SEC_3, 6l); - iotaAPI = fakeBalance(ADDR_2_SEC_3, 5l, iotaAPI); + IotaAPI iotaAPI = fakeBalance(ADDR_1_SEC_3, 6L); + iotaAPI = fakeBalance(ADDR_2_SEC_3, 5L, iotaAPI); - JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-testMulti.store"), new NullOutputStream()); + JsonFlatFileStore json = new JsonFlatFileStore(this.getClass().getResourceAsStream("/accounts/client-testMulti.store"), NULL_OUTPUT_STREAM); store = new AccountFileStore(json); IotaAccount account = new IotaAccount.Builder(TEST_SEED).mwm(MWM).store(store).api(iotaAPI).build(); diff --git a/jota/src/test/java/org/iota/jota/IotaAccountTest.java b/jota/src/test/java/org/iota/jota/IotaAccountTest.java index 6590094b..4c201ce4 100644 --- a/jota/src/test/java/org/iota/jota/IotaAccountTest.java +++ b/jota/src/test/java/org/iota/jota/IotaAccountTest.java @@ -85,7 +85,7 @@ void totalBalance() throws ExecutionException, InterruptedException { assertEquals(0, account.availableBalance(), "Account should have 0 usable balance"); assertEquals(15, account.totalBalance(), "Account should have 15 total balance"); - assertEquals( "GGAOVJJKOHECPAR9GQBFOISLYUXSRXXXPT9GEYBTRBBMTJAN9CMH9EVVRYDGXUTDMECGXKFWPYAXUO9QD", cda.getDepositAddress().getHash(), + assertEquals( "GGAOVJJKOHECPAR9GQBFOISLYUXSRXXXPT9GEYBTRBBMTJAN9CMH9EVVRYDGXUTDMECGXKFWPYAXUO9QD", cda.getDepositAddress().getHashString(), "Should have generated address at index 5"); } diff --git a/jota/src/test/java/org/iota/jota/account/deposits/methods/MagnetTest.java b/jota/src/test/java/org/iota/jota/account/deposits/methods/MagnetTest.java index 90e97c03..236e35c9 100644 --- a/jota/src/test/java/org/iota/jota/account/deposits/methods/MagnetTest.java +++ b/jota/src/test/java/org/iota/jota/account/deposits/methods/MagnetTest.java @@ -20,7 +20,7 @@ public class MagnetTest extends DepositTest { private static final String MAGNET_CHECKSUM = "UDJPO99SI"; - private static final String MAGNET = "iota://" + DepositTest.depositAddress.getHash() + MAGNET_CHECKSUM + "/?" + private static final String MAGNET = "iota://" + DepositTest.depositAddress.getHashString() + MAGNET_CHECKSUM + "/?" + MagnetMethod.CONDITION_EXPIRES + "=" + TIME + "&" + MagnetMethod.CONDITION_MULTI_USE + "=" + MULTI + "&" + MagnetMethod.CONDITION_AMOUNT + "=" + AMOUNT; @@ -65,7 +65,7 @@ public void readMagnet() { @Test public void magnetChecksum() { - String checksum = method.magnetChecksum(DepositTest.depositAddress.getHash(), + String checksum = method.magnetChecksum(DepositTest.depositAddress.getHashString(), TIME, MULTI, AMOUNT); assertEquals(MAGNET_CHECKSUM, checksum, "Checksum should be equal to the pre-generated one"); diff --git a/jota/src/test/java/org/iota/jota/account/store/MongoDBStoreTest.java b/jota/src/test/java/org/iota/jota/account/store/MongoDBStoreTest.java index 87ecddb5..756c14e2 100644 --- a/jota/src/test/java/org/iota/jota/account/store/MongoDBStoreTest.java +++ b/jota/src/test/java/org/iota/jota/account/store/MongoDBStoreTest.java @@ -100,14 +100,14 @@ public void pendingTransferTest() throws Exception{ Map result = store.getPendingTransfers(ID); assertEquals(1, result.size(), "Store should have a value after setting"); - assertEquals(transfer, result.get(hash.getHash()), "Store should have the same value after setting"); + assertEquals(transfer, result.get(hash.getHashString()), "Store should have the same value after setting"); store.addTailHash(ID, hash, nextTail); result = store.getPendingTransfers(ID); transfer.addTail(nextTail); assertEquals(1, result.size(), "Store should have a value after setting"); - assertEquals(transfer, result.get(hash.getHash()), "Store should have the same value after setting"); + assertEquals(transfer, result.get(hash.getHashString()), "Store should have the same value after setting"); store.removePendingTransfer(ID, hash); result = store.getPendingTransfers(ID); diff --git a/jota/src/test/java/org/iota/jota/builder/AccountBuilderTest.java b/jota/src/test/java/org/iota/jota/builder/AccountBuilderTest.java new file mode 100644 index 00000000..14ea8d0d --- /dev/null +++ b/jota/src/test/java/org/iota/jota/builder/AccountBuilderTest.java @@ -0,0 +1,40 @@ +package org.iota.jota.builder; + +import org.iota.jota.account.seedprovider.SeedProvider; +import org.iota.jota.account.seedprovider.SeedProviderImpl; +import org.iota.jota.types.Seed; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class AccountBuilderTest { + + private AccountBuilder testClass = null; + + @BeforeEach + void setUp(){ + String tempSeed = "TESTSEEDAAAAAAAAAAABBBBBBBBBBBBCCCCCCCCCCCCDDDDDDDDDDDDEEEEEEEEEEEEEEFFFFFFFGGGG9"; + testClass = new AccountBuilder(tempSeed); + } + + @Test + void mwm() { + AccountBuilder resultAB = testClass.mwm(0); + assertEquals(0,resultAB.getMwm()); + + resultAB = testClass.mwm(25); + assertEquals(25,resultAB.getMwm()); + assertNotEquals(10,resultAB.getMwm()); + } + + @Test + void depth() { + AccountBuilder resultAB = testClass.depth(0); + assertEquals(0,resultAB.getDepth()); + + resultAB = testClass.depth(25); + assertEquals(25,resultAB.getDepth()); + assertNotEquals(10,resultAB.getDepth()); + } +} \ No newline at end of file diff --git a/jota/src/test/java/org/iota/jota/types/RecipientTest.java b/jota/src/test/java/org/iota/jota/types/RecipientTest.java new file mode 100644 index 00000000..f6744b2b --- /dev/null +++ b/jota/src/test/java/org/iota/jota/types/RecipientTest.java @@ -0,0 +1,148 @@ +package org.iota.jota.types; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class RecipientTest { + + private Recipient testClass; + + @BeforeEach + void setUp() { + long value = 1L; + String message = "TestMessage01"; + String tag = "TestTag01"; + String addresses01 = "TestAddresses01"; + testClass = new Recipient(value, message, tag, addresses01); + } + + @Test + void testToString() { + String toStringOutput = testClass.toString(); + assertEquals("Recipient [value=1, message=TestMessage01, tag=TestTag01, address=[TestAddresses01]]", toStringOutput); + } + + + //*********************************************************** + // hashCode tests + //*********************************************************** + @Test + void testHashCode01() { + assertEquals(-974042593, testClass.hashCode()); + } + + @Test + void testHashCode02() { + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag01", "TestAddresses01"); + assertEquals(testClass.hashCode(), otherRecipient.hashCode()); + } + + @Test + void testHashCode03_otherValue() { + Recipient otherRecipient = new Recipient(2L, "TestMessage01", "TestTag01", "TestAddresses01"); + assertNotEquals(testClass.hashCode(), otherRecipient.hashCode()); + } + + @Test + void testHashCode04_otherMessage() { + Recipient otherRecipient = new Recipient(1L, "TestMessage02", "TestTag01", "TestAddresses01"); + assertNotEquals(testClass.hashCode(), otherRecipient.hashCode()); + } + + @Test + void testHashCode05_otherTag() { + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag02", "TestAddresses01"); + assertNotEquals(testClass.hashCode(), otherRecipient.hashCode()); + } + + @Test + void testHashCode06_otherAddress() { + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag01", "TestAddresses02"); + assertNotEquals(testClass.hashCode(), otherRecipient.hashCode()); + } + + //*********************************************************** + // equals tests + //*********************************************************** + @Test + void testEquals01() { + assertTrue(testClass.equals(testClass)); + } + + @Test + void testEquals02_sameParameterSettings() { + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag01", "TestAddresses01"); + assertTrue(testClass.equals(otherRecipient)); + } + + @Test + void testEquals03_otherValue() { + Recipient otherRecipient = new Recipient(2L, "TestMessage01", "TestTag01", "TestAddresses01"); + assertFalse(testClass.equals(otherRecipient)); + } + + @Test + void testEquals04_otherMessage() { + Recipient otherRecipient = new Recipient(1L, "TestMessage02", "TestTag01", "TestAddresses01"); + assertFalse(testClass.equals(otherRecipient)); + } + + @Test + void testEquals05_otherTag() { + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag02", "TestAddresses01"); + assertFalse(testClass.equals(otherRecipient)); + } + + @Test + void testEquals06_otherAddress() { + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag01", "TestAddresses02"); + assertFalse(testClass.equals(otherRecipient)); + } + + @Test + void testEquals07_manyAddress01() { + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag01", "TestAddresses01", "TestAddresses02"); + assertFalse(testClass.equals(otherRecipient)); + } + + @Test + void testEquals08_manyAddress02() { + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag01", "TestAddresses01", "TestAddresses01"); + assertFalse(testClass.equals(otherRecipient)); + } + + @Test + void testEquals09_otherMessageIsNull() { + Recipient otherRecipient = new Recipient(1L, null, "TestTag01", "TestAddresses01"); + assertFalse(testClass.equals(otherRecipient)); + } + + @Test + void testEquals10_ownClassMessageIsNull() { + Recipient ownRecipient = new Recipient(1L, null, "TestTag01", "TestAddresses01"); + Recipient otherRecipient = new Recipient(1L, "TestMessage01", "TestTag01", "TestAddresses01"); + assertFalse(ownRecipient.equals(otherRecipient)); + } + + @Test + void testEquals10_BothMessageIsNull() { + Recipient ownRecipient = new Recipient(1L, null, "TestTag01", "TestAddresses01"); + Recipient otherRecipient = new Recipient(1L, null, "TestTag01", "TestAddresses01"); + assertTrue(ownRecipient.equals(otherRecipient)); + } + + @Test + void testEquals11_otherObjectIsNull() { + Recipient otherRecipient = null; + assertFalse(testClass.equals(otherRecipient)); + } + + @Test + void testEquals12_otherClassIsDifferent() { + String otherRecipient = "testString"; + assertFalse(testClass.equals(otherRecipient)); + } +} \ No newline at end of file