killbill-aplcache

util: add AuditLog The AuditLog object will be used to retrieve

7/30/2012 3:37:09 PM

Details

diff --git a/api/src/main/java/com/ning/billing/util/audit/AuditLog.java b/api/src/main/java/com/ning/billing/util/audit/AuditLog.java
new file mode 100644
index 0000000..be89e7f
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/util/audit/AuditLog.java
@@ -0,0 +1,66 @@
+/*
+ * 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.util.audit;
+
+import org.joda.time.DateTime;
+
+import com.ning.billing.util.ChangeType;
+
+public interface AuditLog {
+
+    /**
+     * Get the type of change for this log entry
+     *
+     * @return the ChangeType
+     */
+    public ChangeType getChangeType();
+
+    /**
+     * Get the name of the requestor
+     *
+     * @return the requestor user name
+     */
+    public String getUserName();
+
+    /**
+     * Get the time when this change was effective
+     *
+     * @return the created date of this log entry
+     */
+    public DateTime getCreatedDate();
+
+    /**
+     * Get the reason code for this change
+     *
+     * @return the reason code
+     */
+    public String getReasonCode();
+
+    /**
+     * Get the user token of this change requestor
+     *
+     * @return the user token
+     */
+    public String getUserToken();
+
+    /**
+     * Get the comment for this change
+     *
+     * @return the comment
+     */
+    public String getComment();
+}
diff --git a/util/src/main/java/com/ning/billing/util/audit/DefaultAuditLog.java b/util/src/main/java/com/ning/billing/util/audit/DefaultAuditLog.java
new file mode 100644
index 0000000..513697e
--- /dev/null
+++ b/util/src/main/java/com/ning/billing/util/audit/DefaultAuditLog.java
@@ -0,0 +1,106 @@
+/*
+ * 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.util.audit;
+
+import org.joda.time.DateTime;
+
+import com.ning.billing.util.ChangeType;
+import com.ning.billing.util.callcontext.CallContext;
+import com.ning.billing.util.dao.EntityAudit;
+
+public class DefaultAuditLog implements AuditLog {
+
+    private final EntityAudit entityAudit;
+    private final CallContext callContext;
+
+    public DefaultAuditLog(final EntityAudit entityAudit, final CallContext callContext) {
+        this.entityAudit = entityAudit;
+        this.callContext = callContext;
+    }
+
+    @Override
+    public ChangeType getChangeType() {
+        return entityAudit.getChangeType();
+    }
+
+    @Override
+    public String getUserName() {
+        return callContext.getUserName();
+    }
+
+    @Override
+    public DateTime getCreatedDate() {
+        return callContext.getCreatedDate();
+    }
+
+    @Override
+    public String getReasonCode() {
+        return callContext.getReasonCode();
+    }
+
+    @Override
+    public String getUserToken() {
+        if (callContext.getUserToken() == null) {
+            return null;
+        } else {
+            return callContext.getUserToken().toString();
+        }
+    }
+
+    @Override
+    public String getComment() {
+        return callContext.getComment();
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append("DefaultAuditLog");
+        sb.append("{entityAudit=").append(entityAudit);
+        sb.append(", callContext=").append(callContext);
+        sb.append('}');
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final DefaultAuditLog that = (DefaultAuditLog) o;
+
+        if (callContext != null ? !callContext.equals(that.callContext) : that.callContext != null) {
+            return false;
+        }
+        if (entityAudit != null ? !entityAudit.equals(that.entityAudit) : that.entityAudit != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = entityAudit != null ? entityAudit.hashCode() : 0;
+        result = 31 * result + (callContext != null ? callContext.hashCode() : 0);
+        return result;
+    }
+}
diff --git a/util/src/test/java/com/ning/billing/util/audit/TestDefaultAuditLog.java b/util/src/test/java/com/ning/billing/util/audit/TestDefaultAuditLog.java
new file mode 100644
index 0000000..709708f
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/audit/TestDefaultAuditLog.java
@@ -0,0 +1,84 @@
+/*
+ * 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.util.audit;
+
+import java.util.UUID;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.ning.billing.util.ChangeType;
+import com.ning.billing.util.UtilTestSuite;
+import com.ning.billing.util.callcontext.CallContext;
+import com.ning.billing.util.callcontext.CallOrigin;
+import com.ning.billing.util.callcontext.DefaultCallContext;
+import com.ning.billing.util.callcontext.UserType;
+import com.ning.billing.util.clock.ClockMock;
+import com.ning.billing.util.dao.EntityAudit;
+import com.ning.billing.util.dao.TableName;
+
+public class TestDefaultAuditLog extends UtilTestSuite {
+
+    @Test(groups = "fast")
+    public void testGetters() throws Exception {
+        final TableName tableName = TableName.ACCOUNT_EMAIL_HISTORY;
+        final long recordId = Long.MAX_VALUE;
+        final ChangeType changeType = ChangeType.DELETE;
+        final EntityAudit entityAudit = new EntityAudit(tableName, recordId, changeType);
+
+        final String userName = UUID.randomUUID().toString();
+        final CallOrigin callOrigin = CallOrigin.EXTERNAL;
+        final UserType userType = UserType.CUSTOMER;
+        final UUID userToken = UUID.randomUUID();
+        final ClockMock clock = new ClockMock();
+        final CallContext callContext = new DefaultCallContext(userName, callOrigin, userType, userToken, clock);
+
+        final AuditLog auditLog = new DefaultAuditLog(entityAudit, callContext);
+        Assert.assertEquals(auditLog.getChangeType(), changeType);
+        Assert.assertNull(auditLog.getComment());
+        Assert.assertNotNull(auditLog.getCreatedDate());
+        Assert.assertNull(auditLog.getReasonCode());
+        Assert.assertEquals(auditLog.getUserName(), userName);
+        Assert.assertEquals(auditLog.getUserToken(), userToken.toString());
+    }
+
+    @Test(groups = "fast")
+    public void testEquals() throws Exception {
+        final TableName tableName = TableName.ACCOUNT_EMAIL_HISTORY;
+        final long recordId = Long.MAX_VALUE;
+        final ChangeType changeType = ChangeType.DELETE;
+        final EntityAudit entityAudit = new EntityAudit(tableName, recordId, changeType);
+
+        final String userName = UUID.randomUUID().toString();
+        final CallOrigin callOrigin = CallOrigin.EXTERNAL;
+        final UserType userType = UserType.CUSTOMER;
+        final UUID userToken = UUID.randomUUID();
+        final ClockMock clock = new ClockMock();
+        final CallContext callContext = new DefaultCallContext(userName, callOrigin, userType, userToken, clock);
+
+        final AuditLog auditLog = new DefaultAuditLog(entityAudit, callContext);
+        Assert.assertEquals(auditLog, auditLog);
+
+        final AuditLog sameAuditLog = new DefaultAuditLog(entityAudit, callContext);
+        Assert.assertEquals(sameAuditLog, auditLog);
+
+        clock.addMonths(1);
+        final CallContext otherCallContext = new DefaultCallContext(userName, callOrigin, userType, userToken, clock);
+        final AuditLog otherAuditLog = new DefaultAuditLog(entityAudit, otherCallContext);
+        Assert.assertNotEquals(otherAuditLog, auditLog);
+    }
+}