Skip to content

Commit ccd9418

Browse files
author
Ian Saunders
committed
Support for further amendments
Support for more notification handlers
1 parent 1eea9ee commit ccd9418

12 files changed

Lines changed: 187 additions & 19 deletions

File tree

src/main/java/net/billforward/BillForwardClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ public String getApiUrl() {
108108
RuntimeTypeAdapterFactory<Notification> notificationConfigAdapter = RuntimeTypeAdapterFactory.of(Notification.class, "domain");
109109
mappings = Notification.getTypeMappings();
110110
for(GatewayTypeMapping mapping : mappings) {
111-
notificationConfigAdapter.registerSubtype((Class)mapping.getApiType(), mapping.getName());
111+
if(mapping.getName() == null) {
112+
notificationConfigAdapter.registerSubtype((Class)mapping.getApiType(), "default_value_mapping");
113+
} else {
114+
notificationConfigAdapter.registerSubtype((Class)mapping.getApiType(), mapping.getName());
115+
}
112116
}
113117

114118
//2014-09-12T03:00:17Z

src/main/java/net/billforward/gson/typeadapters/RuntimeTypeAdapterFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
212212
String label = labelJsonElement.getAsString();
213213
@SuppressWarnings("unchecked") // registration requires that subtype extends T
214214
TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
215+
if(delegate == null) {
216+
delegate = (TypeAdapter<R>)labelToDelegate.get("default_value_mapping");
217+
}
215218
if (delegate == null) {
216219
throw new JsonParseException("cannot deserialize " + baseType + " subtype named "
217220
+ label + "; did you forget to register a subtype?");

src/main/java/net/billforward/model/gateways/GatewayTypeMapping.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
public class GatewayTypeMapping {
44
public Class<?> type;
55
public String name;
6-
6+
77
public GatewayTypeMapping(Class<?> type, String name) {
88
super();
99
this.type = type;
1010
this.name = name;
1111
}
12+
13+
public GatewayTypeMapping(Class<?> type) {
14+
this(type, null);
15+
}
1216

1317
public Class<?> getApiType() {
1418
return type;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.billforward.model.notifications;
2+
3+
import net.billforward.BillForwardClient;
4+
import net.billforward.model.Account;
5+
6+
import com.google.gson.annotations.Expose;
7+
8+
public class AccountNotification extends Notification {
9+
@Expose protected Account account;
10+
11+
public AccountNotification() {
12+
super();
13+
}
14+
15+
public Account getAccount() {
16+
return account;
17+
}
18+
19+
@Override
20+
protected void buildEntity() {
21+
account = BillForwardClient.GSON.fromJson(this.entity, Account.class);
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.billforward.model.notifications;
2+
3+
import net.billforward.BillForwardClient;
4+
import net.billforward.amendments.Amendment;
5+
6+
import com.google.gson.annotations.Expose;
7+
8+
public class AmendmentNotification extends Notification {
9+
@Expose protected Amendment amendment;
10+
11+
public AmendmentNotification() {
12+
super();
13+
}
14+
15+
public Amendment getInvoice() {
16+
return amendment;
17+
}
18+
19+
@Override
20+
protected void buildEntity() {
21+
amendment = BillForwardClient.GSON.fromJson(this.entity, Amendment.class);
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.billforward.model.notifications;
2+
3+
import net.billforward.BillForwardClient;
4+
import net.billforward.model.InvoiceLine;
5+
6+
import com.google.gson.annotations.Expose;
7+
8+
public class InvoiceLineNotification extends Notification {
9+
@Expose protected InvoiceLine invoiceLine;
10+
11+
public InvoiceLineNotification() {
12+
super();
13+
}
14+
15+
public InvoiceLine getInvoiceLine() {
16+
return invoiceLine;
17+
}
18+
19+
@Override
20+
protected void buildEntity() {
21+
invoiceLine = BillForwardClient.GSON.fromJson(this.entity, InvoiceLine.class);
22+
}
23+
}

src/main/java/net/billforward/model/notifications/Notification.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import com.google.gson.annotations.Expose;
1212

13-
public abstract class Notification extends BillingEntity {
13+
public class Notification extends BillingEntity {
1414

1515
@Expose protected String id;
1616
@Expose protected String type;
@@ -29,7 +29,9 @@ public Notification(BillForwardClient client_) {
2929
super(client_);
3030
}
3131

32-
protected abstract void buildEntity();
32+
protected void buildEntity() {
33+
34+
}
3335

3436
@Override
3537
protected ResourcePath getResourcePath() {
@@ -38,8 +40,18 @@ protected ResourcePath getResourcePath() {
3840

3941
public static GatewayTypeMapping[] getTypeMappings() {
4042
List<GatewayTypeMapping> typeMappings = new ArrayList<GatewayTypeMapping>();
43+
44+
/* Fully Support Notification types */
4145
typeMappings.add(new GatewayTypeMapping(SubscripitonNotification.class, NotificationDomain.Subscription.toString()));
4246
typeMappings.add(new GatewayTypeMapping(InvoiceNotification.class, NotificationDomain.Invoice.toString()));
47+
typeMappings.add(new GatewayTypeMapping(AmendmentNotification.class, NotificationDomain.Amendment.toString()));
48+
typeMappings.add(new GatewayTypeMapping(InvoiceLineNotification.class, NotificationDomain.InvoiceLine.toString()));
49+
typeMappings.add(new GatewayTypeMapping(AccountNotification.class, NotificationDomain.Account.toString()));
50+
typeMappings.add(new GatewayTypeMapping(WebhookNotification.class, NotificationDomain.Webhook.toString()));
51+
52+
53+
/* Catch all for other things */
54+
typeMappings.add(new GatewayTypeMapping(Notification.class));
4355

4456
return typeMappings.toArray(new GatewayTypeMapping[]{});
4557
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.billforward.model.notifications;
2+
3+
import net.billforward.BillForwardClient;
4+
import net.billforward.model.InvoiceLine;
5+
import net.billforward.model.PaymentMethodSubscriptionLink;
6+
7+
import com.google.gson.annotations.Expose;
8+
9+
public class PaymentMethodSubscriptionLinkNotification extends Notification {
10+
@Expose protected PaymentMethodSubscriptionLink paymentMethodSubscriptionLink;
11+
12+
public PaymentMethodSubscriptionLinkNotification() {
13+
super();
14+
}
15+
16+
public PaymentMethodSubscriptionLink getPaymentMethodSubscriptionLink() {
17+
return paymentMethodSubscriptionLink;
18+
}
19+
20+
@Override
21+
protected void buildEntity() {
22+
paymentMethodSubscriptionLink = BillForwardClient.GSON.fromJson(this.entity, PaymentMethodSubscriptionLink.class);
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.billforward.model.notifications;
2+
3+
import net.billforward.BillForwardClient;
4+
import net.billforward.model.Webhook;
5+
6+
import com.google.gson.annotations.Expose;
7+
8+
public class WebhookNotification extends Notification {
9+
@Expose protected Webhook webHook;
10+
11+
public WebhookNotification() {
12+
super();
13+
}
14+
15+
public Webhook getWebhook() {
16+
return webHook;
17+
}
18+
19+
@Override
20+
protected void buildEntity() {
21+
webHook = BillForwardClient.GSON.fromJson(this.entity, Webhook.class);
22+
}
23+
}

src/test/java/net/billforward/model/NotificationTests.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import java.io.FileNotFoundException;
44

55
import net.billforward.exception.BillforwardException;
6+
import net.billforward.model.Invoice.InvoiceState;
67
import net.billforward.model.notifications.InvoiceNotification;
78
import net.billforward.model.notifications.Notification;
89
import net.billforward.model.notifications.NotificationHelper;
910

1011
import org.junit.Test;
1112

1213
public class NotificationTests extends TestBase {
13-
//@Test
14-
public void parseProvisionedSubscription() throws BillforwardException, FileNotFoundException {;
15-
14+
@Test
15+
public void parseProvisionedSubscription() throws BillforwardException, FileNotFoundException {
1616
String content = getResourceData("notificationExamples/subscription/Provisioned.json");
1717
Notification notificaiton = NotificationHelper.parse(content);
1818

@@ -23,9 +23,21 @@ public class NotificationTests extends TestBase {
2323
public void parsePendingInvoice() throws BillforwardException, FileNotFoundException {;
2424

2525
String content = getResourceData("notificationExamples/invoice/Pending.json");
26+
27+
Notification notification = NotificationHelper.parse(content);
28+
InvoiceNotification invoiceNotification = (InvoiceNotification)notification;
29+
if(invoiceNotification.getInvoice().getState() == InvoiceState.Pending) {
30+
invoiceNotification.getInvoice().issue();
31+
}
32+
33+
System.out.println(notification);
34+
}
35+
36+
@Test
37+
public void parseAccountUpdated() throws BillforwardException, FileNotFoundException {;
38+
39+
String content = getResourceData("notificationExamples/account/Updated.json");
2640
Notification notificaiton = NotificationHelper.parse(content);
27-
InvoiceNotification invoiceNotification = (InvoiceNotification)notificaiton;
28-
invoiceNotification.getInvoice().issue();
2941

3042
System.out.println(notificaiton);
3143
}

0 commit comments

Comments
 (0)