Commit 1e4bbf0e authored by Schudel, MJ (Michel)'s avatar Schudel, MJ (Michel)

added unit tests for block and transactions.

parent 96515d94
package nl.craftsmen.blockchain.craftscoinnode.blockchain;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import nl.craftsmen.blockchain.craftscoinnode.transaction.Transaction;
......@@ -8,6 +9,7 @@ import java.util.HashSet;
import java.util.Set;
@JsonPropertyOrder(alphabetic = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
public class Block {
......
package nl.craftsmen.blockchain.craftscoinnode.transaction;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.math.BigDecimal;
import java.util.UUID;
@JsonPropertyOrder(alphabetic = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
public class Transaction {
......
package nl.craftsmen.blockchain.craftscoinnode.wallet;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import nl.craftsmen.blockchain.craftscoinnode.transaction.Transaction;
import java.math.BigDecimal;
import java.util.List;
@JsonPropertyOrder(alphabetic = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
public class Wallet {
private BigDecimal balance;
private List<Transaction> minedTransactions;
private List<Transaction> unconfirmedTransactions;
@SuppressWarnings({"unused", "WeakerAccess"})
public Wallet() {
}
Wallet(BigDecimal balance, List<Transaction> minedTransactions, List<Transaction> unconfirmedTransactions) {
this.balance = balance;
this.minedTransactions = minedTransactions;
......
package nl.craftsmen.blockchain.craftscoinnode.blockchain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BlockTest {
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testIfTransactionHasCorrectFormat() throws JsonProcessingException {
Block block = new Block();
String blockAsJson = objectMapper.writeValueAsString(block);
assertThat(blockAsJson).isEqualTo("{\"index\":0,\"previousHash\":null,\"proof\":0,\"timestamp\":0,\"transactions\":null}");
}
}
\ No newline at end of file
package nl.craftsmen.blockchain.craftscoinnode.transaction;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
public class TransactionTest {
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testIfTransactionHasCorrectFormat() throws JsonProcessingException {
Transaction transaction = new Transaction();
String transactionAsJson = objectMapper.writeValueAsString(transaction);
assertThat(transactionAsJson).isEqualTo("{\"amount\":null,\"from\":null,\"id\":null,\"to\":null}");
}
@Test
public void testIfEqualsTestsOnSameIdOnly() throws IOException {
String transactionAsJson = "{\"id\":\"4c9e82bd-ef03-4b30-8b70-95c63aa5b188\", \"amount\":3,\"from\":\"michel\",\"to\":\"gerry\"}";
String transactionAsJson2 = "{\"id\":\"4c9e82bd-ef03-4b30-8b70-95c63aa5b188\", \"amount\":4,\"from\":\"gerry\",\"to\":\"michel\"}";
Transaction transaction = objectMapper.readValue(transactionAsJson, Transaction.class);
Transaction transaction2 = objectMapper.readValue(transactionAsJson2, Transaction.class);
assertThat(transaction).isEqualTo(transaction2);
assertThat(transaction.hashCode()).isEqualTo(transaction2.hashCode());
}
@Test
public void testIfDifferentIdsAreRegardedAsTwoDifferentTransactions() throws IOException {
String transactionAsJson = "{\"id\":\"4c9e82bd-ef03-4b30-8b70-95c63aa5b189\", \"amount\":3,\"from\":\"michel\",\"to\":\"gerry\"}";
String transactionAsJson2 = "{\"id\":\"4c9e82bd-ef03-4b30-8b70-95c63aa5b188\", \"amount\":4,\"from\":\"gerry\",\"to\":\"michel\"}";
Transaction transaction = objectMapper.readValue(transactionAsJson, Transaction.class);
Transaction transaction2 = objectMapper.readValue(transactionAsJson2, Transaction.class);
assertThat(transaction).isNotEqualTo(transaction2);
assertThat(transaction.hashCode()).isNotEqualTo(transaction2.hashCode());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment