/
TestAnalyticsNotificationQueue.java
128 lines (105 loc) · 6.68 KB
/
TestAnalyticsNotificationQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2010-2014 Ning, Inc.
* Copyright 2014-2019 Groupon, Inc
* Copyright 2014-2019 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.killbill.billing.plugin.analytics;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.awaitility.Awaitility;
import org.killbill.billing.notification.plugin.api.ExtBusEvent;
import org.killbill.billing.notification.plugin.api.ExtBusEventType;
import org.killbill.notificationq.api.NotificationEventWithMetadata;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
public class TestAnalyticsNotificationQueue extends AnalyticsTestSuiteWithEmbeddedDB {
@Test(groups = "slow")
public void testSendOneEvent() throws Exception {
final AnalyticsListener analyticsListener = new AnalyticsListener(killbillAPI,
killbillDataSource,
osgiConfigPropertiesService,
BusinessExecutor.newCachedThreadPool(osgiConfigPropertiesService),
locker,
clock,
analyticsConfigurationHandler,
notificationQueueService);
analyticsListener.start();
// Verify the original state
Assert.assertEquals(analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
final ExtBusEvent event = createExtBusEvent();
analyticsListener.handleKillbillEvent(event);
// Shouldn't be anything right after it
Assert.assertEquals(analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
// Wait for the notification to kick in
Awaitility.await().atMost(15, TimeUnit.SECONDS).until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size() == 1;
}
});
analyticsListener.shutdownNow();
}
@Test(groups = "slow")
public void testVerifyNoDups() throws Exception {
final AnalyticsListener analyticsListener = new AnalyticsListener(killbillAPI,
killbillDataSource,
osgiConfigPropertiesService,
BusinessExecutor.newCachedThreadPool(osgiConfigPropertiesService),
locker,
clock,
analyticsConfigurationHandler,
notificationQueueService);
// Don't start the dequeuer
Assert.assertEquals(Iterables.<NotificationEventWithMetadata>size(analyticsListener.getJobQueue().getFutureNotificationForSearchKeys(accountRecordId, tenantRecordId)), 0);
// Verify the original state
Assert.assertEquals(analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
// Send the first event
final ExtBusEvent firstEvent = createExtBusEvent();
Mockito.when(firstEvent.getEventType()).thenReturn(ExtBusEventType.PAYMENT_FAILED);
analyticsListener.handleKillbillEvent(firstEvent);
// Verify the size of the queue
Assert.assertEquals(Iterables.<NotificationEventWithMetadata>size(analyticsListener.getJobQueue().getFutureNotificationForSearchKeys(accountRecordId, tenantRecordId)), 1);
// Send the same event
analyticsListener.handleKillbillEvent(firstEvent);
// Verify the size of the queue
Assert.assertEquals(Iterables.<NotificationEventWithMetadata>size(analyticsListener.getJobQueue().getFutureNotificationForSearchKeys(accountRecordId, tenantRecordId)), 1);
// Now, send a different event type
final ExtBusEvent secondEvent = createExtBusEvent();
Mockito.when(secondEvent.getEventType()).thenReturn(ExtBusEventType.OVERDUE_CHANGE);
analyticsListener.handleKillbillEvent(secondEvent);
// Verify the size of the queue
Assert.assertEquals(Iterables.<NotificationEventWithMetadata>size(analyticsListener.getJobQueue().getFutureNotificationForSearchKeys(accountRecordId, tenantRecordId)), 2);
// Now, send a different event type, but triggering the same refresh type as the first event
final ExtBusEvent thirdEvent = createExtBusEvent();
Mockito.when(thirdEvent.getEventType()).thenReturn(ExtBusEventType.PAYMENT_SUCCESS);
analyticsListener.handleKillbillEvent(thirdEvent);
// Verify the size of the queue
Assert.assertEquals(Iterables.<NotificationEventWithMetadata>size(analyticsListener.getJobQueue().getFutureNotificationForSearchKeys(accountRecordId, tenantRecordId)), 2);
// Verify the final state
Assert.assertEquals(analyticsSqlDao.getAccountFieldsByAccountRecordId(accountRecordId, tenantRecordId, callContext).size(), 0);
}
private ExtBusEvent createExtBusEvent() {
final ExtBusEvent event = Mockito.mock(ExtBusEvent.class);
final UUID accountId = account.getId();
Mockito.when(event.getAccountId()).thenReturn(accountId);
final UUID tenantId = callContext.getTenantId();
Mockito.when(event.getTenantId()).thenReturn(tenantId);
Mockito.when(event.getEventType()).thenReturn(ExtBusEventType.CUSTOM_FIELD_CREATION);
return event;
}
}