MerchantFunction implements StatefulFunction { @Override @SuppressWarnings("unchecked") public void invoke(Context context, Object input) { if (input instanceof QueryMerchantScore) { queryService(context, context.caller(), 2); } if (input instanceof AsyncOperationResult) { AsyncOperationResult<MerchantMetadata, Integer> result = (AsyncOperationResult<MerchantMetadata, Integer>) input; MerchantMetadata metadata = result.metadata(); if (result.unknown()) { queryService(context, getStatefunAddress(metadata), metadata.getRemainingAttempts()); } else if (result.failure()) { if (metadata.getRemainingAttempts() == 0) { context.send(getStatefunAddress(metadata), error()); } else { queryService(context, getStatefunAddress(metadata), metadata.getRemainingAttempts() - 1); } } else { context.send(getStatefunAddress(metadata), score(result.value())); } } } MerchantFunction(MerchantScoreService client); @Override @SuppressWarnings("unchecked") void invoke(Context context, Object input);  }
@Test public void testAsyncOperation() { FunctionTestHarness harness = FunctionTestHarness.test(new TestProvider(), MERCHANT_FN, SELF_ID); Assert.assertThat( harness.invoke(CALLER, QueryMerchantScore.getDefaultInstance()), sent(messagesTo(CALLER, equalTo(MerchantScore.newBuilder().setScore(1).build())))); }
@Test public void testSingleFailureOperation() { FunctionTestHarness harness = FunctionTestHarness.test(new TestProviderWithSingleFailure(), MERCHANT_FN, SELF_ID); Assert.assertThat( harness.invoke(CALLER, QueryMerchantScore.getDefaultInstance()), sent(messagesTo(CALLER, equalTo(MerchantScore.newBuilder().setScore(1).build())))); }
@Test public void testAsyncFailure() { FunctionTestHarness harness = FunctionTestHarness.test(new TestProviderWithMultipleFailures(), MERCHANT_FN, SELF_ID); Assert.assertThat( harness.invoke(CALLER, QueryMerchantScore.getDefaultInstance()), sent(messagesTo(CALLER, equalTo(MerchantScore.newBuilder().setError(true).build())))); }