Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into
Browse files Browse the repository at this point in the history
issue/106-test-coverage

Conflicts:
	src/main/java/io/proximax/sdk/infrastructure/TransactionMapping.java
  • Loading branch information
tonowie committed Aug 5, 2019
2 parents cff0f5a + 1a39f26 commit f8d242b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/e2e/java/io/proximax/sdk/E2EBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
Expand All @@ -40,7 +39,7 @@
import io.proximax.sdk.model.account.Address;
import io.proximax.sdk.model.mosaic.Mosaic;
import io.proximax.sdk.model.mosaic.NetworkCurrencyMosaic;
import io.proximax.sdk.model.transaction.Deadline;
import io.proximax.sdk.model.transaction.DeadlineRaw;
import io.proximax.sdk.model.transaction.PlainMessage;
import io.proximax.sdk.model.transaction.SignedTransaction;
import io.proximax.sdk.model.transaction.TransactionDeadline;
Expand Down Expand Up @@ -117,7 +116,7 @@ void cleanup() {
* @return deadline
*/
protected TransactionDeadline getDeadline() {
return new Deadline(5, ChronoUnit.MINUTES);
return DeadlineRaw.startNow(BigInteger.valueOf(5*60*1000l));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import io.proximax.sdk.TransactionRepository;
import io.proximax.sdk.gen.model.TransactionStatusDTO;
import io.proximax.sdk.model.transaction.CosignatureSignedTransaction;
import io.proximax.sdk.model.transaction.DeadlineBP;
import io.proximax.sdk.model.transaction.DeadlineRaw;
import io.proximax.sdk.model.transaction.SignedTransaction;
import io.proximax.sdk.model.transaction.Transaction;
import io.proximax.sdk.model.transaction.TransactionAnnounceResponse;
Expand Down Expand Up @@ -91,7 +91,7 @@ public Observable<TransactionStatus> getTransactionStatus(String transactionHash
.map(transactionStatusDTO -> new TransactionStatus(transactionStatusDTO.getGroup(),
transactionStatusDTO.getStatus(),
transactionStatusDTO.getHash(),
new DeadlineBP(toBigInt(transactionStatusDTO.getDeadline())),
new DeadlineRaw(toBigInt(transactionStatusDTO.getDeadline())),
toBigInt(transactionStatusDTO.getHeight())));
}

Expand All @@ -107,7 +107,7 @@ public Observable<List<TransactionStatus>> getTransactionStatuses(List<String> t
.map(transactionStatusDTO -> new TransactionStatus(transactionStatusDTO.getGroup(),
transactionStatusDTO.getStatus(),
transactionStatusDTO.getHash(),
new DeadlineBP(toBigInt(transactionStatusDTO.getDeadline())),
new DeadlineRaw(toBigInt(transactionStatusDTO.getDeadline())),
toBigInt(transactionStatusDTO.getHeight())))
.toList()
.toObservable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.google.gson.JsonObject;

import io.proximax.sdk.model.account.Address;
import io.proximax.sdk.model.transaction.DeadlineBP;
import io.proximax.sdk.model.transaction.DeadlineRaw;
import io.proximax.sdk.model.transaction.TransactionStatusError;
import io.reactivex.Observable;
import io.reactivex.subjects.Subject;
Expand Down Expand Up @@ -38,7 +38,7 @@ private static TransactionStatusError getMessageObject(JsonObject message) {
return new TransactionStatusError(
message.get("hash").getAsString(),
message.get("status").getAsString(),
new DeadlineBP(extractBigInteger(message.getAsJsonArray("deadline"))));
new DeadlineRaw(extractBigInteger(message.getAsJsonArray("deadline"))));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import java.time.temporal.ChronoUnit;

/**
* The deadline of the transaction. The deadline is given as the number of seconds elapsed since the creation of the
* The deadline of the transaction. The deadline is given as the number of milliseconds elapsed since the creation of the
* nemesis block. If a transaction does not get included in a block before the deadline is reached, it is deleted.
*
* @since 1.0
*/
public class Deadline implements TransactionDeadline {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public DeadlineBP(int units, ChronoUnit chronoUnit) {
/**
* Constructor
*
* @param input Deadline in BigInteger format
* @param input milliseconds since epoch
*/
public DeadlineBP(BigInteger input) {
instant = Instant.ofEpochMilli(input.longValue() + NETWORK_EPOCH_START_MILLIS);
Expand All @@ -65,11 +65,7 @@ public static DeadlineBP create(int units, ChronoUnit chronoUnit) {
return new DeadlineBP(units, chronoUnit);
}

/**
* Returns number of seconds elapsed since the creation of the nemesis block.
*
* @return long
*/
@Override
public long getInstant() {
return instant.toEpochMilli() - DeadlineBP.NETWORK_EPOCH.toEpochMilli();
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/io/proximax/sdk/model/transaction/DeadlineRaw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2019 ProximaX Limited. All rights reserved.
* Use of this source code is governed by the Apache 2.0
* license that can be found in the LICENSE file.
*/
package io.proximax.sdk.model.transaction;

import java.math.BigInteger;
import java.util.Date;

/**
* Raw deadline implementation working directly with milliseconds. Consider using {@link Deadline} or {@link DeadlineBP}
*/
public class DeadlineRaw implements TransactionDeadline {

/** milliseconds since epoch */
private final long deadline;

/**
* @param deadline in milliseconds since epoch
*/
public DeadlineRaw(BigInteger deadline) {
this.deadline = deadline.longValue();
}

/**
* create new deadline instance specifying duration since current time
*
* @param duration duration after current time
* @return deadline representing specified time
*/
public static TransactionDeadline startNow(BigInteger duration) {
return new DeadlineRaw(BigInteger
.valueOf(System.currentTimeMillis() - TransactionDeadline.NETWORK_EPOCH_START_MILLIS).add(duration));
}

@Override
public long getInstant() {
return deadline;
}

@Override
public String toString() {
return "DeadlineRaw [deadline=" + new Date(deadline + TransactionDeadline.NETWORK_EPOCH_START_MILLIS) + "]";
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2019 ProximaX Limited. All rights reserved.
* Use of this source code is governed by the Apache 2.0
* license that can be found in the LICENSE file.
*/
package io.proximax.sdk.model.transaction;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.math.BigInteger;
import java.time.temporal.ChronoUnit;

import org.junit.jupiter.api.Test;

/**
* {@link DeadlineRaw} tests
*/
class DeadlineRawTest {
private static final long HOUR_MILLIS = 3_600_000l;
private static final long ACCEPTABLE_THRESHOLD = 500l;
@Test
void constructor() {
DeadlineRaw rawDeadline = new DeadlineRaw(BigInteger.valueOf(HOUR_MILLIS));
assertEquals(HOUR_MILLIS, rawDeadline.getInstant());
}

@Test
void startNow() {
Deadline deadline = new Deadline(1, ChronoUnit.HOURS);
TransactionDeadline rawDeadline = DeadlineRaw.startNow(BigInteger.valueOf(HOUR_MILLIS));
long diff = Math.abs(deadline.getInstant() - rawDeadline.getInstant());
assertTrue(diff < ACCEPTABLE_THRESHOLD);
}

@Test
void compareWithDeadline() {
Deadline deadline = new Deadline(BigInteger.valueOf(HOUR_MILLIS));
DeadlineRaw rawDeadline = new DeadlineRaw(BigInteger.valueOf(HOUR_MILLIS));

long diff = Math.abs(deadline.getInstant() - rawDeadline.getInstant());
assertTrue(diff < ACCEPTABLE_THRESHOLD, "difference was "+diff);
}

@Test
void toStringPasses() {
DeadlineRaw rawDeadline = new DeadlineRaw(BigInteger.valueOf(HOUR_MILLIS));
assertTrue(rawDeadline.toString().startsWith("DeadlineRaw"));
}
}

0 comments on commit f8d242b

Please sign in to comment.