Skip to content

Commit

Permalink
[Webhook] Share the created webhook endpoint by default
Browse files Browse the repository at this point in the history
  • Loading branch information
avano committed Nov 7, 2022
1 parent 4114d18 commit b7433d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import software.tnb.common.account.AccountFactory;
import software.tnb.common.service.Service;
import software.tnb.common.utils.JUnitUtils;
import software.tnb.webhook.account.WebhookAccount;
import software.tnb.webhook.validation.WebhookValidation;

Expand All @@ -17,7 +18,8 @@ public class Webhook implements Service {
private static final Logger LOG = LoggerFactory.getLogger(Webhook.class);

private WebhookAccount account;
private WebhookValidation validation;
// make validation static on purpose to share the created webhook endpoint in multiple test classes (in addition to afterAll method)
private static WebhookValidation validation;

public WebhookAccount account() {
if (account == null) {
Expand All @@ -28,16 +30,23 @@ public WebhookAccount account() {
}

public WebhookValidation validation() {
if (validation == null) {
validation = new WebhookValidation();
}
return validation;
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
if (JUnitUtils.isExtensionStillNeeded(extensionContext, this.getClass())) {
validation.clearEndpoint();
} else {
validation.deleteEndpoint();
validation = null;
}
}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
LOG.debug("Creating new Webhook validation");
validation = new WebhookValidation();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package software.tnb.webhook.validation;

import software.tnb.common.service.Validation;
import software.tnb.common.utils.HTTPUtils;
import software.tnb.webhook.validation.model.WebhookSiteRequest;
import software.tnb.webhook.validation.model.WebhookSiteRequests;
Expand All @@ -19,7 +20,7 @@
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class WebhookValidation {
public class WebhookValidation implements Validation {
private static final Logger LOG = LoggerFactory.getLogger(WebhookValidation.class);
private static final String WEBHOOK_ENDPOINT = "https://webhook.site";

Expand All @@ -31,9 +32,12 @@ public WebhookValidation() {
}

public String createEndpoint() {
LOG.debug("Creating new webhook endpoint");
token = new JSONObject(client.post(WEBHOOK_ENDPOINT + "/token", RequestBody.create(MediaType.parse("application/json"), "{}")).getBody())
.getString("uuid");
if (token == null) {
LOG.debug("Creating new webhook endpoint");
final String body = client.post(WEBHOOK_ENDPOINT + "/token",
RequestBody.create("{}", MediaType.parse("application/json"))).getBody();
token = new JSONObject(body).getString("uuid");
}
final String endpoint = String.format("%s/%s", WEBHOOK_ENDPOINT, token);
LOG.debug("Webhook endpoint: {}", endpoint);
return endpoint;
Expand All @@ -53,6 +57,7 @@ public void deleteEndpoint() {
final String url = String.format("%s/token/%s", WEBHOOK_ENDPOINT, token);
LOG.debug("Deleting webhook endpoint {}", url);
client.delete(url);
token = null;
}

public List<WebhookSiteRequest> getRequests() {
Expand All @@ -73,9 +78,9 @@ public List<WebhookSiteRequest> getRequests(String token, RequestQueryParameters
String body = client.get(url).getBody();

Gson gson = new GsonBuilder().registerTypeAdapter(
LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, type, jsonDeserializationContext) -> {
return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}).create();
LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, type, jsonDeserializationContext) -> {
return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}).create();

return gson.fromJson(body, WebhookSiteRequests.class).getData();
}
Expand Down

0 comments on commit b7433d6

Please sign in to comment.