killbill-aplcache

Merge branch 'integration' of github.com:ning/killbill

6/11/2012 3:46:27 PM

Details

account/pom.xml 5(+5 -0)

diff --git a/account/pom.xml b/account/pom.xml
index c968f25..593fb57 100644
--- a/account/pom.xml
+++ b/account/pom.xml
@@ -79,6 +79,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
             <scope>test</scope>
diff --git a/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java b/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
index be5bdae..c6cea33 100644
--- a/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
+++ b/account/src/main/java/com/ning/billing/account/api/user/DefaultAccountUserApi.java
@@ -16,13 +16,10 @@
 
 package com.ning.billing.account.api.user;
 
+import javax.annotation.Nullable;
 import java.util.List;
 import java.util.UUID;
 
-import com.ning.billing.account.dao.AccountEmailDao;
-import com.ning.billing.util.customfield.dao.CustomFieldDao;
-import com.ning.billing.util.dao.ObjectType;
-import com.ning.billing.util.tag.dao.TagDao;
 import org.joda.time.DateTime;
 
 import com.google.inject.Inject;
@@ -35,11 +32,15 @@ import com.ning.billing.account.api.AccountUserApi;
 import com.ning.billing.account.api.DefaultAccount;
 import com.ning.billing.account.api.MigrationAccountData;
 import com.ning.billing.account.dao.AccountDao;
+import com.ning.billing.account.dao.AccountEmailDao;
 import com.ning.billing.util.callcontext.CallContext;
 import com.ning.billing.util.callcontext.CallContextFactory;
 import com.ning.billing.util.customfield.CustomField;
+import com.ning.billing.util.customfield.dao.CustomFieldDao;
+import com.ning.billing.util.dao.ObjectType;
 import com.ning.billing.util.entity.EntityPersistenceException;
 import com.ning.billing.util.tag.TagDefinition;
