Tal Nisan has uploaded a new change for review. Change subject: core: AuditLogDirector tests and some cleanups ......................................................................
core: AuditLogDirector tests and some cleanups Change-Id: I6cb777a39dbe19510d8a0466e6dff80da1b234ba Signed-off-by: Tal Nisan <tni...@redhat.com> --- M backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/TypeCompat.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java M backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirectorTest.java 3 files changed, 86 insertions(+), 64 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/97/10497/1 diff --git a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/TypeCompat.java b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/TypeCompat.java index 4b76d04..cb14bfc 100644 --- a/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/TypeCompat.java +++ b/backend/manager/modules/compat/src/main/java/org/ovirt/engine/core/compat/backendcompat/TypeCompat.java @@ -22,7 +22,7 @@ try { PropertyDescriptor[] pds = Introspector.getBeanInfo(type).getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { - // Class is a bogud property, remove it + // Class is a bogus property, remove it if (!CLASS.equals(pd.getName())) { returnValue.add(new PropertyInfo(pd)); } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java index afaeb6d..2e5c8d1 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java @@ -28,7 +28,7 @@ private static final Map<AuditLogType, AuditLogSeverity> mSeverities = new EnumMap<AuditLogType, AuditLogSeverity>(AuditLogType.class); private static final Pattern pattern = Pattern.compile("\\$\\{\\w*\\}"); // match ${<alphanumeric>...} - private static final String UNKNOWN_VARIABLE_VALUE = "<UNKNOWN>"; + protected static final String UNKNOWN_VARIABLE_VALUE = "<UNKNOWN>"; static { initMessages(); @@ -775,8 +775,8 @@ if (auditLogable == null || auditLogable.getLegal()) { String message = null; String resolvedMessage = null; - AuditLogSeverity severity = AuditLogSeverity.forValue(0); - if (!((severity = mSeverities.get(logType)) != null)) { + AuditLogSeverity severity; + if ((severity = mSeverities.get(logType)) == null) { severity = AuditLogSeverity.NORMAL; log.infoFormat("No severity for {0} type", logType); } @@ -932,11 +932,8 @@ while (matcher.find()) { token = matcher.group(); - // remove leading ${ - token = token.substring(2); - - // remove trailing } - token = token.substring(0, token.length() - 1); + // remove leading ${ and trailing } + token = token.substring(2, token.length() - 1); // get value from value map value = values.get(token.toLowerCase()); @@ -952,17 +949,18 @@ return buffer.toString(); } + // No need to iterate on the AuditLogable properties every invocation, just do it once + private static final List<PropertyInfo> auditLogableProperties = TypeCompat.GetProperties(AuditLogableBase.class); + static Map<String, String> getAvailableValues(AuditLogableBase logable) { - Map<String, String> returnValue = - new HashMap<String, String>(logable.getCustomValues()); - Class<?> type = AuditLogableBase.class; - for (PropertyInfo propertyInfo : TypeCompat.GetProperties(type)) { + Map<String, String> returnValue = new HashMap<String, String>(logable.getCustomValues()); + for (PropertyInfo propertyInfo : auditLogableProperties) { Object value = propertyInfo.GetValue(logable, null); String stringValue = value != null ? value.toString() : null; if (!returnValue.containsKey(propertyInfo.getName().toLowerCase())) { returnValue.put(propertyInfo.getName().toLowerCase(), stringValue); } else { - log.errorFormat("Try to add duplicate values with same name. Type: {0}. Value: {1}", + log.errorFormat("Try to add duplicate audit log values with same name. Type: {0}. Value: {1}", logable.getAuditLogTypeValue(), propertyInfo.getName().toLowerCase()); } } @@ -976,5 +974,4 @@ private static void defaultLog(AuditLogableBase auditLogable) { auditLogable.DefaultLog(); } - } diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirectorTest.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirectorTest.java index b6d80f4..34f34d0 100644 --- a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirectorTest.java +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirectorTest.java @@ -8,26 +8,18 @@ //import static org.mockito.Mockito.when; // -//import java.util.Collections; -//import java.util.HashMap; -//import java.util.Map; -// -//import junit.framework.Assert; -// -//import org.junit.Before; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.mockito.Mock; -//import org.mockito.Mockito; -//import org.ovirt.engine.core.common.AuditLogType; -//import org.ovirt.engine.core.common.businessentities.AuditLog; -//import org.ovirt.engine.core.dal.dbbroker.DbFacade; -//import org.ovirt.engine.core.dao.AuditLogDAO; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.Assert; + +import org.junit.Test; //@RunWith(PowerMockRunner.class) //@PrepareForTest({ AuditLogDirector.class }) -//public class AuditLogDirectorTest { +public class AuditLogDirectorTest { // // @Mock // DbFacade dbFacade; @@ -49,10 +41,10 @@ // PowerMockito.when(AuditLogDirector.getDbFacadeInstance()).thenReturn(dbFacade); // } // -// @Test -// public void testPropertyLoading() { -// AuditLogDirector.checkSeverities(); -// } + @Test + public void testPropertyLoading() { + AuditLogDirector.checkSeverities(); + } // // /** // * The test assures that audit loggable objects with timeout, which were created without an explicit log type, with @@ -60,15 +52,15 @@ // * The test invokes two {@Code AuditLogDirector.log()} calls and verifies that each call insert an entry into // * the database.<br> // */ -// @Test -// public void testLegalAuditLog() { -// AuditLogableBase logableObject1 = new AuditLogableBase(); -// AuditLogDirector.log(logableObject1, AuditLogType.IRS_DISK_SPACE_LOW); -// -// AuditLogableBase logableObject2 = new AuditLogableBase(); -// AuditLogDirector.log(logableObject2, AuditLogType.IRS_DISK_SPACE_LOW_ERROR); -// Mockito.verify(auditLogDao, Mockito.times(2)).save(Mockito.any(AuditLog.class)); -// } + // @Test + // public void testLegalAuditLog() { + // AuditLogableBase logableObject1 = new AuditLogableBase(); + // AuditLogDirector.log(logableObject1, AuditLogType.IRS_DISK_SPACE_LOW); + // + // AuditLogableBase logableObject2 = new AuditLogableBase(); + // AuditLogDirector.log(logableObject2, AuditLogType.IRS_DISK_SPACE_LOW_ERROR); + // Mockito.verify(auditLogDao, Mockito.times(2)).save(Mockito.any(AuditLog.class)); + // } // // /** // * The test assures that audit loggable objects with timeout, which were created without an explicit log type and @@ -86,23 +78,56 @@ // Mockito.verify(auditLogDao, Mockito.times(1)).save(Mockito.any(AuditLog.class)); // } // -// @Test -// public void testResolveUnknownVariable() { -// final String message = "This is my ${Variable}"; -// final String expectedResolved = "This is my <UNKNOWN>"; -// Map<String, String> values = Collections.emptyMap(); -// String resolvedMessage = AuditLogDirector.resolveMessage(message, values); -// Assert.assertEquals(expectedResolved, resolvedMessage); -// } -// -// @Test -// public void testResolveKnownVariable() { -// final String message = "This is my ${Variable}"; -// final String expectedResolved = "This is my value"; -// Map<String, String> values = new HashMap<String, String>(); -// values.put("variable", "value"); -// String resolvedMessage = AuditLogDirector.resolveMessage(message, values); -// Assert.assertEquals(expectedResolved, resolvedMessage); -// } -// -// } + @Test + public void testResolveUnknownVariable() { + final String message = "This is my ${Variable}"; + final String expectedResolved = String.format("This is my %1s", AuditLogDirector.UNKNOWN_VARIABLE_VALUE); + Map<String, String> values = Collections.emptyMap(); + String resolvedMessage = AuditLogDirector.resolveMessage(message, values); + Assert.assertEquals(expectedResolved, resolvedMessage); + } + + @Test + public void testResolveKnownVariable() { + final String message = "This is my ${Variable}"; + final String expectedResolved = "This is my value"; + Map<String, String> values = new HashMap<String, String>(); + values.put("variable", "value"); + String resolvedMessage = AuditLogDirector.resolveMessage(message, values); + Assert.assertEquals(expectedResolved, resolvedMessage); + } + + @Test + public void testResolveCombinedMessage() { + final String message = + "${first} equals one, ${second} equals two, '${blank}' eqauls blank and ${nonExist} is unknown"; + final String expectedResolved = + String.format("one equals one, two equals two, ' ' eqauls blank and %1s is unknown", + AuditLogDirector.UNKNOWN_VARIABLE_VALUE); + Map<String, String> values = new HashMap<String, String>(); + values.put("first", "one"); + values.put("second", "two"); + values.put("blank", " "); + String resolvedMessage = AuditLogDirector.resolveMessage(message, values); + Assert.assertEquals(expectedResolved, resolvedMessage); + } + + @Test + public void testResolveAuditLogableBase() { + final String vdsName = "TestVDS"; + final String vmName = "TestVM"; + final String message = + "The VM name is ${vmName}, the VDS name is ${vdsName} and the template name is ${vmTemplateName}"; + final String expectedResolved = + String.format("The VM name is %1s, the VDS name is %2s and the template name is %3s", + vmName, + vdsName, + AuditLogDirector.UNKNOWN_VARIABLE_VALUE); + + AuditLogableBase logable = new AuditLogableBase(); + logable.setVmName(vmName); + logable.setVdsName(vdsName); + String resolvedMessage = AuditLogDirector.resolveMessage(message, logable); + Assert.assertEquals(expectedResolved, resolvedMessage); + } +} -- To view, visit http://gerrit.ovirt.org/10497 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6cb777a39dbe19510d8a0466e6dff80da1b234ba Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Tal Nisan <tni...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches