Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,33 @@
import org.apache.camel.component.clickup.util.ClickUpTestSupport;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

public class ClickUpConfigurationTest extends ClickUpTestSupport {
class ClickUpConfigurationTest extends ClickUpTestSupport {

private final static Long WORKSPACE_ID = 12345L;
private final static String BASE_URL = "https://mock-api.clickup.com";
private final static String AUTHORIZATION_TOKEN = "mock-authorization-token";
private final static String WEBHOOK_SECRET = "mock-webhook-secret";
private final static Set<String> EVENTS = new HashSet<>(Arrays.asList("taskTimeTrackedUpdated"));
private static final String BASE_URL = "https://mock-api.clickup.com";
private static final Set<String> EVENTS = new HashSet<>(Arrays.asList("taskTimeTrackedUpdated"));

@Test
public void testClickUpConfiguration() {
void testClickUpConfiguration() {
ClickUpEndpoint endpoint = (ClickUpEndpoint) context().getEndpoints().stream()
.filter(e -> e instanceof ClickUpEndpoint).findAny().get();
ClickUpConfiguration config = endpoint.getConfiguration();

assertEquals(WORKSPACE_ID, config.getWorkspaceId());
assertEquals(BASE_URL, config.getBaseUrl());
assertEquals(AUTHORIZATION_TOKEN, config.getAuthorizationToken());
assertEquals(WEBHOOK_SECRET, config.getWebhookSecret());
assertEquals(EVENTS, config.getEvents());
assertThat(config.getWorkspaceId()).isEqualTo(WORKSPACE_ID);
assertThat(config.getBaseUrl()).isEqualTo(BASE_URL);
assertThat(config.getAuthorizationToken()).isEqualTo(AUTHORIZATION_TOKEN);
assertThat(config.getWebhookSecret()).isEqualTo(WEBHOOK_SECRET);
assertThat(config.getEvents()).isEqualTo(EVENTS);
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("webhook:clickup:" + WORKSPACE_ID + "?baseUrl=" + BASE_URL + "&authorizationToken=" + AUTHORIZATION_TOKEN
+ "&webhookSecret=" + WEBHOOK_SECRET + "&events=" + String.join(",", EVENTS)
+ "&webhookAutoRegister=false")
fromF("webhook:clickup:%s?baseUrl=%s&authorizationToken=%s&webhookSecret=%s&events=%s&webhookAutoRegister=false",
WORKSPACE_ID, BASE_URL, AUTHORIZATION_TOKEN, WEBHOOK_SECRET, String.join(",", EVENTS))
.log("Received: ${body}");
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.apache.camel.component.clickup;

import java.io.InputStream;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.camel.Exchange;
import org.apache.camel.RoutesBuilder;
Expand All @@ -28,45 +31,35 @@
import org.apache.camel.component.webhook.WebhookConfiguration;
import org.apache.camel.component.webhook.WebhookEndpoint;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClickUpWebhookCallTest extends ClickUpTestSupport {
import static java.util.List.of;

private final static Logger LOGGER = LoggerFactory.getLogger(ClickUpWebhookCallTest.class);
class ClickUpWebhookCallTest extends ClickUpTestSupport {

private final static Long WORKSPACE_ID = 12345L;
private final static String AUTHORIZATION_TOKEN = "mock-authorization-token";
private final static String WEBHOOK_SECRET = "mock-webhook-secret";
private final static Set<String> EVENTS = new HashSet<>(List.of("taskTimeTrackedUpdated"));

public static final String MESSAGES_EVENTS_TIME_TRACKING_CREATED_FILENAME = "messages/events/time-tracking-created.json";
public static final String MESSAGES_EVENTS_TIME_TRACKING_CREATED_SIGNATURE
private static final Set<String> EVENTS = new HashSet<>(of("taskTimeTrackedUpdated"));
private static final String TIME_TRACKING_CREATED_RESOURCE = "messages/events/time-tracking-created.json";
private static final String TIME_TRACKING_CREATED_SIGNATURE
= "ac99f10017e28db6839941c184964890ec3262b1d6b1756d33ff53d972d5a361";

@Test
public void testWebhookCall() throws Exception {
void testWebhookCall() throws Exception {
WebhookConfiguration config
= ((WebhookEndpoint) context().getRoute("webhook").getConsumer().getEndpoint()).getConfiguration();
String url = config.computeFullExternalUrl();

LOGGER.info("Webhook external url: {}", url);

try (InputStream content
= getClass().getClassLoader().getResourceAsStream(MESSAGES_EVENTS_TIME_TRACKING_CREATED_FILENAME)) {
LOGGER.info("message content: {}", content);

MockEndpoint mock = getMockEndpoint("mock:endpoint");
mock.expectedMessageCount(1);
mock.expectedMessagesMatches(exchange -> exchange.getIn().getBody() instanceof TaskTimeTrackedUpdatedEvent);
MockEndpoint mock = getMockEndpoint("mock:endpoint");
mock.expectedMessageCount(1);
mock.expectedMessagesMatches(exchange -> exchange.getIn().getBody() instanceof TaskTimeTrackedUpdatedEvent);

try (InputStream content = getClass().getClassLoader().getResourceAsStream(TIME_TRACKING_CREATED_RESOURCE)) {
Map<String, Object> headers = new HashMap<>();
headers.put(Exchange.HTTP_METHOD, "POST");
headers.put(Exchange.CONTENT_TYPE, "application/json");
headers.put("x-signature", MESSAGES_EVENTS_TIME_TRACKING_CREATED_SIGNATURE);
headers.put("x-signature", TIME_TRACKING_CREATED_SIGNATURE);
template().sendBodyAndHeaders("netty-http:" + url, content, headers);
mock.assertIsSatisfied();
}

mock.assertIsSatisfied();
}

@Override
Expand All @@ -78,8 +71,8 @@ public void configure() {
.host("localhost")
.port(port.getPort());

from("webhook:clickup:" + WORKSPACE_ID + "?authorizationToken=" + AUTHORIZATION_TOKEN + "&webhookSecret="
+ WEBHOOK_SECRET + "&events=" + String.join(",", EVENTS) + "&webhookAutoRegister=false")
fromF("webhook:clickup:%s?authorizationToken=%s&webhookSecret=%s&events=%s&webhookAutoRegister=false",
WORKSPACE_ID, AUTHORIZATION_TOKEN, WEBHOOK_SECRET, String.join(",", EVENTS))
.id("webhook")
.to("mock:endpoint");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.UnknownHostException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -42,98 +37,70 @@
import org.apache.camel.component.webhook.WebhookEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.test.junit6.TestExecutionConfiguration;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;

public class ClickUpWebhookRegistrationAlreadyExistsTest extends ClickUpTestSupport {

private final static Long WORKSPACE_ID = 12345L;
private final static String AUTHORIZATION_TOKEN = "mock-authorization-token";
private final static String WEBHOOK_SECRET = "mock-webhook-secret";
private final static Set<String> EVENTS = new HashSet<>(List.of("taskTimeTrackedUpdated"));
class ClickUpWebhookRegistrationAlreadyExistsTest extends ClickUpTestSupport {

private static final Set<String> EVENTS = new HashSet<>(List.of("taskTimeTrackedUpdated"));
private static final ObjectMapper MAPPER = new ObjectMapper();
public static final String WEBHOOK_ALREADY_EXISTS_JSON = "messages/webhook-already-exists.json";
public static final String WEBHOOKS = "messages/webhooks.json";
private static final String WEBHOOK_ALREADY_EXISTS_JSON = "messages/webhook-already-exists.json";
private static final String WEBHOOKS = "messages/webhooks.json";

@Override
public void configureTest(TestExecutionConfiguration testExecutionConfiguration) {
super.configureTest(testExecutionConfiguration);

testExecutionConfiguration.withUseRouteBuilder(false);
}

@Test
public void testAutomaticRegistrationWhenWebhookConfigurationAlreadyExists() throws Exception {
final ClickUpMockRoutes.MockProcessor<String> creationMockProcessor
void testAutomaticRegistrationWhenWebhookConfigurationAlreadyExists() throws Exception {
ClickUpMockRoutes.MockProcessor<String> creationMockProcessor
= getMockRoutes().getMock("POST", "team/" + WORKSPACE_ID + "/webhook");
creationMockProcessor.clearRecordedMessages();

final ClickUpMockRoutes.MockProcessor<String> readMockProcessor
ClickUpMockRoutes.MockProcessor<String> readMockProcessor
= getMockRoutes().getMock("GET", "team/" + WORKSPACE_ID + "/webhook");
readMockProcessor.clearRecordedMessages();

try (final DefaultCamelContext mockContext = new DefaultCamelContext()) {
try (DefaultCamelContext mockContext = new DefaultCamelContext()) {
mockContext.addRoutes(getMockRoutes());
mockContext.start();

/* Make sure the ClickUp mock API is up and running */
Awaitility.await()
.atMost(5, TimeUnit.SECONDS)
.until(() -> {
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + port + "/clickup-api-mock/health")).GET().build();

final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode() == 200;
});

context().addRoutes(new RouteBuilder() {
@Override
public void configure() {
String apiMockBaseUrl = "http://localhost:" + port + "/clickup-api-mock";

from("webhook:clickup:" + WORKSPACE_ID + "?authorizationToken=" + AUTHORIZATION_TOKEN + "&webhookSecret="
+ WEBHOOK_SECRET + "&events=" + String.join(",", EVENTS) + "&webhookAutoRegister=true&baseUrl="
+ apiMockBaseUrl)
.id("webhook")
.to("mock:endpoint");
}
});
waitForClickUpMockAPI();
addWebhookRoute();

context().start();

{
final List<String> creationRecordedMessages = creationMockProcessor.awaitRecordedMessages(1, 5000);
assertEquals(1, creationRecordedMessages.size());
String webhookCreationMessage = creationRecordedMessages.get(0);
List<String> creationRecordedMessages = creationMockProcessor.awaitRecordedMessages(1, 5000);
assertThat(creationRecordedMessages).hasSize(1);

try {
WebhookCreationCommand command = MAPPER.readValue(webhookCreationMessage, WebhookCreationCommand.class);
WebhookCreationCommand command = MAPPER.readValue(creationRecordedMessages.get(0), WebhookCreationCommand.class);
assertThat(command).isNotNull();
creationMockProcessor.clearRecordedMessages();

assertInstanceOf(WebhookCreationCommand.class, command);
} catch (IOException e) {
fail(e);
}
List<String> readRecordedMessages = readMockProcessor.awaitRecordedMessages(1, 5000);
assertThat(readRecordedMessages).hasSize(1);
assertThat(readRecordedMessages.get(0)).isEmpty();
readMockProcessor.clearRecordedMessages();

creationMockProcessor.clearRecordedMessages();
}

{
final List<String> readRecordedMessages = readMockProcessor.awaitRecordedMessages(1, 5000);
assertEquals(1, readRecordedMessages.size());
String webhookReadMessage = readRecordedMessages.get(0);
context().stop();
}
}

assertEquals("", webhookReadMessage);
private void addWebhookRoute() throws Exception {
context().addRoutes(new RouteBuilder() {
@Override
public void configure() {
String apiMockBaseUrl = "http://localhost:" + port + "/clickup-api-mock";

readMockProcessor.clearRecordedMessages();
fromF("webhook:clickup:%s?authorizationToken=%s&webhookSecret=%s&events=%s&webhookAutoRegister=true&baseUrl=%s",
WORKSPACE_ID, AUTHORIZATION_TOKEN, WEBHOOK_SECRET, String.join(",", EVENTS), apiMockBaseUrl)
.id("webhook")
.to("mock:endpoint");
}

context().stop();
}
});
}

@Override
Expand Down
Loading