Update chaincode to be compatible with doc.

E.g. Return key of the queried record

Signed-off-by: Stefan Obermeier <scray@stefan-obermeier.de>
This commit is contained in:
Stefan Obermeier 2020-04-04 01:27:52 +02:00
parent 3e72a249ac
commit 04bdd1d308
3 changed files with 83 additions and 15 deletions

View file

@ -0,0 +1,68 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.fabric.samples.fabcar;
import java.util.Objects;
import org.hyperledger.fabric.contract.annotation.DataType;
import org.hyperledger.fabric.contract.annotation.Property;
import com.owlike.genson.annotation.JsonProperty;
/**
* CarQueryResult structure used for handling result of query
*
*/
@DataType()
public final class CarQueryResult {
@Property()
private final String key;
@Property()
private final Car record;
public CarQueryResult(@JsonProperty("Key") final String key, @JsonProperty("Record") final Car record) {
this.key = key;
this.record = record;
}
public String getKey() {
return key;
}
public Car getRecord() {
return record;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
CarQueryResult other = (CarQueryResult) obj;
Boolean recordsAreEquals = this.getRecord().equals(other.getRecord());
Boolean keysAreEquals = this.getKey().equals(other.getKey());
return recordsAreEquals && keysAreEquals;
}
@Override
public int hashCode() {
return Objects.hash(this.getKey(), this.getRecord());
}
@Override
public String toString() {
return "{\"Key\":\"" + key + "\"" + "\"Record\":{\"" + record + "}\"}";
}
}

View file

@ -140,21 +140,21 @@ public final class FabCar implements ContractInterface {
* @return array of Cars found on the ledger
*/
@Transaction()
public Car[] queryAllCars(final Context ctx) {
public CarQueryResult[] queryAllCars(final Context ctx) {
ChaincodeStub stub = ctx.getStub();
final String startKey = "CAR0";
final String endKey = "CAR999";
List<Car> cars = new ArrayList<Car>();
List<CarQueryResult> queryResults = new ArrayList<CarQueryResult>();
QueryResultsIterator<KeyValue> results = stub.getStateByRange(startKey, endKey);
for (KeyValue result: results) {
Car car = genson.deserialize(result.getStringValue(), Car.class);
cars.add(car);
}
Car[] response = cars.toArray(new Car[cars.size()]);
Car car = genson.deserialize(result.getStringValue(), Car.class);
queryResults.add(new CarQueryResult(result.getKey(), car));
}
CarQueryResult[] response = queryResults.toArray(new CarQueryResult[queryResults.size()]);
return response;
}

View file

@ -213,14 +213,14 @@ public final class FabCarTest {
when(ctx.getStub()).thenReturn(stub);
when(stub.getStateByRange("CAR0", "CAR999")).thenReturn(new MockCarResultsIterator());
Car[] cars = contract.queryAllCars(ctx);
CarQueryResult[] cars = contract.queryAllCars(ctx);
final List<Car> expectedCars = new ArrayList<Car>();
expectedCars.add(new Car("Toyota", "Prius", "blue", "Tomoko"));
expectedCars.add(new Car("Ford", "Mustang", "red", "Brad"));
expectedCars.add(new Car("Hyundai", "Tucson", "green", "Jin Soo"));
expectedCars.add(new Car("Fiat", "Punto", "violet", "Pari"));
expectedCars.add(new Car("Holden", "Barina", "brown", "Shotaro"));
final List<CarQueryResult> expectedCars = new ArrayList<CarQueryResult>();
expectedCars.add(new CarQueryResult("CAR0", new Car("Toyota", "Prius", "blue", "Tomoko")));
expectedCars.add(new CarQueryResult("CAR1", new Car("Ford", "Mustang", "red", "Brad")));
expectedCars.add(new CarQueryResult("CAR2", new Car("Hyundai", "Tucson", "green", "Jin Soo")));
expectedCars.add(new CarQueryResult("CAR7", new Car("Fiat", "Punto", "violet", "Pari")));
expectedCars.add(new CarQueryResult("CAR9", new Car("Holden", "Barina", "brown", "Shotaro")));
assertThat(cars).containsExactlyElementsOf(expectedCars);
}
@ -239,7 +239,7 @@ public final class FabCarTest {
Car car = contract.changeCarOwner(ctx, "CAR0", "Dr Evil");
assertThat(car).isEqualTo(new Car("Toyota", "Prius", "blue", "Dr Evil"));
assertThat(car).isEqualTo(new CarQueryResult("CAR0", new Car("Toyota", "Prius", "blue", "Dr Evil")));
}
@Test