Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manish2aug/b4133b9fbedc2175e15b303a0682b552 to your computer and use it in GitHub Desktop.
Save manish2aug/b4133b9fbedc2175e15b303a0682b552 to your computer and use it in GitHub Desktop.
@RegisterRestClient
@Path("/parties")
public interface MClient {
@GET
@Path("/")
@ClientHeaderParam(name = "Accept", value = "application/vnd.finplan-mc.v1+json")
@Produces("application/vnd.finplan-mc.v1+json")
CompletionStage<Response> getClients(
@QueryParam("contextType") String contextType,
@QueryParam("contextKey") String contextKey,
@QueryParam("offset") int offset,
@QueryParam("limit") int limit) throws Exception;
@GET
@Path("/total-parties")
@ClientHeaderParam(name = "Accept", value = "application/vnd.finplan-mc.v1+json")
CompletionStage<Response> getClientsCount(
@QueryParam("contextType") String contextType,
@QueryParam("contextKey") String contextKey) throws Exception;
}
@ApplicationScoped
public class MPartyRepositoryImpl implements MPartyRepository {
@Inject
@RestClient
private MClient client;
@Override
@Timed(name = "M_getClients", absolute = true, description = "Time needed to get the MC client list", unit = MetricUnits.MICROSECONDS)
public MCAdapter getMCClients(String cd1, String cd2, int offset, int limit) throws Exception {
final CompletableFuture<Response> clientCallFuture = (CompletableFuture<Response>) client.getClients(
"CDB",
cd1 + "-" + cd2,
offset,
limit);
final CompletableFuture<Response> countCallFuture = (CompletableFuture<Response>) client.getClientsCount("CDB",
cd1 + "-" + cd2);
CompletableFuture.allOf(countCallFuture, clientCallFuture);
final Response clientCallRes = clientCallFuture.join();
final Response countCallRes = countCallFuture.join();
MCAdapter adapter = new MCAdapter();
if(countCallRes.getStatus() == 200 && clientCallRes.getStatus()==200){
adapter.setClients(McClientFactory.getMappedClients(clientCallRes.readEntity(MParty.class)));
adapter.setCount(countCallRes.readEntity(MPartyCount.class).getTotalParties());
}
return adapter;
}
@ExtendWith(MockitoExtension.class)
class MPartyRepositoryImplTest {
@InjectMocks
MPartyRepositoryImpl repository = new MPartyRepositoryImpl();
private static final String CONTENT_TYPE_HEADER = "Content-Type: application/vnd.finplan-mc.v1+json";
private MClient client;
private static final String CD1 = "039999";
private static final String CD2 = "060267";
private static final int OFFSET = 0;
private static final int LIMIT = 100;
private static final String CONTEXT_KEY = "039996-507311";
private static final String CONTEXT_TYPE = "CDB";
private MockWebServer mockWebServer = new MockWebServer();
@BeforeAll
static void setupClass() {
System.setProperty("audit-file-folder", "logs");
System.setProperty("audit-logger-standalone", "true");
}
@BeforeEach
void setup() throws IOException {
client = RestClientBuilder
.newBuilder()
.baseUri(mockWebServer.url("/").uri())
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build(MClient.class);
MockitoAnnotations.initMocks(repository);
repository.setClient(client);
}
@AfterEach
public void tearDown() throws IOException {
mockWebServer.close();
}
@Test
void testTimeOut_getMClients() {
mockWebServer.enqueue(new MockResponse().setResponseCode(Status.GATEWAY_TIMEOUT.getStatusCode()));
mockWebServer.enqueue(new MockResponse().setResponseCode(Status.GATEWAY_TIMEOUT.getStatusCode()));
Exception be = assertThrows(BFFException.class, () -> repository.getMClients(CD1, CD2, OFFSET, LIMIT));
assertEquals(Status.GATEWAY_TIMEOUT.getStatusCode(), be.getCode());
}
}
@andymc12
Copy link

@manish2aug - are you setting the connect and read timeout for the client instance that is injected into MPartyRepositoryImpl ? I see that you are setting each to 5 seconds in the test class, but that will be a different instance. The timeouts need to be set using MP Config properties com.mypackage.MClient/mp-rest/connectTimeout and com.mypackage.MClient/mp-rest/readTimeout (the values are the time in milliseconds before timing out).

I'm suspecting that the problem is that the base URI for the endpoint may be specified incorrectly, and it is timing out - but using the default timeouts which may seem like it is hung. I'm planning to reproduce this to verify, but thought that you might want to look into the base URI and timeouts in the mean time.

Thanks, Andy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment