Ori Liel has uploaded a new change for review.

Change subject: resatpi: Eliminate Exceptions in Mapper Tests
......................................................................

resatpi: Eliminate Exceptions in Mapper Tests

Change-Id: I84cabed0ff38196d8fd57d2f72ffdfe213f2ffed
Signed-off-by: Ori Liel <[email protected]>
---
M 
backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/AbstractInvertibleMappingTest.java
M 
backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/GlusterBrickDetailMapperTest.java
M 
backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/MappingTestHelper.java
3 files changed, 76 insertions(+), 80 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/73/19673/1

diff --git 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/AbstractInvertibleMappingTest.java
 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/AbstractInvertibleMappingTest.java
index 938cb93..81a76fd 100644
--- 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/AbstractInvertibleMappingTest.java
+++ 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/AbstractInvertibleMappingTest.java
@@ -6,6 +6,7 @@
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.ovirt.engine.api.model.BaseResource;
 import org.ovirt.engine.core.utils.MockConfigRule;
 
 /**
@@ -20,7 +21,7 @@
  * @param <I>
  *            inverse type (may be identical to T)
  */
-public abstract class AbstractInvertibleMappingTest<F, T, I> extends Assert {
+public abstract class AbstractInvertibleMappingTest<F extends BaseResource, T, 
I> extends Assert {
 
     @Rule
     public MockConfigRule mcr = new MockConfigRule();
diff --git 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/GlusterBrickDetailMapperTest.java
 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/GlusterBrickDetailMapperTest.java
index f705c7e..4ccac73 100644
--- 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/GlusterBrickDetailMapperTest.java
+++ 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/GlusterBrickDetailMapperTest.java
@@ -15,16 +15,15 @@
 import org.ovirt.engine.core.common.businessentities.gluster.Mempool;
 import org.ovirt.engine.core.utils.RandomUtils;
 
-public class GlusterBrickDetailMapperTest extends 
AbstractInvertibleMappingTest<GlusterVolumeAdvancedDetails,
-                                                        GlusterBrick, 
GlusterBrick> {
+public class GlusterBrickDetailMapperTest extends 
AbstractInvertibleMappingTest<GlusterBrick, GlusterVolumeAdvancedDetails, 
GlusterVolumeAdvancedDetails> {
 
      public GlusterBrickDetailMapperTest() {
-        super(GlusterVolumeAdvancedDetails.class, GlusterBrick.class,
-                GlusterBrick.class);
+        super(GlusterBrick.class, GlusterVolumeAdvancedDetails.class,
+                GlusterVolumeAdvancedDetails.class);
     }
 
     @Override
-    protected void verify(GlusterVolumeAdvancedDetails model, 
GlusterVolumeAdvancedDetails transform) {
+    protected void verify(GlusterBrick model, GlusterBrick transform) {
        assertNotNull(transform);
     }
 
diff --git 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/MappingTestHelper.java
 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/MappingTestHelper.java
index ac4409c..2548b79 100644
--- 
a/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/MappingTestHelper.java
+++ 
b/backend/manager/modules/restapi/types/src/test/java/org/ovirt/engine/api/restapi/types/MappingTestHelper.java
@@ -3,20 +3,27 @@
 import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.util.ArrayList;
+import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.UUID;
 
+import javax.xml.datatype.DatatypeFactory;
+import javax.xml.datatype.XMLGregorianCalendar;
+
 import org.ovirt.engine.api.model.BaseResource;
 import org.ovirt.engine.api.restapi.utils.GuidUtils;
-import org.ovirt.engine.core.utils.log.Log;
-import org.ovirt.engine.core.utils.log.LogFactory;
 
 public class MappingTestHelper {
 
+    private static final String SLAVE_SINGLE = "HostNIC";
+    private static final String SLAVES_PLURAL = "Slaves";
+    private static final String FLOPPY_SINGLE = "Floppy";
+    private static final String FLOPPIES_PLURAL = "Floppies";
+    private static final String NIC_SINGLE = "NIC";
+    private static final String NICS_PLURAL = "Nics";
+    private static final String XML_GREGORIAN_CALENDAR = 
"javax.xml.datatype.XMLGregorianCalendar";
     private static final String SET_ROOT = "set";
     private static final String GET_ROOT = "get";
-
-    private static final Log logger = 
LogFactory.getLog(MappingTestHelper.class);
 
     /**
      * Populate a JAXB model type by recursively walking element tree and
@@ -26,7 +33,7 @@
      *            the model type
      * @return a populated instance
      */
-    public static Object populate(Class<?> clz) {
+    public static Object populate(Class<?> clz) throws Exception {
         List<Class<?>> seen = getSetMethodTypes(clz);
         return populate(instantiate(clz), clz, seen, 1);
     }
@@ -56,8 +63,7 @@
      *            model types seen so far
      * @return a populated instance
      */
-    public static Object populate(Object model, Class<?> clz, List<Class<?>> 
seen, int level) {
-
+    public static Object populate(Object model, Class<?> clz, List<Class<?>> 
seen, int level) throws Exception {
         for (Method method : clz.getMethods()) {
             if (isSetter(method)) {
                 if (takesPrimitive(method)) {
@@ -78,22 +84,13 @@
         return model;
     }
 
-    private static void populateBigDecimal(Method method, Object model) {
-        try {
-            method.invoke(model, new BigDecimal(rand(100)));
-        } catch (Exception e) {
-           logger.error("Failed to populate big decimal in " + 
method.getDeclaringClass() + "." + method.getName(),e);
-        }
+    private static void populateBigDecimal(Method method, Object model) throws 
Exception {
+        method.invoke(model, new BigDecimal(rand(100)));
     }
 
-    private static Object instantiate(Class<?> clz) {
+    private static Object instantiate(Class<?> clz) throws Exception {
         Object model = null;
-        try {
-            model = clz.newInstance();
-        } catch (Exception e) {
-            // should never occur, trivial instantiation
-            logger.error("Failed to instantiate class " + 
clz.getSimpleName(),e);
-        }
+        model = clz.newInstance();
         return model;
     }
 
@@ -102,24 +99,20 @@
                 && (takesString(m) || takesBoolean(m) || takesShort(m) || 
takesInteger(m) || takesLong(m));
     }
 
-    private static void random(Method m, Object model) {
-        try {
-            m.invoke(
-                     model,
-                     takesString(m)
-                     ? garble(m)
-                     : takesShort(m)
-                       ? Short.valueOf((short) rand(100))
-                       : takesInteger(m)
-                         ? Integer.valueOf(rand(100))
-                         : takesLong(m)
-                            ? Long.valueOf(rand(1000000000))
-                            : takesBoolean(m)
-                              ? Boolean.valueOf(Math.random() < 0.5D)
-                              : null);
-        } catch (Exception e) {
-            // simple setter, exception should not be thrown
-        }
+    private static void random(Method m, Object model) throws Exception {
+        m.invoke(
+                model,
+                takesString(m)
+                        ? garble(m)
+                        : takesShort(m)
+                                ? Short.valueOf((short) rand(100))
+                                : takesInteger(m)
+                                        ? Integer.valueOf(rand(100))
+                                        : takesLong(m)
+                                                ? 
Long.valueOf(rand(1000000000))
+                                                : takesBoolean(m)
+                                                        ? 
Boolean.valueOf(Math.random() < 0.5D)
+                                                        : null);
     }
 
     public static <E extends Enum> E shuffle(Class<E> enumType) {
@@ -127,48 +120,43 @@
         return values[rand(values.length)];
     }
 
-    private static void shuffle(Method method, Object model) {
+    private static void shuffle(Method method, Object model) throws Exception {
         Class<? extends Enum> enumType = (Class<? extends 
Enum>)method.getParameterTypes()[0];
-        try {
-            method.invoke(model, shuffle(enumType));
-        } catch (Exception e) {
-            // simple setter, exception should not be thrown
-        }
+        method.invoke(model, shuffle(enumType));
     }
 
-    private static void descend(Method method, Object model, List<Class<?>> 
seen, int level) {
-        try {
-            Object child = method.getParameterTypes()[0].newInstance();
-            method.invoke(model, child);
-            if (level == 1 || unseen(method, seen)) {
-                populate(child, child.getClass(), seen, ++level);
-            }
-        } catch (Exception e) {
-            // simple setter, exception should not be thrown
+    private static void descend(Method method, Object model, List<Class<?>> 
seen, int level) throws Exception {
+        Class<?> type = method.getParameterTypes()[0];
+        Object child;
+        if (type.getName().equals(XML_GREGORIAN_CALENDAR)) {
+            child = DatatypeFactory.newInstance().newXMLGregorianCalendar(new 
GregorianCalendar(1111, 10, 29));
+        } else {
+            child = type.newInstance();
+        }
+        method.invoke(model, child);
+        if ((level == 1 || unseen(method, seen))
+                && 
(!XMLGregorianCalendar.class.isAssignableFrom(child.getClass()))) {
+            populate(child, child.getClass(), seen, ++level);
         }
     }
 
     @SuppressWarnings("unchecked")
-    private static void fill(Method method, Object model, List<Class<?>> seen, 
int level) {
-        try {
-            // List<T> type parameter removed by erasure, hence we attempt to
-            // infer from method name
-            String elementType = method.getName().substring(GET_ROOT.length());
-            Class<?> childType = coPackaged(model, elementType);
-            if (level == 1 || unseen(childType, seen)) {
-                List<Object> list = (List<Object>) method.invoke(model);
-                Object child = null;
-                if (childType.isEnum()) {
-                    Object[] labels = childType.getEnumConstants();
-                    child = labels[rand(labels.length)];
-                } else {
-                    child = childType.newInstance();
-                }
-                list.add(child);
-                populate(child, child.getClass(), seen, ++level);
+    private static void fill(Method method, Object model, List<Class<?>> seen, 
int level) throws Exception {
+        // List<T> type parameter removed by erasure, hence we attempt to
+        // infer from method name
+        String elementType = method.getName().substring(GET_ROOT.length());
+        Class<?> childType = coPackaged(model, elementType);
+        if (level == 1 || unseen(childType, seen)) {
+            List<Object> list = (List<Object>) method.invoke(model);
+            Object child = null;
+            if (childType.isEnum()) {
+                Object[] labels = childType.getEnumConstants();
+                child = labels[rand(labels.length)];
+            } else {
+                child = childType.newInstance();
             }
-        } catch (Exception e) {
-            // simple getter, exception should not be thrown
+            list.add(child);
+            populate(child, child.getClass(), seen, ++level);
         }
     }
 
@@ -219,7 +207,9 @@
     private static Class<?> coPackaged(Object model, String elementType) 
throws ClassNotFoundException {
         String packageRoot = model.getClass().getPackage().getName() + ".";
         try {
-            return Class.forName(packageRoot + singular(elementType));
+            // Consider special case: "Usages" contains a list of Strings, not 
'Usage' elements.
+            return elementType.equals("Usages") ? String.class :
+                    Class.forName(packageRoot + singular(elementType));
         } catch (ClassNotFoundException cnf) {
             try {
                 return Class.forName(packageRoot + elementType);
@@ -231,7 +221,13 @@
     }
 
     private static String singular(String s) {
-        return s.endsWith("s") ? s.substring(0, s.length() - 1) : s;
+        // Consider exceptional cases:
+        // 'Nics' is plural of 'NIC' (uppercase)
+        // 'Floppies' is plural of 'Floppy' (not 'Floppie')
+        // 'Slaves' is plural of 'HostNIC' (we don't have a 'Slave' entity)
+        return s.equals(NICS_PLURAL) ? NIC_SINGLE : s.equals(FLOPPIES_PLURAL) 
? FLOPPY_SINGLE : s.equals(SLAVES_PLURAL) ? SLAVE_SINGLE :
+                s.endsWith("s") ? s.substring(0,
+                s.length() - 1) : s;
     }
 
     private static boolean unseen(Class<?> type, List<Class<?>> seen) {


-- 
To view, visit http://gerrit.ovirt.org/19673
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I84cabed0ff38196d8fd57d2f72ffdfe213f2ffed
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Ori Liel <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to