Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CRUD operation for wehooks #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ext {
httpClientVersion = '4.5.7'
okhttpVersion = '3.12.1'
springWebVersion = '3.2.2.RELEASE'
jacksonVersion = '2.9.8'
jacksonVersion = '2.12.3'
slf4jVersion = '1.7.25'
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/julienvey/trello/Trello.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.julienvey.trello.domain.MyPrefs;
import com.julienvey.trello.domain.Organization;
import com.julienvey.trello.domain.TList;
import com.julienvey.trello.domain.Webhook;

public interface Trello {

Expand Down Expand Up @@ -263,4 +264,16 @@ List<CardWithActions> getBoardMemberActivity(String boardId, String memberId,
List<Card> getMemberCards(String userId, Argument... args);

List<Action> getMemberActions(String userId, Argument... args);

Webhook createWebhook(Webhook webhook);

Webhook getWebhook(String webhookId);

Webhook updateWebhook(Webhook webhook);

Webhook deleteWebhook(String webhookId);

Member me();

List<Webhook> getWebhooks();
}
125 changes: 125 additions & 0 deletions src/main/java/com/julienvey/trello/domain/Webhook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.julienvey.trello.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.julienvey.trello.Trello;

import java.util.Objects;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Webhook extends TrelloEntity {
private String id;
private String description;
private String idModel;
private String callbackURL;
private boolean active;
private int consecutiveFailures;
private String firstConsecutiveFailDate;

public String getId() {
return id;
}

public Webhook setId(String id) {
this.id = id;
return this;
}

public String getDescription() {
return description;
}

public Webhook setDescription(String description) {
this.description = description;
return this;
}

public String getIdModel() {
return idModel;
}

public Webhook setIdModel(String idModel) {
this.idModel = idModel;
return this;
}

public String getCallbackURL() {
return callbackURL;
}

public Webhook setCallbackURL(String callbackUrl) {
this.callbackURL = callbackUrl;
return this;
}

public boolean isActive() {
return active;
}

public Webhook setActive(boolean active) {
this.active = active;
return this;
}

public int getConsecutiveFailures() {
return consecutiveFailures;
}

public Webhook setConsecutiveFailures(int consecutiveFailures) {
this.consecutiveFailures = consecutiveFailures;
return this;
}

public String getFirstConsecutiveFailDate() {
return firstConsecutiveFailDate;
}

public Webhook setFirstConsecutiveFailDate(String firstConsecutiveFailDate) {
this.firstConsecutiveFailDate = firstConsecutiveFailDate;
return this;
}

public Webhook create() {
Webhook webhook = getTrelloService().createWebhook(this);
id = webhook.id;

return this;
}

@Override
@SuppressWarnings("unchecked")
public Webhook setInternalTrello(Trello trelloService) {
return super.setInternalTrello(trelloService);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Webhook webhook = (Webhook) o;
return active == webhook.active &&
consecutiveFailures == webhook.consecutiveFailures &&
id.equals(webhook.id) &&
Objects.equals(description, webhook.description) &&
idModel.equals(webhook.idModel) &&
callbackURL.equals(webhook.callbackURL) &&
Objects.equals(firstConsecutiveFailDate, webhook.firstConsecutiveFailDate);
}

@Override
public int hashCode() {
return Objects.hash(id, description, idModel, callbackURL, active, consecutiveFailures, firstConsecutiveFailDate);
}

@Override
public String toString() {
return "Webhook{" +
"id='" + id + '\'' +
", description='" + description + '\'' +
", idModel='" + idModel + '\'' +
", callbackUrl='" + callbackURL + '\'' +
", active=" + active +
", consecutiveFailures=" + consecutiveFailures +
", firstConsecutiveFailDate='" + firstConsecutiveFailDate + '\'' +
'}';
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/julienvey/trello/impl/TrelloImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import static com.julienvey.trello.impl.TrelloUrl.CREATE_CARD;
import static com.julienvey.trello.impl.TrelloUrl.CREATE_CHECKLIST;
import static com.julienvey.trello.impl.TrelloUrl.CREATE_LABEL;
import static com.julienvey.trello.impl.TrelloUrl.CREATE_WEBHOOK;
import static com.julienvey.trello.impl.TrelloUrl.DELETE_ATTACHMENT;
import static com.julienvey.trello.impl.TrelloUrl.DELETE_CARD;
import static com.julienvey.trello.impl.TrelloUrl.DELETE_LABEL;
import static com.julienvey.trello.impl.TrelloUrl.DELETE_WEBHOOK;
import static com.julienvey.trello.impl.TrelloUrl.GET_ACTION;
import static com.julienvey.trello.impl.TrelloUrl.GET_ACTION_BOARD;
import static com.julienvey.trello.impl.TrelloUrl.GET_ACTION_CARD;
Expand Down Expand Up @@ -52,11 +54,15 @@
import static com.julienvey.trello.impl.TrelloUrl.GET_ORGANIZATION;
import static com.julienvey.trello.impl.TrelloUrl.GET_ORGANIZATION_BOARD;
import static com.julienvey.trello.impl.TrelloUrl.GET_ORGANIZATION_MEMBER;
import static com.julienvey.trello.impl.TrelloUrl.GET_WEBHOOK;
import static com.julienvey.trello.impl.TrelloUrl.REMOVE_MEMBER_FROM_BOARD;
import static com.julienvey.trello.impl.TrelloUrl.REMOVE_MEMBER_FROM_CARD;
import static com.julienvey.trello.impl.TrelloUrl.UPDATE_CARD;
import static com.julienvey.trello.impl.TrelloUrl.UPDATE_CARD_COMMENT;
import static com.julienvey.trello.impl.TrelloUrl.UPDATE_LABEL;
import static com.julienvey.trello.impl.TrelloUrl.UPDATE_WEBHOOK;
import static com.julienvey.trello.impl.TrelloUrl.ME;
import static com.julienvey.trello.impl.TrelloUrl.TOKEN_WEBHOOKS;
import static com.julienvey.trello.impl.TrelloUrl.createUrl;
import static com.julienvey.trello.impl.TrelloUrl.createUrlWithNoArgs;

Expand Down Expand Up @@ -97,6 +103,7 @@
import com.julienvey.trello.domain.Organization;
import com.julienvey.trello.domain.TList;
import com.julienvey.trello.domain.TrelloEntity;
import com.julienvey.trello.domain.Webhook;
import com.julienvey.trello.impl.domaininternal.Comment;
import com.julienvey.trello.impl.http.JDKTrelloHttpClient;

Expand Down Expand Up @@ -563,6 +570,38 @@ public List<Member> removeMemberFromCard(String idCard, String userId) {
return asList(() -> delete(createUrl(REMOVE_MEMBER_FROM_CARD).asString(), Member[].class, idCard, userId));
}

@Override
public Webhook createWebhook(Webhook webhook) {
Webhook createdWebhook = postForObject(createUrlWithNoArgs(CREATE_WEBHOOK), webhook, Webhook.class);
return createdWebhook.setInternalTrello(this);
}

@Override
public Webhook getWebhook(String webhookId) {
return get(createUrl(GET_WEBHOOK).asString(), Webhook.class, webhookId);
}

@Override
public Webhook updateWebhook(Webhook webhook) {
Webhook createdWebhook = put(createUrlWithNoArgs(UPDATE_WEBHOOK), webhook, Webhook.class, webhook.getId());
return createdWebhook.setInternalTrello(this);
}

@Override
public Webhook deleteWebhook(String webhookId) {
return delete(createUrl(DELETE_WEBHOOK).asString(), Webhook.class, webhookId);
}

@Override
public Member me() {
return get(createUrlWithNoArgs(ME), Member.class);
}

@Override
public List<Webhook> getWebhooks() {
return asList(() -> get(createUrl(TOKEN_WEBHOOKS).asString(), Webhook[].class, accessToken));
}

/* internal methods */

private <T> T postFileForObject(String url, File file, Class<T> objectClass, String... params) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/julienvey/trello/impl/TrelloUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public class TrelloUrl {
public static final String UPDATE_LABEL = "/labels/{labelId}?";
public static final String DELETE_LABEL = "/labels/{labelId}?";

public static final String CREATE_WEBHOOK = "/webhooks/?";
public static final String UPDATE_WEBHOOK = "/webhooks/{webhookId}?";
public static final String GET_WEBHOOK = "/webhooks/{webhookId}?";
public static final String DELETE_WEBHOOK = "/webhooks/{webhookId}?";

public static final String GET_CHECK_LIST = "/checklists/{checkListId}?";
public static final String CREATE_CHECKLIST = "/checklists?";
public static final String ADD_CHECKITEMS_TO_CHECKLIST = "/checklists/{checkListId}/checkitems?";
Expand All @@ -72,6 +77,9 @@ public class TrelloUrl {
public static final String DELETE_ATTACHMENT = "/cards/{cardId}/attachments/{attachmentId}?";
public static final String UPDATE_CARD = "/cards/{cardId}?";

public static final String ME = "/members/me?";
public static final String TOKEN_WEBHOOKS = "/tokens/{userToken}/webhooks?";

private String baseUrl;
private Argument[] args = {};

Expand Down Expand Up @@ -104,4 +112,4 @@ public String asString() {
}
return builder.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.julienvey.trello.integration;

import com.julienvey.trello.NotFoundException;
import com.julienvey.trello.Trello;
import com.julienvey.trello.TrelloHttpClient;
import com.julienvey.trello.domain.*;
import com.julienvey.trello.impl.TrelloImpl;
import com.julienvey.trello.impl.http.ApacheHttpClient;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.List;

import static org.fest.assertions.Assertions.assertThat;

@RunWith(Parameterized.class)
public class WebhookCRUDCase {

private static final String TEST_APPLICATION_KEY = "db555c528ce160c33305d2ea51ae1197";

private Trello trello;

private TrelloHttpClient httpClient;

@Parameterized.Parameters
public static List<TrelloHttpClient> data() {
return TrelloHttpClients.all();
}

public WebhookCRUDCase(TrelloHttpClient httpClient) {
this.httpClient = httpClient;
}

@Before
public void setUp() {
trello = new TrelloImpl(TEST_APPLICATION_KEY, "", httpClient);
}

@Test
public void testCreateGetAndDeleteWebhook() {
final String callbackUrl = "<provided callbackURL must be a valid URL during the creation of the webhook and return 200 on HEAD request during hook creation time>";
final String boardId = "518baad5b05dbf4703004852";
final Webhook newWebhook = new Webhook()
.setCallbackURL(callbackUrl)
.setIdModel(boardId);

final Webhook createdWebhook = trello.createWebhook(newWebhook);
System.out.println(createdWebhook.getId());

assertThat(createdWebhook).isNotNull();
assertThat(createdWebhook.getId()).isNotNull();
assertThat(createdWebhook.getId()).isNotEmpty();
assertThat(createdWebhook.getIdModel()).isEqualTo(boardId);
assertThat(createdWebhook.getCallbackURL()).isEqualTo(callbackUrl);
assertThat(createdWebhook.getDescription()).isEmpty();

String updatedDescription = "Updated description";
createdWebhook.setDescription(updatedDescription);
Webhook updatedWebhook = trello.updateWebhook(createdWebhook);

final Webhook foundWebhook = trello.getWebhook(createdWebhook.getId());
assertThat(foundWebhook).isNotNull();
assertThat(foundWebhook.getId()).isNotNull();
assertThat(foundWebhook.getId()).isEqualTo(updatedWebhook.getId());
assertThat(foundWebhook.getIdModel()).isEqualTo(updatedWebhook.getIdModel());
assertThat(foundWebhook.getCallbackURL()).isEqualTo(updatedWebhook.getCallbackURL());
assertThat(foundWebhook.getDescription()).isEqualTo(updatedDescription);

trello.deleteWebhook(foundWebhook.getId());

boolean isCatchVisited = false;
try {
trello.getWebhook(foundWebhook.getId());
} catch (NotFoundException notFoundException) {
isCatchVisited = true;
assertThat(notFoundException.getMessage().contains("Resource not found: "));
}

assertThat(isCatchVisited).isTrue();
}

}
Loading