+import com.ning.billing.util.tag.dao.TagDao;
 
 public class DefaultAccountUserApi implements AccountUserApi {
     private final CallContextFactory factory;
@@ -60,9 +61,9 @@ public class DefaultAccountUserApi implements AccountUserApi {
     }
 
     @Override
-    public Account createAccount(final AccountData data, final List<CustomField> fields,
-                                 final List<TagDefinition> tagDefinitions, final CallContext context) throws AccountApiException {
-        Account account = new DefaultAccount(data);
+    public Account createAccount(final AccountData data, @Nullable final List<CustomField> fields,
+                                 @Nullable final List<TagDefinition> tagDefinitions, final CallContext context) throws AccountApiException {
+        final Account account = new DefaultAccount(data);
 
         try {
             // TODO: move this into a transaction?
@@ -83,8 +84,8 @@ public class DefaultAccountUserApi implements AccountUserApi {
 
     @Override
     public Account getAccountByKey(final String key) throws AccountApiException {
-        Account account = accountDao.getAccountByKey(key);
-        if(account == null) {
+        final Account account = accountDao.getAccountByKey(key);
+        if (account == null) {
             throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, key);
         }
         return account;
@@ -92,8 +93,8 @@ public class DefaultAccountUserApi implements AccountUserApi {
 
     @Override
     public Account getAccountById(final UUID id) throws AccountApiException {
-        Account account = accountDao.getById(id);
-        if(account == null) {
+        final Account account = accountDao.getById(id);
+        if (account == null) {
             throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_ID, id);
         }
         return account;
@@ -117,11 +118,11 @@ public class DefaultAccountUserApi implements AccountUserApi {
             throw new AccountApiException(e, ErrorCode.ACCOUNT_UPDATE_FAILED);
         }
     }
-    
+
     @Override
     public void updateAccount(final UUID accountId, final AccountData accountData, final CallContext context)
             throws AccountApiException {
-        Account account = new DefaultAccount(accountId, accountData);
+        final Account account = new DefaultAccount(accountId, accountData);
 
         try {
             accountDao.update(account, context);
@@ -132,21 +133,21 @@ public class DefaultAccountUserApi implements AccountUserApi {
 
     @Override
     public void updateAccount(final String externalKey, final AccountData accountData, final CallContext context) throws AccountApiException {
-    	UUID accountId = getIdFromKey(externalKey);
-    	if (accountId == null) {
-    		throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, externalKey);
-    	}
-    	updateAccount(accountId, accountData, context);
-     }
-
-	@Override
-	public Account migrateAccount(final MigrationAccountData data, final List<CustomField> fields,
+        final UUID accountId = getIdFromKey(externalKey);
+        if (accountId == null) {
+            throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, externalKey);
+        }
+        updateAccount(accountId, accountData, context);
+    }
+
+    @Override
+    public Account migrateAccount(final MigrationAccountData data, final List<CustomField> fields,
                                   final List<TagDefinition> tagDefinitions, final CallContext context)
             throws AccountApiException {
-        DateTime createdDate = data.getCreatedDate() == null ? context.getCreatedDate() : data.getCreatedDate();
-        DateTime updatedDate = data.getUpdatedDate() == null ? context.getUpdatedDate() : data.getUpdatedDate();
-        CallContext migrationContext = factory.toMigrationCallContext(context, createdDate, updatedDate);
-		Account account = new DefaultAccount(data);
+        final DateTime createdDate = data.getCreatedDate() == null ? context.getCreatedDate() : data.getCreatedDate();
+        final DateTime updatedDate = data.getUpdatedDate() == null ? context.getUpdatedDate() : data.getUpdatedDate();
+        final CallContext migrationContext = factory.toMigrationCallContext(context, createdDate, updatedDate);
+        final Account account = new DefaultAccount(data);
 
         try {
             // TODO: move this into a transaction?
@@ -158,7 +159,7 @@ public class DefaultAccountUserApi implements AccountUserApi {
         }
 
         return account;
-	}
+    }
 
     @Override
     public List<AccountEmail> getEmails(final UUID accountId) {
diff --git a/account/src/test/java/com/ning/billing/account/api/user/TestDefaultAccountUserApi.java b/account/src/test/java/com/ning/billing/account/api/user/TestDefaultAccountUserApi.java
new file mode 100644
index 0000000..73fffbd
--- /dev/null
+++ b/account/src/test/java/com/ning/billing/account/api/user/TestDefaultAccountUserApi.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2010-2012 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.account.api.user;
+
+import java.util.Map;
+import java.util.UUID;
+
+import org.joda.time.DateTimeZone;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.ning.billing.account.api.Account;
+import com.ning.billing.account.api.AccountData;
+import com.ning.billing.account.api.DefaultAccount;
+import com.ning.billing.account.dao.AccountDao;
+import com.ning.billing.account.dao.AccountEmailDao;
+import com.ning.billing.account.dao.MockAccountDao;
+import com.ning.billing.catalog.api.Currency;
+import com.ning.billing.util.bus.Bus;
+import com.ning.billing.util.callcontext.CallContext;
+import com.ning.billing.util.callcontext.CallContextFactory;
+import com.ning.billing.util.customfield.dao.CustomFieldDao;
+import com.ning.billing.util.customfield.dao.MockCustomFieldDao;
+import com.ning.billing.util.dao.ObjectType;
+import com.ning.billing.util.tag.DefaultTagDefinition;
+import com.ning.billing.util.tag.Tag;
+import com.ning.billing.util.tag.TagDefinition;
+import com.ning.billing.util.tag.dao.MockTagDao;
+import com.ning.billing.util.tag.dao.TagDao;
+
+public class TestDefaultAccountUserApi {
+    private final CallContextFactory factory = Mockito.mock(CallContextFactory.class);
+    private final CallContext callContext = Mockito.mock(CallContext.class);
+    private final AccountEmailDao accountEmailDao = Mockito.mock(AccountEmailDao.class);
+
+    private AccountDao accountDao;
+    private TagDao tagDao;
+    private DefaultAccountUserApi accountUserApi;
+
+    @BeforeMethod(groups = "fast")
+    public void setUp() throws Exception {
+        accountDao = new MockAccountDao(Mockito.mock(Bus.class));
+        tagDao = new MockTagDao();
+        final CustomFieldDao customFieldDao = new MockCustomFieldDao();
+        accountUserApi = new DefaultAccountUserApi(factory, accountDao, accountEmailDao, tagDao, customFieldDao);
+    }
+
+    @Test(groups = "fast")
+    public void testCreateWithTag() throws Exception {
+        final UUID id = UUID.randomUUID();
+        final String externalKey = UUID.randomUUID().toString();
+        final String email = UUID.randomUUID().toString();
+        final String name = UUID.randomUUID().toString();
+        final int firstNameLength = Integer.MAX_VALUE;
+        final Currency currency = Currency.BRL;
+        final int billCycleDay = Integer.MIN_VALUE;
+        final UUID paymentMethodId = UUID.randomUUID();
+        final DateTimeZone timeZone = DateTimeZone.UTC;
+        final String locale = UUID.randomUUID().toString();
+        final String address1 = UUID.randomUUID().toString();
+        final String address2 = UUID.randomUUID().toString();
+        final String companyName = UUID.randomUUID().toString();
+        final String city = UUID.randomUUID().toString();
+        final String stateOrProvince = UUID.randomUUID().toString();
+        final String country = UUID.randomUUID().toString();
+        final String postalCode = UUID.randomUUID().toString();
+        final String phone = UUID.randomUUID().toString();
+        final boolean isMigrated = true;
+        final boolean isNotifiedForInvoices = false;
+        final AccountData data = new DefaultAccount(id, externalKey, email, name, firstNameLength, currency, billCycleDay,
+                                                    paymentMethodId, timeZone, locale, address1, address2, companyName,
+                                                    city, stateOrProvince, country, postalCode, phone, isMigrated, isNotifiedForInvoices);
+
+        final String tagName = UUID.randomUUID().toString();
+        final String tagDescription = UUID.randomUUID().toString();
+        final TagDefinition tagDefinition = new DefaultTagDefinition(tagName, tagDescription, true);
+        accountUserApi.createAccount(data, null, ImmutableList.<TagDefinition>of(tagDefinition), callContext);
+
+        final Account account = accountDao.getAccountByKey(externalKey);
+        Assert.assertEquals(account.getExternalKey(), externalKey);
+        Assert.assertEquals(account.getEmail(), email);
+        Assert.assertEquals(account.getName(), name);
+        Assert.assertEquals(account.getFirstNameLength(), firstNameLength);
+        Assert.assertEquals(account.getCurrency(), currency);
+        Assert.assertEquals(account.getBillCycleDay(), billCycleDay);
+        Assert.assertEquals(account.getPaymentMethodId(), paymentMethodId);
+        Assert.assertEquals(account.getTimeZone(), timeZone);
+        Assert.assertEquals(account.getLocale(), locale);
+        Assert.assertEquals(account.getAddress1(), address1);
+        Assert.assertEquals(account.getAddress2(), address2);
+        Assert.assertEquals(account.getCompanyName(), companyName);
+        Assert.assertEquals(account.getCity(), city);
+        Assert.assertEquals(account.getStateOrProvince(), stateOrProvince);
+        Assert.assertEquals(account.getCountry(), country);
+        Assert.assertEquals(account.getPostalCode(), postalCode);
+        Assert.assertEquals(account.getPhone(), phone);
+        Assert.assertEquals(account.isMigrated(), isMigrated);
+        Assert.assertEquals(account.isNotifiedForInvoices(), isNotifiedForInvoices);
+
+        final Map<String, Tag> tags = tagDao.loadEntities(account.getId(), ObjectType.ACCOUNT);
+        Assert.assertEquals(tags.keySet().size(), 1);
+        Assert.assertEquals(tags.get(tagName).getTagDefinitionName(), tagName);
+    }
+}
diff --git a/account/src/test/java/com/ning/billing/account/dao/MockAccountDao.java b/account/src/test/java/com/ning/billing/account/dao/MockAccountDao.java
index 907447d..d8348af 100644
--- a/account/src/test/java/com/ning/billing/account/dao/MockAccountDao.java
+++ b/account/src/test/java/com/ning/billing/account/dao/MockAccountDao.java
@@ -67,9 +67,9 @@ public class MockAccountDao implements AccountDao {
     }
 
     @Override
-    public Account getAccountByKey(String key) {
+    public Account getAccountByKey(String externalKey) {
         for (Account account : accounts.values()) {
-            if (key.equals(account.getExternalKey())) {
+            if (externalKey.equals(account.getExternalKey())) {
                 return account;
             }
         }