use of jakarta.ws.rs.client.WebTarget in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method getPropertyNamesIsImmutableTest.
/*
* @testName: getPropertyNamesIsImmutableTest
*
* @assertion_ids: JAXRS:JAVADOC:997; JAXRS:JAVADOC:758;
*
* @test_Strategy: Returns an immutable collection containing the property names
* available within the context of the current configuration instance. Set the
* new configuration property
*/
@Test
public void getPropertyNamesIsImmutableTest() throws Fault {
Assertable assertable = new Assertable() {
@Override
public void check1OnClient(Client client) throws Fault {
setNewProperty(client.getConfiguration());
assertSizeAndLog(client.getConfiguration(), 1);
}
@Override
public void check2OnTarget(WebTarget target) throws Fault {
setNewProperty(target.getConfiguration());
assertSizeAndLog(target.getConfiguration(), 2);
}
void setNewProperty(Configuration config) {
try {
config.getPropertyNames().add("property88");
} catch (Exception e) {
// can throw an exception or do nothing
// or getPropertyNames can be hard copy
}
}
void assertSizeAndLog(Configuration config, int size) throws Fault {
int pSize = config.getPropertyNames().size();
assertEqualsInt(pSize, size + registeredPropertiesCnt, "getPropertyNames() is NOT immutable", getLocation(), "got", pSize, "properties");
logMsg("Found", pSize, " properties");
}
};
checkConfigWithProperties(assertable);
}
use of jakarta.ws.rs.client.WebTarget in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method getInstancesTest.
/*
* @testName: getInstancesTest
*
* @assertion_ids: JAXRS:JAVADOC:994; JAXRS:JAVADOC:758;
*
* @test_Strategy: Get the immutable set of registered provider instances to be
* utilized by the configurable instance.
*
* Register a provider ("singleton") instance
*/
@Test
public void getInstancesTest() throws Fault {
Assertable assertable = new Assertable() {
@Override
public void check1OnClient(Client client) throws Fault {
assertSizeAndLog(client.getConfiguration(), 1);
}
@Override
public void check2OnTarget(WebTarget target) throws Fault {
assertSizeAndLog(target.getConfiguration(), 2);
}
void assertSizeAndLog(Configuration config, int count) throws Fault {
assertTrue(config.getInstances().size() == count + registeredInstancesCnt, "config.getClasses() return unexcepted size " + getLocation() + " : " + config.getInstances().size());
logMsg("Found", config.getInstances().size(), "providers");
}
};
Object[] providerObjects = createProviderInstances();
checkConfig(assertable, providerObjects);
}
use of jakarta.ws.rs.client.WebTarget in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method connectionLostFor1500msTest.
/*
* @testName: connectionLostFor1500msTest
*
* @assertion_ids: JAXRS:JAVADOC:1242; JAXRS:JAVADOC:1197; JAXRS:JAVADOC:1198;
* JAXRS:JAVADOC:1199; JAXRS:JAVADOC:1200; JAXRS:JAVADOC:1201;
* JAXRS:JAVADOC:1202; JAXRS:JAVADOC:1241; JAXRS:JAVADOC:1240;
* JAXRS:JAVADOC:1236;
*
* @test_Strategy: reconnectingEvery should send request to the server just
* twice, once without response, once after reconnect timeout with a response
*/
@Test
public void connectionLostFor1500msTest() throws Fault {
resetUnavailableServer();
// set to wait 3 seconds
setRequestContentEntity("1");
setProperty(Property.REQUEST, buildRequest(Request.POST, "su/lost"));
setProperty(Property.REQUEST_HEADERS, buildContentType(MediaType.TEXT_PLAIN_TYPE));
setProperty(Property.SEARCH_STRING, "1");
invoke();
Holder<InboundSseEvent> holder = new Holder<>();
WebTarget target = ClientBuilder.newClient().target(getAbsoluteUrl("su/sselost"));
try (SseEventSource source = SseEventSource.target(target).reconnectingEvery(2000L, TimeUnit.MILLISECONDS).build()) {
source.register(holder::set);
source.open();
sleep = sleepUntilHolderGetsFilled(holder);
}
int cnt = getServerCount();
logTrace("Slept for", sleep * millis, "ms");
assertNotNull(holder.get(), "Message not received, reconnect was done", cnt - 1, "times.");
logTrace("Received message", holder.get().readData());
assertTrue(sleep > 3, "The message has been sent unexpectedly soon, sooner then 2000ms");
assertTrue(sleep < 5, "The message has been sent unexpectedly late, after 3000ms");
assertEquals(SSEMessage.MESSAGE, holder.get().readData(), "Unexpected message received", holder.get().readData());
assertEquals(cnt, 2, "Server was reconnected", cnt, "times, unexpectedly");
}
use of jakarta.ws.rs.client.WebTarget in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method defaultWaiting1s.
/* Run test */
/*
* @testName: defaultWaiting1s
*
* @assertion_ids: JAXRS:JAVADOC:1197; JAXRS:JAVADOC:1198; JAXRS:JAVADOC:1199;
* JAXRS:JAVADOC:1200; JAXRS:JAVADOC:1201; JAXRS:JAVADOC:1202;
* JAXRS:JAVADOC:1241; JAXRS:JAVADOC:1240; JAXRS:JAVADOC:1236;
*
* @test_Strategy: In addition to handling the standard connection loss
* failures, JAX-RS SseEventSource automatically deals with any HTTP 503
* Service Unavailable responses from an SSE endpoint, that contain a
* "Retry-After" HTTP header with a valid value. The HTTP 503 + "Retry-After"
* technique is often used by HTTP endpoints as a means of connection and
* traffic throttling. In case a HTTP 503 + "Retry-After" response is received
* in return to a connection request, JAX-RS SSE event source will
* automatically schedule a new reconnect attempt and use the received
* "Retry-After" HTTP header value as a one-time override of the reconnect
* delay.
*/
@Test
public void defaultWaiting1s() throws Fault {
// define
Holder<InboundSseEvent> holder = new Holder<>();
WebTarget target = ClientBuilder.newClient().target(getAbsoluteUrl("su/sse"));
// check its working
try (SseEventSource source = SseEventSource.target(target).build()) {
source.register(holder::set);
source.open();
sleepUntilHolderGetsFilled(holder);
} catch (Exception e) {
throw new Fault(e);
}
assertNotNull(holder.get(), "Holder was not filled");
logTrace("Received message", holder.get().readData());
holder.set(null);
// set to return 503
setProperty(Property.REQUEST, buildRequest(Request.GET, "su/available"));
setProperty(Property.SEARCH_STRING, "OK");
invoke();
try (SseEventSource source = SseEventSource.target(target).build()) {
source.register(holder::set);
source.open();
} catch (Exception e) {
throw new Fault(e);
}
assertNull(holder.get(), "The event should be sent after default 1s in retry, not sooner");
// SseEventSource.close() has been called
// set to return 503
setProperty(Property.REQUEST, buildRequest(Request.GET, "su/available"));
setProperty(Property.SEARCH_STRING, "OK");
invoke();
holder = querySSEEndpoint("su/sse");
logTrace("Received message", holder.get().readData());
logTrace("Slept for", sleep * millis, "ms");
assertTrue(sleep < 4, "The message has been sent unexpectedly late, after 2000ms");
}
use of jakarta.ws.rs.client.WebTarget in project jaxrs-api by eclipse-ee4j.
the class SSEJAXRSClient method querySSEEndpoint.
protected Holder<InboundSseEvent> querySSEEndpoint(String endpoint, BiConsumer<SseEventSource, Holder<InboundSseEvent>> registrator) throws Fault {
Holder<InboundSseEvent> holder = new Holder<>();
WebTarget target = ClientBuilder.newClient().target(getAbsoluteUrl(endpoint));
try (SseEventSource source = SseEventSource.target(target).build()) {
registrator.accept(source, holder);
source.open();
sleep = sleepUntilHolderGetsFilled(holder);
assertTrue(source.isOpen(), "SseEventSource#isOpen returns false");
// } catch (Fault f) {
// throw f;
} catch (Exception e) {
throw new Fault(e);
}
assertNotNull(holder.get(), "The message was not received");
return holder;
}
Aggregations