Repository: incubator-eagle Updated Branches: refs/heads/master 30b671cf2 -> 03dc7d848
EAGLE-781 Add unit test for eagle-audit-base module EAGLE-781 Add unit test for eagle-audit-base module - Add unit test for eagle-audit-base module. https://issues.apache.org/jira/browse/EAGLE-781 Author: chitin <chitin1...@gmail.com> Closes #660 from chitin/EAGLE-781. Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/03dc7d84 Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/03dc7d84 Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/03dc7d84 Branch: refs/heads/master Commit: 03dc7d8488366ad87889ee2398ef5aac62eaf985 Parents: 30b671c Author: chitin <chitin1...@gmail.com> Authored: Fri Nov 18 11:03:02 2016 +0800 Committer: wujinhu <wujinhu...@126.com> Committed: Fri Nov 18 11:03:02 2016 +0800 ---------------------------------------------------------------------- .../audit/dao/TestSearchRequestBuilder.java | 48 ++++++++++ .../audit/entity/TestGenericAuditEntity.java | 81 ++++++++++++++++ .../audit/listener/AuditListenerTestHelper.java | 32 +++++++ .../eagle/audit/listener/TestAuditSupport.java | 98 ++++++++++++++++++++ 4 files changed, 259 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/03dc7d84/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/dao/TestSearchRequestBuilder.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/dao/TestSearchRequestBuilder.java b/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/dao/TestSearchRequestBuilder.java new file mode 100644 index 0000000..c369142 --- /dev/null +++ b/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/dao/TestSearchRequestBuilder.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.eagle.audit.dao; + +import junit.framework.Assert; +import org.apache.commons.lang.time.DateUtils; +import org.apache.eagle.service.client.EagleServiceSingleEntityQueryRequest; +import org.apache.eagle.service.client.IEagleServiceClient; +import org.apache.eagle.service.client.impl.EagleServiceClientImpl; +import org.apache.eagle.service.client.impl.SearchRequestBuilder; +import org.junit.Test; + +import java.lang.reflect.Field; + +/** + * @Since 11/15/16. + */ +public class TestSearchRequestBuilder { + + @Test + public void testQuery() throws Exception { + IEagleServiceClient client = new EagleServiceClientImpl("localhost", 9090); + String site = "sandbox"; + String query = "AuditService" + "[@serviceName=\"AlertDataSourceService\" AND @site=\"" + site + "\"]{*}"; + SearchRequestBuilder searchRequestBuilder = client.search().startTime(0).endTime(10 * DateUtils.MILLIS_PER_DAY).pageSize(Integer.MAX_VALUE).query(query); + Field requestField = SearchRequestBuilder.class.getDeclaredField("request"); + requestField.setAccessible(true); + EagleServiceSingleEntityQueryRequest request = (EagleServiceSingleEntityQueryRequest)requestField.get(searchRequestBuilder); + String expected = "query=AuditService%5B%40serviceName%3D%22AlertDataSourceService%22+AND+%40site%3D%22sandbox%22%5D%7B*%7D&pageSize=2147483647&startTime=1970-01-01%2000:00:00&endTime=1970-01-11%2000:00:00&treeAgg=false"; + Assert.assertEquals(expected, request.getQueryParameterString()); + } +} + + http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/03dc7d84/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/entity/TestGenericAuditEntity.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/entity/TestGenericAuditEntity.java b/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/entity/TestGenericAuditEntity.java new file mode 100644 index 0000000..24b558a --- /dev/null +++ b/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/entity/TestGenericAuditEntity.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.eagle.audit.entity; + +import org.apache.eagle.audit.common.AuditConstants; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @Since 11/14/16. + */ +public class TestGenericAuditEntity { + + @Test + public void testEqualsObj() { + GenericAuditEntity genericAuditEntity = new GenericAuditEntity(); + Assert.assertTrue(genericAuditEntity.equals(genericAuditEntity)); + } + + @Test + public void testEqualsInstanceOf() { + GenericAuditEntity genericAuditEntity = new GenericAuditEntity(); + Object obj = new Object(); + Assert.assertFalse(genericAuditEntity.equals(obj)); + } + + @Test + public void testEqualsTrue() { + GenericAuditEntity genericAuditEntity1 = new GenericAuditEntity(); + GenericAuditEntity genericAuditEntity2 = new GenericAuditEntity(); + Map<String, String> tags1 = new HashMap<>(); + tags1.put(AuditConstants.AUDIT_COLUMN_SERVICE_NAME, "testService"); + tags1.put(AuditConstants.AUDIT_COLUMN_USER_ID, "eagle1"); + tags1.put(AuditConstants.AUDIT_COLUMN_OPERATION, "UPDATE"); + tags1.put(AuditConstants.AUDIT_COLUMN_TIMESTAMP, "1479107183"); + Map<String, String> tags2 = new HashMap<>(); + tags2.put(AuditConstants.AUDIT_COLUMN_SERVICE_NAME, "testService"); + tags2.put(AuditConstants.AUDIT_COLUMN_USER_ID, "eagle1"); + tags2.put(AuditConstants.AUDIT_COLUMN_OPERATION, "UPDATE"); + tags2.put(AuditConstants.AUDIT_COLUMN_TIMESTAMP, "1479107183"); + genericAuditEntity1.setTags(tags1); + genericAuditEntity2.setTags(tags2); + Assert.assertTrue(genericAuditEntity1.equals(genericAuditEntity2)); + } + + @Test + public void testEqualsFalse() { + GenericAuditEntity genericAuditEntity1 = new GenericAuditEntity(); + GenericAuditEntity genericAuditEntity2 = new GenericAuditEntity(); + Map<String, String> tags1 = new HashMap<>(); + tags1.put(AuditConstants.AUDIT_COLUMN_SERVICE_NAME, "testService"); + tags1.put(AuditConstants.AUDIT_COLUMN_USER_ID, "eagle1"); + tags1.put(AuditConstants.AUDIT_COLUMN_OPERATION, "UPDATE"); + tags1.put(AuditConstants.AUDIT_COLUMN_TIMESTAMP, null); + Map<String, String> tags2 = new HashMap<>(); + tags2.put(AuditConstants.AUDIT_COLUMN_SERVICE_NAME, "testService"); + tags2.put(AuditConstants.AUDIT_COLUMN_USER_ID, "eagle1"); + tags2.put(AuditConstants.AUDIT_COLUMN_OPERATION, "UPDATE"); + tags2.put(AuditConstants.AUDIT_COLUMN_TIMESTAMP, "1479107183"); + genericAuditEntity1.setTags(tags1); + genericAuditEntity2.setTags(tags2); + Assert.assertFalse(genericAuditEntity1.equals(genericAuditEntity2)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/03dc7d84/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/listener/AuditListenerTestHelper.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/listener/AuditListenerTestHelper.java b/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/listener/AuditListenerTestHelper.java new file mode 100644 index 0000000..e2622cf --- /dev/null +++ b/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/listener/AuditListenerTestHelper.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.eagle.audit.listener; + +import org.apache.eagle.audit.common.AuditEvent; + +/** + * @Since 11/11/16. + */ +public class AuditListenerTestHelper implements AuditListener { + private AuditSupport auditSupport = new AuditSupport(this); + public AuditListenerTestHelper() { + auditSupport.addAuditListener(this); + } + + @Override + public void auditEvent(AuditEvent event) {} +} http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/03dc7d84/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/listener/TestAuditSupport.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/listener/TestAuditSupport.java b/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/listener/TestAuditSupport.java new file mode 100644 index 0000000..6e7b5cf --- /dev/null +++ b/eagle-core/eagle-query/eagle-audit-base/src/test/java/org/apache/eagle/audit/listener/TestAuditSupport.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.eagle.audit.listener; + +import junit.framework.Assert; +import org.apache.eagle.audit.common.AuditListenerMap; +import org.apache.eagle.audit.entity.GenericAuditEntity; +import org.junit.Test; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * @Since 11/9/16. + */ +public class TestAuditSupport { + + @Test + public void addAuditLFieldListenerNull() throws Exception { + AuditSupport auditSupport = new AuditSupport(this); + Field mapField = AuditSupport.class.getDeclaredField("map"); + mapField.setAccessible(true); + AuditListenerMap map = (AuditListenerMap)mapField.get(auditSupport); + AuditListener auditListener = null; + auditSupport.addAuditListener(auditListener); + Assert.assertTrue(map.getEntries() == Collections.<Map.Entry<String, AuditListener[]>>emptySet()); + } + + @Test + public void addAuditProxyNullListener() throws Exception { + AuditSupport auditSupport = new AuditSupport(this); + Field mapField = AuditSupport.class.getDeclaredField("map"); + mapField.setAccessible(true); + AuditListenerMap map = (AuditListenerMap)mapField.get(auditSupport); + AuditListener auditListener = new AuditListenerProxy("test", null); + auditSupport.addAuditListener(auditListener); + Assert.assertNull(map.get("test")); + } + + @Test + public void addAuditProxyHbase() throws Exception { + AuditSupport auditSupport = new AuditSupport(this); + Field mapField = AuditSupport.class.getDeclaredField("map"); + mapField.setAccessible(true); + AuditListenerMap map = (AuditListenerMap)mapField.get(auditSupport); + AuditListener auditListener = new AuditListenerTestHelper(); + auditSupport.addAuditListener(auditListener); + Assert.assertNull(map.get("test")); + } + + @Test + public void testRemoveNull() { + AuditSupport auditSupport = new AuditSupport(this); + AuditListener auditListener = null; + auditSupport.removeAuditListener(auditListener); + } + + @Test + public void testRemoveAuditProxy() { + AuditSupport auditSupport = new AuditSupport(this); + AuditListener auditListener = new AuditListenerTestHelper(); + auditSupport.addAuditListener(auditListener); + auditSupport.removeAuditListener(auditListener); + } + + @Test + public void testFireAudit() throws Exception { + AuditSupport auditSupport = new AuditSupport(this); + Field mapField = AuditSupport.class.getDeclaredField("map"); + mapField.setAccessible(true); + AuditListenerMap map = (AuditListenerMap)mapField.get(auditSupport); + AuditListener auditListener1 = new AuditListenerTestHelper(); + AuditListener auditListener2 = new AuditListenerTestHelper(); + AuditListener[] listeners = {auditListener1, auditListener2}; + map.set("testServiceName", listeners); + map.set(null, listeners); + List<GenericAuditEntity> auditEntities = new ArrayList<>(); + GenericAuditEntity genericAuditEntity = new GenericAuditEntity(); + auditEntities.add(genericAuditEntity); + auditSupport.fireAudit("testServiceName", auditEntities); + } +}