RadixJsonRpcClient { public Maybe<Atom> getAtom(EUID hid) { JsonObject params = new JsonObject(); params.addProperty("hid", hid.toString()); JsonJavaType listOfAtom = Serialize.getInstance().jsonCollectionType(List.class, Atom.class); return this.jsonRpcCall("Ledger.getAtoms", params) .map(JsonRpcResponse::getResult) .<List<Atom>>map(result -> Serialize.getInstance().fromJson(result.toString(), listOfAtom)) .flatMapMaybe(list -> list.isEmpty() ? Maybe.empty() : Maybe.just(list.get(0))); } RadixJsonRpcClient(PersistentChannel channel); Single<JsonRpcResponse> jsonRpcCall(String method, JsonElement params); Single<JsonRpcResponse> jsonRpcCall(String method); Single<Integer> apiVersion(); Single<RadixUniverseConfig> universe(); Single<NodeRunnerData> getInfo(); Single<List<NodeRunnerData>> getLivePeers(); Completable pushAtom(Atom atom); Completable sendGetAtomStatusNotifications(String subscriberId, AID aid); Completable closeAtomStatusNotifications(String subscriberId); Observable<Notification<AtomStatusEvent>> observeAtomStatusNotifications(String subscriberId); Single<AtomStatus> getAtomStatus(AID aid); Maybe<Atom> getAtom(EUID hid); Observable<Notification<AtomObservation>> observeAtoms(String subscriberId); Completable cancelAtomsSubscribe(String subscriberId); Completable sendAtomsSubscribe(String subscriberId, AtomQuery atomQuery); static final Integer API_VERSION; }
@Test public void getAtomDoesNotExistTest() { PersistentChannel channel = mock(PersistentChannel.class); AtomicReference<Consumer<String>> listener = new AtomicReference<>(); doAnswer(a -> { Consumer<String> l = a.getArgument(0); listener.set(l); return mock(Cancellable.class); }).when(channel).addListener(any()); JsonParser parser = new JsonParser(); when(channel.sendMessage(any())).then(invocation -> { String msg = (String) invocation.getArguments()[0]; JsonObject jsonObject = parser.parse(msg).getAsJsonObject(); String id = jsonObject.get("id").getAsString(); JsonArray atoms = new JsonArray(); JsonObject response = new JsonObject(); response.addProperty("id", id); response.add("result", atoms); listener.get().accept(GsonJson.getInstance().stringFromGson(response)); return true; }); RadixJsonRpcClient jsonRpcClient = new RadixJsonRpcClient(channel); TestObserver<Atom> observer = new TestObserver<>(); jsonRpcClient.getAtom(new EUID(1)).subscribe(observer); observer.assertValueCount(0); observer.assertComplete(); observer.assertNoErrors(); }
@Test public void getAtomTest() { PersistentChannel channel = mock(PersistentChannel.class); AtomicReference<Consumer<String>> listener = new AtomicReference<>(); doAnswer(a -> { Consumer<String> l = a.getArgument(0); listener.set(l); return mock(Cancellable.class); }).when(channel).addListener(any()); JsonParser parser = new JsonParser(); when(channel.sendMessage(any())).then(invocation -> { String msg = (String) invocation.getArguments()[0]; JsonObject jsonObject = parser.parse(msg).getAsJsonObject(); String id = jsonObject.get("id").getAsString(); JsonArray atoms = new JsonArray(); Atom atom = Atom.create(Collections.emptyList()); String atomJson = Serialize.getInstance().toJson(atom, Output.API); atoms.add(parser.parse(atomJson)); JsonObject response = new JsonObject(); response.addProperty("id", id); response.add("result", atoms); listener.get().accept(GsonJson.getInstance().stringFromGson(response)); return true; }); RadixJsonRpcClient jsonRpcClient = new RadixJsonRpcClient(channel); TestObserver<Atom> observer = new TestObserver<>(); jsonRpcClient.getAtom(new EUID(1)).subscribe(observer); observer.assertValueCount(1); observer.assertComplete(); observer.assertNoErrors(); }