mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-19 08:15:08 +00:00
Fix fabcar Java chaincode test & coverage failures (#218)
Also updates queryAllCars to match other implementations Signed-off-by: James Taylor <jamest@uk.ibm.com>
This commit is contained in:
parent
2f58fb29c4
commit
c0570df2f8
4 changed files with 109 additions and 14 deletions
|
|
@ -61,7 +61,7 @@ public final class CarQueryResult {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"Key\":\"" + key + "\"" + "\"Record\":{\"" + record + "}\"}";
|
||||
return this.getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()) + " [key=" + key + ", record="
|
||||
+ record + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,7 +140,7 @@ public final class FabCar implements ContractInterface {
|
|||
* @return array of Cars found on the ledger
|
||||
*/
|
||||
@Transaction()
|
||||
public CarQueryResult[] queryAllCars(final Context ctx) {
|
||||
public String queryAllCars(final Context ctx) {
|
||||
ChaincodeStub stub = ctx.getStub();
|
||||
|
||||
final String startKey = "";
|
||||
|
|
@ -154,7 +154,7 @@ public final class FabCar implements ContractInterface {
|
|||
queryResults.add(new CarQueryResult(result.getKey(), car));
|
||||
}
|
||||
|
||||
CarQueryResult[] response = queryResults.toArray(new CarQueryResult[queryResults.size()]);
|
||||
final String response = genson.serialize(queryResults);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package org.hyperledger.fabric.samples.fabcar;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public final class CarQueryResultTest {
|
||||
|
||||
@Nested
|
||||
class Equality {
|
||||
|
||||
@Test
|
||||
public void isReflexive() {
|
||||
CarQueryResult cqr = new CarQueryResult("CAR1", new Car("Toyota", "Prius", "blue", "Tomoko"));
|
||||
|
||||
assertThat(cqr).isEqualTo(cqr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSymmetric() {
|
||||
Car car = new Car("Toyota", "Prius", "blue", "Tomoko");
|
||||
CarQueryResult cqrA = new CarQueryResult("CAR1", car);
|
||||
CarQueryResult cqrB = new CarQueryResult("CAR1", car);
|
||||
|
||||
assertThat(cqrA).isEqualTo(cqrB);
|
||||
assertThat(cqrB).isEqualTo(cqrA);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isTransitive() {
|
||||
Car car = new Car("Toyota", "Prius", "blue", "Tomoko");
|
||||
CarQueryResult cqrA = new CarQueryResult("CAR1", car);
|
||||
CarQueryResult cqrB = new CarQueryResult("CAR1", car);
|
||||
CarQueryResult cqrC = new CarQueryResult("CAR1", car);
|
||||
|
||||
assertThat(cqrA).isEqualTo(cqrB);
|
||||
assertThat(cqrB).isEqualTo(cqrC);
|
||||
assertThat(cqrA).isEqualTo(cqrC);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesKeyInequality() {
|
||||
CarQueryResult cqrA = new CarQueryResult("CAR1", new Car("Toyota", "Prius", "blue", "Tomoko"));
|
||||
CarQueryResult cqrB = new CarQueryResult("CAR2", new Car("Toyota", "Prius", "blue", "Tomoko"));
|
||||
|
||||
assertThat(cqrA).isNotEqualTo(cqrB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesRecordInequality() {
|
||||
CarQueryResult cqrA = new CarQueryResult("CAR1", new Car("Toyota", "Prius", "blue", "Tomoko"));
|
||||
CarQueryResult cqrB = new CarQueryResult("CAR1", new Car("Ford", "Mustang", "red", "Brad"));
|
||||
|
||||
assertThat(cqrA).isNotEqualTo(cqrB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesKeyRecordInequality() {
|
||||
CarQueryResult cqrA = new CarQueryResult("CAR1", new Car("Toyota", "Prius", "blue", "Tomoko"));
|
||||
CarQueryResult cqrB = new CarQueryResult("CAR2", new Car("Ford", "Mustang", "red", "Brad"));
|
||||
|
||||
assertThat(cqrA).isNotEqualTo(cqrB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesOtherObjects() {
|
||||
CarQueryResult cqrA = new CarQueryResult("CAR1", new Car("Toyota", "Prius", "blue", "Tomoko"));
|
||||
String cqrB = "not a car";
|
||||
|
||||
assertThat(cqrA).isNotEqualTo(cqrB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesNull() {
|
||||
CarQueryResult cqr = new CarQueryResult("CAR1", new Car("Toyota", "Prius", "blue", "Tomoko"));
|
||||
|
||||
assertThat(cqr).isNotEqualTo(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringIdentifiesCarQueryResult() {
|
||||
CarQueryResult cqr = new CarQueryResult("CAR1", new Car("Toyota", "Prius", "blue", "Tomoko"));
|
||||
|
||||
assertThat(cqr.toString()).isEqualTo("CarQueryResult@65766eb3 [key=CAR1, "
|
||||
+ "record=Car@61a77e4f [make=Toyota, model=Prius, color=blue, owner=Tomoko]]");
|
||||
}
|
||||
}
|
||||
|
|
@ -213,16 +213,18 @@ public final class FabCarTest {
|
|||
when(ctx.getStub()).thenReturn(stub);
|
||||
when(stub.getStateByRange("", "")).thenReturn(new MockCarResultsIterator());
|
||||
|
||||
CarQueryResult[] cars = contract.queryAllCars(ctx);
|
||||
String cars = contract.queryAllCars(ctx);
|
||||
|
||||
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);
|
||||
assertThat(cars).isEqualTo("[{\"key\":\"CAR0\","
|
||||
+ "\"record\":{\"color\":\"blue\",\"make\":\"Toyota\",\"model\":\"Prius\",\"owner\":\"Tomoko\"}},"
|
||||
+ "{\"key\":\"CAR1\","
|
||||
+ "\"record\":{\"color\":\"red\",\"make\":\"Ford\",\"model\":\"Mustang\",\"owner\":\"Brad\"}},"
|
||||
+ "{\"key\":\"CAR2\","
|
||||
+ "\"record\":{\"color\":\"green\",\"make\":\"Hyundai\",\"model\":\"Tucson\",\"owner\":\"Jin Soo\"}},"
|
||||
+ "{\"key\":\"CAR7\","
|
||||
+ "\"record\":{\"color\":\"violet\",\"make\":\"Fiat\",\"model\":\"Punto\",\"owner\":\"Pari\"}},"
|
||||
+ "{\"key\":\"CAR9\","
|
||||
+ "\"record\":{\"color\":\"brown\",\"make\":\"Holden\",\"model\":\"Barina\",\"owner\":\"Shotaro\"}}]");
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
|
@ -239,7 +241,7 @@ public final class FabCarTest {
|
|||
|
||||
Car car = contract.changeCarOwner(ctx, "CAR0", "Dr Evil");
|
||||
|
||||
assertThat(car).isEqualTo(new CarQueryResult("CAR0", new Car("Toyota", "Prius", "blue", "Dr Evil")));
|
||||
assertThat(car).isEqualTo(new Car("Toyota", "Prius", "blue", "Dr Evil"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in a new issue