diff --git a/.classpath b/.classpath index 110d123..83cab8a 100644 --- a/.classpath +++ b/.classpath @@ -13,8 +13,9 @@ - + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index ca4c66e..2bd4e77 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,11 +1,16 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.8 +org.eclipse.jdt.core.compiler.source=11 diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ab43d2..ae5a8cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm # Changelog for "event-publisher-library" ## [v1.2.0-SNAPSHOT] +- Restored correct behavior for event publishing with workflow id only sent back +- Extracted `AbstractHTTPWithJWTTokenAuthEventSender` class for easy subclassing +- Added new outcome check methods to inspect last send and last check actions results and some other facilities ## [v1.1.0] - Added `BufferedEventProcessor` to manual send bunch of events and controlling their output status (#23628) diff --git a/src/main/java/org/gcube/event/publisher/AbstractEventPublisher.java b/src/main/java/org/gcube/event/publisher/AbstractEventPublisher.java index 346e23a..fafce24 100644 --- a/src/main/java/org/gcube/event/publisher/AbstractEventPublisher.java +++ b/src/main/java/org/gcube/event/publisher/AbstractEventPublisher.java @@ -1,5 +1,7 @@ package org.gcube.event.publisher; +import java.net.HttpURLConnection; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,16 +36,61 @@ public abstract class AbstractEventPublisher implements EventPublisher { return null; } + @Override + public boolean isLastPublishOK() { + return getLastPublishEventHTTPResponseCode() == HttpURLConnection.HTTP_OK + || getLastPublishEventHTTPResponseCode() == HttpURLConnection.HTTP_CREATED; + } + + @Override + public int getLastPublishEventHTTPResponseCode() { + return eventSender.getLastSendHTTPResponseCode(); + } + + @Override + public EventStatus publishAndCheck(Event event) { + return publishAndCheck(event, 0); + } + + @Override + public EventStatus publishAndCheck(Event event, int delayMS) { + String instanceId = publish(event, true); + try { + if (delayMS > 0) { + Thread.sleep(delayMS); + } + return check(instanceId); + } catch (InterruptedException e) { + logger.error("Sleeping before performing the event status check", e); + return null; + } + } + @Override public EventStatus check(String instanceId) { if (instanceId != null) { - return getResultsParser().parseResults(eventSender.retrive(instanceId)); + return getResultsParser().parseResults(instanceId, eventSender.retrive(instanceId)); } else { logger.warn("Cannot check with a null instance ID"); - return EventStatus.NOT_FOUND(); + return EventStatus.NOT_FOUND(instanceId); } } + @Override + public EventStatus refresh(EventStatus eventStatus) { + return check(eventStatus.getInstanceId()); + } + + @Override + public boolean isLastCheckOK() { + return getLastCheckHTTPResponseCode() == 200; + } + + @Override + public int getLastCheckHTTPResponseCode() { + return eventSender.getLastRetrieveHTTPResponseCode(); + } + protected abstract EventSender createEventSender(); protected ResultsParser createResultsParser() { diff --git a/src/main/java/org/gcube/event/publisher/AbstractHTTPWithJWTTokenAuthEventSender.java b/src/main/java/org/gcube/event/publisher/AbstractHTTPWithJWTTokenAuthEventSender.java index 08266ec..6200e43 100644 --- a/src/main/java/org/gcube/event/publisher/AbstractHTTPWithJWTTokenAuthEventSender.java +++ b/src/main/java/org/gcube/event/publisher/AbstractHTTPWithJWTTokenAuthEventSender.java @@ -26,6 +26,8 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe protected URL tokenURL; protected int connectionTimeout; protected int readTimeout; + protected HTTPPost lastHTTPPost; + protected HTTPGet lastHTTPGet; public AbstractHTTPWithJWTTokenAuthEventSender(URL baseEndpointURL, String clientId, String clientSecret, URL tokenURL) { @@ -35,8 +37,8 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe this.clientId = clientId; this.clientSecret = clientSecret; this.tokenURL = tokenURL; - connectionTimeout = getDefaultConnectionTimeout(); - readTimeout = getDefaultReadTimeout(); + this.connectionTimeout = getDefaultConnectionTimeout(); + this.readTimeout = getDefaultReadTimeout(); } protected int getDefaultReadTimeout() { @@ -65,19 +67,25 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe @Override public void send(Event event) { - log.debug("Starting HTTP POST thread to: {}", baseEndpointURL); - new Thread(new HTTPPost(baseEndpointURL, event)).start(); + log.debug("Starting HTTP POST thread with base URL: {}", baseEndpointURL); + lastHTTPPost = new HTTPPost(baseEndpointURL, event); + new Thread(lastHTTPPost).start(); + } + + @Override + public int getLastSendHTTPResponseCode() { + return lastHTTPPost != null ? lastHTTPPost.gethttpResponseCode() : -1; } @Override public String sendAndGetResult(Event event) { log.debug("Starting HTTP POST thread to: {}", baseEndpointURL); - HTTPPost post = new HTTPPost(baseEndpointURL, event); - Thread postThread = new Thread(post); + lastHTTPPost = new HTTPPost(baseEndpointURL, event); + Thread postThread = new Thread(lastHTTPPost); postThread.start(); try { postThread.join(); - return post.getResult(); + return lastHTTPPost.getResult(); } catch (InterruptedException e) { log.error("While waiting for HTTP Post thread termination", e); return null; @@ -86,7 +94,13 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe @Override public JSONObject retrive(String id) { - return new HTTPGet(baseEndpointURL, id).readJSON(); + lastHTTPGet = new HTTPGet(baseEndpointURL, id); + return lastHTTPGet.readJSON(); + } + + @Override + public int getLastRetrieveHTTPResponseCode() { + return lastHTTPGet != null ? lastHTTPGet.gethttpResponseCode() : -1; } protected URL getTokenURL() { @@ -101,11 +115,17 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe protected static final int DEFAULT_READ_TIMEOUT = 5000; protected URL baseEndpoint; + protected int httpResponseCode = -1; + protected String httpContentType; public HTTPVerb(URL baseEndpoint) { this.baseEndpoint = baseEndpoint; } + public int gethttpResponseCode() { + return httpResponseCode; + } + } public class HTTPPost extends HTTPVerb implements Runnable { @@ -131,19 +151,23 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe public void run() { try { URL eventEndpoint = null; - try { - eventEndpoint = new URL(baseEndpointURL, event.getName()); - } catch (MalformedURLException e) { - log.error("Cannot compute event endpoint URL. Event name: " + event.getName() + ", base endpoint: " - + baseEndpointURL, e); - return; + if (baseEndpoint.toString().endsWith("/")) { + try { + log.debug("Removing trailing slash from base URL since the endpoint computation API changed"); + eventEndpoint = new URL(baseEndpoint.toString().substring(0, baseEndpoint.toString().length() - 1)); + } catch (MalformedURLException e) { + log.error("Cannot remove trailing slash from base endpoint URL", e); + return; + } + } else { + eventEndpoint = baseEndpoint; } boolean OK = false; do { try { log.debug("Getting auth token for client '{}' if needed", clientId); JWTToken token = getAuthorizationToken(); - log.debug("Performing HTTP POST to: {}", baseEndpoint); + log.debug("Performing HTTP POST to: {}", eventEndpoint); HttpURLConnection connection = (HttpURLConnection) eventEndpoint.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(connectionTimeout); @@ -151,8 +175,7 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe connection.setReadTimeout(readTimeout); log.trace("HTTP connection Read timeout set to: {}", connection.getReadTimeout()); connection.setRequestProperty("Content-Type", "application/json"); - // Commented out as per the Conductor issue: https://github.com/Netflix/conductor/issues/376 - // connection.setRequestProperty("Accept", "application/json"); + connection.setRequestProperty("Accept", "text/plain"); connection.setDoOutput(true); if (token != null) { log.debug("Setting authorization header as: {}", token.getAccessTokenAsBearer()); @@ -168,12 +191,14 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe os.close(); StringBuilder sb = new StringBuilder(); - int httpResultCode = connection.getResponseCode(); - log.trace("HTTP Response code: {}", httpResultCode); + httpResponseCode = connection.getResponseCode(); + log.trace("HTTP Response code: {}", httpResponseCode); + httpContentType = connection.getContentType(); + log.trace("HTTP Response content type is: {}", httpContentType); log.trace("Reading response"); InputStreamReader isr = null; - if (httpResultCode == HttpURLConnection.HTTP_OK) { + if (httpResponseCode == HttpURLConnection.HTTP_OK) { OK = true; // From this point ahead every exception should not set OK to false // since the server acknowledged with 200 @@ -191,12 +216,12 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe sb.deleteCharAt(0); result = sb.toString(); if (OK) { - log.debug("[{}] Event publish for {} is OK", httpResultCode, event.getName()); + log.debug("[{}] Event publish for {} is OK", httpResponseCode, event.getName()); } else { log.trace("Response message from server:\n{}", result); - if (shouldRetryWithCode(httpResultCode)) { + if (shouldRetryWithCode(httpResponseCode)) { if (retryings <= MAX_RETRYINGS) { - log.warn("[{}] Event publish ERROR, retrying in {} seconds", httpResultCode, + log.warn("[{}] Event publish ERROR, retrying in {} seconds", httpResponseCode, actualPause); Thread.sleep(actualPause * 1000); @@ -205,20 +230,21 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe retryings += 1; } else { log.error("[{}] Event publish ERROR, exhausted tries after {} retryings", - httpResultCode, + httpResponseCode, retryings); break; } } else { log.info("[{}] Event publish ERROR but should not retry with this HTTP code", - httpResultCode); + httpResponseCode); break; } } } catch (IOException | OpenIdConnectRESTHelperException e) { log.error("POSTing JSON to: " + eventEndpoint, e); + break; } } while (!OK); } catch (InterruptedException e) { @@ -249,7 +275,12 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe URL endpoint = null; JSONObject results = null; try { - endpoint = new URL(baseEndpoint, id); + String baseEndpointString = baseEndpoint.toString(); + if (baseEndpointString.endsWith("/")) { + endpoint = new URL(baseEndpointString + id); + } else { + endpoint = new URL(baseEndpointString + "/" + id); + } } catch (MalformedURLException e) { log.error("Cannot compute retrieve endpoint URL. ID: " + id + ", base endpoint: " + baseEndpointURL, e); return null; @@ -273,12 +304,14 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe } StringBuilder sb = new StringBuilder(); - int httpResultCode = connection.getResponseCode(); - log.trace("HTTP Response code: {}", httpResultCode); + httpResponseCode = connection.getResponseCode(); + log.trace("HTTP Response code: {}", httpResponseCode); + httpContentType = connection.getContentType(); + log.trace("HTTP Response content type is: {}", httpContentType); log.trace("Reading response"); InputStreamReader isr = null; - if (httpResultCode == HttpURLConnection.HTTP_OK) { + if (httpResponseCode == HttpURLConnection.HTTP_OK) { isr = new InputStreamReader(connection.getInputStream(), "UTF-8"); } else { isr = new InputStreamReader(connection.getErrorStream(), "UTF-8"); @@ -290,15 +323,19 @@ public abstract class AbstractHTTPWithJWTTokenAuthEventSender implements EventSe } br.close(); isr.close(); - if (httpResultCode == HttpURLConnection.HTTP_OK) { - log.debug("[{}] Got results for {}", httpResultCode, id); - try { - results = (JSONObject) new JSONParser().parse(sb.toString()); - } catch (ParseException e) { - log.warn("Error parsing results string as JSON: {}", sb.toString()); + if (httpResponseCode == HttpURLConnection.HTTP_OK) { + log.debug("[{}] Got results for {}", httpResponseCode, id); + if (httpContentType.equals("application/json")) { + try { + results = (JSONObject) new JSONParser().parse(sb.toString()); + } catch (ParseException e) { + log.warn("Error parsing results string as JSON: {}", sb.toString()); + } + } else { + log.warn("Received a non JSON reponse: {}", sb.toString()); } } else { - log.warn("[{}] Error getting results for ID {}", httpResultCode, id); + log.warn("[{}] Error getting results for ID {}", httpResponseCode, id); } } catch (IOException | OpenIdConnectRESTHelperException e) { log.error("Getting results from: " + endpoint, e); diff --git a/src/main/java/org/gcube/event/publisher/ConductorResultsParser.java b/src/main/java/org/gcube/event/publisher/ConductorResultsParser.java index e0512d8..33c6c4f 100644 --- a/src/main/java/org/gcube/event/publisher/ConductorResultsParser.java +++ b/src/main/java/org/gcube/event/publisher/ConductorResultsParser.java @@ -12,32 +12,32 @@ public class ConductorResultsParser implements ResultsParser { } @Override - public EventStatus parseResults(JSONObject results) { + public EventStatus parseResults(String uuid, JSONObject results) { EventStatus eventStatus = null; if (results != null) { JSONObject input = (JSONObject) results.get("input"); JSONObject output = (JSONObject) results.get("output"); switch ((String) results.get("status")) { case "COMPLETED": - eventStatus = EventStatus.COMPLETED(input, output); + eventStatus = EventStatus.COMPLETED(uuid, input, output); break; case "FAILED": - eventStatus = EventStatus.FAILED(input, output); + eventStatus = EventStatus.FAILED(uuid, input, output); break; case "NOT_FOUND": - eventStatus = EventStatus.NOT_FOUND(); + eventStatus = EventStatus.NOT_FOUND(uuid); break; case "PAUSED": - eventStatus = EventStatus.PAUSED(input); + eventStatus = EventStatus.PAUSED(uuid, input); break; case "RUNNING": - eventStatus = EventStatus.RUNNING(input); + eventStatus = EventStatus.RUNNING(uuid, input); break; case "TERMINATED": - eventStatus = EventStatus.TERMINATED(input, output); + eventStatus = EventStatus.TERMINATED(uuid, input, output); break; default: - eventStatus = EventStatus.NOT_FOUND(); + eventStatus = EventStatus.NOT_FOUND(uuid); } } else { logger.warn("Nothing to parse since JSON object is null"); diff --git a/src/main/java/org/gcube/event/publisher/EventPublisher.java b/src/main/java/org/gcube/event/publisher/EventPublisher.java index 0458668..f281ec7 100644 --- a/src/main/java/org/gcube/event/publisher/EventPublisher.java +++ b/src/main/java/org/gcube/event/publisher/EventPublisher.java @@ -2,10 +2,76 @@ package org.gcube.event.publisher; public interface EventPublisher { + /** + * Publish a new event and nothing more. The sender is not interested to the success/failure of the send. + * The results on the workflow engine, is the start of an instance of the workflow identified by the {@link Event#getName()} string. + * @param event the event to be published + */ void publish(Event event); + /** + * Publishes a new event and optionally wait for the result. + * If waitForResult parameter is false the behavior is the same of the {@link #publish(Event)} method, + * if true, the workflow id is returned as string if the publish had success. + * @param event the vent to be published + * @param waitForResult if the sender is interested or not to the resulting workflow id + * @return the resulting workflow id + */ String publish(Event event, boolean waitForResult); + /** + * Tells if the last publish was a success or not. + * @return true if the publish was OK, false otherwise + */ + boolean isLastPublishOK(); + + /** + * Returns the last returned HTTP response code of a publish. E.g. 200 if the send was OK or 404 if the event doesn't have a corresponding workflow definition. + * @return the HTTP response code of the last publish or -1 if an error occurred before the call (e.g. during the authorization or connection) + */ + int getLastPublishEventHTTPResponseCode(); + + /** + * Publish an event and immediately checks for the results status. + * The behavior is the same of the {@link #publishAndCheck(Event, int)} with delayMS argument less or equal to 0. + * @param event the event to be published + * @return an object with info about the event's running status + */ + EventStatus publishAndCheck(Event event); + + /** + * Publish an event and checks for the results status after a delay. + * The behavior is the same of the {@link #publishAndCheck(Event)} if delayMS argument is less or equal to 0. + * @param event the event to be published + * @param delayMS the delay betwen the publish and the query calls + * @return an object with info about the event's running status + */ + EventStatus publishAndCheck(Event event, int delayMS); + + /** + * Checks for the workflow results status. + * @param instanceId the workflow instance id, resulting of the {@link #publish(Event, boolean)} with waitForResult as true. + * @return an object with info about the event's running status + */ EventStatus check(String instanceId); + /** + * Refreshes an event status by checking for the status of the workflow execution represented by the {@link EventStatus#getInstanceId()} string. + * @param eventStatus a previously obtained event status. + * @return an object with new info about the event's running status + */ + EventStatus refresh(EventStatus eventStatus); + + /** + * Tells if the last check was a success or not. + * @return true if the publish was OK, false otherwise + */ + boolean isLastCheckOK(); + + /** + * Returns the last returned HTTP response code of a check. E.g. 200 if the send was OK or 404 if the event doesn't have a corresponding workflow instance. + * @return the HTTP response code of the last publish or -1 if an error occurred before the call (e.g. during the authorization or connection) + */ + int getLastCheckHTTPResponseCode(); + } \ No newline at end of file diff --git a/src/main/java/org/gcube/event/publisher/EventSender.java b/src/main/java/org/gcube/event/publisher/EventSender.java index 58b7f58..eff14e3 100644 --- a/src/main/java/org/gcube/event/publisher/EventSender.java +++ b/src/main/java/org/gcube/event/publisher/EventSender.java @@ -4,10 +4,36 @@ import org.json.simple.JSONObject; public interface EventSender { + /** + * Sends an event. + * @param event the event to send + */ void send(Event event); + /** + * Return the last send HTTP response code. + * @return the HTTP response code or -1 if an error occurred before the call (e.g. during the authorization or connection) + */ + int getLastSendHTTPResponseCode(); + + /** + * Send an event and get results. + * @param event the event to send + * @return the result of the call + */ String sendAndGetResult(Event event); + /** + * Retrieves the instance of the provided workflow instance. + * @param id the workflow instance id + * @return + */ JSONObject retrive(String id); + /** + * Return the last retrieve HTTP response code. + * @return the HTTP response code or -1 if an error occurred before the call (e.g. during the authorization or connection) + */ + int getLastRetrieveHTTPResponseCode(); + } \ No newline at end of file diff --git a/src/main/java/org/gcube/event/publisher/EventStatus.java b/src/main/java/org/gcube/event/publisher/EventStatus.java index c1a751e..d3d8c1a 100644 --- a/src/main/java/org/gcube/event/publisher/EventStatus.java +++ b/src/main/java/org/gcube/event/publisher/EventStatus.java @@ -8,52 +8,62 @@ public class EventStatus { RUNNING, COMPLETED, FAILED, TIMED_OUT, TERMINATED, PAUSED, NOT_FOUND; } - public static EventStatus RUNNING(JSONObject input) { - return new EventStatus(Status.RUNNING, input); + public static EventStatus RUNNING(String instanceId, JSONObject input) { + return new EventStatus(instanceId, Status.RUNNING, input); } - public static EventStatus COMPLETED(JSONObject input, JSONObject output) { - return new EventStatus(Status.COMPLETED, input, output); + public static EventStatus COMPLETED(String instanceId, JSONObject input, JSONObject output) { + return new EventStatus(instanceId, Status.COMPLETED, input, output); } - public static EventStatus FAILED(JSONObject input, JSONObject output) { - return new EventStatus(Status.FAILED, input, output); + public static EventStatus FAILED(String instanceId, JSONObject input, JSONObject output) { + return new EventStatus(instanceId, Status.FAILED, input, output); } - public static EventStatus TIMED_OUT(JSONObject input) { - return new EventStatus(Status.TIMED_OUT, input); + public static EventStatus TIMED_OUT(String instanceId, JSONObject input) { + return new EventStatus(instanceId, Status.TIMED_OUT, input); } - public static EventStatus TERMINATED(JSONObject input, JSONObject output) { - return new EventStatus(Status.TERMINATED, input, output); + public static EventStatus TERMINATED(String instanceId, JSONObject input, JSONObject output) { + return new EventStatus(instanceId, Status.TERMINATED, input, output); } - public static EventStatus PAUSED(JSONObject input) { - return new EventStatus(Status.PAUSED, input); + public static EventStatus PAUSED(String instanceId, JSONObject input) { + return new EventStatus(instanceId, Status.PAUSED, input); } - public static EventStatus NOT_FOUND() { - return new EventStatus(Status.NOT_FOUND); + public static EventStatus NOT_FOUND(String instanceId) { + return new EventStatus(instanceId, Status.NOT_FOUND); } + private String instanceId; private Status status; private JSONObject input; private JSONObject output; - private EventStatus(Status status) { - this(status, null); + private EventStatus(String uuid, Status status) { + this(uuid, status, null); } - private EventStatus(Status status, JSONObject input) { - this(status, input, null); + private EventStatus(String instanceId, Status status, JSONObject input) { + this(instanceId, status, input, null); } - private EventStatus(Status status, JSONObject input, JSONObject output) { + private EventStatus(String instanceId, Status status, JSONObject input, JSONObject output) { + setInstanceId(instanceId); setStatus(status); setInput(input); setOutput(output); } + public void setInstanceId(String instanceId) { + this.instanceId = instanceId; + } + + public String getInstanceId() { + return instanceId; + } + public void setStatus(Status status) { this.status = status; } diff --git a/src/main/java/org/gcube/event/publisher/ResultsParser.java b/src/main/java/org/gcube/event/publisher/ResultsParser.java index 921dd94..08d4cff 100644 --- a/src/main/java/org/gcube/event/publisher/ResultsParser.java +++ b/src/main/java/org/gcube/event/publisher/ResultsParser.java @@ -4,6 +4,6 @@ import org.json.simple.JSONObject; public interface ResultsParser { - EventStatus parseResults(JSONObject results); + EventStatus parseResults(String uuid, JSONObject results); } diff --git a/src/test/java/org/gcube/event/publisher/HTTPEventSenderTest.java b/src/test/java/org/gcube/event/publisher/HTTPEventSenderTest.java index 03b1177..c9f4393 100644 --- a/src/test/java/org/gcube/event/publisher/HTTPEventSenderTest.java +++ b/src/test/java/org/gcube/event/publisher/HTTPEventSenderTest.java @@ -3,8 +3,13 @@ package org.gcube.event.publisher; import java.net.MalformedURLException; import java.net.URL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class HTTPEventSenderTest { + protected static final Logger logger = LoggerFactory.getLogger(HTTPEventSenderTest.class); + public HTTPEventSenderTest() { } @@ -14,7 +19,8 @@ public class HTTPEventSenderTest { protected EventSender createEventSender() { try { return new HTTPWithOIDCAuthEventSender( - new URL("https://conductor.dev.d4science.org/api/workflow/"), "lr62_portal", + new URL("https://conductor.cloud-dev.d4science.org/api/workflow/"), + "lr62_portal", "6937125d-6d70-404a-9d63-908c1e6415c4", new URL("https://accounts.dev.d4science.org/auth/realms/d4science/protocol/openid-connect/token")); } catch (MalformedURLException e) { @@ -24,9 +30,31 @@ public class HTTPEventSenderTest { } }; - System.out.println("Published: " + publisher.publish(new Event("startup", "portal", "gcube"), true)); - EventStatus status = publisher.check("93675866-c209-4584-8576-ec641df63e9a"); - System.out.println(status); + String id = publisher.publish(new Event("test", "test", "test"), true); + logger.info("*** Published: {}", id); + logger.info("*** Publish was {} and HTTP response status is: {}", + publisher.isLastPublishOK() ? "OK" : "KO", publisher.getLastPublishEventHTTPResponseCode()); + + EventStatus status = null; + do { + status = publisher.check(id); + logger.trace("*** Check was {} and HTTP response status is: {}", publisher.isLastCheckOK() ? "OK" : "KO", + publisher.getLastCheckHTTPResponseCode()); + } while (status.getStatus() != EventStatus.Status.COMPLETED && status.getStatus() != EventStatus.Status.FAILED); + + logger.info("*** Final status is: {}", status.toString()); + + status = publisher.publishAndCheck(new Event("test", "test", "test"), 1000); + logger.info("*** Publish was {} and HTTP response status is: {}", + publisher.isLastPublishOK() ? "OK" : "KO", publisher.getLastPublishEventHTTPResponseCode()); + + do { + status = publisher.refresh(status); + logger.trace("*** Check was {} and HTTP response status is: {}", publisher.isLastCheckOK() ? "OK" : "KO", + publisher.getLastCheckHTTPResponseCode()); + } while (status.getStatus() != EventStatus.Status.COMPLETED && status.getStatus() != EventStatus.Status.FAILED); + + logger.info("*** Final status is: {}", status.toString()); } }