committed final fixes to chaincode-java

Signed-off-by: fraVlaca <ocsenarf@outlook.com>
This commit is contained in:
fraVlaca 2021-09-03 09:25:52 +01:00
parent 5b8acd666d
commit f1d1c89ff4
3 changed files with 24 additions and 31 deletions

View file

@ -16,7 +16,6 @@ dependencies {
implementation 'org.hyperledger.fabric-chaincode-java:fabric-chaincode-shim:2.2.0' implementation 'org.hyperledger.fabric-chaincode-java:fabric-chaincode-shim:2.2.0'
implementation 'com.owlike:genson:1.6' implementation 'com.owlike:genson:1.6'
implementation 'com.google.code.gson:gson:2.8.7' implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.8.9'
implementation 'io.vertx:vertx-core:3.5.3' implementation 'io.vertx:vertx-core:3.5.3'
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2' testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testImplementation 'org.assertj:assertj-core:3.11.1' testImplementation 'org.assertj:assertj-core:3.11.1'

View file

@ -6,9 +6,6 @@ package org.hyperledger.fabric.samples.assettransfer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.TreeMap;
import java.util.Map;
import java.util.HashMap;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException; import java.io.IOException;
@ -24,11 +21,7 @@ import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException; import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub; import org.hyperledger.fabric.shim.ChaincodeStub;
import org.hyperledger.fabric.shim.ledger.KeyValue; import org.hyperledger.fabric.shim.ledger.KeyValue;
import org.hyperledger.fabric.shim.ledger.QueryResultsIterator; import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.owlike.genson.Genson; import com.owlike.genson.Genson;
@Contract( @Contract(
@ -48,7 +41,6 @@ import com.owlike.genson.Genson;
public final class AssetTransfer implements ContractInterface { public final class AssetTransfer implements ContractInterface {
private final Genson genson = new Genson(); private final Genson genson = new Genson();
private ObjectMapper om = new ObjectMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);;
private enum AssetTransferErrors { private enum AssetTransferErrors {
ASSET_NOT_FOUND, ASSET_NOT_FOUND,
@ -61,7 +53,7 @@ public final class AssetTransfer implements ContractInterface {
* @param ctx the transaction context * @param ctx the transaction context
*/ */
@Transaction(intent = Transaction.TYPE.SUBMIT) @Transaction(intent = Transaction.TYPE.SUBMIT)
public void InitLedger(final Context ctx) throws JsonProcessingException{ public void InitLedger(final Context ctx) throws JsonProcessingException {
ChaincodeStub stub = ctx.getStub(); ChaincodeStub stub = ctx.getStub();
CreateAsset(ctx, "asset1", "blue", 5, "Tomoko", 300); CreateAsset(ctx, "asset1", "blue", 5, "Tomoko", 300);
@ -86,7 +78,7 @@ public final class AssetTransfer implements ContractInterface {
*/ */
@Transaction(intent = Transaction.TYPE.SUBMIT) @Transaction(intent = Transaction.TYPE.SUBMIT)
public Asset CreateAsset(final Context ctx, final String assetID, final String color, final int size, public Asset CreateAsset(final Context ctx, final String assetID, final String color, final int size,
final String owner, final int appraisedValue) throws JsonProcessingException{ final String owner, final int appraisedValue) throws JsonProcessingException {
ChaincodeStub stub = ctx.getStub(); ChaincodeStub stub = ctx.getStub();
if (AssetExists(ctx, assetID)) { if (AssetExists(ctx, assetID)) {
@ -97,8 +89,8 @@ public final class AssetTransfer implements ContractInterface {
Asset asset = new Asset(assetID, color, size, owner, appraisedValue); Asset asset = new Asset(assetID, color, size, owner, appraisedValue);
//Use a Jackson ObjectMapper to conver the Asset into string, sort it alphabetically and serialize it into a json string //Use Genson to convert the Asset into string, sort it alphabetically and serialize it into a json string
String sortedJson = om.writeValueAsString(asset); String sortedJson = genson.serialize(asset);
stub.putStringState(assetID, sortedJson); stub.putStringState(assetID, sortedJson);
return asset; return asset;
@ -112,7 +104,7 @@ public final class AssetTransfer implements ContractInterface {
* @return the asset found on the ledger if there was one * @return the asset found on the ledger if there was one
*/ */
@Transaction(intent = Transaction.TYPE.EVALUATE) @Transaction(intent = Transaction.TYPE.EVALUATE)
public Asset ReadAsset(final Context ctx, final String assetID) throws JsonProcessingException,IOException{ public Asset ReadAsset(final Context ctx, final String assetID) throws JsonProcessingException, IOException {
ChaincodeStub stub = ctx.getStub(); ChaincodeStub stub = ctx.getStub();
String assetJSON = stub.getStringState(assetID); String assetJSON = stub.getStringState(assetID);
@ -121,7 +113,6 @@ public final class AssetTransfer implements ContractInterface {
System.out.println(errorMessage); System.out.println(errorMessage);
throw new ChaincodeException(errorMessage, AssetTransferErrors.ASSET_NOT_FOUND.toString()); throw new ChaincodeException(errorMessage, AssetTransferErrors.ASSET_NOT_FOUND.toString());
} }
Asset asset = genson.deserialize(assetJSON, Asset.class); Asset asset = genson.deserialize(assetJSON, Asset.class);
return asset; return asset;
} }
@ -139,7 +130,7 @@ public final class AssetTransfer implements ContractInterface {
*/ */
@Transaction(intent = Transaction.TYPE.SUBMIT) @Transaction(intent = Transaction.TYPE.SUBMIT)
public Asset UpdateAsset(final Context ctx, final String assetID, final String color, final int size, public Asset UpdateAsset(final Context ctx, final String assetID, final String color, final int size,
final String owner, final int appraisedValue) throws JsonProcessingException{ final String owner, final int appraisedValue) throws JsonProcessingException {
ChaincodeStub stub = ctx.getStub(); ChaincodeStub stub = ctx.getStub();
if (!AssetExists(ctx, assetID)) { if (!AssetExists(ctx, assetID)) {
@ -150,8 +141,8 @@ public final class AssetTransfer implements ContractInterface {
Asset newAsset = new Asset(assetID, color, size, owner, appraisedValue); Asset newAsset = new Asset(assetID, color, size, owner, appraisedValue);
//Use a Jackson ObjectMapper to conver the Asset into string, sort it alphabetically and serialize it into a json string //Use Genson to convert the Asset into string, sort it alphabetically and serialize it into a json string
String sortedJson = om.writeValueAsString(newAsset); String sortedJson = genson.serialize(newAsset);
stub.putStringState(assetID, sortedJson); stub.putStringState(assetID, sortedJson);
return newAsset; return newAsset;
@ -200,7 +191,7 @@ public final class AssetTransfer implements ContractInterface {
* @return the updated asset * @return the updated asset
*/ */
@Transaction(intent = Transaction.TYPE.SUBMIT) @Transaction(intent = Transaction.TYPE.SUBMIT)
public Asset TransferAsset(final Context ctx, final String assetID, final String newOwner) throws JsonProcessingException{ public Asset TransferAsset(final Context ctx, final String assetID, final String newOwner) throws JsonProcessingException {
ChaincodeStub stub = ctx.getStub(); ChaincodeStub stub = ctx.getStub();
String assetJSON = stub.getStringState(assetID); String assetJSON = stub.getStringState(assetID);
@ -214,8 +205,8 @@ public final class AssetTransfer implements ContractInterface {
Asset asset = genson.deserialize(assetJSON, Asset.class); Asset asset = genson.deserialize(assetJSON, Asset.class);
Asset newAsset = new Asset(asset.getAssetID(), asset.getColor(), asset.getSize(), newOwner, asset.getAppraisedValue()); Asset newAsset = new Asset(asset.getAssetID(), asset.getColor(), asset.getSize(), newOwner, asset.getAppraisedValue());
//Use a Jackson ObjectMapper to conver the Asset into string, sort it alphabetically and serialize it into a json string //Use a Genson to conver the Asset into string, sort it alphabetically and serialize it into a json string
String sortedJson = om.writeValueAsString(newAsset); String sortedJson = genson.serialize(newAsset);
stub.putStringState(assetID, sortedJson); stub.putStringState(assetID, sortedJson);
return newAsset; return newAsset;
@ -228,7 +219,7 @@ public final class AssetTransfer implements ContractInterface {
* @return array of assets found on the ledger * @return array of assets found on the ledger
*/ */
@Transaction(intent = Transaction.TYPE.EVALUATE) @Transaction(intent = Transaction.TYPE.EVALUATE)
public String GetAllAssets(final Context ctx) throws JsonProcessingException,IOException{ public String GetAllAssets(final Context ctx) throws JsonProcessingException, IOException {
ChaincodeStub stub = ctx.getStub(); ChaincodeStub stub = ctx.getStub();
List<Asset> queryResults = new ArrayList<Asset>(); List<Asset> queryResults = new ArrayList<Asset>();
@ -241,7 +232,7 @@ public final class AssetTransfer implements ContractInterface {
for (KeyValue result: results) { for (KeyValue result: results) {
Asset asset = genson.deserialize(result.getStringValue(), Asset.class); Asset asset = genson.deserialize(result.getStringValue(), Asset.class);
System.out.println(asset); System.out.println(asset);
queryResults.add(asset); queryResults.add(asset);
} }
@ -249,4 +240,4 @@ public final class AssetTransfer implements ContractInterface {
final String response = genson.serialize(queryResults); final String response = genson.serialize(queryResults);
return response; return response;
} }
} }

View file

@ -24,6 +24,9 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
public final class AssetTransferTest { public final class AssetTransferTest {
private final class MockKeyValue implements KeyValue { private final class MockKeyValue implements KeyValue {
@ -109,7 +112,7 @@ public final class AssetTransferTest {
class InvokeReadAssetTransaction { class InvokeReadAssetTransaction {
@Test @Test
public void whenAssetExists() { public void whenAssetExists() throws JsonProcessingException, IOException {
AssetTransfer contract = new AssetTransfer(); AssetTransfer contract = new AssetTransfer();
Context ctx = mock(Context.class); Context ctx = mock(Context.class);
ChaincodeStub stub = mock(ChaincodeStub.class); ChaincodeStub stub = mock(ChaincodeStub.class);
@ -141,7 +144,7 @@ public final class AssetTransferTest {
} }
@Test @Test
void invokeInitLedgerTransaction() { void invokeInitLedgerTransaction() throws JsonProcessingException {
AssetTransfer contract = new AssetTransfer(); AssetTransfer contract = new AssetTransfer();
Context ctx = mock(Context.class); Context ctx = mock(Context.class);
ChaincodeStub stub = mock(ChaincodeStub.class); ChaincodeStub stub = mock(ChaincodeStub.class);
@ -180,7 +183,7 @@ public final class AssetTransferTest {
} }
@Test @Test
public void whenAssetDoesNotExist() { public void whenAssetDoesNotExist() throws JsonProcessingException {
AssetTransfer contract = new AssetTransfer(); AssetTransfer contract = new AssetTransfer();
Context ctx = mock(Context.class); Context ctx = mock(Context.class);
ChaincodeStub stub = mock(ChaincodeStub.class); ChaincodeStub stub = mock(ChaincodeStub.class);
@ -194,7 +197,7 @@ public final class AssetTransferTest {
} }
@Test @Test
void invokeGetAllAssetsTransaction() { void invokeGetAllAssetsTransaction() throws JsonProcessingException, IOException {
AssetTransfer contract = new AssetTransfer(); AssetTransfer contract = new AssetTransfer();
Context ctx = mock(Context.class); Context ctx = mock(Context.class);
ChaincodeStub stub = mock(ChaincodeStub.class); ChaincodeStub stub = mock(ChaincodeStub.class);
@ -216,7 +219,7 @@ public final class AssetTransferTest {
class TransferAssetTransaction { class TransferAssetTransaction {
@Test @Test
public void whenAssetExists() { public void whenAssetExists() throws JsonProcessingException {
AssetTransfer contract = new AssetTransfer(); AssetTransfer contract = new AssetTransfer();
Context ctx = mock(Context.class); Context ctx = mock(Context.class);
ChaincodeStub stub = mock(ChaincodeStub.class); ChaincodeStub stub = mock(ChaincodeStub.class);
@ -251,7 +254,7 @@ public final class AssetTransferTest {
class UpdateAssetTransaction { class UpdateAssetTransaction {
@Test @Test
public void whenAssetExists() { public void whenAssetExists() throws JsonProcessingException {
AssetTransfer contract = new AssetTransfer(); AssetTransfer contract = new AssetTransfer();
Context ctx = mock(Context.class); Context ctx = mock(Context.class);
ChaincodeStub stub = mock(ChaincodeStub.class); ChaincodeStub stub = mock(ChaincodeStub.class);