killbill-memoizeit
test: add TestAnalyticsService to test integration with EventBus Signed-off-by: …
11/17/2011 2:35:11 AM
Details
analytics/pom.xml 20(+20 -0)
diff --git a/analytics/pom.xml b/analytics/pom.xml
index 3755178..0ead039 100644
--- a/analytics/pom.xml
+++ b/analytics/pom.xml
@@ -74,6 +74,26 @@
</dependency>
<dependency>
<groupId>com.ning.billing</groupId>
+ <artifactId>killbill-account</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.ning.billing</groupId>
+ <artifactId>killbill-catalog</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.ning.billing</groupId>
+ <artifactId>killbill-entitlement</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.ning.billing</groupId>
+ <artifactId>killbill-util</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.ning.billing</groupId>
<artifactId>killbill-util</artifactId>
<type>test-jar</type>
<scope>test</scope>
diff --git a/analytics/src/main/java/com/ning/billing/analytics/AnalyticsListener.java b/analytics/src/main/java/com/ning/billing/analytics/AnalyticsListener.java
index f07627a..2372a51 100644
--- a/analytics/src/main/java/com/ning/billing/analytics/AnalyticsListener.java
+++ b/analytics/src/main/java/com/ning/billing/analytics/AnalyticsListener.java
@@ -25,18 +25,12 @@ import com.ning.billing.catalog.api.Currency;
import com.ning.billing.entitlement.api.user.IEntitlementUserApi;
import com.ning.billing.entitlement.api.user.ISubscriptionBundle;
import com.ning.billing.entitlement.api.user.ISubscriptionTransition;
-import com.ning.billing.lifecycle.LyfecycleHandlerType;
-import com.ning.billing.util.eventbus.IEventBus;
import org.joda.time.DateTime;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.util.List;
public class AnalyticsListener
{
- private static final Logger log = LoggerFactory.getLogger(AnalyticsListener.class);
-
private final BusinessSubscriptionTransitionDao dao;
private final IEntitlementUserApi entitlementApi;
private final IAccountUserApi accountApi;
@@ -136,7 +130,7 @@ public class AnalyticsListener
// of the previous plan. We need to retrieve it from our own transitions table
DateTime previousEffectiveTransitionTime = null;
final List<BusinessSubscriptionTransition> transitions = dao.getTransitions(transitionKey);
- if (transitions != null) {
+ if (transitions != null && transitions.size() > 0) {
final BusinessSubscriptionTransition lastTransition = transitions.get(transitions.size() - 1);
if (lastTransition != null && lastTransition.getNextSubscription() != null) {
previousEffectiveTransitionTime = lastTransition.getNextSubscription().getStartDate();
diff --git a/analytics/src/main/java/com/ning/billing/analytics/api/AnalyticsService.java b/analytics/src/main/java/com/ning/billing/analytics/api/AnalyticsService.java
index b9d989f..216ff9b 100644
--- a/analytics/src/main/java/com/ning/billing/analytics/api/AnalyticsService.java
+++ b/analytics/src/main/java/com/ning/billing/analytics/api/AnalyticsService.java
@@ -16,16 +16,15 @@
package com.ning.billing.analytics.api;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.google.inject.Inject;
import com.ning.billing.analytics.AnalyticsListener;
import com.ning.billing.lifecycle.LyfecycleHandlerType;
import com.ning.billing.util.eventbus.IEventBus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-public class AnalyticsService implements IAnalyticsService {
-
+public class AnalyticsService implements IAnalyticsService
+{
private static final Logger log = LoggerFactory.getLogger(AnalyticsService.class);
private static final String ANALYTICS_SERVICE = "analytics-service";
@@ -34,21 +33,25 @@ public class AnalyticsService implements IAnalyticsService {
private final IEventBus eventBus;
@Inject
- public AnalyticsService(AnalyticsListener listener, IEventBus eventBus) {
+ public AnalyticsService(final AnalyticsListener listener, final IEventBus eventBus)
+ {
this.listener = listener;
this.eventBus = eventBus;
}
@Override
- public String getName() {
+ public String getName()
+ {
return ANALYTICS_SERVICE;
}
@LyfecycleHandlerType(LyfecycleHandlerType.LyfecycleLevel.REGISTER_EVENTS)
- public void registerForNotifications() {
+ public void registerForNotifications()
+ {
try {
eventBus.register(listener);
- } catch (IEventBus.EventBusException e) {
+ }
+ catch (IEventBus.EventBusException e) {
log.error("Unable to register to the EventBus!", e);
}
}
diff --git a/analytics/src/test/java/com/ning/billing/analytics/AnalyticsTestModule.java b/analytics/src/test/java/com/ning/billing/analytics/AnalyticsTestModule.java
new file mode 100644
index 0000000..03e102d
--- /dev/null
+++ b/analytics/src/test/java/com/ning/billing/analytics/AnalyticsTestModule.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.analytics;
+
+import com.ning.billing.account.glue.AccountModule;
+import com.ning.billing.analytics.setup.AnalyticsModule;
+import com.ning.billing.catalog.glue.CatalogModule;
+import com.ning.billing.dbi.MysqlTestingHelper;
+import com.ning.billing.entitlement.glue.EntitlementModule;
+import com.ning.billing.util.glue.EventBusModule;
+import org.skife.jdbi.v2.DBI;
+import org.skife.jdbi.v2.IDBI;
+
+public class AnalyticsTestModule extends AnalyticsModule
+{
+ @Override
+ protected void configure()
+ {
+ super.configure();
+
+ // Need to configure a few more things for the EventBus
+ install(new AccountModule());
+ install(new CatalogModule());
+ install(new EventBusModule());
+ install(new EntitlementModule());
+
+ // Install the Dao layer
+ final MysqlTestingHelper helper = new MysqlTestingHelper();
+ bind(MysqlTestingHelper.class).toInstance(helper);
+ final DBI dbi = helper.getDBI();
+ bind(IDBI.class).toInstance(dbi);
+ bind(DBI.class).toInstance(dbi);
+ }
+}
diff --git a/analytics/src/test/java/com/ning/billing/analytics/api/TestAnalyticsService.java b/analytics/src/test/java/com/ning/billing/analytics/api/TestAnalyticsService.java
new file mode 100644
index 0000000..7560c40
--- /dev/null
+++ b/analytics/src/test/java/com/ning/billing/analytics/api/TestAnalyticsService.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.analytics.api;
+
+import com.google.inject.Inject;
+import com.ning.billing.analytics.AnalyticsTestModule;
+import com.ning.billing.analytics.BusinessSubscription;
+import com.ning.billing.analytics.BusinessSubscriptionEvent;
+import com.ning.billing.analytics.BusinessSubscriptionTransition;
+import com.ning.billing.analytics.MockAccount;
+import com.ning.billing.analytics.MockDuration;
+import com.ning.billing.analytics.MockPhase;
+import com.ning.billing.analytics.MockPlan;
+import com.ning.billing.analytics.MockProduct;
+import com.ning.billing.analytics.dao.BusinessSubscriptionTransitionDao;
+import com.ning.billing.catalog.api.IPlan;
+import com.ning.billing.catalog.api.IPlanPhase;
+import com.ning.billing.catalog.api.IProduct;
+import com.ning.billing.catalog.api.PhaseType;
+import com.ning.billing.catalog.api.ProductCategory;
+import com.ning.billing.dbi.MysqlTestingHelper;
+import com.ning.billing.entitlement.api.user.EntitlementUserApiException;
+import com.ning.billing.entitlement.api.user.IEntitlementUserApi;
+import com.ning.billing.entitlement.api.user.ISubscription;
+import com.ning.billing.entitlement.api.user.ISubscriptionBundle;
+import com.ning.billing.entitlement.api.user.ISubscriptionTransition;
+import com.ning.billing.entitlement.api.user.SubscriptionTransition;
+import com.ning.billing.entitlement.events.IEvent;
+import com.ning.billing.entitlement.events.user.ApiEventType;
+import com.ning.billing.util.eventbus.IEventBus;
+import org.apache.commons.io.IOUtils;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.UUID;
+
+@Guice(modules = AnalyticsTestModule.class)
+public class TestAnalyticsService
+{
+ private static final String KEY = "1234";
+
+ @Inject
+ private IEntitlementUserApi entitlementApi;
+
+ @Inject
+ private AnalyticsService service;
+
+ @Inject
+ private IEventBus bus;
+
+ @Inject
+ private BusinessSubscriptionTransitionDao dao;
+
+ @Inject
+ private MysqlTestingHelper helper;
+
+ private ISubscriptionTransition transition;
+ private BusinessSubscriptionTransition expectedTransition;
+
+ @BeforeClass(alwaysRun = true)
+ public void startMysql() throws IOException, ClassNotFoundException, SQLException, EntitlementUserApiException
+ {
+ final String analyticsDdl = IOUtils.toString(BusinessSubscriptionTransitionDao.class.getResourceAsStream("/com/ning/billing/analytics/ddl.sql"));
+ // For bundles
+ final String accountDdl = IOUtils.toString(BusinessSubscriptionTransitionDao.class.getResourceAsStream("/com/ning/billing/account/ddl.sql"));
+ final String entitlementDdl = IOUtils.toString(BusinessSubscriptionTransitionDao.class.getResourceAsStream("/com/ning/billing/entitlement/ddl.sql"));
+
+ helper.startMysql();
+ helper.initDb(analyticsDdl);
+ helper.initDb(accountDdl);
+ helper.initDb(entitlementDdl);
+
+ // We need a bundle to retrieve the event key
+ final ISubscriptionBundle bundle = entitlementApi.createBundleForAccount(new MockAccount(KEY), KEY);
+
+ // Verify we correctly initialized the account subsystem
+ Assert.assertNotNull(bundle);
+ Assert.assertEquals(bundle.getKey(), KEY);
+
+ // Create a subscription transition
+ final IProduct product = new MockProduct("platinium", "subscription", ProductCategory.BASE);
+ final IPlan plan = new MockPlan("platinum-monthly", product);
+ final IPlanPhase phase = new MockPhase(PhaseType.EVERGREEN, plan, MockDuration.UNLIMITED(), 25.95);
+ final UUID subscriptionId = UUID.randomUUID();
+ final DateTime effectiveTransitionTime = new DateTime(DateTimeZone.UTC);
+ final DateTime requestedTransitionTime = new DateTime(DateTimeZone.UTC);
+ transition = new SubscriptionTransition(
+ subscriptionId,
+ bundle.getId(),
+ IEvent.EventType.API_USER,
+ ApiEventType.CREATE,
+ requestedTransitionTime,
+ effectiveTransitionTime,
+ null,
+ null,
+ null,
+ null,
+ ISubscription.SubscriptionState.ACTIVE,
+ plan,
+ phase,
+ "something"
+ );
+ expectedTransition = new BusinessSubscriptionTransition(
+ KEY,
+ requestedTransitionTime,
+ BusinessSubscriptionEvent.subscriptionCreated(plan),
+ null,
+ new BusinessSubscription(plan, phase, null, effectiveTransitionTime, ISubscription.SubscriptionState.ACTIVE, subscriptionId, bundle.getId())
+ );
+ }
+
+ @AfterClass(alwaysRun = true)
+ public void stopMysql()
+ {
+ helper.stopMysql();
+ }
+
+ @Test(groups = "slow")
+ public void testRegisterForNotifications() throws Exception
+ {
+ // Make sure the service has been instantiated
+ Assert.assertEquals(service.getName(), "analytics-service");
+
+ // Test the bus and make sure we can register our service
+ try {
+ bus.start();
+ service.registerForNotifications();
+ }
+ catch (Throwable t) {
+ Assert.fail("Unable to start the bus or service!");
+ }
+
+ // Send an event to the bus and make sure our Dao got it
+ bus.post(transition);
+ Thread.sleep(1000);
+ Assert.assertEquals(dao.getTransitions(KEY).size(), 1);
+ Assert.assertEquals(dao.getTransitions(KEY).get(0), expectedTransition);
+
+ // Test the shutdown sequence
+ try {
+ bus.stop();
+ }
+ catch (Throwable t) {
+ Assert.fail("Unable to stop the bus!");
+ }
+ }
+}
diff --git a/analytics/src/test/java/com/ning/billing/analytics/MockAccount.java b/analytics/src/test/java/com/ning/billing/analytics/MockAccount.java
new file mode 100644
index 0000000..c2ab5fd
--- /dev/null
+++ b/analytics/src/test/java/com/ning/billing/analytics/MockAccount.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.analytics;
+
+import com.ning.billing.account.api.IAccount;
+import com.ning.billing.catalog.api.Currency;
+
+import java.util.UUID;
+
+public class MockAccount implements IAccount
+{
+ private final String key;
+
+ public MockAccount(final String key)
+ {
+ this.key = key;
+ }
+
+ @Override
+ public String getName()
+ {
+ return "accountName";
+ }
+
+ @Override
+ public String getEmail()
+ {
+ return "accountName@yahoo.com";
+ }
+
+ @Override
+ public String getPhone()
+ {
+ return "4152876341";
+ }
+
+ @Override
+ public String getKey()
+ {
+ return key;
+ }
+
+ @Override
+ public int getBillCycleDay()
+ {
+ return 1;
+ }
+
+ @Override
+ public Currency getCurrency()
+ {
+ return Currency.USD;
+ }
+
+ @Override
+ public UUID getId()
+ {
+ return UUID.randomUUID();
+ }
+
+ @Override
+ public void load()
+ {
+ }
+
+ @Override
+ public void save()
+ {
+ }
+
+ @Override
+ public String getFieldValue(final String fieldName)
+ {
+ return null;
+ }
+
+ @Override
+ public void setFieldValue(final String fieldName, final String fieldValue)
+ {
+ }
+}
pom.xml 5(+5 -0)
diff --git a/pom.xml b/pom.xml
index 8038652..4ed2b26 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,6 +56,11 @@
</dependency>
<dependency>
<groupId>com.ning.billing</groupId>
+ <artifactId>killbill-account</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.ning.billing</groupId>
<artifactId>killbill-entitlement</artifactId>
<version>${project.version}</version>
</dependency>
diff --git a/util/src/test/java/com/ning/billing/dbi/MysqlTestingHelper.java b/util/src/test/java/com/ning/billing/dbi/MysqlTestingHelper.java
index 3c1a051..e8a77f6 100644
--- a/util/src/test/java/com/ning/billing/dbi/MysqlTestingHelper.java
+++ b/util/src/test/java/com/ning/billing/dbi/MysqlTestingHelper.java
@@ -25,6 +25,7 @@ import org.skife.jdbi.v2.IDBI;
import org.skife.jdbi.v2.tweak.HandleCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.testng.Assert;
import java.io.File;
import java.io.IOException;
@@ -47,13 +48,22 @@ public class MysqlTestingHelper
private MysqldResource mysqldResource;
private int port = 0;
- public void startMysql() throws IOException
+ public MysqlTestingHelper()
{
// New socket on any free port
- final ServerSocket socket = new ServerSocket(0);
- port = socket.getLocalPort();
- socket.close();
+ final ServerSocket socket;
+ try {
+ socket = new ServerSocket(0);
+ port = socket.getLocalPort();
+ socket.close();
+ }
+ catch (IOException e) {
+ Assert.fail();
+ }
+ }
+ public void startMysql() throws IOException
+ {
dbDir = File.createTempFile("mysql", "");
dbDir.delete();
dbDir.mkdir();
@@ -103,7 +113,7 @@ public class MysqlTestingHelper
}
}
- public IDBI getDBI()
+ public DBI getDBI()
{
final String dbiString = "jdbc:mysql://localhost:" + port + "/" + DB_NAME + "?createDatabaseIfNotExist=true";
return new DBI(dbiString, USERNAME, PASSWORD);